19 #include <leveldb/cache.h>
20 #include <leveldb/db.h>
21 #include <leveldb/env.h>
22 #include <leveldb/filter_policy.h>
23 #include <leveldb/helpers/memenv/memenv.h>
24 #include <leveldb/iterator.h>
25 #include <leveldb/options.h>
26 #include <leveldb/status.h>
39 for (
int iter = 0; iter < 2; iter++) {
43 bufsize =
sizeof(buffer);
48 base =
new char[bufsize];
51 char* limit = base + bufsize;
56 va_copy(backup_ap, ap);
58 p += vsnprintf(p, limit - p,
format, backup_ap);
73 if (p == base || p[-1] !=
'\n') {
78 base[std::min(bufsize - 1, (
int)(p - base))] =
'\0';
103 int default_open_files = options->max_open_files;
105 if (
sizeof(
void*) < 8) {
106 options->max_open_files = 64;
110 options->max_open_files, default_open_files);
115 leveldb::Options options;
116 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
117 options.write_buffer_size = nCacheSize / 4;
118 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
119 options.compression = leveldb::kNoCompression;
121 if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
124 options.paranoid_checks =
true;
131 : m_name{
fs::
PathToString(path.stem())}, m_path{path}, m_is_memory{fMemory}
139 options.create_if_missing =
true;
141 penv = leveldb::NewMemEnv(leveldb::Env::Default());
158 LogPrintf(
"Opened LevelDB successfully\n");
162 pdb->CompactRange(
nullptr,
nullptr);
171 if (!key_exists && obfuscate &&
IsEmpty()) {
191 options.filter_policy =
nullptr;
203 double mem_before = 0;
212 m_name, mem_before, mem_after);
220 std::optional<size_t> parsed;
221 if (!
pdb->GetProperty(
"leveldb.approximate-memory-usage", &memory) || !(parsed = ToIntegral<size_t>(memory))) {
225 return parsed.value();
251 return !(it->Valid());
265 const std::string errmsg =
"Fatal LevelDB error: " + status.ToString();
267 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
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::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 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)
#define LogPrintLevel(category, level,...)
#define LogPrint(category,...)
static bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
Return true if log accepts specified category, at the specified level.
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 a byte string.
void GetRandBytes(Span< unsigned char > 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.