Skip to content

Commit

Permalink
Implement iterator over nodes; refactor key-value iterator (#517)
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 13, 2024
1 parent 9bbe19b commit 94155c2
Show file tree
Hide file tree
Showing 4 changed files with 640 additions and 418 deletions.
5 changes: 3 additions & 2 deletions firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,15 @@ impl<S: ShaleStore<Node> + Send + Sync> api::DbView for DbRev<S> {

impl<S: ShaleStore<Node> + Send + Sync> DbRev<S> {
pub fn stream(&self) -> merkle::MerkleKeyValueStream<'_, S, Bincode> {
self.merkle.iter(self.header.kv_root)
self.merkle.key_value_iter(self.header.kv_root)
}

pub fn stream_from(
&self,
start_key: Box<[u8]>,
) -> merkle::MerkleKeyValueStream<'_, S, Bincode> {
self.merkle.iter_from(self.header.kv_root, start_key)
self.merkle
.key_value_iter_from_key(self.header.kv_root, start_key)
}

fn flush_dirty(&mut self) -> Option<()> {
Expand Down
10 changes: 6 additions & 4 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,11 +1715,11 @@ impl<S: ShaleStore<Node> + Send + Sync, T> Merkle<S, T> {
self.store.flush_dirty()
}

pub(crate) fn iter(&self, root: DiskAddress) -> MerkleKeyValueStream<'_, S, T> {
pub(crate) fn key_value_iter(&self, root: DiskAddress) -> MerkleKeyValueStream<'_, S, T> {
MerkleKeyValueStream::new(self, root)
}

pub(crate) fn iter_from(
pub(crate) fn key_value_iter_from_key(
&self,
root: DiskAddress,
key: Box<[u8]>,
Expand Down Expand Up @@ -1750,8 +1750,10 @@ impl<S: ShaleStore<Node> + Send + Sync, T> Merkle<S, T> {

let mut stream = match first_key {
// TODO: fix the call-site to force the caller to do the allocation
Some(key) => self.iter_from(root, key.as_ref().to_vec().into_boxed_slice()),
None => self.iter(root),
Some(key) => {
self.key_value_iter_from_key(root, key.as_ref().to_vec().into_boxed_slice())
}
None => self.key_value_iter(root),
};

// fetch the first key from the stream
Expand Down
Loading

0 comments on commit 94155c2

Please sign in to comment.