diff --git a/src/init.cpp b/src/init.cpp index 30f0d0049..4d5720306 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -652,7 +652,7 @@ bool AppInit2() uiInterface.InitMessage(_("Loading block index...")); printf("Loading block index...\n"); nStart = GetTimeMillis(); - pblocktree = new CBlockTreeDB("cr+"); + pblocktree = new CBlockTreeDB(); pcoinsdbview = new CCoinsViewDB(); pcoinsTip = new CCoinsViewCache(*pcoinsdbview); diff --git a/src/leveldb.cpp b/src/leveldb.cpp index 29e5e6a7f..e8a0fbe87 100644 --- a/src/leveldb.cpp +++ b/src/leveldb.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -20,7 +21,7 @@ static leveldb::Options GetOptions() { return options; } -CLevelDB::CLevelDB(const boost::filesystem::path &path) { +CLevelDB::CLevelDB(const boost::filesystem::path &path, bool fMemory) { penv = NULL; readoptions.verify_checksums = true; iteroptions.verify_checksums = true; @@ -28,8 +29,13 @@ CLevelDB::CLevelDB(const boost::filesystem::path &path) { syncoptions.sync = true; options = GetOptions(); options.create_if_missing = true; - boost::filesystem::create_directory(path); - printf("Opening LevelDB in %s\n", path.string().c_str()); + if (fMemory) { + penv = leveldb::NewMemEnv(leveldb::Env::Default()); + options.env = penv; + } else { + boost::filesystem::create_directory(path); + printf("Opening LevelDB in %s\n", path.string().c_str()); + } leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb); if (!status.ok()) throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str())); diff --git a/src/leveldb.h b/src/leveldb.h index 28484dac9..ee9079c3c 100644 --- a/src/leveldb.h +++ b/src/leveldb.h @@ -69,7 +69,7 @@ private: leveldb::DB *pdb; public: - CLevelDB(const boost::filesystem::path &path); + CLevelDB(const boost::filesystem::path &path, bool fMemory = false); ~CLevelDB(); template bool Read(const K& key, V& value) { diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index 4580877cd..0173c0064 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -2,6 +2,7 @@ #include #include "db.h" +#include "txdb.h" #include "main.h" #include "wallet.h" @@ -18,8 +19,13 @@ struct TestingSetup { fPrintToDebugger = true; // don't want to write to debug.log file noui_connect(); bitdb.MakeMock(); - pblocktree = new CBlockTreeDB("cr+"); +#ifdef USE_LEVELDB + pblocktree = new CBlockTreeDB(true); + pcoinsdbview = new CCoinsViewDB(true); +#else + pblocktree = new CBlockTreeDB(); pcoinsdbview = new CCoinsViewDB(); +#endif pcoinsTip = new CCoinsViewCache(*pcoinsdbview); LoadBlockIndex(true); bool fFirstRun; diff --git a/src/txdb-bdb.cpp b/src/txdb-bdb.cpp index 5e4609664..8954b8b30 100644 --- a/src/txdb-bdb.cpp +++ b/src/txdb-bdb.cpp @@ -64,7 +64,7 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) { return Read('l', nFile); } -CCoinsViewDB::CCoinsViewDB() : db("cr+") {} +CCoinsViewDB::CCoinsViewDB() : db() {} bool CCoinsViewDB::GetCoins(uint256 txid, CCoins &coins) { return db.ReadCoins(txid, coins); } bool CCoinsViewDB::SetCoins(uint256 txid, const CCoins &coins) { return db.WriteCoins(txid, coins); } bool CCoinsViewDB::HaveCoins(uint256 txid) { return db.HaveCoins(txid); } diff --git a/src/txdb-bdb.h b/src/txdb-bdb.h index d61e95bac..3e8d40c27 100644 --- a/src/txdb-bdb.h +++ b/src/txdb-bdb.h @@ -11,7 +11,7 @@ class CCoinsDB : public CDB { public: - CCoinsDB(const char* pszMode="r+") : CDB("coins.dat", pszMode) { } + CCoinsDB() : CDB("coins.dat", "cr+") { } private: CCoinsDB(const CCoinsDB&); void operator=(const CCoinsDB&); @@ -43,7 +43,7 @@ public: class CBlockTreeDB : public CDB { public: - CBlockTreeDB(const char* pszMode="r+") : CDB("blktree.dat", pszMode) { } + CBlockTreeDB() : CDB("blktree.dat", "cr+") { } private: CBlockTreeDB(const CBlockTreeDB&); void operator=(const CBlockTreeDB&); diff --git a/src/txdb-leveldb.cpp b/src/txdb-leveldb.cpp index 83a71c97d..4e2abe63d 100644 --- a/src/txdb-leveldb.cpp +++ b/src/txdb-leveldb.cpp @@ -19,7 +19,7 @@ void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) { batch.Write('B', hash); } -CCoinsViewDB::CCoinsViewDB() : db(GetDataDir() / "coins") { +CCoinsViewDB::CCoinsViewDB(bool fMemory) : db(GetDataDir() / "coins", fMemory) { } bool CCoinsViewDB::GetCoins(uint256 txid, CCoins &coins) { @@ -63,6 +63,9 @@ bool CCoinsViewDB::BatchWrite(const std::map &mapCoins, CBlockI return db.WriteBatch(batch); } +CBlockTreeDB::CBlockTreeDB(bool fMemory) : CLevelDB(GetDataDir() / "blktree", fMemory) { +} + bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex) { return Write(make_pair('b', blockindex.GetBlockHash()), blockindex); diff --git a/src/txdb-leveldb.h b/src/txdb-leveldb.h index e66ba8f8a..1254422cf 100644 --- a/src/txdb-leveldb.h +++ b/src/txdb-leveldb.h @@ -14,7 +14,7 @@ class CCoinsViewDB : public CCoinsView protected: CLevelDB db; public: - CCoinsViewDB(); + CCoinsViewDB(bool fMemory = false); bool GetCoins(uint256 txid, CCoins &coins); bool SetCoins(uint256 txid, const CCoins &coins); @@ -28,7 +28,7 @@ public: class CBlockTreeDB : public CLevelDB { public: - CBlockTreeDB(const char* pszMode="r+") : CLevelDB(GetDataDir() / "blktree") { } + CBlockTreeDB(bool fMemory = false); private: CBlockTreeDB(const CBlockTreeDB&); void operator=(const CBlockTreeDB&);