Skip to content

Commit

Permalink
Prevent database fragmentation
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Apr 14, 2021
1 parent 86b05b7 commit 15985c4
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/flushablestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class CStorageLevelDB : public CStorageKV {
return true;
}
bool Erase(const TBytes& key) override {
begin.empty() ? (begin = key) : (end = key);
batch.Erase(refTBytes(key));
return true;
}
Expand All @@ -144,6 +145,12 @@ class CStorageLevelDB : public CStorageKV {
bool Flush() override { // Commit batch
auto result = db.WriteBatch(batch);
batch.Clear();
// prevent db fragmentation
if (!begin.empty() && !end.empty()) {
db.CompactRange(refTBytes(begin), refTBytes(end));
}
end.clear();
begin.clear();
return result;
}
size_t SizeEstimate() const override {
Expand All @@ -157,6 +164,8 @@ class CStorageLevelDB : public CStorageKV {
}

private:
TBytes end;
TBytes begin;
CDBWrapper db;
CDBBatch batch;
};
Expand Down

0 comments on commit 15985c4

Please sign in to comment.