Skip to main content

MvStore

Struct MvStore 

Source
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

Source

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.

Source

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.

Source

pub fn clock(&self) -> &Arc<MvccClock>

Returns the shared clock. The same Arc every consumer (commit path, read path, GC) holds.

Source

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.

Source

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.

Source

pub fn total_versions(&self) -> usize

Total versions across every chain. Linear in row count; intended for tests + assertions, not the hot path.

Source

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 begin is a committed timestamp <= begin_ts, and
  • the version’s end is None (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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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 whose end timestamp is > watermark may 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 — its end is None).

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for MvStore

Source§

fn clone(&self) -> MvStore

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MvStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.