Skip to content

Commit

Permalink
Return keys from Tree::keys in lexicographic order
Browse files Browse the repository at this point in the history
  • Loading branch information
DrChat committed Dec 30, 2024
1 parent cbb301b commit 49fb8df
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions atrium-repo/src/mst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,20 +651,28 @@ impl<S: AsyncBlockStoreRead> Tree<S> {
}
}

/// Returns a stream of all keys in this tree, in breadth-first order.
/// 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 {
// Start from the root of the tree.
let mut stack = vec![self.root];
let mut stack = vec![Located::InSubtree(self.root)];

try_stream! {
while let Some(cid) = stack.pop() {
let node = Node::read_from(&mut self.storage, cid).await?;
for entry in node.trees() {
stack.push(entry.clone());
}

for entry in node.leaves() {
yield entry.key.clone();
while let Some(e) = stack.pop() {
match e {
Located::InSubtree(cid) => {
let node = Node::read_from(&mut self.storage, cid).await?;
for entry in node.entries.iter().rev() {
match entry {
NodeEntry::Tree(entry) => {
stack.push(Located::InSubtree(entry.clone()));
}
NodeEntry::Leaf(entry) => {
stack.push(Located::Entry(entry.key.clone()));
}
}
}
}
Located::Entry(key) => yield key,
}
}
}
Expand Down Expand Up @@ -1095,6 +1103,19 @@ mod test {
tree.root,
Cid::from_str("bafyreicmahysq4n6wfuxo522m6dpiy7z7qzym3dzs756t5n7nfdgccwq7m").unwrap()
);

// Ensure keys are returned in lexicographic order.
let keys = tree.keys().try_collect::<Vec<_>>().await.unwrap();
assert_eq!(
keys.as_slice(),
&[
"com.example.record/3jqfcqzm3fp2j",
"com.example.record/3jqfcqzm3fr2j",
"com.example.record/3jqfcqzm3fs2j",
"com.example.record/3jqfcqzm3ft2j",
"com.example.record/3jqfcqzm4fc2j",
]
)
}

#[tokio::test]
Expand Down

0 comments on commit 49fb8df

Please sign in to comment.