Skip to content

Commit

Permalink
bin/reth/db: use macro to dry (paradigmxyz#7159)
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <[email protected]>
  • Loading branch information
jsvisa authored Mar 15, 2024
1 parent 6f0b129 commit e7c23ed
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions bin/reth/src/commands/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ pub enum Subcommands {
Path,
}

/// db_ro_exec opens a database in read-only mode, and then execute with the provided command
macro_rules! db_ro_exec {
($chain:expr, $db_path:expr, $db_args:ident, $sfp:ident, $tool:ident, $command:block) => {
let db = open_db_read_only($db_path, $db_args)?;
let provider_factory = ProviderFactory::new(db, $chain.clone(), $sfp)?;

let $tool = DbTool::new(provider_factory, $chain.clone())?;
$command;
};
}

impl Command {
/// Execute `db` command
pub async fn execute(self) -> eyre::Result<()> {
Expand All @@ -101,36 +112,24 @@ impl Command {
match self.command {
// TODO: We'll need to add this on the DB trait.
Subcommands::Stats(command) => {
let db = open_db_read_only(&db_path, db_args)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(data_dir, &tool)?;
db_ro_exec!(self.chain, &db_path, db_args, static_files_path, tool, {
command.execute(data_dir, &tool)?;
});
}
Subcommands::List(command) => {
let db = open_db_read_only(&db_path, db_args)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(&tool)?;
db_ro_exec!(self.chain, &db_path, db_args, static_files_path, tool, {
command.execute(&tool)?;
});
}
Subcommands::Diff(command) => {
let db = open_db_read_only(&db_path, db_args)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(&tool)?;
db_ro_exec!(self.chain, &db_path, db_args, static_files_path, tool, {
command.execute(&tool)?;
});
}
Subcommands::Get(command) => {
let db = open_db_read_only(&db_path, db_args)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(&tool)?;
db_ro_exec!(self.chain, &db_path, db_args, static_files_path, tool, {
command.execute(&tool)?;
});
}
Subcommands::Drop { force } => {
if !force {
Expand Down

0 comments on commit e7c23ed

Please sign in to comment.