pub struct MvStore { /* private fields */ }Expand description
In-memory MVCC version index. Cheap to clone — the heavy state
is behind Arcs.
Implementations§
Source§impl MvStore
impl MvStore
Sourcepub fn new(clock: Arc<MvccClock>) -> Self
pub fn new(clock: Arc<MvccClock>) -> Self
Builds an empty store wired to a shared clock + registry.
Phase 11.3 wires this into Database so every connection
observes the same version index; 11.2’s Wal::clock_high_water
seeds the clock at open time.
Sourcepub fn fresh() -> (Self, Arc<MvccClock>)
pub fn fresh() -> (Self, Arc<MvccClock>)
Convenience for tests + standalone callers — builds a store
over a freshly-allocated clock seeded at 0. The clock is
returned so the caller can tick() it to allocate
timestamps for hand-built versions.
Sourcepub fn clock(&self) -> &Arc<MvccClock>
pub fn clock(&self) -> &Arc<MvccClock>
Returns the shared clock. The same Arc every consumer
(commit path, read path, GC) holds.
Sourcepub fn active_registry(&self) -> &ActiveTxRegistry
pub fn active_registry(&self) -> &ActiveTxRegistry
Returns the active-transaction registry. Phase 11.4 will
register BEGIN CONCURRENT transactions here; Phase 11.6
reads min_active_begin_ts() to set the GC watermark.
Sourcepub fn tracked_rows(&self) -> usize
pub fn tracked_rows(&self) -> usize
Number of rows the store holds at least one version for. Cheap diagnostic — locks only the outer map briefly.
Sourcepub fn total_versions(&self) -> usize
pub fn total_versions(&self) -> usize
Total versions across every chain. Linear in row count; intended for tests + assertions, not the hot path.
Sourcepub fn read(&self, row_id: &RowID, begin_ts: u64) -> Option<VersionPayload>
pub fn read(&self, row_id: &RowID, begin_ts: u64) -> Option<VersionPayload>
Returns the version of row_id that’s visible to a reader
transaction whose begin-timestamp is begin_ts, or None
if no version satisfies the snapshot-isolation rule.
Snapshot-isolation visibility:
- the version’s
beginis a committed timestamp<= begin_ts, and - the version’s
endisNone(still latest) or a committed timestamp> begin_ts.
In-flight versions (begin = Id(_)) are never visible to
other readers — they’re a placeholder until the producing
transaction either commits (the version’s begin is rewritten
to a Timestamp) or aborts (the version is dropped). The
producing transaction itself reads its own writes through a
separate path (Phase 11.4); it doesn’t go through this
function.
The chain is scanned front to back: in v0 we don’t trust
any insertion order, so the loop must not exit early. When
the chain becomes ordered-by-begin (a natural property of
the commit path’s append-only writes in 11.4), this can
short-circuit on the first visible version.
Sourcepub fn visible_at(version: &RowVersion, begin_ts: u64) -> bool
pub fn visible_at(version: &RowVersion, begin_ts: u64) -> bool
Returns true if version is visible to a reader whose
begin-timestamp is begin_ts. Pure function — exposed for
tests + future GC code.
Sourcepub fn latest_committed_begin(&self, row_id: &RowID) -> Option<u64>
pub fn latest_committed_begin(&self, row_id: &RowID) -> Option<u64>
Returns the begin-timestamp of the latest committed version
in row_id’s chain, or None if the row has no committed
versions (the chain is empty or only carries in-flight
placeholders).
Phase 11.4 — the commit-validation pass calls this for
every row in its write-set. If the latest committed begin
is greater than the validating transaction’s begin_ts,
some other transaction superseded the row after our
snapshot — abort with crate::error::SQLRiteError::Busy.
Sourcepub fn push_committed(
&self,
row_id: RowID,
version: RowVersion,
) -> Result<(), MvStoreError>
pub fn push_committed( &self, row_id: RowID, version: RowVersion, ) -> Result<(), MvStoreError>
Pushes a new version onto the chain for row_id. Caps the
chain’s previous latest version (if any) at version.begin
— the canonical write-side bookkeeping the commit path will
use in 11.4.
version.begin must be a Timestamp (committed) — pushing
an in-flight version through this entry point would break
the cap rule. Use MvStore::push_in_flight for in-flight
versions; commit will rewrite their begin later.
Errors if the new begin is <= the previous latest's begin (violates monotonicity — the commit path must always
hand out increasing timestamps via the MvccClock).
Sourcepub fn push_in_flight(&self, row_id: RowID, version: RowVersion)
pub fn push_in_flight(&self, row_id: RowID, version: RowVersion)
Pushes an in-flight version onto the chain. Used by the
11.4 write path while a BEGIN CONCURRENT transaction is
open; the version’s begin is rewritten from Id(tx) to
Timestamp(commit_ts) on commit, and the previous latest
gets capped at the same timestamp (via Self::push_committed
at commit time, after the in-flight version is removed).
11.3 ships this as standalone API for tests; 11.4 wires it into the executor.
Sourcepub fn active_watermark(&self) -> u64
pub fn active_watermark(&self) -> u64
Returns the GC watermark — the timestamp below which any committed-and-superseded version is reclaimable.
- If there’s at least one in-flight transaction, the
watermark is its
begin_ts(the smallest one across the active set). Versions whoseendtimestamp is> watermarkmay still be visible to that reader and must be kept. - With no in-flight transactions the watermark is
u64::MAX, meaning every superseded version can go (the latest version per row stays — itsendisNone).
The +1 shift versus the strict snapshot-isolation
reclamation rule keeps the math simple: gc_chain retains
versions whose end-timestamp is strictly greater than the
watermark, so watermark = u64::MAX reclaims every version
with end = Some(_) cleanly.
Sourcepub fn gc_chain(&self, row_id: &RowID, watermark: u64) -> usize
pub fn gc_chain(&self, row_id: &RowID, watermark: u64) -> usize
Garbage-collects row_id’s version chain against
watermark. A committed version is reclaimable when its
end timestamp is <= watermark — at that point no
reader’s begin_ts falls in the half-open [begin, end)
interval that the snapshot-isolation rule requires for
visibility. In-flight versions and the latest committed
version (end == None) are always kept.
Returns the number of versions reclaimed. Drops the chain from the outer map entirely if it ends up empty (no versions left after the sweep), so the per-row entry doesn’t leak memory.
Sourcepub fn gc_all(&self, watermark: u64) -> usize
pub fn gc_all(&self, watermark: u64) -> usize
Sweeps every row in the store against watermark. Returns
the total number of versions reclaimed. Used by
crate::Connection::vacuum_mvcc for an explicit full
drain; per-commit callers should prefer
MvStore::gc_chain over the rows they actually touched.