You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.6 KiB
78 lines
2.6 KiB
![]()
11 years ago
|
// Copyright (c) 2012-2013 The Bitcoin developers
|
||
![]()
12 years ago
|
// Distributed under the MIT/X11 software license, see the accompanying
|
||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
|
|
||
![]()
11 years ago
|
#include "leveldbwrapper.h"
|
||
![]()
12 years ago
|
#include "util.h"
|
||
|
|
||
|
#include <leveldb/env.h>
|
||
|
#include <leveldb/cache.h>
|
||
|
#include <leveldb/filter_policy.h>
|
||
![]()
12 years ago
|
#include <memenv/memenv.h>
|
||
![]()
12 years ago
|
|
||
|
#include <boost/filesystem.hpp>
|
||
|
|
||
![]()
12 years ago
|
void HandleError(const leveldb::Status &status) throw(leveldb_error) {
|
||
|
if (status.ok())
|
||
|
return;
|
||
![]()
11 years ago
|
LogPrintf("%s\n", status.ToString().c_str());
|
||
![]()
12 years ago
|
if (status.IsCorruption())
|
||
|
throw leveldb_error("Database corrupted");
|
||
|
if (status.IsIOError())
|
||
|
throw leveldb_error("Database I/O error");
|
||
|
if (status.IsNotFound())
|
||
|
throw leveldb_error("Database entry missing");
|
||
|
throw leveldb_error("Unknown database error");
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
static leveldb::Options GetOptions(size_t nCacheSize) {
|
||
![]()
12 years ago
|
leveldb::Options options;
|
||
![]()
12 years ago
|
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
|
||
|
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
|
||
![]()
12 years ago
|
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
|
||
|
options.compression = leveldb::kNoCompression;
|
||
![]()
12 years ago
|
options.max_open_files = 64;
|
||
![]()
12 years ago
|
return options;
|
||
|
}
|
||
|
|
||
![]()
11 years ago
|
CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) {
|
||
![]()
12 years ago
|
penv = NULL;
|
||
|
readoptions.verify_checksums = true;
|
||
|
iteroptions.verify_checksums = true;
|
||
|
iteroptions.fill_cache = false;
|
||
|
syncoptions.sync = true;
|
||
![]()
12 years ago
|
options = GetOptions(nCacheSize);
|
||
![]()
12 years ago
|
options.create_if_missing = true;
|
||
![]()
12 years ago
|
if (fMemory) {
|
||
|
penv = leveldb::NewMemEnv(leveldb::Env::Default());
|
||
|
options.env = penv;
|
||
|
} else {
|
||
![]()
12 years ago
|
if (fWipe) {
|
||
![]()
11 years ago
|
LogPrintf("Wiping LevelDB in %s\n", path.string().c_str());
|
||
![]()
12 years ago
|
leveldb::DestroyDB(path.string(), options);
|
||
|
}
|
||
![]()
12 years ago
|
boost::filesystem::create_directory(path);
|
||
![]()
11 years ago
|
LogPrintf("Opening LevelDB in %s\n", path.string().c_str());
|
||
![]()
12 years ago
|
}
|
||
![]()
12 years ago
|
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
|
||
![]()
11 years ago
|
HandleError(status);
|
||
![]()
11 years ago
|
LogPrintf("Opened LevelDB successfully\n");
|
||
![]()
12 years ago
|
}
|
||
|
|
||
![]()
11 years ago
|
CLevelDBWrapper::~CLevelDBWrapper() {
|
||
![]()
12 years ago
|
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;
|
||
|
}
|
||
|
|
||
![]()
11 years ago
|
bool CLevelDBWrapper::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error) {
|
||
![]()
12 years ago
|
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
|
||
![]()
11 years ago
|
HandleError(status);
|
||
![]()
12 years ago
|
return true;
|
||
|
}
|