10 #include <leveldb/cache.h> 11 #include <leveldb/env.h> 12 #include <leveldb/filter_policy.h> 26 for (
int iter = 0; iter < 2; iter++) {
30 bufsize =
sizeof(buffer);
35 base =
new char[bufsize];
38 char* limit = base + bufsize;
43 va_copy(backup_ap, ap);
45 p += vsnprintf(p, limit - p,
format, backup_ap);
60 if (p == base || p[-1] !=
'\n') {
65 base[std::min(bufsize - 1, (
int)(p - base))] =
'\0';
66 LogPrintf(
"leveldb: %s", base);
75 static void SetMaxOpenFiles(leveldb::Options *options) {
90 int default_open_files = options->max_open_files;
92 if (
sizeof(
void*) < 8) {
93 options->max_open_files = 64;
96 LogPrint(
BCLog::LEVELDB,
"LevelDB using max_open_files=%d (default=%d)\n",
97 options->max_open_files, default_open_files);
100 static leveldb::Options GetOptions(
size_t nCacheSize)
102 leveldb::Options options;
103 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
104 options.write_buffer_size = nCacheSize / 4;
105 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
106 options.compression = leveldb::kNoCompression;
108 if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
111 options.paranoid_checks =
true;
113 SetMaxOpenFiles(&options);
118 : m_name(fs::basename(path))
125 options = GetOptions(nCacheSize);
126 options.create_if_missing =
true;
128 penv = leveldb::NewMemEnv(leveldb::Env::Default());
132 LogPrintf(
"Wiping LevelDB in %s\n", path.string());
133 leveldb::Status result = leveldb::DestroyDB(path.string(),
options);
137 LogPrintf(
"Opening LevelDB in %s\n", path.string());
139 leveldb::Status status = leveldb::DB::Open(
options, path.string(), &
pdb);
141 LogPrintf(
"Opened LevelDB successfully\n");
144 LogPrintf(
"Starting database compaction of %s\n", path.string());
145 pdb->CompactRange(
nullptr,
nullptr);
146 LogPrintf(
"Finished database compaction of %s\n", path.string());
154 if (!key_exists && obfuscate &&
IsEmpty()) {
174 options.filter_policy =
nullptr;
186 double mem_before = 0;
194 LogPrint(
BCLog::LEVELDB,
"WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
195 m_name, mem_before, mem_after);
202 if (!
pdb->GetProperty(
"leveldb.approximate-memory-usage", &memory)) {
203 LogPrint(
BCLog::LEVELDB,
"Failed to get approximate-memory-usage property\n");
206 return stoul(memory);
233 return !(it->Valid());
247 const std::string errmsg =
"Fatal LevelDB error: " + status.ToString();
248 LogPrintf(
"%s\n", errmsg);
249 LogPrintf(
"You can use -debug=leveldb to get more complete diagnostic messages\n");
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) const
Return boolean argument or default value.
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...
CDBIterator * NewIterator()
leveldb::ReadOptions readoptions
options used when reading from the database
size_t DynamicMemoryUsage() const
void Logv(const char *format, va_list ap) override
leveldb::WriteOptions syncoptions
options used when sync writing to the database
leveldb::DB * pdb
the database itself
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost's create_directories if the requested directory exists...
leveldb::WriteOptions writeoptions
options used when writing to the database
bool IsEmpty()
Return true if the database managed by this class contains no entries.
bool Read(const K &key, V &value) const
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
leveldb::Iterator * piter
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
bool Write(const K &key, const V &value, bool fSync=false)
std::string m_name
the name of this database
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment) ...
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
bool WriteBatch(CDBBatch &batch, bool fSync=false)
leveldb::Options options
database options used
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database