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;
134 readoptions.verify_checksums =
true;
135 iteroptions.verify_checksums =
true;
136 iteroptions.fill_cache =
false;
137 syncoptions.sync =
true;
139 options.create_if_missing =
true;
141 penv = leveldb::NewMemEnv(leveldb::Env::Default());
146 leveldb::Status result = leveldb::DestroyDB(
fs::PathToString(path), options);
156 leveldb::Status status = leveldb::DB::Open(options,
fs::PathToString(path), &pdb);
158 LogPrintf(
"Opened LevelDB successfully\n");
162 pdb->CompactRange(
nullptr,
nullptr);
167 obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES,
'\000');
169 bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
171 if (!key_exists && obfuscate && IsEmpty()) {
174 std::vector<unsigned char> new_key = CreateObfuscateKey();
177 Write(OBFUSCATE_KEY_KEY, new_key);
178 obfuscate_key = new_key;
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");
These should be considered an implementation detail of the specific database.
#define LogPrint(category,...)
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)
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()
size_t DynamicMemoryUsage() const
void Logv(const char *format, va_list ap) override
leveldb::WriteOptions syncoptions
options used when sync writing to the database
static std::string PathToString(const path &path)
Convert path object to a byte string.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
leveldb::DB * pdb
the database itself
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
#define LogPrintLevel(category, level,...)
leveldb::WriteOptions writeoptions
options used when writing to the database
bool IsEmpty()
Return true if the database managed by this class contains no entries.
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
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
static void SetMaxOpenFiles(leveldb::Options *options)
void GetRandBytes(Span< unsigned char > bytes) noexcept
Overall design of the RNG and entropy sources.
static bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
Return true if log accepts specified category, at the specified level.
bool WriteBatch(CDBBatch &batch, bool fSync=false)
static leveldb::Options GetOptions(size_t nCacheSize)
leveldb::Options options
database options used
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...