Skip to main content

GraphTransaction

Struct GraphTransaction 

Source
pub struct GraphTransaction<'g> { /* private fields */ }
Expand description

A transactional wrapper for batching graph mutations.

Collects added nodes and edges, deferring event emission until commit(). If dropped without commit, all additions are rolled back (nodes and edges removed).

The &'g mut ComponentGraph borrow ensures Rust’s borrow checker prevents any concurrent access to the graph during the transaction — zero-cost safety.

§Cross-system usage

For operations that span the graph and external systems (runtime initialization, HashMap insertion), use this pattern:

// Phase 1: Graph transaction
{
    let mut graph = self.graph.write().await;
    let mut txn = graph.begin();
    txn.add_component(node)?;
    txn.commit(); // graph is consistent
}

// Phase 2: Runtime init (no graph lock)
let instance = match RuntimeType::new(...) {
    Ok(i) => i,
    Err(e) => {
        // Compensating rollback
        let mut graph = self.graph.write().await;
        let _ = graph.remove_component(&id);
        return Err(e);
    }
};

Implementations§

Source§

impl<'g> GraphTransaction<'g>

Source

pub fn add_component(&mut self, node: ComponentNode) -> Result<NodeIndex>

Add a component node to the graph within this transaction.

The node is added immediately (so subsequent add_relationship calls can reference it), but the event is deferred until commit(). On rollback (drop without commit), the node and its ownership edges are removed.

Source

pub fn add_relationship( &mut self, from_id: &str, to_id: &str, forward: RelationshipKind, ) -> Result<()>

Add a bidirectional relationship within this transaction.

The edges are added immediately; on rollback they are removed. Idempotent: if the relationship already exists, this is a no-op.

Source

pub fn commit(self)

Commit the transaction: emit all deferred events.

After commit, the mutations are permanent and cannot be rolled back through this transaction. For cross-system rollback, use compensating actions (e.g., graph.remove_component()).

Trait Implementations§

Source§

impl<'g> Drop for GraphTransaction<'g>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'g> Freeze for GraphTransaction<'g>

§

impl<'g> !RefUnwindSafe for GraphTransaction<'g>

§

impl<'g> Send for GraphTransaction<'g>

§

impl<'g> Sync for GraphTransaction<'g>

§

impl<'g> Unpin for GraphTransaction<'g>

§

impl<'g> UnsafeUnpin for GraphTransaction<'g>

§

impl<'g> !UnwindSafe for GraphTransaction<'g>

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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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