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>
impl<'g> GraphTransaction<'g>
Sourcepub fn add_component(&mut self, node: ComponentNode) -> Result<NodeIndex>
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.
Sourcepub fn add_relationship(
&mut self,
from_id: &str,
to_id: &str,
forward: RelationshipKind,
) -> Result<()>
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.