Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cob: Move graph sorting to `evaluate` function
Alexis Sellier committed 3 years ago
commit 86e56d20530658706eae8d1415707ed181c6bc52
parent 70260236b28bc29b6a67f8bb613b1b7b46506850
3 files changed +20 -32
modified radicle-cob/src/change_graph.rs
@@ -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,
modified radicle-cob/src/change_graph/evaluation.rs
@@ -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>,
}
modified radicle-cob/src/history.rs
@@ -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)
    }