// consider taking a backup of the folder ChainLachain in case anything goes wrong public void ShrinkDb(ulong depth, ulong totalBlocks, bool consistencyCheck) { if (dbShrinkStatus != DbShrinkStatus.Stopped) { if (dbShrinkDepth is null) { throw new Exception("DbCompact process was started but depth was not written. This should not happen."); } if (dbShrinkDepth != depth) { throw new Exception($"Process was started before with depth {dbShrinkDepth} but was not finished." + $" Got new depth {depth}. Use depth {dbShrinkDepth} and finish the process before " + "using new depth."); } } DbShrinkUtils.ResetCounter(); switch (dbShrinkStatus) { case DbShrinkStatus.Stopped: if (!CheckIfDbShrinkNecessary(depth, totalBlocks)) { Logger.LogTrace("Nothing to delete."); return; } SetDbShrinkDepth(depth); Logger.LogTrace("Starting hard db optimization"); Logger.LogTrace($"Keeping latest {depth} snapshots from last approved snapshot" + $"for blocks: {StartingBlockToKeep(depth, totalBlocks)} to {totalBlocks}"); SetDbShrinkStatus(DbShrinkStatus.SaveNodeId); goto case DbShrinkStatus.SaveNodeId; case DbShrinkStatus.SaveNodeId: SaveRecentSnapshotNodeId(depth, totalBlocks); SetDbShrinkStatus(DbShrinkStatus.DeleteOldSnapshot); goto case DbShrinkStatus.DeleteOldSnapshot; case DbShrinkStatus.DeleteOldSnapshot: Logger.LogTrace($"Deleting nodes from DB that are not reachable from last {depth} snapshots"); ulong fromBlock = GetOldestSnapshotInDb(), toBlock = StartingBlockToKeep(depth, totalBlocks) - 1; DeleteOldSnapshot(fromBlock, toBlock); SetDbShrinkStatus(DbShrinkStatus.DeleteNodeId); goto case DbShrinkStatus.DeleteNodeId; case DbShrinkStatus.DeleteNodeId: DeleteRecentSnapshotNodeId(depth, totalBlocks); SetDbShrinkStatus(DbShrinkStatus.CheckConsistency); goto case DbShrinkStatus.CheckConsistency; case DbShrinkStatus.CheckConsistency: if (consistencyCheck) { CheckSnapshots(depth, totalBlocks); } Stop(); break; default: throw new Exception("invalid db-shrink-status"); } }
private void Delete(byte[] key, bool tryCommit = true) { batch.Delete(key); var keyHash = key.Keccak(); _memDb[keyHash] = null; DbShrinkUtils.UpdateCounter(); if (tryCommit && DbShrinkUtils.CycleEnded()) { Commit(); } }
private void Save(byte[] key, byte[] content, bool tryCommit = true) { batch.Put(key, content); var keyHash = key.Keccak(); _memDb[keyHash] = content; DbShrinkUtils.UpdateCounter(); if (tryCommit && DbShrinkUtils.CycleEnded()) { Commit(); } }
private void Commit() { batch.Commit(); DbShrinkUtils.ResetCounter(); UpdateBatch(); }