2014-02-08 22:50:24 +01:00
|
|
|
// Copyright (c) 2012-2014 The Bitcoin developers
|
2014-11-04 14:34:04 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-09-03 19:05:30 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-11-06 02:58:43 +01:00
|
|
|
#include "leveldbwrapper.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-09-03 19:05:30 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <boost/filesystem.hpp>
|
2014-11-04 14:34:04 +01:00
|
|
|
|
2012-09-03 19:05:30 +02:00
|
|
|
#include <leveldb/cache.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <leveldb/env.h>
|
2012-09-03 19:05:30 +02:00
|
|
|
#include <leveldb/filter_policy.h>
|
2013-09-09 04:02:28 +02:00
|
|
|
#include <memenv.h>
|
2012-09-03 19:05:30 +02:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
void HandleError(const leveldb::Status& status) throw(leveldb_error)
|
|
|
|
{
|
2013-01-29 01:44:19 +01:00
|
|
|
if (status.ok())
|
|
|
|
return;
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("%s\n", status.ToString());
|
2013-01-29 01:44:19 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02: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;
|
2013-04-24 00:10:23 +02:00
|
|
|
options.max_open_files = 64;
|
2014-05-12 12:24:22 +02:00
|
|
|
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
|
|
|
|
// LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
|
|
|
|
// on corruption in later versions.
|
|
|
|
options.paranoid_checks = true;
|
|
|
|
}
|
2012-09-03 19:05:30 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe)
|
|
|
|
{
|
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 {
|
2012-10-21 21:23:13 +02:00
|
|
|
if (fWipe) {
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("Wiping LevelDB in %s\n", path.string());
|
2012-10-21 21:23:13 +02:00
|
|
|
leveldb::DestroyDB(path.string(), options);
|
|
|
|
}
|
2014-03-24 02:14:43 +01:00
|
|
|
TryCreateDirectory(path);
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("Opening LevelDB in %s\n", path.string());
|
2012-09-04 18:12:00 +02:00
|
|
|
}
|
2012-09-03 19:05:30 +02:00
|
|
|
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
|
2013-08-12 10:06:17 +02:00
|
|
|
HandleError(status);
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("Opened LevelDB successfully\n");
|
2012-09-03 19:05:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
CLevelDBWrapper::~CLevelDBWrapper()
|
|
|
|
{
|
2012-09-03 19:05:30 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
bool CLevelDBWrapper::WriteBatch(CLevelDBBatch& batch, bool fSync) throw(leveldb_error)
|
|
|
|
{
|
2012-09-03 19:05:30 +02:00
|
|
|
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
|
2013-08-12 10:06:17 +02:00
|
|
|
HandleError(status);
|
2012-09-03 19:05:30 +02:00
|
|
|
return true;
|
|
|
|
}
|