10 #include <boost/filesystem.hpp>
12 #include <leveldb/cache.h>
13 #include <leveldb/env.h>
14 #include <leveldb/filter_policy.h>
18 static leveldb::Options GetOptions(
size_t nCacheSize)
20 leveldb::Options options;
21 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
22 options.write_buffer_size = nCacheSize / 4;
23 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
24 options.compression = leveldb::kNoCompression;
25 options.max_open_files = 64;
26 if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
29 options.paranoid_checks =
true;
34 CDBWrapper::CDBWrapper(
const boost::filesystem::path& path,
size_t nCacheSize,
bool fMemory,
bool fWipe,
bool obfuscate)
41 options = GetOptions(nCacheSize);
42 options.create_if_missing =
true;
44 penv = leveldb::NewMemEnv(leveldb::Env::Default());
48 LogPrintf(
"Wiping LevelDB in %s\n", path.string());
49 leveldb::Status result = leveldb::DestroyDB(path.string(),
options);
53 LogPrintf(
"Opening LevelDB in %s\n", path.string());
55 leveldb::Status status = leveldb::DB::Open(
options, path.string(), &
pdb);
57 LogPrintf(
"Opened LevelDB successfully\n");
64 if (!key_exists && obfuscate &&
IsEmpty()) {
122 return !(it->Valid());
137 if (status.IsCorruption())
139 if (status.IsIOError())
141 if (status.IsNotFound())
Batch of changes queued to be written to a CDBWrapper.
leveldb::WriteBatch batch
leveldb::Iterator * piter
CDBIterator * NewIterator()
CDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
leveldb::Env * penv
custom environment this database is using (may be NULL in case of default environment)
bool WriteBatch(CDBBatch &batch, bool fSync=false)
bool Read(const K &key, V &value) const
bool Write(const K &key, const V &value, bool fSync=false)
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
leveldb::Options options
database options used
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
leveldb::WriteOptions writeoptions
options used when writing to the database
leveldb::WriteOptions syncoptions
options used when sync writing to the database
leveldb::DB * pdb
the database itself
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
bool IsEmpty()
Return true if the database managed by this class contains no entries.
leveldb::ReadOptions readoptions
options used when reading from the database
These should be considered an implementation detail of the specific database.
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
bool TryCreateDirectory(const boost::filesystem::path &p)
Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)