snapshotRead<T> method
Snapshot-read primitive: clones the current table set and runs
body against the clone. Multiple snapshotRead calls can
progress concurrently, and they don't observe writes that happen
after the clone moment. The clone is acquired under the read
arm of rwLock (so it is consistent with whatever a parallel
writer has just committed) and then released immediately, so the
body itself runs with no locks held.
This is the closest the engine gets to "real MVCC": readers and writers don't block each other once the snapshot is taken.
Implementation
Future<T> snapshotRead<T>(FutureOr<T> Function(Database snap) body) async {
final clonedTables = await _lock.read(
() => {for (final e in _tables.entries) e.key: e.value.clone()},
);
final snap = Database();
snap._tables.addAll(clonedTables);
snap._views.addAll(_views);
return body(snap);
}