Skip to content

Commit

Permalink
Support passing CDataStream as key into CDBWrapper/CDBBatch/CDBIterator
Browse files Browse the repository at this point in the history
This allow to pre-serialize keys in advance and pass the serialized form
into these classes.
  • Loading branch information
codablock committed Apr 4, 2019
1 parent 92c1cdc commit 5482083
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ class CDBBatch
{
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(ssKey.data(), ssKey.size());
Write(ssKey, value);
ssKey.clear();
}

template <typename V>
void Write(const CDataStream& _ssKey, const V& value)
{
leveldb::Slice slKey(_ssKey.data(), _ssKey.size());

ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
ssValue << value;
Expand All @@ -91,7 +98,6 @@ class CDBBatch
// - 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();
}

Expand All @@ -100,15 +106,19 @@ class CDBBatch
{
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(ssKey.data(), ssKey.size());
Erase(ssKey);
ssKey.clear();
}

void Erase(const CDataStream& _ssKey) {
leveldb::Slice slKey(_ssKey.data(), _ssKey.size());

batch.Delete(slKey);
// - 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; }
Expand Down Expand Up @@ -138,6 +148,10 @@ class CDBIterator
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
Seek(ssKey);
}

void Seek(const CDataStream& ssKey) {
leveldb::Slice slKey(ssKey.data(), ssKey.size());
piter->Seek(slKey);
}
Expand Down Expand Up @@ -231,6 +245,11 @@ class CDBWrapper
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
return ReadDataStream(ssKey, ssValue);
}

bool ReadDataStream(const CDataStream& ssKey, CDataStream& ssValue) const
{
leveldb::Slice slKey(ssKey.data(), ssKey.size());

std::string strValue;
Expand All @@ -249,9 +268,18 @@ class CDBWrapper

template <typename K, typename V>
bool Read(const K& key, V& value) const
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
return Read(ssKey, value);
}

template <typename V>
bool Read(const CDataStream& ssKey, V& value) const
{
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
if (!ReadDataStream(key, ssValue)) {
if (!ReadDataStream(ssKey, ssValue)) {
return false;
}

Expand All @@ -277,7 +305,12 @@ class CDBWrapper
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey(ssKey.data(), ssKey.size());
return Exists(ssKey);
}

bool Exists(const CDataStream& key) const
{
leveldb::Slice slKey(key.data(), key.size());

std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
Expand Down

0 comments on commit 5482083

Please sign in to comment.