Skip to content

Commit

Permalink
add support for start key argument in fwdctl dump command (#476)
Browse files Browse the repository at this point in the history
Co-authored-by: Ron Kuris <ron.kuris@avalabs.org>
  • Loading branch information
Dan Laine and rkuris authored Feb 15, 2024
1 parent 94155c2 commit 7be6471
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion fwdctl/src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ pub struct Options {
help = "Name of the database"
)]
pub db: String,

/// The key to start dumping from (if no key is provided, start from the beginning).
/// Defaults to None.
#[arg(
required = false,
value_name = "START_KEY",
value_parser = key_parser,
help = "Start dumping from this key (inclusive)."
)]
pub start_key: Option<Box<[u8]>>,
}

pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
Expand All @@ -32,7 +42,8 @@ pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
let db = Db::new(opts.db.clone(), &cfg.build()).await?;
let latest_hash = db.root_hash().await?;
let latest_rev = db.revision(latest_hash).await?;
let mut stream = latest_rev.stream();
let start_key = opts.start_key.clone().unwrap_or(Box::new([]));
let mut stream = latest_rev.stream_from(start_key);
loop {
match stream.next().await {
None => break,
Expand All @@ -44,6 +55,11 @@ pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
}
Ok(())
}

fn u8_to_string(data: &[u8]) -> Cow<'_, str> {
String::from_utf8_lossy(data)
}

fn key_parser(s: &str) -> Result<Box<[u8]>, std::io::Error> {
return Ok(Box::from(s.as_bytes()));
}

0 comments on commit 7be6471

Please sign in to comment.