9 #include <leveldb/cache.h>
10 #include <leveldb/env.h>
11 #include <leveldb/filter_policy.h>
28 for (
int iter = 0; iter < 2; iter++) {
32 bufsize =
sizeof(buffer);
36 base =
new char[bufsize];
39 char *limit = base + bufsize;
44 va_copy(backup_ap, ap);
47 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';
91 int default_open_files = options->max_open_files;
93 if (
sizeof(
void *) < 8) {
94 options->max_open_files = 64;
98 options->max_open_files, default_open_files);
102 leveldb::Options options;
103 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
105 options.write_buffer_size = nCacheSize / 4;
106 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
107 options.compression = leveldb::kNoCompression;
109 if (leveldb::kMajorVersion > 1 ||
110 (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
113 options.paranoid_checks =
true;
120 bool fWipe,
bool obfuscate)
121 : m_name{
fs::
PathToString(path.stem())}, m_path{path}, m_is_memory{
129 options.create_if_missing =
true;
131 penv = leveldb::NewMemEnv(leveldb::Env::Default());
136 leveldb::Status result =
147 leveldb::Status status =
150 LogPrintf(
"Opened LevelDB successfully\n");
153 LogPrintf(
"Starting database compaction of %s\n",
155 pdb->CompactRange(
nullptr,
nullptr);
156 LogPrintf(
"Finished database compaction of %s\n",
165 if (!key_exists && obfuscate &&
IsEmpty()) {
174 LogPrintf(
"Wrote new obfuscate key for %s: %s\n",
186 options.filter_policy =
nullptr;
197 double mem_before = 0;
201 leveldb::Status status =
208 "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
209 m_name, mem_before, mem_after);
216 if (!
pdb->GetProperty(
"leveldb.approximate-memory-usage", &memory)) {
218 "Failed to get approximate-memory-usage property\n");
221 return stoul(memory);
245 return !(it->Valid());
252 return piter->Valid();
255 piter->SeekToFirst();
267 const std::string errmsg =
"Fatal LevelDB error: " + status.ToString();
269 LogPrintf(
"You can use -debug=leveldb to get more complete diagnostic "
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
void Logv(const char *format, va_list ap) override
Batch of changes queued to be written to a CDBWrapper.
leveldb::WriteBatch batch
leveldb::Iterator * piter
CDBIterator * NewIterator()
size_t DynamicMemoryUsage() const
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment)
bool WriteBatch(CDBBatch &batch, bool fSync=false)
bool Read(const K &key, V &value) const
std::vector< uint8_t > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
std::string m_name
the name of this database
bool Write(const K &key, const V &value, bool fSync=false)
std::vector< uint8_t > 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
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
leveldb::DB * pdb
the database itself
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
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static leveldb::Options GetOptions(size_t nCacheSize)
static void SetMaxOpenFiles(leveldb::Options *options)
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
#define LogPrint(category,...)
#define LogPrintfToBeContinued
These are aliases used to explicitly state that the message should not end with a newline character.
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< uint8_t > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Filesystem operations and types.
static std::string PathToString(const path &path)
Convert path object to byte string.
void GetRandBytes(Span< uint8_t > bytes) noexcept
Overall design of the RNG and entropy sources.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.