mirror of
https://github.com/twisterarmy/twister-core.git
synced 2025-01-23 13:04:17 +00:00
Add LevelDB MemEnv support
Support LevelDB memory-backed environments, and use them in unit tests.
This commit is contained in:
parent
2d8a48292b
commit
e1bfbab802
@ -652,7 +652,7 @@ bool AppInit2()
|
|||||||
uiInterface.InitMessage(_("Loading block index..."));
|
uiInterface.InitMessage(_("Loading block index..."));
|
||||||
printf("Loading block index...\n");
|
printf("Loading block index...\n");
|
||||||
nStart = GetTimeMillis();
|
nStart = GetTimeMillis();
|
||||||
pblocktree = new CBlockTreeDB("cr+");
|
pblocktree = new CBlockTreeDB();
|
||||||
pcoinsdbview = new CCoinsViewDB();
|
pcoinsdbview = new CCoinsViewDB();
|
||||||
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
|
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <leveldb/env.h>
|
#include <leveldb/env.h>
|
||||||
#include <leveldb/cache.h>
|
#include <leveldb/cache.h>
|
||||||
#include <leveldb/filter_policy.h>
|
#include <leveldb/filter_policy.h>
|
||||||
|
#include <memenv/memenv.h>
|
||||||
|
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ static leveldb::Options GetOptions() {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLevelDB::CLevelDB(const boost::filesystem::path &path) {
|
CLevelDB::CLevelDB(const boost::filesystem::path &path, bool fMemory) {
|
||||||
penv = NULL;
|
penv = NULL;
|
||||||
readoptions.verify_checksums = true;
|
readoptions.verify_checksums = true;
|
||||||
iteroptions.verify_checksums = true;
|
iteroptions.verify_checksums = true;
|
||||||
@ -28,8 +29,13 @@ CLevelDB::CLevelDB(const boost::filesystem::path &path) {
|
|||||||
syncoptions.sync = true;
|
syncoptions.sync = true;
|
||||||
options = GetOptions();
|
options = GetOptions();
|
||||||
options.create_if_missing = true;
|
options.create_if_missing = true;
|
||||||
boost::filesystem::create_directory(path);
|
if (fMemory) {
|
||||||
printf("Opening LevelDB in %s\n", path.string().c_str());
|
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);
|
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
|
||||||
if (!status.ok())
|
if (!status.ok())
|
||||||
throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str()));
|
throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str()));
|
||||||
|
@ -69,7 +69,7 @@ private:
|
|||||||
leveldb::DB *pdb;
|
leveldb::DB *pdb;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CLevelDB(const boost::filesystem::path &path);
|
CLevelDB(const boost::filesystem::path &path, bool fMemory = false);
|
||||||
~CLevelDB();
|
~CLevelDB();
|
||||||
|
|
||||||
template<typename K, typename V> bool Read(const K& key, V& value) {
|
template<typename K, typename V> bool Read(const K& key, V& value) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
#include "db.h"
|
#include "db.h"
|
||||||
|
#include "txdb.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "wallet.h"
|
#include "wallet.h"
|
||||||
|
|
||||||
@ -18,8 +19,13 @@ struct TestingSetup {
|
|||||||
fPrintToDebugger = true; // don't want to write to debug.log file
|
fPrintToDebugger = true; // don't want to write to debug.log file
|
||||||
noui_connect();
|
noui_connect();
|
||||||
bitdb.MakeMock();
|
bitdb.MakeMock();
|
||||||
pblocktree = new CBlockTreeDB("cr+");
|
#ifdef USE_LEVELDB
|
||||||
|
pblocktree = new CBlockTreeDB(true);
|
||||||
|
pcoinsdbview = new CCoinsViewDB(true);
|
||||||
|
#else
|
||||||
|
pblocktree = new CBlockTreeDB();
|
||||||
pcoinsdbview = new CCoinsViewDB();
|
pcoinsdbview = new CCoinsViewDB();
|
||||||
|
#endif
|
||||||
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
|
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
|
||||||
LoadBlockIndex(true);
|
LoadBlockIndex(true);
|
||||||
bool fFirstRun;
|
bool fFirstRun;
|
||||||
|
@ -64,7 +64,7 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
|
|||||||
return Read('l', 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::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::SetCoins(uint256 txid, const CCoins &coins) { return db.WriteCoins(txid, coins); }
|
||||||
bool CCoinsViewDB::HaveCoins(uint256 txid) { return db.HaveCoins(txid); }
|
bool CCoinsViewDB::HaveCoins(uint256 txid) { return db.HaveCoins(txid); }
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
class CCoinsDB : public CDB
|
class CCoinsDB : public CDB
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CCoinsDB(const char* pszMode="r+") : CDB("coins.dat", pszMode) { }
|
CCoinsDB() : CDB("coins.dat", "cr+") { }
|
||||||
private:
|
private:
|
||||||
CCoinsDB(const CCoinsDB&);
|
CCoinsDB(const CCoinsDB&);
|
||||||
void operator=(const CCoinsDB&);
|
void operator=(const CCoinsDB&);
|
||||||
@ -43,7 +43,7 @@ public:
|
|||||||
class CBlockTreeDB : public CDB
|
class CBlockTreeDB : public CDB
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CBlockTreeDB(const char* pszMode="r+") : CDB("blktree.dat", pszMode) { }
|
CBlockTreeDB() : CDB("blktree.dat", "cr+") { }
|
||||||
private:
|
private:
|
||||||
CBlockTreeDB(const CBlockTreeDB&);
|
CBlockTreeDB(const CBlockTreeDB&);
|
||||||
void operator=(const CBlockTreeDB&);
|
void operator=(const CBlockTreeDB&);
|
||||||
|
@ -19,7 +19,7 @@ void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) {
|
|||||||
batch.Write('B', 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) {
|
bool CCoinsViewDB::GetCoins(uint256 txid, CCoins &coins) {
|
||||||
@ -63,6 +63,9 @@ bool CCoinsViewDB::BatchWrite(const std::map<uint256, CCoins> &mapCoins, CBlockI
|
|||||||
return db.WriteBatch(batch);
|
return db.WriteBatch(batch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CBlockTreeDB::CBlockTreeDB(bool fMemory) : CLevelDB(GetDataDir() / "blktree", fMemory) {
|
||||||
|
}
|
||||||
|
|
||||||
bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
|
bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
|
||||||
{
|
{
|
||||||
return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
|
return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
|
||||||
|
@ -14,7 +14,7 @@ class CCoinsViewDB : public CCoinsView
|
|||||||
protected:
|
protected:
|
||||||
CLevelDB db;
|
CLevelDB db;
|
||||||
public:
|
public:
|
||||||
CCoinsViewDB();
|
CCoinsViewDB(bool fMemory = false);
|
||||||
|
|
||||||
bool GetCoins(uint256 txid, CCoins &coins);
|
bool GetCoins(uint256 txid, CCoins &coins);
|
||||||
bool SetCoins(uint256 txid, const CCoins &coins);
|
bool SetCoins(uint256 txid, const CCoins &coins);
|
||||||
@ -28,7 +28,7 @@ public:
|
|||||||
class CBlockTreeDB : public CLevelDB
|
class CBlockTreeDB : public CLevelDB
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CBlockTreeDB(const char* pszMode="r+") : CLevelDB(GetDataDir() / "blktree") { }
|
CBlockTreeDB(bool fMemory = false);
|
||||||
private:
|
private:
|
||||||
CBlockTreeDB(const CBlockTreeDB&);
|
CBlockTreeDB(const CBlockTreeDB&);
|
||||||
void operator=(const CBlockTreeDB&);
|
void operator=(const CBlockTreeDB&);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user