Skip to content

Commit 3ac6a7d

Browse files
authored
feat(bin): delete snapshot jars in db clear (#6311)
1 parent b499acc commit 3ac6a7d

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

bin/reth/src/commands/db/clear.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
1-
use clap::Parser;
1+
use clap::{Parser, Subcommand};
22
use reth_db::{
33
database::Database,
4+
snapshot::iter_snapshots,
45
table::Table,
56
transaction::{DbTx, DbTxMut},
67
TableViewer, Tables,
78
};
9+
use reth_primitives::{snapshot::find_fixed_range, SnapshotSegment};
10+
use reth_provider::ProviderFactory;
811

912
/// The arguments for the `reth db clear` command
1013
#[derive(Parser, Debug)]
1114
pub struct Command {
12-
/// Table name
13-
pub table: Tables,
15+
#[clap(subcommand)]
16+
subcommand: Subcommands,
1417
}
1518

1619
impl Command {
1720
/// 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+
}
2040

2141
Ok(())
2242
}
2343
}
2444

45+
#[derive(Subcommand, Debug)]
46+
enum Subcommands {
47+
Mdbx { table: Tables },
48+
Snapshot { segment: SnapshotSegment },
49+
}
50+
2551
struct ClearViewer<'a, DB: Database> {
2652
db: &'a DB,
2753
}

bin/reth/src/commands/db/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ impl Command {
249249
Subcommands::Clear(command) => {
250250
let db =
251251
open_db(&db_path, DatabaseArguments::default().log_level(self.db.log_level))?;
252-
command.execute(&db)?;
252+
let provider_factory = ProviderFactory::new(db, self.chain.clone())
253+
.with_snapshots(data_dir.snapshots_path())?;
254+
command.execute(provider_factory)?;
253255
}
254256
Subcommands::Snapshot(command) => {
255257
command.execute(&db_path, self.db.log_level, self.chain.clone())?;

0 commit comments

Comments
 (0)