Skip to content

Commit

Permalink
Re-enable fwdctl (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuris authored Sep 5, 2024
1 parent 65de7d3 commit 4bbb4da
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 84 deletions.
6 changes: 2 additions & 4 deletions fwdctl/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// See the file LICENSE.md for licensing terms.

use clap::{value_parser, Args};
use firewood::{
db::{Db, DbConfig},
v2::api,
};
use firewood::db::{Db, DbConfig};
use firewood::v2::api;

#[derive(Args)]
pub struct Options {
Expand Down
26 changes: 14 additions & 12 deletions fwdctl/src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// See the file LICENSE.md for licensing terms.

use clap::Args;
use firewood::db::{BatchOp, Db, DbConfig};
use firewood::v2::api::{self, Db as _, Proposal as _};

#[derive(Debug, Args)]
pub struct Options {
Expand All @@ -20,18 +22,18 @@ pub struct Options {
pub db: String,
}

// pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
// log::debug!("deleting key {:?}", opts);
// let cfg = DbConfig::builder().truncate(false);
pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
log::debug!("deleting key {:?}", opts);
let cfg = DbConfig::builder().truncate(false);

// let db = Db::new(opts.db.clone(), cfg.build()).await?;
let db = Db::new(opts.db.clone(), cfg.build()).await?;

// let batch: Vec<BatchOp<String, String>> = vec![BatchOp::Delete {
// key: opts.key.clone(),
// }];
// let proposal = db.propose(batch).await?;
// proposal.commit().await?;
let batch: Vec<BatchOp<String, String>> = vec![BatchOp::Delete {
key: opts.key.clone(),
}];
let proposal = db.propose(batch).await?;
proposal.commit().await?;

// println!("key {} deleted successfully", opts.key);
// Ok(())
// }
println!("key {} deleted successfully", opts.key);
Ok(())
}
45 changes: 26 additions & 19 deletions fwdctl/src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// See the file LICENSE.md for licensing terms.

use clap::Args;
use firewood::db::{Db, DbConfig};
use firewood::merkle::Key;
use firewood::v2::api::{self, Db as _};
use std::borrow::Cow;

#[derive(Debug, Args)]
Expand All @@ -27,26 +29,31 @@ pub struct Options {
pub start_key: Option<Key>,
}

// pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
// log::debug!("dump database {:?}", opts);
// let cfg = DbConfig::builder().truncate(false);
pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
log::debug!("dump database {:?}", opts);
let cfg = DbConfig::builder().truncate(false);

// 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 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,
// Some(Ok((key, value))) => {
// println!("'{}': '{}'", u8_to_string(&key), u8_to_string(&value));
// }
// Some(Err(e)) => return Err(e),
// }
// }
// Ok(())
// }
let db = Db::new(opts.db.clone(), cfg.build()).await?;
let latest_hash = db.root_hash().await?;
let Some(_latest_hash) = latest_hash else {
println!("Database is empty");
return Ok(());
};
todo!()
// let latest_rev = db.revision(latest_hash).await?;
// 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,
// Some(Ok((key, value))) => {
// println!("'{}': '{}'", u8_to_string(&key), u8_to_string(&value));
// }
// Some(Err(e)) => return Err(e),
// }
// }
// Ok(())
}

fn _u8_to_string(data: &[u8]) -> Cow<'_, str> {
String::from_utf8_lossy(data)
Expand Down
52 changes: 31 additions & 21 deletions fwdctl/src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use clap::Args;
use std::str;

use firewood::db::{Db, DbConfig};
use firewood::v2::api::{self, Db as _, DbView as _};

#[derive(Debug, Args)]
pub struct Options {
/// The key to get the value for
Expand All @@ -21,24 +24,31 @@ pub struct Options {
pub db: String,
}

// pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
// log::debug!("get key value pair {:?}", opts);
// let cfg = DbConfig::builder().truncate(false);

// let db = Db::new(opts.db.clone(), cfg.build()).await?;

// let rev = db.revision(db.root_hash().await?).await?;

// match rev.val(opts.key.as_bytes()).await {
// Ok(Some(val)) => {
// let s = String::from_utf8_lossy(val.as_ref());
// println!("{s:?}");
// Ok(())
// }
// Ok(None) => {
// eprintln!("Key '{}' not found", opts.key);
// Ok(())
// }
// Err(e) => Err(e),
// }
// }
pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
log::debug!("get key value pair {:?}", opts);
let cfg = DbConfig::builder().truncate(false);

let db = Db::new(opts.db.clone(), cfg.build()).await?;

let hash = db.root_hash().await?;

let Some(hash) = hash else {
println!("Database is empty");
return Ok(());
};

let rev = db.revision(hash).await?;

match rev.val(opts.key.as_bytes()).await {
Ok(Some(val)) => {
let s = String::from_utf8_lossy(val.as_ref());
println!("{s:?}");
Ok(())
}
Ok(None) => {
eprintln!("Key '{}' not found", opts.key);
Ok(())
}
Err(e) => Err(e),
}
}
28 changes: 15 additions & 13 deletions fwdctl/src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// See the file LICENSE.md for licensing terms.

use clap::Args;
use firewood::db::{BatchOp, Db, DbConfig};
use firewood::v2::api::{self, Db as _, Proposal as _};

#[derive(Debug, Args)]
pub struct Options {
Expand All @@ -24,19 +26,19 @@ pub struct Options {
pub db: String,
}

// pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
// log::debug!("inserting key value pair {:?}", opts);
// let cfg = DbConfig::builder().truncate(false);
pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
log::debug!("inserting key value pair {:?}", opts);
let cfg = DbConfig::builder().truncate(false);

// let db = Db::new(opts.db.clone(), cfg.build()).await?;
let db = Db::new(opts.db.clone(), cfg.build()).await?;

// let batch: Vec<BatchOp<Vec<u8>, Vec<u8>>> = vec![BatchOp::Put {
// key: opts.key.clone().into(),
// value: opts.value.bytes().collect(),
// }];
// let proposal = db.propose(batch).await?;
// proposal.commit().await?;
let batch: Vec<BatchOp<Vec<u8>, Vec<u8>>> = vec![BatchOp::Put {
key: opts.key.clone().into(),
value: opts.value.bytes().collect(),
}];
let proposal = db.propose(batch).await?;
proposal.commit().await?;

// println!("{}", opts.key);
// Ok(())
// }
println!("{}", opts.key);
Ok(())
}
11 changes: 5 additions & 6 deletions fwdctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ async fn main() -> Result<(), api::Error> {

match &cli.command {
Commands::Create(opts) => create::run(opts).await,
_ => todo!(),
//Commands::Insert(opts) => insert::run(opts).await,
// Commands::Get(opts) => get::run(opts).await,
//Commands::Delete(opts) => delete::run(opts).await,
//Commands::Root(opts) => root::run(opts).await,
//Commands::Dump(opts) => dump::run(opts).await,
Commands::Insert(opts) => insert::run(opts).await,
Commands::Get(opts) => get::run(opts).await,
Commands::Delete(opts) => delete::run(opts).await,
Commands::Root(opts) => root::run(opts).await,
Commands::Dump(opts) => dump::run(opts).await,
}
}
19 changes: 11 additions & 8 deletions fwdctl/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use clap::Args;
use std::str;

use firewood::db::{Db, DbConfig};
use firewood::v2::api::{self, Db as _};

#[derive(Debug, Args)]
pub struct Options {
/// The database path (if no path is provided, return an error). Defaults to firewood.
Expand All @@ -17,13 +20,13 @@ pub struct Options {
pub db: String,
}

// pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
// log::debug!("root hash {:?}", opts);
// let cfg = DbConfig::builder().truncate(false);
pub(super) async fn run(opts: &Options) -> Result<(), api::Error> {
let cfg = DbConfig::builder().truncate(false);

let db = Db::new(opts.db.clone(), cfg.build()).await?;

// let db = Db::new(opts.db.clone(), cfg.build()).await?;
let hash = db.root_hash().await?;

// let root = db.root_hash().await?;
// println!("{root:X?}");
// Ok(())
// }
println!("{hash:?}");
Ok(())
}
3 changes: 2 additions & 1 deletion fwdctl/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use anyhow::{anyhow, Result};
use assert_cmd::Command;
use predicates::prelude::*;
use serial_test::serial;
use std::{fs::remove_dir_all, path::PathBuf};
use std::fs::remove_dir_all;
use std::path::PathBuf;

const PRG: &str = "fwdctl";
const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down

0 comments on commit 4bbb4da

Please sign in to comment.