 |
Bitcoin Core
21.99.0
P2P Digital Currency
|
Go to the documentation of this file.
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;
118 : m_name{path.stem().string()}
121 readoptions.verify_checksums =
true;
122 iteroptions.verify_checksums =
true;
123 iteroptions.fill_cache =
false;
124 syncoptions.sync =
true;
126 options.create_if_missing =
true;
128 penv = leveldb::NewMemEnv(leveldb::Env::Default());
132 LogPrintf(
"Wiping LevelDB in %s\n", path.string());
133 leveldb::Status result = leveldb::DestroyDB(path.string(), options);
137 LogPrintf(
"Opening LevelDB in %s\n", path.string());
139 leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
141 LogPrintf(
"Opened LevelDB successfully\n");
144 LogPrintf(
"Starting database compaction of %s\n", path.string());
145 pdb->CompactRange(
nullptr,
nullptr);
146 LogPrintf(
"Finished database compaction of %s\n", path.string());
150 obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES,
'\000');
152 bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
154 if (!key_exists && obfuscate && IsEmpty()) {
157 std::vector<unsigned char> new_key = CreateObfuscateKey();
160 Write(OBFUSCATE_KEY_KEY, new_key);
161 obfuscate_key = new_key;
163 LogPrintf(
"Wrote new obfuscate key for %s: %s\n", path.string(),
HexStr(obfuscate_key));
166 LogPrintf(
"Using obfuscation key for %s: %s\n", path.string(),
HexStr(obfuscate_key));
174 options.filter_policy =
nullptr;
186 double mem_before = 0;
195 m_name, mem_before, mem_after);
202 if (!
pdb->GetProperty(
"leveldb.approximate-memory-usage", &memory)) {
206 return stoul(memory);
233 return !(
it->Valid());
247 const std::string errmsg =
"Fatal LevelDB error: " + status.ToString();
249 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.
Batch of changes queued to be written to a CDBWrapper.
leveldb::WriteOptions syncoptions
options used when sync writing to the database
static void SetMaxOpenFiles(leveldb::Options *options)
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
static leveldb::Options GetOptions(size_t nCacheSize)
CDBIterator * NewIterator()
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
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
leveldb::WriteBatch batch
leveldb::DB * pdb
the database itself
These should be considered an implementation detail of the specific database.
leveldb::Iterator * piter
#define LogPrint(category,...)
size_t DynamicMemoryUsage() const
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
leveldb::WriteOptions writeoptions
options used when writing to the database
std::string m_name
the name of this database
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
leveldb::Options options
database options used
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
bool IsEmpty()
Return true if the database managed by this class contains no entries.
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
void Logv(const char *format, va_list ap) override
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
bool WriteBatch(CDBBatch &batch, bool fSync=false)