10#include <leveldb/cache.h>
11#include <leveldb/env.h>
12#include <leveldb/filter_policy.h>
62 if (
p == base ||
p[-1] !=
'\n') {
67 base[std::min(
bufsize - 1, (
int)(
p - base))] =
'\0';
94 if (
sizeof(
void *) < 8) {
95 options->max_open_files = 64;
103 leveldb::Options options;
104 options.block_cache = leveldb::NewLRUCache(
nCacheSize / 2);
107 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
108 options.compression = leveldb::kNoCompression;
110 if (leveldb::kMajorVersion > 1 ||
111 (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
114 options.paranoid_checks =
true;
121 : m_name{
fs::PathToString(params.path.
stem())}, m_path{params.path},
122 m_is_memory{params.memory_only} {
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",
174 LogPrintf(
"Wrote new obfuscate key for %s: %s\n",
178 LogPrintf(
"Using obfuscation key for %s: %s\n",
186 options.filter_policy =
nullptr;
201 leveldb::Status status =
208 "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
216 if (!
pdb->GetProperty(
"leveldb.approximate-memory-usage", &
memory)) {
218 "Failed to get approximate-memory-usage property\n");
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 "
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.
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.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
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.