Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of rev.rs #217

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions firewood/examples/rev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::{collections::VecDeque, path::Path};

use firewood::{
db::{BatchOp, Db, DbConfig, Proposal, Revision, WalConfig},
db::{BatchOp, Db, DbConfig, DbError, Proposal, Revision, WalConfig},
merkle::{Node, TrieHash},
proof::Proof,
storage::StoreRevShared,
Expand All @@ -14,7 +14,7 @@ use shale::compact::CompactSpace;
type SharedStore = CompactSpace<Node, StoreRevShared>;

/// cargo run --example rev
fn main() {
fn main() -> Result<(), DbError> {
let cfg = DbConfig::builder().wal(WalConfig::builder().max_revisions(10).build());

let db = Db::new("rev_db", &cfg.clone().truncate(true).build())
Expand All @@ -23,9 +23,9 @@ fn main() {

let mut revision_tracker = RevisionTracker::new(db);

revision_tracker.create_revisions(items.into_iter());
revision_tracker.create_revisions(items.into_iter())?;

revision_tracker.db.kv_dump(&mut std::io::stdout()).unwrap();
revision_tracker.db.kv_dump(&mut std::io::stdout())?;

verify_root_hashes(&mut revision_tracker);

Expand Down Expand Up @@ -102,6 +102,7 @@ fn main() {
.unwrap();
});
});
Ok(())
}

struct RevisionTracker {
Expand All @@ -117,15 +118,18 @@ impl RevisionTracker {
}
}

fn create_revisions<K, V>(&mut self, iter: impl Iterator<Item = (K, V)>)
fn create_revisions<K, V>(&mut self, iter: impl Iterator<Item = (K, V)>) -> Result<(), DbError>
where
K: AsRef<[u8]>,
V: AsRef<[u8]>,
{
iter.for_each(|(k, v)| self.create_revision(k, v));
for (k, v) in iter {
self.create_revision(k, v)?
}
Ok(())
xinifinity marked this conversation as resolved.
Show resolved Hide resolved
}

fn create_revision<K, V>(&mut self, k: K, v: V)
fn create_revision<K, V>(&mut self, k: K, v: V) -> Result<(), DbError>
where
K: AsRef<[u8]>,
V: AsRef<[u8]>,
Expand All @@ -134,11 +138,11 @@ impl RevisionTracker {
key: k,
value: v.as_ref().to_vec(),
}];
let proposal = self.db.new_proposal(batch).unwrap();
proposal.commit().unwrap();
self.db.new_proposal(batch)?.commit()?;

let hash = self.db.kv_root_hash().expect("root-hash should exist");
self.hashes.push_front(hash);
Ok(())
}

fn commit_proposal(&mut self, proposal: Proposal) {
Expand Down
Loading