reth_cli_commands/db/
clear.rs1use clap::{Parser, Subcommand};
2use reth_db::static_file::iter_static_files;
3use reth_db_api::{
4 database::Database,
5 table::Table,
6 transaction::{DbTx, DbTxMut},
7 TableViewer, Tables,
8};
9use reth_db_common::DbTool;
10use reth_node_builder::NodeTypesWithDB;
11use reth_provider::StaticFileProviderFactory;
12use reth_static_file_types::StaticFileSegment;
13
14#[derive(Parser, Debug)]
16pub struct Command {
17 #[command(subcommand)]
18 subcommand: Subcommands,
19}
20
21impl Command {
22 pub fn execute<N: NodeTypesWithDB>(self, tool: &DbTool<N>) -> eyre::Result<()> {
24 match self.subcommand {
25 Subcommands::Mdbx { table } => {
26 table.view(&ClearViewer { db: tool.provider_factory.db_ref() })?
27 }
28 Subcommands::StaticFile { segment } => {
29 let static_file_provider = tool.provider_factory.static_file_provider();
30 let static_files = iter_static_files(static_file_provider.directory())?;
31
32 if let Some(segment_static_files) = static_files.get(&segment) {
33 for (block_range, _) in segment_static_files {
34 static_file_provider.delete_jar(segment, block_range.start())?;
35 }
36 }
37 }
38 }
39
40 Ok(())
41 }
42}
43
44#[derive(Subcommand, Debug)]
45enum Subcommands {
46 Mdbx { table: Tables },
48 StaticFile { segment: StaticFileSegment },
50}
51
52struct ClearViewer<'a, DB: Database> {
53 db: &'a DB,
54}
55
56impl<DB: Database> TableViewer<()> for ClearViewer<'_, DB> {
57 type Error = eyre::Report;
58
59 fn view<T: Table>(&self) -> Result<(), Self::Error> {
60 let tx = self.db.tx_mut()?;
61 tx.clear::<T>()?;
62 tx.commit()?;
63 Ok(())
64 }
65}