Skip to content

Commit

Permalink
feat(bin): db clear (paradigmxyz#3934)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Jul 26, 2023
1 parent 49e1127 commit 1c4d127
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bin/reth/src/db/clear.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use clap::Parser;

use reth_db::{
database::Database,
table::Table,
transaction::{DbTx, DbTxMut},
TableViewer, Tables,
};

/// The arguments for the `reth db clear` command
#[derive(Parser, Debug)]
pub struct Command {
/// Table name
#[arg()]
pub table: Tables,
}

impl Command {
/// Execute `db clear` command
pub fn execute<DB: Database>(self, db: &DB) -> eyre::Result<()> {
self.table.view(&ClearViewer { db })?;

Ok(())
}
}

struct ClearViewer<'a, DB: Database> {
db: &'a DB,
}

impl<DB: Database> TableViewer<()> for ClearViewer<'_, DB> {
type Error = eyre::Report;

fn view<T: Table>(&self) -> Result<(), Self::Error> {
let tx = self.db.tx_mut()?;
tx.clear::<T>()?;
tx.commit()?;

Ok(())
}
}
7 changes: 7 additions & 0 deletions bin/reth/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reth_db::{
use reth_primitives::ChainSpec;
use std::sync::Arc;

mod clear;
mod get;
mod list;
/// DB List TUI
Expand Down Expand Up @@ -71,6 +72,8 @@ pub enum Subcommands {
Get(get::Command),
/// Deletes all database entries
Drop,
/// Deletes all table entries
Clear(clear::Command),
/// Lists current and local database versions
Version,
/// Returns the full database path
Expand Down Expand Up @@ -172,6 +175,10 @@ impl Command {
let mut tool = DbTool::new(&db, self.chain.clone())?;
tool.drop(db_path)?;
}
Subcommands::Clear(command) => {
let db = open_db(&db_path, self.db.log_level)?;
command.execute(&db)?;
}
Subcommands::Version => {
let local_db_version = match get_db_version(&db_path) {
Ok(version) => Some(version),
Expand Down

0 comments on commit 1c4d127

Please sign in to comment.