Skip to main content

EventDag

Struct EventDag 

Source
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

Source

pub fn new() -> Self

Create an empty DAG.

Source

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.

Source

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.

Source

pub fn with_capacity(capacity: usize) -> Self

Create an empty DAG with pre-allocated capacity.

Source

pub fn len(&self) -> usize

Number of events in the DAG.

Source

pub fn is_empty(&self) -> bool

Returns true if the DAG has no events.

Source

pub fn get(&self, hash: &str) -> Option<&DagNode>

Look up a node by its event hash.

Source

pub fn get_event(&self, hash: &str) -> Option<&Event>

Return the event for a given hash.

Source

pub fn contains(&self, hash: &str) -> bool

Returns true if the DAG contains an event with the given hash.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn are_concurrent(&self, a: &str, b: &str) -> bool

Check if two events are concurrent (neither is an ancestor of the other).

Source

pub fn hashes(&self) -> impl Iterator<Item = &str>

Return an iterator over all event hashes in the DAG.

Source

pub fn nodes(&self) -> impl Iterator<Item = (&str, &DagNode)>

Return an iterator over all nodes in the DAG.

Trait Implementations§

Source§

impl Clone for EventDag

Source§

fn clone(&self) -> EventDag

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EventDag

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EventDag

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more