11 #include <leveldb/cache.h> 12 #include <leveldb/env.h> 13 #include <leveldb/filter_policy.h> 22 virtual void Logv(
const char *
format, va_list ap)
override {
27 for (
int iter = 0; iter < 2; iter++) {
31 bufsize =
sizeof(buffer);
36 base =
new char[bufsize];
39 char* limit = base + bufsize;
44 va_copy(backup_ap, ap);
46 p += vsnprintf(p, limit - p, format, backup_ap);
61 if (p == base || p[-1] !=
'\n') {
66 base[
std::min(bufsize - 1, (
int)(p - base))] =
'\0';
76 static leveldb::Options GetOptions(
size_t nCacheSize)
78 leveldb::Options options;
79 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
80 options.write_buffer_size = nCacheSize / 4;
81 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
82 options.compression = leveldb::kNoCompression;
83 options.max_open_files = 64;
85 if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
88 options.paranoid_checks =
true;
96 readoptions.verify_checksums =
true;
97 iteroptions.verify_checksums =
true;
98 iteroptions.fill_cache =
false;
99 syncoptions.sync =
true;
100 options = GetOptions(nCacheSize);
101 options.create_if_missing =
true;
103 penv = leveldb::NewMemEnv(leveldb::Env::Default());
107 LogPrintf(
"Wiping LevelDB in %s\n", path.string());
108 leveldb::Status result = leveldb::DestroyDB(path.string(), options);
112 LogPrintf(
"Opening LevelDB in %s\n", path.string());
114 leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
116 LogPrintf(
"Opened LevelDB successfully\n");
119 LogPrintf(
"Starting database compaction of %s\n", path.string());
120 pdb->CompactRange(
nullptr,
nullptr);
121 LogPrintf(
"Finished database compaction of %s\n", path.string());
125 obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES,
'\000');
127 bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
129 if (!key_exists && obfuscate && IsEmpty()) {
132 std::vector<unsigned char> new_key = CreateObfuscateKey();
135 Write(OBFUSCATE_KEY_KEY, new_key);
136 obfuscate_key = new_key;
138 LogPrintf(
"Wrote new obfuscate key for %s: %s\n", path.string(),
HexStr(obfuscate_key));
141 LogPrintf(
"Using obfuscation key for %s: %s\n", path.string(),
HexStr(obfuscate_key));
148 delete options.filter_policy;
149 options.filter_policy =
nullptr;
150 delete options.info_log;
151 options.info_log =
nullptr;
152 delete options.block_cache;
153 options.block_cache =
nullptr;
155 options.env =
nullptr;
160 leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.
batch);
179 unsigned char buff[OBFUSCATE_KEY_NUM_BYTES];
181 return std::vector<unsigned char>(&buff[0], &buff[OBFUSCATE_KEY_NUM_BYTES]);
187 std::unique_ptr<CDBIterator> it(NewIterator());
189 return !(it->Valid());
204 if (status.IsCorruption())
206 if (status.IsIOError())
208 if (status.IsNotFound())
These should be considered an implementation detail of the specific database.
Batch of changes queued to be written to a CDBWrapper.
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
assert(len-trim+(2 *lenIndices)<=WIDTH)
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
leveldb::WriteBatch batch
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key...
virtual void Logv(const char *format, va_list ap) override
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost's create_directories if the requested directory exists...
bool IsEmpty()
Return true if the database managed by this class contains no entries.
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
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
int LogPrintStr(const std::string &str, bool useVMLog)
Send a string to the log output.
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
bool WriteBatch(CDBBatch &batch, bool fSync=false)
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database