Skip to content

Commit

Permalink
Add new Tree::entries function to return key/value tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
DrChat committed Dec 30, 2024
1 parent 256365f commit 50b5cd6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions atrium-repo/src/mst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ impl<S: AsyncBlockStoreRead> Tree<S> {
}
}

/// Returns a stream of all keys in this tree, in lexicographic order.
pub fn keys<'a>(&'a mut self) -> impl Stream<Item = Result<String, Error>> + 'a {
/// Returns a stream of all entries in this tree, in lexicographic order.
pub fn entries<'a>(&'a mut self) -> impl Stream<Item = Result<(String, Cid), Error>> + 'a {
// Start from the root of the tree.
let mut stack = vec![Located::InSubtree(self.root)];

Expand All @@ -667,17 +667,22 @@ impl<S: AsyncBlockStoreRead> Tree<S> {
stack.push(Located::InSubtree(entry.clone()));
}
NodeEntry::Leaf(entry) => {
stack.push(Located::Entry(entry.key.clone()));
stack.push(Located::Entry((entry.key.clone(), entry.value.clone())));
}
}
}
}
Located::Entry(key) => yield key,
Located::Entry((key, value)) => yield (key, value),
}
}
}
}

/// Returns a stream of all keys in this tree, in lexicographic order.
pub fn keys<'a>(&'a mut self) -> impl Stream<Item = Result<String, Error>> + 'a {
self.entries().map(|e| e).map(|e| e.map(|(k, _)| k))
}

/// Returns the specified record from the repository, or `None` if it does not exist.
pub async fn get(&mut self, key: &str) -> Result<Option<Cid>, Error> {
match algos::traverse(&mut self.storage, self.root, algos::traverse_find(key)).await {
Expand Down

0 comments on commit 50b5cd6

Please sign in to comment.