pub struct EventDag { /* private fields */ }Expand description
An in-memory DAG of events, indexed by content-addressed hash.
Provides O(1) insertion, O(1) node lookup, and efficient traversal in both causal directions (ancestors and descendants).
Implementations§
Source§impl EventDag
impl EventDag
Sourcepub fn insert(&mut self, event: Event)
pub fn insert(&mut self, event: Event)
Insert an event into the DAG.
- Registers the event as a node with its declared parents.
- Creates bidirectional parent→child links for any parents already in the DAG.
- If a later-inserted parent references this event, the link is created then.
- Duplicate events (same
event_hash) are silently skipped.
Runs in O(P) where P is the number of parents for this event.
Sourcepub fn from_events(events: &[Event]) -> Self
pub fn from_events(events: &[Event]) -> Self
Build a DAG from a slice of events.
Events are inserted in the order given. For replay from an event log, this is typically chronological order.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create an empty DAG with pre-allocated capacity.
Sourcepub fn contains(&self, hash: &str) -> bool
pub fn contains(&self, hash: &str) -> bool
Returns true if the DAG contains an event with the given hash.
Sourcepub fn roots(&self) -> Vec<&str>
pub fn roots(&self) -> Vec<&str>
Return the hashes of all root events (events with no parents in the DAG).
Multiple roots occur when agents create items concurrently without seeing each other’s genesis events.
Sourcepub fn tips(&self) -> Vec<&str>
pub fn tips(&self) -> Vec<&str>
Return the hashes of all tip events (events with no children).
Tips are the “current heads” of the DAG — events that no other event has yet referenced as a parent.
Sourcepub fn ancestors(&self, hash: &str) -> HashSet<String>
pub fn ancestors(&self, hash: &str) -> HashSet<String>
Get all ancestor hashes of the given event (transitive parents).
Performs a BFS walk up the parent chain. Returns an empty set for root events. Does NOT include the starting event itself.
Sourcepub fn descendants(&self, hash: &str) -> HashSet<String>
pub fn descendants(&self, hash: &str) -> HashSet<String>
Get all descendant hashes of the given event (transitive children).
Performs a BFS walk down the children chain. Returns an empty set for tip events. Does NOT include the starting event itself.
Sourcepub fn topological_order(&self) -> Vec<&Event>
pub fn topological_order(&self) -> Vec<&Event>
Iterate events in topological (causal) order via Kahn’s algorithm.
Events with no unresolved parents come first. If multiple events are ready simultaneously, they are returned in hash-sorted order for determinism.
§Multiple Roots
The algorithm handles multiple roots naturally — all root events start in the ready set.
Sourcepub fn is_ancestor(&self, a: &str, b: &str) -> bool
pub fn is_ancestor(&self, a: &str, b: &str) -> bool
Check if event a is a causal ancestor of event b.
Returns true if there is a directed path from a to b in the DAG
(i.e., a happened before b).
Sourcepub fn are_concurrent(&self, a: &str, b: &str) -> bool
pub fn are_concurrent(&self, a: &str, b: &str) -> bool
Check if two events are concurrent (neither is an ancestor of the other).