Skip to content

Commit

Permalink
refactor: more ergonomic WorkplanItem
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Oct 2, 2024
1 parent 4d0e96f commit 65e2251
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion mp2-v1/tests/common/cases/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ where
// closure performing all the operations necessary beofre jumping to the next iteration
let mut end_iteration = |proven_nodes: &mut HashSet<K>| -> Result<()> {
proven_nodes.insert(k.clone());
workplan.done(&wk)?;
workplan.done(wk.k())?;
Ok(())
};
// since epoch starts at genesis now, we can directly give the value of the block
Expand Down
2 changes: 1 addition & 1 deletion mp2-v1/tests/common/celltree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl TestContext {
.unwrap()
)
);
workplan.done(&wk).unwrap();
workplan.done(wk.k()).unwrap();
}
let root = tree.root().await.unwrap();
let root_data = tree.root_data().await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion mp2-v1/tests/common/index_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl TestContext {
.store_proof(ProofKey::Index(proof_key), proof)
.expect("unable to store index tree proof");

workplan.done(&wk).unwrap();
workplan.done(wk.k()).unwrap();
}
let root = t.root().await.unwrap();
let root_proof_key = IndexProofIdentifier {
Expand Down
2 changes: 1 addition & 1 deletion mp2-v1/tests/common/rowtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl TestContext {
new_proof_key,
hex::encode(extract_hash_from_proof(&proof).unwrap().to_bytes())
);
workplan.done(&wk).unwrap();
workplan.done(wk.k()).unwrap();
}
let root = t.root().await.unwrap();
let row = table.row.fetch(&root).await;
Expand Down
2 changes: 1 addition & 1 deletion ryhope/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ where

let mut payload = self.storage.data().fetch(item.k()).await;
payload.aggregate(child_data.into_iter());
plan.done(&item)?;
plan.done(&item.k())?;
self.storage
.data_mut()
.store(item.k().to_owned(), payload)
Expand Down
36 changes: 24 additions & 12 deletions ryhope/src/storage/updatetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ impl<K: Clone + Hash + Eq + Debug> WorkplanItem<K> {
WorkplanItem::Subtree { k, .. } | WorkplanItem::Node { k, .. } => k,
}
}

pub fn as_subtree(&self) -> &UpdateTree<K> {
if let WorkplanItem::Subtree { subtree, .. } = self {
subtree
} else {
unreachable!()
}
}

pub fn as_node(&self) -> bool {
if let WorkplanItem::Node { is_path_end, .. } = self {
*is_path_end
} else {
unreachable!()
}
}
}

/// An update plan to recompute all the hashes of the touched nodes stored in a
Expand Down Expand Up @@ -419,15 +435,11 @@ impl<T: Debug + Clone + Hash + Eq> UpdatePlan<T> {

/// Mark the given item as having been completed. Its dependent will not be
/// generated by the iterator until the item has been marked as completed.
pub fn done(&mut self, item: &WorkplanItem<T>) -> Result<()> {
let i = *self
.t
.idx
.get(&item.k())
.ok_or_else(|| anyhow!("unknwown key"))?;
pub fn done(&mut self, k: &T) -> Result<()> {
let i = *self.t.idx.get(k).ok_or_else(|| anyhow!("unknwown key"))?;

// May happen when restarting a plan
self.anchors.retain(|k| k != item.k());
self.anchors.retain(|_k| _k != k);

// Root node is hardcoded to 0
if i == 0 {
Expand Down Expand Up @@ -541,14 +553,14 @@ mod tests {

loop {
let mut done = vec![];
while let Some(Next::Ready(k)) = workplan.next() {
println!("Doing {}", k.k());
done.push(k);
while let Some(Next::Ready(item)) = workplan.next() {
println!("Doing {}", item.k());
done.push(item);
workplan.t.print();
}

for d in done.iter() {
workplan.done(d).unwrap();
workplan.done(d.k()).unwrap();
}
if done.is_empty() {
break;
Expand Down Expand Up @@ -599,7 +611,7 @@ mod tests {
count_done += 1;
}
}
workplan.done(&item).unwrap();
workplan.done(item.k()).unwrap();
}

assert_eq!(count_done, mt.nodes.len());
Expand Down

0 comments on commit 65e2251

Please sign in to comment.