attempted optimization of dbwrapper
it will only work for single-threaded access
This commit is contained in:
parent
4a5d310fd3
commit
d96253cd40
3 changed files with 109 additions and 109 deletions
|
@ -100,10 +100,10 @@ static void SetMaxOpenFiles(leveldb::Options *options) {
|
||||||
static leveldb::Options GetOptions(size_t nCacheSize)
|
static leveldb::Options GetOptions(size_t nCacheSize)
|
||||||
{
|
{
|
||||||
leveldb::Options options;
|
leveldb::Options options;
|
||||||
auto write_cache = std::min(nCacheSize / 4, size_t(16) << 20U); // cap write_cache at 16MB (4x default)
|
auto write_cache = std::min(nCacheSize / 4, size_t(32 * 1024 * 1024)); // cap write_cache
|
||||||
options.block_cache = leveldb::NewLRUCache(nCacheSize - write_cache * 2);
|
options.block_cache = leveldb::NewLRUCache(nCacheSize - write_cache * 2);
|
||||||
options.write_buffer_size = write_cache; // up to two write buffers may be held in memory simultaneously
|
options.write_buffer_size = write_cache; // up to two write buffers may be held in memory simultaneously
|
||||||
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
|
options.filter_policy = leveldb::NewBloomFilterPolicy(12);
|
||||||
options.compression = leveldb::kNoCompression;
|
options.compression = leveldb::kNoCompression;
|
||||||
options.info_log = new CBitcoinLevelDBLogger();
|
options.info_log = new CBitcoinLevelDBLogger();
|
||||||
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
|
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
|
||||||
|
@ -116,7 +116,7 @@ static leveldb::Options GetOptions(size_t nCacheSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
|
CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
|
||||||
: m_name(fs::basename(path))
|
: m_name(fs::basename(path)), ssKey(SER_DISK, CLIENT_VERSION), ssValue(SER_DISK, CLIENT_VERSION)
|
||||||
{
|
{
|
||||||
penv = nullptr;
|
penv = nullptr;
|
||||||
readoptions.verify_checksums = true;
|
readoptions.verify_checksums = true;
|
||||||
|
@ -181,6 +181,11 @@ CDBWrapper::~CDBWrapper()
|
||||||
options.env = nullptr;
|
options.env = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CDBWrapper::Sync() {
|
||||||
|
CDBBatch batch(*this);
|
||||||
|
return WriteBatch(batch, true);
|
||||||
|
}
|
||||||
|
|
||||||
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
|
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
|
||||||
{
|
{
|
||||||
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB);
|
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB);
|
||||||
|
|
203
src/dbwrapper.h
203
src/dbwrapper.h
|
@ -16,9 +16,6 @@
|
||||||
#include <leveldb/db.h>
|
#include <leveldb/db.h>
|
||||||
#include <leveldb/write_batch.h>
|
#include <leveldb/write_batch.h>
|
||||||
|
|
||||||
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
|
|
||||||
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
|
|
||||||
|
|
||||||
class dbwrapper_error : public std::runtime_error
|
class dbwrapper_error : public std::runtime_error
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -33,87 +30,16 @@ namespace dbwrapper_private {
|
||||||
|
|
||||||
/** Handle database error by throwing dbwrapper_error exception.
|
/** Handle database error by throwing dbwrapper_error exception.
|
||||||
*/
|
*/
|
||||||
void HandleError(const leveldb::Status& status);
|
void HandleError(const leveldb::Status& status);
|
||||||
|
|
||||||
/** Work around circular dependency, as well as for testing in dbwrapper_tests.
|
/** Work around circular dependency, as well as for testing in dbwrapper_tests.
|
||||||
* Database obfuscation should be considered an implementation detail of the
|
* Database obfuscation should be considered an implementation detail of the
|
||||||
* specific database.
|
* specific database.
|
||||||
*/
|
*/
|
||||||
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w);
|
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Batch of changes queued to be written to a CDBWrapper */
|
|
||||||
class CDBBatch
|
|
||||||
{
|
|
||||||
friend class CDBWrapper;
|
|
||||||
|
|
||||||
private:
|
|
||||||
const CDBWrapper &parent;
|
|
||||||
leveldb::WriteBatch batch;
|
|
||||||
|
|
||||||
CDataStream ssKey;
|
|
||||||
CDataStream ssValue;
|
|
||||||
|
|
||||||
size_t size_estimate;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @param[in] _parent CDBWrapper that this batch is to be submitted to
|
|
||||||
*/
|
|
||||||
explicit CDBBatch(const CDBWrapper &_parent) : parent(_parent), ssKey(SER_DISK, CLIENT_VERSION), ssValue(SER_DISK, CLIENT_VERSION), size_estimate(0) { };
|
|
||||||
|
|
||||||
void Clear()
|
|
||||||
{
|
|
||||||
batch.Clear();
|
|
||||||
size_estimate = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename K, typename V>
|
|
||||||
void Write(const K& key, const V& value)
|
|
||||||
{
|
|
||||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
|
||||||
ssKey << key;
|
|
||||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
|
||||||
|
|
||||||
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
|
|
||||||
ssValue << value;
|
|
||||||
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
|
||||||
leveldb::Slice slValue(ssValue.data(), ssValue.size());
|
|
||||||
|
|
||||||
batch.Put(slKey, slValue);
|
|
||||||
// LevelDB serializes writes as:
|
|
||||||
// - byte: header
|
|
||||||
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
|
|
||||||
// - byte[]: key
|
|
||||||
// - varint: value length
|
|
||||||
// - byte[]: value
|
|
||||||
// The formula below assumes the key and value are both less than 16k.
|
|
||||||
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
|
|
||||||
ssKey.clear();
|
|
||||||
ssValue.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename K>
|
|
||||||
void Erase(const K& key)
|
|
||||||
{
|
|
||||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
|
||||||
ssKey << key;
|
|
||||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
|
||||||
|
|
||||||
batch.Delete(slKey);
|
|
||||||
// LevelDB serializes erases as:
|
|
||||||
// - byte: header
|
|
||||||
// - varint: key length
|
|
||||||
// - byte[]: key
|
|
||||||
// The formula below assumes the key is less than 16kB.
|
|
||||||
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
|
|
||||||
ssKey.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t SizeEstimate() const { return size_estimate; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class CDBIterator
|
class CDBIterator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
@ -127,7 +53,7 @@ public:
|
||||||
* @param[in] _piter The original leveldb iterator.
|
* @param[in] _piter The original leveldb iterator.
|
||||||
*/
|
*/
|
||||||
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter) :
|
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter) :
|
||||||
parent(_parent), piter(_piter) { };
|
parent(_parent), piter(_piter) { };
|
||||||
~CDBIterator();
|
~CDBIterator();
|
||||||
|
|
||||||
bool Valid() const;
|
bool Valid() const;
|
||||||
|
@ -135,10 +61,10 @@ public:
|
||||||
void SeekToFirst();
|
void SeekToFirst();
|
||||||
|
|
||||||
template<typename K> void Seek(const K& key) {
|
template<typename K> void Seek(const K& key) {
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey1(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
// ssKey1.reserve(parent.ssKey.capacity());
|
||||||
ssKey << key;
|
ssKey1 << key;
|
||||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
leveldb::Slice slKey(ssKey1.data(), ssKey1.size());
|
||||||
piter->Seek(slKey);
|
piter->Seek(slKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,6 +99,8 @@ public:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CDBBatch;
|
||||||
|
|
||||||
class CDBWrapper
|
class CDBWrapper
|
||||||
{
|
{
|
||||||
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
|
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
|
||||||
|
@ -213,6 +141,8 @@ private:
|
||||||
std::vector<unsigned char> CreateObfuscateKey() const;
|
std::vector<unsigned char> CreateObfuscateKey() const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
mutable CDataStream ssKey, ssValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param[in] path Location in the filesystem where leveldb data will be stored.
|
* @param[in] path Location in the filesystem where leveldb data will be stored.
|
||||||
* @param[in] nCacheSize Configures various leveldb cache settings.
|
* @param[in] nCacheSize Configures various leveldb cache settings.
|
||||||
|
@ -230,13 +160,12 @@ public:
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
bool Read(const K& key, V& value) const
|
bool Read(const K& key, V& value) const
|
||||||
{
|
{
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
||||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
||||||
|
|
||||||
std::string strValue;
|
std::string strValue;
|
||||||
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
||||||
|
ssKey.clear();
|
||||||
if (!status.ok()) {
|
if (!status.ok()) {
|
||||||
if (status.IsNotFound())
|
if (status.IsNotFound())
|
||||||
return false;
|
return false;
|
||||||
|
@ -254,23 +183,17 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
bool Write(const K& key, const V& value, bool fSync = false)
|
bool Write(const K& key, const V& value, bool fSync = false);
|
||||||
{
|
|
||||||
CDBBatch batch(*this);
|
|
||||||
batch.Write(key, value);
|
|
||||||
return WriteBatch(batch, fSync);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename K>
|
template <typename K>
|
||||||
bool Exists(const K& key) const
|
bool Exists(const K& key) const
|
||||||
{
|
{
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
||||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
||||||
|
|
||||||
std::string strValue;
|
std::string strValue;
|
||||||
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
||||||
|
ssKey.clear();
|
||||||
if (!status.ok()) {
|
if (!status.ok()) {
|
||||||
if (status.IsNotFound())
|
if (status.IsNotFound())
|
||||||
return false;
|
return false;
|
||||||
|
@ -281,12 +204,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename K>
|
template <typename K>
|
||||||
bool Erase(const K& key, bool fSync = false)
|
bool Erase(const K& key, bool fSync = false);
|
||||||
{
|
|
||||||
CDBBatch batch(*this);
|
|
||||||
batch.Erase(key);
|
|
||||||
return WriteBatch(batch, fSync);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WriteBatch(CDBBatch& batch, bool fSync = false);
|
bool WriteBatch(CDBBatch& batch, bool fSync = false);
|
||||||
|
|
||||||
|
@ -299,11 +217,7 @@ public:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Sync()
|
bool Sync();
|
||||||
{
|
|
||||||
CDBBatch batch(*this);
|
|
||||||
return WriteBatch(batch, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
CDBIterator *NewIterator()
|
CDBIterator *NewIterator()
|
||||||
{
|
{
|
||||||
|
@ -319,8 +233,6 @@ public:
|
||||||
size_t EstimateSize(const K& key_begin, const K& key_end) const
|
size_t EstimateSize(const K& key_begin, const K& key_end) const
|
||||||
{
|
{
|
||||||
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
|
||||||
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
|
||||||
ssKey1 << key_begin;
|
ssKey1 << key_begin;
|
||||||
ssKey2 << key_end;
|
ssKey2 << key_end;
|
||||||
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
|
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
|
||||||
|
@ -338,8 +250,8 @@ public:
|
||||||
void CompactRange(const K& key_begin, const K& key_end) const
|
void CompactRange(const K& key_begin, const K& key_end) const
|
||||||
{
|
{
|
||||||
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
ssKey1.reserve(ssKey.capacity());
|
||||||
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
ssKey2.reserve(ssKey.capacity());
|
||||||
ssKey1 << key_begin;
|
ssKey1 << key_begin;
|
||||||
ssKey2 << key_end;
|
ssKey2 << key_end;
|
||||||
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
|
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
|
||||||
|
@ -349,4 +261,83 @@ public:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Batch of changes queued to be written to a CDBWrapper */
|
||||||
|
class CDBBatch
|
||||||
|
{
|
||||||
|
friend class CDBWrapper;
|
||||||
|
const CDBWrapper &parent;
|
||||||
|
leveldb::WriteBatch batch;
|
||||||
|
|
||||||
|
size_t size_estimate;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @param[in] _parent CDBWrapper that this batch is to be submitted to
|
||||||
|
*/
|
||||||
|
explicit CDBBatch(const CDBWrapper &_parent) : parent(_parent), size_estimate(0) { };
|
||||||
|
|
||||||
|
void Clear()
|
||||||
|
{
|
||||||
|
batch.Clear();
|
||||||
|
size_estimate = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void Write(const K& key, const V& value)
|
||||||
|
{
|
||||||
|
assert(parent.ssKey.empty());
|
||||||
|
parent.ssKey << key;
|
||||||
|
leveldb::Slice slKey(parent.ssKey.data(), parent.ssKey.size());
|
||||||
|
|
||||||
|
assert(parent.ssValue.empty());
|
||||||
|
parent.ssValue << value;
|
||||||
|
parent.ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
||||||
|
leveldb::Slice slValue(parent.ssValue.data(), parent.ssValue.size());
|
||||||
|
|
||||||
|
batch.Put(slKey, slValue);
|
||||||
|
// LevelDB serializes writes as:
|
||||||
|
// - byte: header
|
||||||
|
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
|
||||||
|
// - byte[]: key
|
||||||
|
// - varint: value length
|
||||||
|
// - byte[]: value
|
||||||
|
// The formula below assumes the key and value are both less than 16k.
|
||||||
|
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
|
||||||
|
parent.ssKey.clear();
|
||||||
|
parent.ssValue.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K>
|
||||||
|
void Erase(const K& key)
|
||||||
|
{
|
||||||
|
parent.ssKey << key;
|
||||||
|
leveldb::Slice slKey(parent.ssKey.data(), parent.ssKey.size());
|
||||||
|
|
||||||
|
batch.Delete(slKey);
|
||||||
|
// LevelDB serializes erases as:
|
||||||
|
// - byte: header
|
||||||
|
// - varint: key length
|
||||||
|
// - byte[]: key
|
||||||
|
// The formula below assumes the key is less than 16kB.
|
||||||
|
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
|
||||||
|
parent.ssKey.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t SizeEstimate() const { return size_estimate; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename K>
|
||||||
|
bool CDBWrapper::Erase(const K &key, bool fSync) {
|
||||||
|
CDBBatch batch(*this);
|
||||||
|
batch.Erase(key);
|
||||||
|
return WriteBatch(batch, fSync);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename K, typename V>
|
||||||
|
bool CDBWrapper::Write(const K &key, const V &value, bool fSync) {
|
||||||
|
CDBBatch batch(*this);
|
||||||
|
batch.Write(key, value);
|
||||||
|
return WriteBatch(batch, fSync);
|
||||||
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_DBWRAPPER_H
|
#endif // BITCOIN_DBWRAPPER_H
|
||||||
|
|
|
@ -227,6 +227,10 @@ public:
|
||||||
return (std::string(begin(), end()));
|
return (std::string(begin(), end()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::size_t capacity() const
|
||||||
|
{
|
||||||
|
return vch.capacity();
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Vector subset
|
// Vector subset
|
||||||
|
|
Loading…
Reference in a new issue