File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -321,6 +321,23 @@ class CDBWrapper
321
321
pdb->GetApproximateSizes (&range, 1 , &size);
322
322
return size;
323
323
}
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
+
324
341
};
325
342
326
343
#endif // BITCOIN_DBWRAPPER_H
Original file line number Diff line number Diff line change @@ -375,12 +375,13 @@ bool CCoinsViewDB::Upgrade() {
375
375
CDBBatch batch (db);
376
376
uiInterface.SetProgressBreakAction (StartShutdown);
377
377
int reportDone = 0 ;
378
+ std::pair<unsigned char , uint256> key;
379
+ std::pair<unsigned char , uint256> prev_key = {DB_COINS, uint256 ()};
378
380
while (pcursor->Valid ()) {
379
381
boost::this_thread::interruption_point ();
380
382
if (ShutdownRequested ()) {
381
383
break ;
382
384
}
383
- std::pair<unsigned char , uint256> key;
384
385
if (pcursor->GetKey (key) && key.first == DB_COINS) {
385
386
if (count++ % 256 == 0 ) {
386
387
uint32_t high = 0x100 * *key.second .begin () + *(key.second .begin () + 1 );
@@ -409,13 +410,16 @@ bool CCoinsViewDB::Upgrade() {
409
410
if (batch.SizeEstimate () > batch_size) {
410
411
db.WriteBatch (batch);
411
412
batch.Clear ();
413
+ db.CompactRange (prev_key, key);
414
+ prev_key = key;
412
415
}
413
416
pcursor->Next ();
414
417
} else {
415
418
break ;
416
419
}
417
420
}
418
421
db.WriteBatch (batch);
422
+ db.CompactRange ({DB_COINS, uint256 ()}, key);
419
423
uiInterface.SetProgressBreakAction (std::function<void (void )>());
420
424
LogPrintf (" [%s].\n " , ShutdownRequested () ? " CANCELLED" : " DONE" );
421
425
return true ;
You can’t perform that action at this time.
0 commit comments