2012-09-03 19:05:30 +02:00
|
|
|
// Copyright (c) 2012 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "leveldb.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <leveldb/env.h>
|
|
|
|
#include <leveldb/cache.h>
|
|
|
|
#include <leveldb/filter_policy.h>
|
2012-09-04 18:12:00 +02:00
|
|
|
#include <memenv/memenv.h>
|
2012-09-03 19:05:30 +02:00
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2012-11-04 17:11:48 +01:00
|
|
|
static leveldb::Options GetOptions(size_t nCacheSize) {
|
2012-09-03 19:05:30 +02:00
|
|
|
leveldb::Options options;
|
2012-11-04 17:11:48 +01:00
|
|
|
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
|
|
|
|
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
|
2012-09-03 19:05:30 +02:00
|
|
|
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
|
|
|
|
options.compression = leveldb::kNoCompression;
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2012-11-04 17:11:48 +01:00
|
|
|
CLevelDB::CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory) {
|
2012-09-03 19:05:30 +02:00
|
|
|
penv = NULL;
|
|
|
|
readoptions.verify_checksums = true;
|
|
|
|
iteroptions.verify_checksums = true;
|
|
|
|
iteroptions.fill_cache = false;
|
|
|
|
syncoptions.sync = true;
|
2012-11-04 17:11:48 +01:00
|
|
|
options = GetOptions(nCacheSize);
|
2012-09-03 19:05:30 +02:00
|
|
|
options.create_if_missing = true;
|
2012-09-04 18:12:00 +02:00
|
|
|
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());
|
|
|
|
}
|
2012-09-03 19:05:30 +02:00
|
|
|
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()));
|
|
|
|
printf("Opened LevelDB successfully\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
CLevelDB::~CLevelDB() {
|
|
|
|
delete pdb;
|
|
|
|
pdb = NULL;
|
|
|
|
delete options.filter_policy;
|
|
|
|
options.filter_policy = NULL;
|
|
|
|
delete options.block_cache;
|
|
|
|
options.block_cache = NULL;
|
|
|
|
delete penv;
|
|
|
|
options.env = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) {
|
|
|
|
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
|
|
|
|
if (!status.ok()) {
|
|
|
|
printf("LevelDB write failure: %s\n", status.ToString().c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|