Skip to content

Commit

Permalink
Add verify option to insert example (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuris authored Oct 17, 2023
1 parent d43b95f commit d31113c
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions firewood/examples/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// insert some random keys using the front-end API.

use clap::Parser;
use std::{error::Error, ops::RangeInclusive, sync::Arc, time::Instant};
use std::{collections::HashMap, error::Error, ops::RangeInclusive, sync::Arc, time::Instant};

use firewood::{
db::{Batch, BatchOp, Db, DbConfig},
v2::api::{Db as DbApi, Proposal},
v2::api::{Db as DbApi, DbView, Proposal},
};
use rand::{distributions::Alphanumeric, Rng};

Expand All @@ -23,6 +23,8 @@ struct Args {
batch_keys: usize,
#[arg(short, long, default_value_t = 100)]
inserts: usize,
#[arg(short, long, default_value_t = 0, value_parser = clap::value_parser!(u16).range(0..=100))]
read_verify_percent: u16,
}

fn string_to_range(input: &str) -> Result<RangeInclusive<usize>, Box<dyn Error + Sync + Send>> {
Expand Down Expand Up @@ -69,12 +71,51 @@ async fn main() -> Result<(), Box<dyn Error>> {
})
.map(|(key, value)| BatchOp::Put { key, value })
.collect();

let verify = get_keys_to_verify(&batch, args.read_verify_percent);

let proposal: Arc<firewood::db::Proposal> = db.propose(batch).await.unwrap().into();
proposal.commit().await?;
verify_keys(&db, verify).await?;
}

let duration = start.elapsed();
println!("Generated and inserted {keys} in {duration:?}");
println!(
"Generated and inserted {} batches of size {keys} in {duration:?}",
args.inserts
);

Ok(())
}

fn get_keys_to_verify(batch: &Batch<Vec<u8>, Vec<u8>>, pct: u16) -> HashMap<Vec<u8>, Vec<u8>> {
if pct == 0 {
HashMap::new()
} else {
batch
.iter()
.filter(|_last_key| rand::thread_rng().gen_range(0..=(100 - pct)) == 0)
.map(|op| {
if let BatchOp::Put { key, value } = op {
(key.clone(), value.clone())
} else {
unreachable!()
}
})
.collect()
}
}

async fn verify_keys(
db: &Db,
verify: HashMap<Vec<u8>, Vec<u8>>,
) -> Result<(), firewood::v2::api::Error> {
if !verify.is_empty() {
let hash = db.root_hash().await?;
let revision = db.revision(hash).await?;
for (key, value) in verify {
assert_eq!(*value, revision.val(key).await?.unwrap());
}
}
Ok(())
}

0 comments on commit d31113c

Please sign in to comment.