10#include <leveldb/cache.h>
11#include <leveldb/env.h>
12#include <leveldb/filter_policy.h>
29 for (
int iter = 0; iter < 2; iter++) {
33 bufsize =
sizeof(buffer);
37 base =
new char[bufsize];
40 char *limit = base + bufsize;
45 va_copy(backup_ap, ap);
48 p += vsnprintf(p, limit - p,
format, backup_ap);
62 if (p == base || p[-1] !=
'\n') {
67 base[std::min(bufsize - 1, (
int)(p - base))] =
'\0';
93 int default_open_files = options->max_open_files;
95 if (
sizeof(
void *) < 8) {
96 options->max_open_files = 64;
100 options->max_open_files, default_open_files);
104 leveldb::Options options;
105 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
107 options.write_buffer_size = nCacheSize / 4;
108 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
109 options.compression = leveldb::kNoCompression;
111 if (leveldb::kMajorVersion > 1 ||
112 (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
115 options.paranoid_checks =
true;
122 : m_name{
fs::
PathToString(params.path.stem())}, m_path{params.path},
123 m_is_memory{params.memory_only} {
130 options.create_if_missing =
true;
132 penv = leveldb::NewMemEnv(leveldb::Env::Default());
137 leveldb::Status result =
148 leveldb::Status status =
151 LogPrintf(
"Opened LevelDB successfully\n");
154 LogPrintf(
"Starting database compaction of %s\n",
156 pdb->CompactRange(
nullptr,
nullptr);
157 LogPrintf(
"Finished database compaction of %s\n",
175 LogPrintf(
"Wrote new obfuscate key for %s: %s\n",
179 LogPrintf(
"Using obfuscation key for %s: %s\n",
187 options.filter_policy =
nullptr;
197 const bool log_memory =
199 double mem_before = 0;
203 leveldb::Status status =
210 "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
211 m_name, mem_before, mem_after);
218 if (!
pdb->GetProperty(
"leveldb.approximate-memory-usage", &memory)) {
220 "Failed to get approximate-memory-usage property\n");
223 return stoul(memory);
247 return !(it->Valid());
254 return piter->Valid();
257 piter->SeekToFirst();
269 const std::string errmsg =
"Fatal LevelDB error: " + status.ToString();
271 LogPrintf(
"You can use -debug=leveldb to get more complete diagnostic "
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
std::vector< uint8_t > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
CDBIterator * NewIterator()
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
CDBWrapper(const DBParams ¶ms)
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
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
static leveldb::Options GetOptions(size_t nCacheSize)
static void SetMaxOpenFiles(leveldb::Options *options)
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
#define LogPrintLevelToBeContinued
#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< 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.
bool force_compact
Compact database on startup.
Application-specific storage settings.
DBOptions options
Passed-through options.
bool obfuscate
If true, store data obfuscated via simple XOR.
bool wipe_data
If true, remove all existing data.
size_t cache_bytes
Configures various leveldb cache settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
bool memory_only
If true, use leveldb's memory environment.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.