diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 70d520d7e..1d590ec73 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -323,6 +323,23 @@ public: pdb->GetApproximateSizes(&range, 1, &size); return size; } + + /** + * Compact a certain range of keys in the database. + */ + template + void CompactRange(const K& key_begin, const K& key_end) const + { + 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; + ssKey2 << key_end; + leveldb::Slice slKey1(ssKey1.data(), ssKey1.size()); + leveldb::Slice slKey2(ssKey2.data(), ssKey2.size()); + pdb->CompactRange(&slKey1, &slKey2); + } + }; #endif // BITCOIN_DBWRAPPER_H diff --git a/src/txdb.cpp b/src/txdb.cpp index deef70f06..17d075c60 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -462,12 +462,13 @@ bool CCoinsViewDB::Upgrade() { CDBBatch batch(db); uiInterface.SetProgressBreakAction(StartShutdown); int reportDone = 0; + std::pair key; + std::pair prev_key = {DB_COINS, uint256()}; while (pcursor->Valid()) { boost::this_thread::interruption_point(); if (ShutdownRequested()) { break; } - std::pair key; if (pcursor->GetKey(key) && key.first == DB_COINS) { if (count++ % 256 == 0) { uint32_t high = 0x100 * *key.second.begin() + *(key.second.begin() + 1); @@ -496,6 +497,8 @@ bool CCoinsViewDB::Upgrade() { if (batch.SizeEstimate() > batch_size) { db.WriteBatch(batch); batch.Clear(); + db.CompactRange(prev_key, key); + prev_key = key; } pcursor->Next(); } else { @@ -503,6 +506,7 @@ bool CCoinsViewDB::Upgrade() { } } db.WriteBatch(batch); + db.CompactRange({DB_COINS, uint256()}, key); uiInterface.SetProgressBreakAction(std::function()); LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE"); return true;