Add LevelDB MemEnv support

Support LevelDB memory-backed environments, and use them in unit tests.
This commit is contained in:
Pieter Wuille
2012-09-04 18:12:00 +02:00
parent 2d8a48292b
commit e1bfbab802
8 changed files with 27 additions and 12 deletions

View File

@@ -8,6 +8,7 @@
#include <leveldb/env.h>
#include <leveldb/cache.h>
#include <leveldb/filter_policy.h>
#include <memenv/memenv.h>
#include <boost/filesystem.hpp>
@@ -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()));