From 57e80c94c41d92cd50d6ce2013b0a95b86020a3a Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Wed, 14 Apr 2021 09:04:15 +0300 Subject: [PATCH] Prevent database fragmentation Signed-off-by: Anthony Fieroni --- src/flushablestorage.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/flushablestorage.h b/src/flushablestorage.h index d2d242f22cd..b584327be44 100644 --- a/src/flushablestorage.h +++ b/src/flushablestorage.h @@ -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; } @@ -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 { @@ -157,6 +164,8 @@ class CStorageLevelDB : public CStorageKV { } private: + TBytes end; + TBytes begin; CDBWrapper db; CDBBatch batch; };