GraphTransaction<T>
Models & Types · AiDotNet.RetrievalAugmentedGeneration.Graph
Transaction coordinator for managing transactions on graph stores with best-effort rollback.
Implements: IDisposable
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 store = new MemoryGraphStore<double>();
var wal = new WriteAheadLog("graph.wal");
var node1 = new GraphNode<double>("n1", "Person");
var edge1 = new GraphEdge<double>("n1", "n1", "KNOWS");
var txn = new GraphTransaction<double>(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!
How It Works
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.
Constructors
| Constructor | Summary |
|---|---|
GraphTransaction(IGraphStore<T> store, WriteAheadLog wal) |
Initializes a new instance of the GraphTransaction class. |
Properties
| Property | Summary |
|---|---|
TransactionState State { get; } |
Gets the current state of the transaction. |
long TransactionId { get; } |
Gets the transaction ID. |
Methods
| Method | Summary |
|---|---|
void AddEdge(GraphEdge<T> edge) |
Adds an edge within the transaction. |
void AddNode(GraphNode<T> node) |
Adds a node within the transaction. |
void Begin() |
Begins a new transaction. |
void Commit() |
Commits the transaction, applying all operations with best-effort atomicity. |
void Dispose() |
Disposes the transaction, rolling back if still active. |
void RemoveEdge(string edgeId) |
Removes an edge within the transaction. |
void RemoveNode(string nodeId) |
Removes a node within the transaction. |
void Rollback() |
Rolls back the transaction, discarding all operations. |