Table of Contents

Class GraphTransaction<T>

Namespace
AiDotNet.RetrievalAugmentedGeneration.Graph
Assembly
AiDotNet.dll

Transaction coordinator for managing transactions on graph stores with best-effort rollback.

public class GraphTransaction<T> : IDisposable

Type Parameters

T

The numeric type used for vector operations.

Inheritance
GraphTransaction<T>
Implements
Inherited Members

Remarks

This class provides transaction management with the following guarantees: - Atomicity (Best-Effort): If an operation fails during commit, compensating rollback is attempted in reverse order. However, if an undo operation fails, it is swallowed and rollback continues with remaining operations. Full atomicity is not guaranteed. - Consistency: Graph validation rules are enforced during operations. - Isolation: Single-threaded; no concurrent transaction support. - Durability: When a WAL is provided, operations are logged before execution. Without a WAL, durability is not guaranteed.

Important: This is a lightweight transaction implementation suitable for single-process use cases. For full ACID compliance with crash recovery, ensure a WriteAheadLog is configured.

For Beginners: Transactions ensure your changes are safe.

Think of a bank transfer:

  • Debit $100 from Alice
  • Credit $100 to Bob

Without transactions:

  • If crash happens after debit but before credit, $100 disappears!

With transactions:

  • Begin transaction
  • Debit Alice
  • Credit Bob
  • Commit (both succeed) OR Rollback (both undone)
  • Money never disappears!

In graphs:

var txn = new GraphTransaction(store, wal);
txn.Begin();
try
{
    txn.AddNode(node1);
    txn.AddEdge(edge1);
    txn.Commit(); // Both saved
}
catch
{
    txn.Rollback(); // Both undone
}

This ensures your graph is never in a broken state!

Constructors

GraphTransaction(IGraphStore<T>, WriteAheadLog?)

Initializes a new instance of the GraphTransaction<T> class.

public GraphTransaction(IGraphStore<T> store, WriteAheadLog? wal = null)

Parameters

store IGraphStore<T>

The graph store to operate on.

wal WriteAheadLog

Optional Write-Ahead Log for durability.

Properties

State

Gets the current state of the transaction.

public TransactionState State { get; }

Property Value

TransactionState

TransactionId

Gets the transaction ID.

public long TransactionId { get; }

Property Value

long

Methods

AddEdge(GraphEdge<T>)

Adds an edge within the transaction.

public void AddEdge(GraphEdge<T> edge)

Parameters

edge GraphEdge<T>

The edge to add.

AddNode(GraphNode<T>)

Adds a node within the transaction.

public void AddNode(GraphNode<T> node)

Parameters

node GraphNode<T>

The node to add.

Begin()

Begins a new transaction.

public void Begin()

Exceptions

InvalidOperationException

Thrown if transaction already started.

Commit()

Commits the transaction, applying all operations with best-effort atomicity.

public void Commit()

Remarks

If an operation fails mid-way, compensating rollback logic is executed to undo already-applied operations in reverse order. However, this is best-effort: if an undo operation throws an exception, it is caught and swallowed, and rollback continues with remaining operations.

This means that after a failed commit, the graph may be left in a partially modified state if undo operations also fail. For production use cases requiring strict atomicity, consider using a database-backed graph store with native transaction support.

Exceptions

InvalidOperationException

Thrown if transaction not active.

Dispose()

Disposes the transaction, rolling back if still active.

public void Dispose()

RemoveEdge(string)

Removes an edge within the transaction.

public void RemoveEdge(string edgeId)

Parameters

edgeId string

The ID of the edge to remove.

RemoveNode(string)

Removes a node within the transaction.

public void RemoveNode(string nodeId)

Parameters

nodeId string

The ID of the node to remove.

Rollback()

Rolls back the transaction, discarding all operations.

public void Rollback()