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 (root, root_node) = roots.first().unwrap();
let manifest = root_node.manifest.clone(); let manifest = root_node.manifest.clone();
let rng = fastrand::Rng::new(); let rng = fastrand::Rng::new();
let items = self.graph.sorted(rng).into_iter().map(|oid| { let history = evaluate(*self.graph[*root].id(), &self.graph, rng);
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)
};
CollaborativeObject { CollaborativeObject {
manifest, manifest,

View File

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

View File

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