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';
90 int default_open_files = options->max_open_files;
92 if (
sizeof(
void*) < 8) {
93 options->max_open_files = 64;
97 options->max_open_files, default_open_files);
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;
126 options.create_if_missing =
true;
128 penv = leveldb::NewMemEnv(leveldb::Env::Default());
141 LogPrintf(
"Opened LevelDB successfully\n");
145 pdb->CompactRange(
nullptr,
nullptr);
154 if (!key_exists && obfuscate &&
IsEmpty()) {
174 options.filter_policy =
nullptr;
186 double mem_before = 0;
195 m_name, mem_before, mem_after);
203 std::optional<size_t> parsed;
204 if (!
pdb->GetProperty(
"leveldb.approximate-memory-usage", &memory) || !(parsed = ToIntegral<size_t>(memory))) {
208 return parsed.value();
234 return !(it->Valid());
248 const std::string errmsg =
"Fatal LevelDB error: " + status.ToString();
250 LogPrintf(
"You can use -debug=leveldb to get more complete diagnostic messages\n");
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
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
CDBIterator * NewIterator()
std::string m_name
the name of this database
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
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
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
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
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,...)
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.
Filesystem operations and types.
static std::string PathToString(const path &path)
Convert path object to byte string.
void GetRandBytes(unsigned char *buf, int num) 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 Boost's create_directories if the requested directory exists.