Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cob: Remove redundant history indices
Alexis Sellier committed 3 years ago
commit 57fcb117fc3f87ac34e947862fc9f86de218689f
parent 2f07b76fcbec54e1fb691d00c8858750726c6ddd
2 files changed +22 -12
modified radicle-cob/src/history.rs
@@ -21,7 +21,6 @@ pub use entry::{Clock, Contents, Entry, EntryId, EntryWithClock, Timestamp};
#[derive(Clone, Debug)]
pub struct History {
    graph: Dag<EntryId, EntryWithClock>,
-
    indices: HashMap<EntryId, Oid>,
}

impl PartialEq for History {
@@ -150,30 +149,23 @@ impl History {
            },
        );
        for tip in tips {
-
            let tip_ix = self.indices.get(&tip.into()).unwrap();
-
            self.graph.dependency(new_id, (*tip_ix).into());
+
            self.graph.dependency(new_id, (*tip).into());
        }
    }
}

fn create_dag<'a>(root: &'a EntryId, entries: &'a HashMap<EntryId, EntryWithClock>) -> History {
-
    let mut graph: Dag<EntryId, EntryWithClock> = Dag::new();
-
    let mut indices = HashMap::<EntryId, Oid>::new();
    let root_entry = entries.get(root).unwrap().clone();
-
    graph.node(*root, root_entry.clone());
-
    indices.insert(root_entry.id, (*root).into());
+
    let mut graph: Dag<EntryId, EntryWithClock> = Dag::root(*root, root_entry.clone());
    let mut to_process = vec![root_entry];

    while let Some(entry) = to_process.pop() {
-
        let entry_ix = indices[&entry.id];
-

        for child_id in entry.children() {
            let child = entries[child_id].clone();
            graph.node(*child_id, child.clone());
-
            indices.insert(child.id, (*child_id).into());
-
            graph.dependency(*child_id, entry_ix.into());
+
            graph.dependency(*child_id, entry.id);
            to_process.push(child.clone());
        }
    }
-
    History { graph, indices }
+
    History { graph }
}
modified radicle-dag/src/lib.rs
@@ -17,6 +17,16 @@ pub struct Node<K: Eq + Hash, V> {
    pub dependents: HashSet<K>,
}

+
impl<K: Eq + Hash, V> Node<K, V> {
+
    fn new(value: V) -> Self {
+
        Self {
+
            value,
+
            dependencies: HashSet::new(),
+
            dependents: HashSet::new(),
+
        }
+
    }
+
}
+

impl<K: Eq + Hash, V> Borrow<V> for &Node<K, V> {
    fn borrow(&self) -> &V {
        &self.value
@@ -49,6 +59,14 @@ impl<K: Eq + Copy + Hash, V> Dag<K, V> {
        }
    }

+
    pub fn root(key: K, value: V) -> Self {
+
        Self {
+
            graph: HashMap::from_iter([(key, Node::new(value))]),
+
            tips: HashSet::from_iter([key]),
+
            roots: HashSet::from_iter([key]),
+
        }
+
    }
+

    /// Check whether there are any nodes in the graph.
    pub fn is_empty(&self) -> bool {
        self.graph.is_empty()