beginSnapshot method
Begin a read-only snapshot transaction. The current state of every table is cloned and used as the live view for the duration of the transaction; concurrent writers in the same process block on the writer arm of rwLock until the snapshot is committed/rolled back. Any mutation inside the snapshot is rejected.
Implementation
Future<QueryResult> beginSnapshot() => _lock.write(() async {
if (inTransaction) {
throw StateError('Already in a transaction');
}
// Stash the live state; install a deep-cloned view as _tables
// so all read paths transparently see the snapshot.
_liveTables = Map<String, Table>.from(_tables);
_liveViews = Map<String, SelectStmt>.from(_views);
final cloned = {
for (final e in _tables.entries) e.key: e.value.clone()
};
_snapshot = Map<String, Table>.from(cloned);
_viewSnapshot = Map<String, SelectStmt>.from(_views);
_tables
..clear()
..addAll(cloned);
_readOnlySnapshot = true;
return QueryResult.message('Snapshot transaction started');
});