cob: Remove redundant history indices

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-12-28 17:53:00 +01:00
parent 2f07b76fcb
commit 57fcb117fc
No known key found for this signature in database
2 changed files with 22 additions and 12 deletions

View File

@ -21,7 +21,6 @@ pub use entry::{Clock, Contents, Entry, EntryId, EntryWithClock, Timestamp};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct History { pub struct History {
graph: Dag<EntryId, EntryWithClock>, graph: Dag<EntryId, EntryWithClock>,
indices: HashMap<EntryId, Oid>,
} }
impl PartialEq for History { impl PartialEq for History {
@ -150,30 +149,23 @@ impl History {
}, },
); );
for tip in tips { for tip in tips {
let tip_ix = self.indices.get(&tip.into()).unwrap(); self.graph.dependency(new_id, (*tip).into());
self.graph.dependency(new_id, (*tip_ix).into());
} }
} }
} }
fn create_dag<'a>(root: &'a EntryId, entries: &'a HashMap<EntryId, EntryWithClock>) -> History { 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(); let root_entry = entries.get(root).unwrap().clone();
graph.node(*root, root_entry.clone()); let mut graph: Dag<EntryId, EntryWithClock> = Dag::root(*root, root_entry.clone());
indices.insert(root_entry.id, (*root).into());
let mut to_process = vec![root_entry]; let mut to_process = vec![root_entry];
while let Some(entry) = to_process.pop() { while let Some(entry) = to_process.pop() {
let entry_ix = indices[&entry.id];
for child_id in entry.children() { for child_id in entry.children() {
let child = entries[child_id].clone(); let child = entries[child_id].clone();
graph.node(*child_id, child.clone()); graph.node(*child_id, child.clone());
indices.insert(child.id, (*child_id).into()); graph.dependency(*child_id, entry.id);
graph.dependency(*child_id, entry_ix.into());
to_process.push(child.clone()); to_process.push(child.clone());
} }
} }
History { graph, indices } History { graph }
} }

View File

@ -17,6 +17,16 @@ pub struct Node<K: Eq + Hash, V> {
pub dependents: HashSet<K>, 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> { impl<K: Eq + Hash, V> Borrow<V> for &Node<K, V> {
fn borrow(&self) -> &V { fn borrow(&self) -> &V {
&self.value &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. /// Check whether there are any nodes in the graph.
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.graph.is_empty() self.graph.is_empty()