|
1 |
| -use clap::Parser; |
| 1 | +use clap::{Parser, Subcommand}; |
2 | 2 | use reth_db::{
|
3 | 3 | database::Database,
|
| 4 | + snapshot::iter_snapshots, |
4 | 5 | table::Table,
|
5 | 6 | transaction::{DbTx, DbTxMut},
|
6 | 7 | TableViewer, Tables,
|
7 | 8 | };
|
| 9 | +use reth_primitives::{snapshot::find_fixed_range, SnapshotSegment}; |
| 10 | +use reth_provider::ProviderFactory; |
8 | 11 |
|
9 | 12 | /// The arguments for the `reth db clear` command
|
10 | 13 | #[derive(Parser, Debug)]
|
11 | 14 | pub struct Command {
|
12 |
| - /// Table name |
13 |
| - pub table: Tables, |
| 15 | + #[clap(subcommand)] |
| 16 | + subcommand: Subcommands, |
14 | 17 | }
|
15 | 18 |
|
16 | 19 | impl Command {
|
17 | 20 | /// Execute `db clear` command
|
18 |
| - pub fn execute<DB: Database>(self, db: &DB) -> eyre::Result<()> { |
19 |
| - self.table.view(&ClearViewer { db })?; |
| 21 | + pub fn execute<DB: Database>(self, provider_factory: ProviderFactory<DB>) -> eyre::Result<()> { |
| 22 | + match self.subcommand { |
| 23 | + Subcommands::Mdbx { table } => { |
| 24 | + table.view(&ClearViewer { db: provider_factory.db_ref() })? |
| 25 | + } |
| 26 | + Subcommands::Snapshot { segment } => { |
| 27 | + let snapshot_provider = provider_factory |
| 28 | + .snapshot_provider() |
| 29 | + .expect("snapshot provider initialized via provider factory"); |
| 30 | + let snapshots = iter_snapshots(snapshot_provider.directory())?; |
| 31 | + |
| 32 | + if let Some(segment_snapshots) = snapshots.get(&segment) { |
| 33 | + for (block_range, _) in segment_snapshots { |
| 34 | + snapshot_provider |
| 35 | + .delete_jar(segment, find_fixed_range(*block_range.start()))?; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
20 | 40 |
|
21 | 41 | Ok(())
|
22 | 42 | }
|
23 | 43 | }
|
24 | 44 |
|
| 45 | +#[derive(Subcommand, Debug)] |
| 46 | +enum Subcommands { |
| 47 | + Mdbx { table: Tables }, |
| 48 | + Snapshot { segment: SnapshotSegment }, |
| 49 | +} |
| 50 | + |
25 | 51 | struct ClearViewer<'a, DB: Database> {
|
26 | 52 | db: &'a DB,
|
27 | 53 | }
|
|
0 commit comments