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;
15mod format;
16mod graph_cache;
17mod graph_guards;
18mod graph_lifetimes;
19mod graph_lookup;
20mod key;
21mod layout;
22mod maintenance;
23mod notifications;
24pub(super) use maintenance::NativeMaintenanceRecords;
25mod occurrence_guards;
26mod owners;
27mod physical;
28pub(crate) mod populations;
29mod projection;
30mod queue;
31mod row;
32mod sequence_values;
33mod sequences;
34mod session;
35mod standalone_graph;
36mod vector_guards;
37
38pub(crate) use session::{NativeSnapshot, PrivateRows};
39
40pub(super) use capture::{capture_key, CapturedKey};
41pub(super) use format::{check_mapping, initialize, initialize_in, present, reject_mapped};
42pub(super) use format::{initialize_restoration, validate_restoration};
43pub(super) use projection::materialize;
44
45#[cfg(test)]
46mod tests;
47
48pub use family::NativeRecordFamily;
49pub use key::{NativeRecordIdentity, NativeRecordOwner};
50pub use layout::{NativeColumnType, NativeRecordLayout};
51pub use row::{decode_row, encode_row};
52
53use rusqlite::types::ValueRef;
54use uqa_core::memory::BudgetedVec;
55use uqa_storage::mvcc::{CommitSequence, DatabaseId, RecordWrite, VersionError, VersionResult};
56use uqa_storage::read_control::StorageReadControl;
57
58/// Stable data addressing, independent of the transaction history incarnation.
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub(super) struct NativeRecordNamespace(pub(super) DatabaseId);
61
62pub(super) struct NativeMapping {
63    pub(super) identity: DatabaseId,
64    pub(super) namespace: NativeRecordNamespace,
65}
66
67/// One evaluated native row in the provider's record encoding. Its charged buffers can be borrowed by common storage's conditional commit preparation.
68#[derive(Debug)]
69pub struct NativeRecord {
70    key: BudgetedVec<u8>,
71    row: BudgetedVec<u8>,
72}
73
74impl NativeRecord {
75    pub fn encode(
76        family: NativeRecordFamily,
77        owner: NativeRecordOwner,
78        values: &[ValueRef<'_>],
79        control: &StorageReadControl,
80    ) -> VersionResult<Self> {
81        control.cancellation().check()?;
82        let layout = family.layout();
83        let identity = NativeRecordIdentity::new(family, owner)?;
84        identity.validate_row(values)?;
85        let mut components = BudgetedVec::new(control.memory());
86        for &column in layout.identity_columns {
87            components.push(values[column])?;
88        }
89        let key = identity.encode_key(&components, control)?;
90        let row = encode_row(values, control)?;
91        Ok(Self { key, row })
92    }
93
94    pub fn key(&self) -> &[u8] {
95        &self.key
96    }
97
98    pub fn row(&self) -> &[u8] {
99        &self.row
100    }
101
102    pub fn write(&self, expected: Option<CommitSequence>) -> RecordWrite<'_> {
103        RecordWrite {
104            key: self.key(),
105            expected,
106            value: Some(self.row()),
107        }
108    }
109}
110
111/// 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.
112pub fn decode_record<'a>(
113    key: &[u8],
114    row: &'a [u8],
115    control: &StorageReadControl,
116) -> VersionResult<(NativeRecordIdentity, BudgetedVec<ValueRef<'a>>)> {
117    control.cancellation().check()?;
118    let identity = NativeRecordIdentity::decode(key)?;
119    let layout = identity.family().layout();
120    let values = decode_row(row, layout.columns.len(), control)?;
121    identity.validate_row(&values)?;
122    if !identity.matches_row_key(key, &values, control)? {
123        return Err(invalid("native row does not match its record key"));
124    }
125    Ok((identity, values))
126}
127
128fn invalid(message: &'static str) -> VersionError {
129    VersionError::InvalidEncoding(message)
130}