Skip to content

Commit

Permalink
prune receipts by logs with range instead of iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Aug 22, 2023
1 parent 08f71e2 commit b941ed4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
7 changes: 5 additions & 2 deletions crates/prune/src/pruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ impl<DB: Database> Pruner<DB> {
let (deleted, done) = provider.prune_table_with_range::<tables::Receipts>(
tx_range,
self.batch_sizes.receipts,
|_| false,
|row| last_pruned_transaction = row.0,
)?;
trace!(target: "pruner", %deleted, %done, "Pruned receipts");
Expand Down Expand Up @@ -499,7 +500,7 @@ impl<DB: Database> Pruner<DB> {

last_pruned_transaction = Some(tx_range_end);
let deleted;
(deleted, done) = provider.prune_table_with_iterator::<tables::Receipts>(
(deleted, done) = provider.prune_table_with_range::<tables::Receipts>(
tx_range,
limit,
|receipt| {
Expand Down Expand Up @@ -628,7 +629,6 @@ impl<DB: Database> Pruner<DB> {
let (deleted, done) = provider.prune_table_with_iterator::<tables::TxHashNumber>(
hashes,
self.batch_sizes.transaction_lookup,
|_| false,
|row| last_pruned_transaction = row.1,
)?;
trace!(target: "pruner", %deleted, %done, "Pruned transaction lookup");
Expand Down Expand Up @@ -678,6 +678,7 @@ impl<DB: Database> Pruner<DB> {
let (deleted, done) = provider.prune_table_with_range::<tables::TxSenders>(
tx_range,
self.batch_sizes.transaction_senders,
|_| false,
|row| last_pruned_transaction = row.0,
)?;
trace!(target: "pruner", %deleted, %done, "Pruned transaction senders");
Expand Down Expand Up @@ -726,6 +727,7 @@ impl<DB: Database> Pruner<DB> {
let (rows, done) = provider.prune_table_with_range::<tables::AccountChangeSet>(
range,
self.batch_sizes.account_history,
|_| false,
|row| last_pruned_block_number = Some(row.0),
)?;
trace!(target: "pruner", %rows, %done, "Pruned account history (changesets)");
Expand Down Expand Up @@ -777,6 +779,7 @@ impl<DB: Database> Pruner<DB> {
let (rows, done) = provider.prune_table_with_range::<tables::StorageChangeSet>(
BlockNumberAddress::range(range),
self.batch_sizes.storage_history,
|_| false,
|row| last_pruned_block_number = Some(row.0.block_number()),
)?;
trace!(target: "pruner", %rows, %done, "Pruned storage history (changesets)");
Expand Down
18 changes: 9 additions & 9 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> {
&self,
keys: impl IntoIterator<Item = T::Key>,
limit: usize,
skip_filter: impl Fn(&T::Value) -> bool,
mut delete_callback: impl FnMut(TableRow<T>),
) -> std::result::Result<(usize, bool), DatabaseError> {
let mut cursor = self.tx.cursor_write::<T>()?;
Expand All @@ -640,11 +639,9 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> {
for key in &mut keys {
let row = cursor.seek_exact(key.clone())?;
if let Some(row) = row {
if !skip_filter(&row.1) {
cursor.delete_current()?;
deleted += 1;
delete_callback(row);
}
cursor.delete_current()?;
deleted += 1;
delete_callback(row);
}

if deleted == limit {
Expand All @@ -662,16 +659,19 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> {
&self,
keys: impl RangeBounds<T::Key> + Clone + Debug,
limit: usize,
skip_filter: impl Fn(&T::Value) -> bool,
mut delete_callback: impl FnMut(TableRow<T>),
) -> std::result::Result<(usize, bool), DatabaseError> {
let mut cursor = self.tx.cursor_write::<T>()?;
let mut walker = cursor.walk_range(keys.clone())?;
let mut deleted = 0;

while let Some(row) = walker.next().transpose()? {
walker.delete_current()?;
deleted += 1;
delete_callback(row);
if !skip_filter(&row.1) {
walker.delete_current()?;
deleted += 1;
delete_callback(row);
}

if deleted == limit {
break
Expand Down

0 comments on commit b941ed4

Please sign in to comment.