Changeset View
Changeset View
Standalone View
Standalone View
src/dbwrapper.cpp
Show First 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | if (leveldb::kMajorVersion > 1 || | ||||
// LevelDB versions before 1.16 consider short writes to be corruption. | // LevelDB versions before 1.16 consider short writes to be corruption. | ||||
// Only trigger error on corruption in later versions. | // Only trigger error on corruption in later versions. | ||||
options.paranoid_checks = true; | options.paranoid_checks = true; | ||||
} | } | ||||
return options; | return options; | ||||
} | } | ||||
CDBWrapper::CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory, | CDBWrapper::CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory, | ||||
bool fWipe, bool obfuscate) { | bool fWipe, bool obfuscate) | ||||
: m_name(fs::basename(path)) { | |||||
penv = nullptr; | penv = nullptr; | ||||
readoptions.verify_checksums = true; | readoptions.verify_checksums = true; | ||||
iteroptions.verify_checksums = true; | iteroptions.verify_checksums = true; | ||||
iteroptions.fill_cache = false; | iteroptions.fill_cache = false; | ||||
syncoptions.sync = true; | syncoptions.sync = true; | ||||
options = GetOptions(nCacheSize); | options = GetOptions(nCacheSize); | ||||
options.create_if_missing = true; | options.create_if_missing = true; | ||||
if (fMemory) { | if (fMemory) { | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | CDBWrapper::~CDBWrapper() { | ||||
options.info_log = nullptr; | options.info_log = nullptr; | ||||
delete options.block_cache; | delete options.block_cache; | ||||
options.block_cache = nullptr; | options.block_cache = nullptr; | ||||
delete penv; | delete penv; | ||||
options.env = nullptr; | options.env = nullptr; | ||||
} | } | ||||
bool CDBWrapper::WriteBatch(CDBBatch &batch, bool fSync) { | bool CDBWrapper::WriteBatch(CDBBatch &batch, bool fSync) { | ||||
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB); | |||||
double mem_before = 0; | |||||
if (log_memory) { | |||||
mem_before = DynamicMemoryUsage() / 1024.0 / 1024; | |||||
} | |||||
leveldb::Status status = | leveldb::Status status = | ||||
pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch); | pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch); | ||||
dbwrapper_private::HandleError(status); | dbwrapper_private::HandleError(status); | ||||
if (log_memory) { | |||||
double mem_after = DynamicMemoryUsage() / 1024.0 / 1024; | |||||
LogPrint( | |||||
BCLog::LEVELDB, | |||||
"WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n", | |||||
m_name, mem_before, mem_after); | |||||
} | |||||
return true; | return true; | ||||
} | } | ||||
size_t CDBWrapper::DynamicMemoryUsage() const { | |||||
std::string memory; | |||||
if (!pdb->GetProperty("leveldb.approximate-memory-usage", &memory)) { | |||||
LogPrint(BCLog::LEVELDB, | |||||
"Failed to get approximate-memory-usage property\n"); | |||||
return 0; | |||||
} | |||||
return stoul(memory); | |||||
} | |||||
// Prefixed with null character to avoid collisions with other keys | // Prefixed with null character to avoid collisions with other keys | ||||
// | // | ||||
// We must use a string constructor which specifies length so that we copy past | // We must use a string constructor which specifies length so that we copy past | ||||
// the null-terminator. | // the null-terminator. | ||||
const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14); | const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14); | ||||
const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8; | const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8; | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |