rollbackTxn method

void rollbackTxn(
  1. int txnId
)

Undo all changes made by txnId: • New versions (createdByTxn == txnId): remove from chain. • Deleted versions (deletedByTxn == txnId): restore.

Implementation

void rollbackTxn(int txnId) {
  // Collect chains that need rollback
  final toRemove = <int>[];

  for (final entry in _chains.entries) {
    final chain = entry.value;
    // Undo deletes
    if (chain.head.deletedByTxn == txnId) {
      chain.head.deletedByTxn = null;
    }

    // Walk and prune created-by-txn versions
    MvccRowVersion? prev;
    MvccRowVersion? v = chain.head;
    while (v != null) {
      if (v.createdByTxn == txnId) {
        // Remove this version from the chain
        if (prev == null) {
          // It's the head
          if (v.prev != null) {
            chain.head = v.prev!;
          } else {
            toRemove.add(entry.key);
          }
        } else {
          prev.prev = v.prev;
        }
      } else {
        prev = v;
      }
      v = v.prev;
    }
  }

  for (final id in toRemove) {
    _chains.remove(id);
  }
}