Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DatabaseProvider remove method #9405

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,22 @@ impl<TX: DbTxMut + DbTx> DatabaseProvider<TX> {
))
}

/// Remove list of entries from the table. Returns the number of entries removed.
#[inline]
pub fn remove<T: Table>(
&self,
range: impl RangeBounds<T::Key>,
) -> Result<usize, DatabaseError> {
let mut entries = 0;
let mut cursor_write = self.tx.cursor_write::<T>()?;
let mut walker = cursor_write.walk_range(range)?;
while walker.next().transpose()?.is_some() {
walker.delete_current()?;
entries += 1;
}
Ok(entries)
}

/// Return list of entries from table
///
/// If TAKE is true, opened cursor would be write and it would delete all values from db.
Expand Down Expand Up @@ -894,7 +910,9 @@ impl<TX: DbTxMut + DbTx> DatabaseProvider<TX> {
// Remove TransactionBlocks index if there are transaction present
if !transactions.is_empty() {
let tx_id_range = transactions.first().unwrap().0..=transactions.last().unwrap().0;
self.get_or_take::<tables::TransactionBlocks, TAKE>(tx_id_range)?;
// NOTE: we are in this branch because `TAKE` is true, so we can use the `remove`
// method
self.remove::<tables::TransactionBlocks>(tx_id_range)?;
}
}

Expand Down Expand Up @@ -959,7 +977,8 @@ impl<TX: DbTxMut + DbTx> DatabaseProvider<TX> {

if TAKE {
// rm HeaderTerminalDifficulties
self.get_or_take::<tables::HeaderTerminalDifficulties, TAKE>(range)?;
// NOTE: we are in this branch because `TAKE` is true, so we can use the `remove` method
self.remove::<tables::HeaderTerminalDifficulties>(range)?;
// rm HeaderNumbers
let mut header_number_cursor = self.tx.cursor_write::<tables::HeaderNumbers>()?;
for (_, hash) in &block_header_hashes {
Expand Down Expand Up @@ -2609,7 +2628,8 @@ impl<TX: DbTxMut + DbTx> BlockExecutionWriter for DatabaseProvider<TX> {
// that is why it is deleted afterwards.
if TAKE {
// rm block bodies
self.get_or_take::<tables::BlockBodyIndices, TAKE>(range)?;
// NOTE: we are in this branch because `TAKE` is true, so we can use the `remove` method
self.remove::<tables::BlockBodyIndices>(range)?;

// Update pipeline progress
if let Some(fork_number) = unwind_to {
Expand Down
Loading