Skip to main content

uqa_storage_sqlite/mvcc/native/
mod.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Native `SQLite` row mapping, atomic baseline conversion and evaluated commit materialization. The record adapter preserves existing physical tables while shared storage owns private changes and visibility; native catalog/store session routing remains separate.
8
9mod capture;
10mod diskann;
11pub(crate) use row::{binary_pair_limit, variable_fields_limit, vector_row_limit};
12mod family;
13mod field_guards;
14pub(crate) use field_guards::VECTOR_FIELD_GUARD_PREFIX;
15pub(crate) mod foreign_servers;
16mod format;
17mod graph_cache;
18mod graph_guards;
19mod graph_lifetimes;
20mod graph_lookup;
21mod key;
22mod layout;
23mod maintenance;
24mod notifications;
25pub(super) use maintenance::NativeMaintenanceRecords;
26mod occurrence_guards;
27mod owners;
28mod physical;
29pub(crate) mod populations;
30mod projection;
31mod queue;
32mod row;
33mod sequence_values;
34mod sequences;
35mod session;
36mod standalone_graph;
37mod vector_guards;
38
39pub(crate) use session::{NativeSnapshot, PrivateRows};
40
41pub(super) use capture::{capture_key, CapturedKey};
42pub(super) use format::{check_mapping, initialize, initialize_in, present, reject_mapped};
43pub(super) use format::{initialize_restoration, validate_restoration};
44pub(super) use projection::materialize;
45
46#[cfg(test)]
47mod tests;
48
49pub use family::NativeRecordFamily;
50pub use key::{NativeRecordIdentity, NativeRecordOwner};
51pub use layout::{NativeColumnType, NativeRecordLayout};
52pub use row::{decode_row, encode_row};
53
54use rusqlite::types::ValueRef;
55use uqa_core::memory::BudgetedVec;
56use uqa_storage::mvcc::{CommitSequence, DatabaseId, RecordWrite, VersionError, VersionResult};
57use uqa_storage::read_control::StorageReadControl;
58
59/// Stable data addressing, independent of the transaction history incarnation.
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61pub(super) struct NativeRecordNamespace(pub(super) DatabaseId);
62
63pub(super) struct NativeMapping {
64    pub(super) identity: DatabaseId,
65    pub(super) namespace: NativeRecordNamespace,
66}
67
68/// One evaluated native row in the provider's record encoding. Its charged buffers can be borrowed by common storage's conditional commit preparation.
69#[derive(Debug)]
70pub struct NativeRecord {
71    key: BudgetedVec<u8>,
72    row: BudgetedVec<u8>,
73}
74
75impl NativeRecord {
76    pub fn encode(
77        family: NativeRecordFamily,
78        owner: NativeRecordOwner,
79        values: &[ValueRef<'_>],
80        control: &StorageReadControl,
81    ) -> VersionResult<Self> {
82        control.cancellation().check()?;
83        let layout = family.layout();
84        let identity = NativeRecordIdentity::new(family, owner)?;
85        identity.validate_row(values)?;
86        let mut components = BudgetedVec::new(control.memory());
87        for &column in layout.identity_columns {
88            components.push(values[column])?;
89        }
90        let key = identity.encode_key(&components, control)?;
91        let row = encode_row(values, control)?;
92        Ok(Self { key, row })
93    }
94
95    pub fn key(&self) -> &[u8] {
96        &self.key
97    }
98
99    pub fn row(&self) -> &[u8] {
100        &self.row
101    }
102
103    pub fn write(&self, expected: Option<CommitSequence>) -> RecordWrite<'_> {
104        RecordWrite {
105            key: self.key(),
106            expected,
107            value: Some(self.row()),
108        }
109    }
110}
111
112/// Validate a retained row against its full native key and return borrowed column values. This rejects a correct-looking payload addressed to a different tuple or family before physical materialization.
113pub fn decode_record<'a>(
114    key: &[u8],
115    row: &'a [u8],
116    control: &StorageReadControl,
117) -> VersionResult<(NativeRecordIdentity, BudgetedVec<ValueRef<'a>>)> {
118    control.cancellation().check()?;
119    let identity = NativeRecordIdentity::decode(key)?;
120    let layout = identity.family().layout();
121    let values = decode_row(row, layout.columns.len(), control)?;
122    identity.validate_row(&values)?;
123    if !identity.matches_row_key(key, &values, control)? {
124        return Err(invalid("native row does not match its record key"));
125    }
126    Ok((identity, values))
127}
128
129fn invalid(message: &'static str) -> VersionError {
130    VersionError::InvalidEncoding(message)
131}