Skip to content

Commit efeb273

Browse files
committed
Force on-the-fly compaction during pertxout upgrade
1 parent 0c70e84 commit efeb273

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/dbwrapper.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,23 @@ class CDBWrapper
321321
pdb->GetApproximateSizes(&range, 1, &size);
322322
return size;
323323
}
324+
325+
/**
326+
* Compact a certain range of keys in the database.
327+
*/
328+
template<typename K>
329+
void CompactRange(const K& key_begin, const K& key_end) const
330+
{
331+
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
332+
ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
333+
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
334+
ssKey1 << key_begin;
335+
ssKey2 << key_end;
336+
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
337+
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
338+
pdb->CompactRange(&slKey1, &slKey2);
339+
}
340+
324341
};
325342

326343
#endif // BITCOIN_DBWRAPPER_H

src/txdb.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,13 @@ bool CCoinsViewDB::Upgrade() {
375375
CDBBatch batch(db);
376376
uiInterface.SetProgressBreakAction(StartShutdown);
377377
int reportDone = 0;
378+
std::pair<unsigned char, uint256> key;
379+
std::pair<unsigned char, uint256> prev_key = {DB_COINS, uint256()};
378380
while (pcursor->Valid()) {
379381
boost::this_thread::interruption_point();
380382
if (ShutdownRequested()) {
381383
break;
382384
}
383-
std::pair<unsigned char, uint256> key;
384385
if (pcursor->GetKey(key) && key.first == DB_COINS) {
385386
if (count++ % 256 == 0) {
386387
uint32_t high = 0x100 * *key.second.begin() + *(key.second.begin() + 1);
@@ -409,13 +410,16 @@ bool CCoinsViewDB::Upgrade() {
409410
if (batch.SizeEstimate() > batch_size) {
410411
db.WriteBatch(batch);
411412
batch.Clear();
413+
db.CompactRange(prev_key, key);
414+
prev_key = key;
412415
}
413416
pcursor->Next();
414417
} else {
415418
break;
416419
}
417420
}
418421
db.WriteBatch(batch);
422+
db.CompactRange({DB_COINS, uint256()}, key);
419423
uiInterface.SetProgressBreakAction(std::function<void(void)>());
420424
LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE");
421425
return true;

0 commit comments

Comments
 (0)