cob: Move graph sorting to `evaluate` function

This avoids passing the sorted items as an iterator.

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-12-28 18:38:01 +01:00
parent 70260236b2
commit 86e56d2053
No known key found for this signature in database
3 changed files with 20 additions and 32 deletions

View File

@ -93,20 +93,7 @@ impl ChangeGraph {
let (root, root_node) = roots.first().unwrap();
let manifest = root_node.manifest.clone();
let rng = fastrand::Rng::new();
let items = self.graph.sorted(rng).into_iter().map(|oid| {
let node = &self.graph[&oid];
let child_commits = node
.dependents
.iter()
.map(|e| *self.graph[e].id())
.collect::<Vec<_>>();
(&node.value, oid, child_commits)
});
let history = {
let root_change = &self.graph[*root];
evaluate(*root_change.id(), &self.graph, items)
};
let history = evaluate(*self.graph[*root].id(), &self.graph, rng);
CollaborativeObject {
manifest,

View File

@ -15,17 +15,18 @@ use crate::{change::Change, history, pruning_fold};
/// # Panics
///
/// If the change corresponding to the root OID is not in `items`
pub fn evaluate<'b>(
root: Oid,
graph: &Dag<Oid, Change>,
items: impl Iterator<Item = (&'b Change, Oid, Vec<Oid>)>,
) -> history::History {
pub fn evaluate(root: Oid, graph: &Dag<Oid, Change>, rng: fastrand::Rng) -> history::History {
let entries = pruning_fold::pruning_fold(
HashMap::<EntryId, EntryWithClock>::new(),
items.map(|(change, idx, children)| ChangeWithChildren {
idx,
change,
child_commits: children,
graph.sorted(rng).into_iter().map(|oid| {
let node = &graph[&oid];
let child_commits = node.dependents.iter().copied().collect();
ChangeWithChildren {
oid,
change: &node.value,
child_commits,
}
}),
|mut entries, c| match evaluate_change(c.change, &c.child_commits) {
Err(RejectionReason::InvalidSignatures) => {
@ -37,11 +38,11 @@ pub fn evaluate<'b>(
}
Ok(entry) => {
// Get parent commits and calculate this node's clock based on theirs.
let clock = graph[&c.idx]
let clock = graph[&c.oid]
.dependencies
.iter()
.map(|e| {
let entry = &entries[&graph[e].id.into()];
let entry = &entries[&EntryId::from(*e)];
let clock = entry.clock();
clock + entry.contents().len() as Clock - 1
@ -81,7 +82,7 @@ fn evaluate_change(
}
struct ChangeWithChildren<'a> {
idx: Oid,
oid: Oid,
change: &'a Change,
child_commits: Vec<Oid>,
}

View File

@ -105,12 +105,12 @@ impl History {
where
F: for<'r> FnMut(A, &'r EntryWithClock) -> ControlFlow<A, A>,
{
let sorted = self.graph.sorted(fastrand::Rng::new());
#[allow(clippy::let_and_return)]
let items = sorted.iter().map(|idx| {
let entry = &self.graph[idx];
entry
});
let items = self
.graph
.sorted(fastrand::Rng::new())
.into_iter()
.map(|idx| &self.graph[&idx]);
pruning_fold::pruning_fold(init, items, f)
}