Skip to content

Commit

Permalink
Fix up example, use new async interface (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuris authored Oct 16, 2023
1 parent 1d836ad commit d1a7c4b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 246 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ jobs:
# benchmarks were not being done in --release mode, we can enable this again later
# - name: Run benchmark example
# run: RUST_BACKTRACE=1 cargo run --example benchmark -- --nbatch 100 --batch-size 1000
- name: Run rev example
run: RUST_BACKTRACE=1 cargo run --example rev
- name: Run insert example
run: RUST_BACKTRACE=1 cargo run --example insert

docs:
needs: build
Expand Down
80 changes: 80 additions & 0 deletions firewood/examples/insert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE.md for licensing terms.

// This example isn't an actual benchmark, it's just an example of how to
// insert some random keys using the front-end API.

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

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

#[derive(Parser, Debug)]
struct Args {
#[arg(short, long, default_value = "1-64", value_parser = string_to_range)]
keylen: RangeInclusive<usize>,
#[arg(short, long, default_value = "32", value_parser = string_to_range)]
datalen: RangeInclusive<usize>,
#[arg(short, long, default_value_t = 1)]
batch_keys: usize,
#[arg(short, long, default_value_t = 100)]
inserts: usize,
}

fn string_to_range(input: &str) -> Result<RangeInclusive<usize>, Box<dyn Error + Sync + Send>> {
//<usize as std::str::FromStr>::Err> {
let parts: Vec<&str> = input.split('-').collect();
match parts.len() {
1 => Ok(input.parse()?..=input.parse()?),
2 => Ok(parts[0].parse()?..=parts[1].parse()?),
_ => Err("Too many dashes in input string".into()),
}
}

/// cargo run --release --example insert
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
let cfg = DbConfig::builder().truncate(true).build();

let args = Args::parse();

let db = tokio::task::spawn_blocking(move || {
Db::new("rev_db", &cfg).expect("db initiation should succeed")
})
.await
.unwrap();

let keys = args.batch_keys;
let start = Instant::now();

for _ in 0..args.inserts {
let keylen = rand::thread_rng().gen_range(args.keylen.clone());
let datalen = rand::thread_rng().gen_range(args.datalen.clone());
let batch: Batch<Vec<u8>, Vec<u8>> = (0..keys)
.map(|_| {
(
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(keylen as usize)
.collect::<Vec<u8>>(),
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(datalen as usize)
.collect::<Vec<u8>>(),
)
})
.map(|(key, value)| BatchOp::Put { key, value })
.collect();
let proposal: Arc<firewood::db::Proposal> = db.propose(batch).await.unwrap().into();
proposal.commit().await?;
}

let duration = start.elapsed();
println!("Generated and inserted {keys} in {duration:?}");

Ok(())
}
242 changes: 0 additions & 242 deletions firewood/examples/rev.rs

This file was deleted.

2 changes: 1 addition & 1 deletion firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl api::Db for Db {
&self,
batch: api::Batch<K, V>,
) -> Result<Self::Proposal, api::Error> {
self.new_proposal(batch).map_err(Into::into)
block_in_place(|| self.new_proposal(batch)).map_err(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion firewood/src/db/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl crate::v2::api::Proposal for Proposal {
self: Arc<Self>,
data: api::Batch<K, V>,
) -> Result<Self::Proposal, api::Error> {
self.propose_sync(data).map_err(Into::into)
block_in_place(|| self.propose_sync(data)).map_err(Into::into)
}
}

Expand Down

0 comments on commit d1a7c4b

Please sign in to comment.