uqa-storage-sqlite 0.6.2

SQLite catalog, indexes, compressed storage, graph and key/value providers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Ordered native record identities separate physical families, object incarnations and primary-key components.

use rusqlite::types::ValueRef;
use uqa_core::memory::{BudgetedVec, MemoryError};
use uqa_storage::mvcc::{DatabaseId, VersionResult};
use uqa_storage::read_control::StorageReadControl;

use super::{invalid, NativeRecordFamily};

const PREFIX: &[u8] = b"\0uqa-native-record\x01";

/// The database incarnation owns global catalog/name records. Tuple and index records require the object's identity and storage generation, independently of its current SQL name.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NativeRecordOwner {
    Database(DatabaseId),
    Object {
        identity: [u8; 16],
        generation: [u8; 16],
    },
}

impl NativeRecordOwner {
    /// Whether this owner's rows are the documents of `namespace`. An object's identity and storage generation key its rows and name the namespace of its document identities alike, so what the namespace's watermark shows about an identity holds for the rows of this owner and of no other.
    pub(crate) fn stores(
        self,
        namespace: uqa_storage::document_store::identifiers::DocumentIdNamespace,
    ) -> bool {
        self == Self::Object {
            identity: namespace.object,
            generation: namespace.generation,
        }
    }
}

/// A provider-assigned physical family and stable owner. The durable family number must come from a fixed format registry, never schema enumeration order.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NativeRecordIdentity {
    family: NativeRecordFamily,
    owner: NativeRecordOwner,
}

impl NativeRecordIdentity {
    /// Named/entity families retain their complete key, vector families aggregate by owner and field, and other relevant families aggregate by owner. Family IDs separate these scopes from complete keys of other families.
    pub(crate) fn revision_scope(key: &[u8]) -> Option<&[u8]> {
        use NativeRecordFamily as Family;

        let Ok(identity) = Self::decode(key) else {
            // Preserve malformed raw records for the existing read/publication validator.
            return Some(key);
        };
        match identity.family {
            family if family.is_standalone_graph() => return None,
            Family::TableOwners
            | Family::OccurrenceGuards
            | Family::VectorGuards
            | Family::GraphLookups
            | Family::GraphPathPairs
            | Family::GraphPathIndexState => return None,
            Family::Metadata
            | Family::NamedGraphs
            | Family::GraphMembership
            | Family::GraphVertices
            | Family::GraphEdges => return Some(key),
            _ => {}
        }
        let owner_bytes = match identity.owner {
            NativeRecordOwner::Database(_) => 16,
            NativeRecordOwner::Object { .. } => 32,
        };
        let header_len = PREFIX.len() + 2 + 1 + owner_bytes;
        if matches!(
            identity.family,
            Family::Vectors
                | Family::HNSWIndexes
                | Family::HNSWNodes
                | Family::HNSWEdges
                | Family::IVFIndexes
                | Family::IVFCentroids
                | Family::IVFAssignments
        ) {
            // These layouts begin with an escaped TEXT field name. Include its
            // complete terminator so distinct fields never share a generation.
            let Some(rest) = key
                .get(header_len..)
                .and_then(|rest| rest.strip_prefix(&[2]))
            else {
                return Some(key);
            };
            let mut position = 0;
            while position < rest.len() {
                if rest[position] == 0 {
                    match rest.get(position + 1) {
                        Some(0) => return Some(&key[..header_len + 1 + position + 2]),
                        Some(255) => position += 1,
                        _ => return Some(key),
                    }
                }
                position += 1;
            }
            return Some(key);
        }
        Some(&key[..header_len])
    }

    /// Address every retained generation of one object without scanning other object payloads.
    pub(crate) fn object_prefix(
        family: NativeRecordFamily,
        identity: [u8; 16],
        control: &StorageReadControl,
    ) -> VersionResult<BudgetedVec<u8>> {
        if !family.layout().object_owned || identity == [0; 16] {
            return Err(invalid(
                "native object prefix requires an assigned object identity",
            ));
        }
        let mut key = Self::family_prefix(family, control)?;
        key.push(1)?;
        key.extend_from_slice(&identity)?;
        Ok(key)
    }

    pub(crate) fn family_prefix(
        family: NativeRecordFamily,
        control: &StorageReadControl,
    ) -> VersionResult<BudgetedVec<u8>> {
        control.cancellation().check()?;
        let mut key = BudgetedVec::new(control.memory());
        key.extend_from_slice(PREFIX)?;
        key.extend_from_slice(&family.id().to_be_bytes())?;
        Ok(key)
    }

    pub fn new(family: NativeRecordFamily, owner: NativeRecordOwner) -> VersionResult<Self> {
        if family.layout().object_owned != matches!(owner, NativeRecordOwner::Object { .. }) {
            return Err(invalid("native family does not match its owner kind"));
        }
        if let NativeRecordOwner::Object {
            identity,
            generation,
        } = owner
        {
            if identity == [0; 16] || generation == [0; 16] {
                return Err(invalid(
                    "native object identity and generation must be assigned",
                ));
            }
        }
        Ok(Self { family, owner })
    }

    pub fn family(self) -> NativeRecordFamily {
        self.family
    }

    pub fn owner(self) -> NativeRecordOwner {
        self.owner
    }

    /// Compare the canonical encoding directly, without allocating a second copy of a retained record key.
    pub(super) fn matches_row_key(
        self,
        key: &[u8],
        values: &[ValueRef<'_>],
        control: &StorageReadControl,
    ) -> VersionResult<bool> {
        let owner_bytes = match self.owner {
            NativeRecordOwner::Database(_) => 16,
            NativeRecordOwner::Object { .. } => 32,
        };
        // decode_record already validated this exact family/owner header.
        let mut input = &key[PREFIX.len() + 2 + 1 + owner_bytes..];
        for &column in self.family.layout().identity_columns {
            control.cancellation().check()?;
            let matches = match values[column] {
                ValueRef::Integer(value) => {
                    let ordered = u64::from_be_bytes(value.to_be_bytes()) ^ (1 << 63);
                    consume(&mut input, &[1]) && consume(&mut input, &ordered.to_be_bytes())
                }
                ValueRef::Text(bytes) => {
                    consume(&mut input, &[2]) && matches_escaped(&mut input, bytes, control)?
                }
                ValueRef::Blob(bytes) => {
                    consume(&mut input, &[3]) && matches_escaped(&mut input, bytes, control)?
                }
                ValueRef::Null | ValueRef::Real(_) => {
                    return Err(invalid("native primary key must be INTEGER, TEXT or BLOB"));
                }
            };
            if !matches {
                return Ok(false);
            }
        }
        control.cancellation().check()?;
        Ok(input.is_empty())
    }

    pub(super) fn validate_row(self, values: &[ValueRef<'_>]) -> VersionResult<()> {
        let layout = self.family.layout();
        layout.validate_values(values)?;
        if self.family.is_standalone_graph() {
            super::standalone_graph::validate_values(self.family, values)?;
        }
        let generation_column = match self.family {
            NativeRecordFamily::Tables => "storage_generation",
            NativeRecordFamily::Sequences | NativeRecordFamily::SequenceValues => {
                "definition_generation"
            }
            _ => return Ok(()),
        };
        let NativeRecordOwner::Object {
            identity,
            generation,
        } = self.owner
        else {
            return Err(invalid("native definition requires an object owner"));
        };
        for (name, expected) in [("object_id", identity), (generation_column, generation)] {
            let column = layout
                .columns
                .iter()
                .position(|column| *column == name)
                .ok_or_else(|| invalid("native definition layout lacks its identity column"))?;
            if values[column] != ValueRef::Blob(&expected) {
                return Err(invalid(
                    "native definition does not match its owner generation",
                ));
            }
        }
        Ok(())
    }

    pub(crate) fn decode(key: &[u8]) -> VersionResult<Self> {
        let bytes = key
            .strip_prefix(PREFIX)
            .ok_or_else(|| invalid("unknown native record key codec"))?;
        let family_bytes = bytes
            .get(..2)
            .ok_or_else(|| invalid("truncated native record family"))?;
        let family =
            NativeRecordFamily::from_id(u16::from_be_bytes([family_bytes[0], family_bytes[1]]))
                .ok_or_else(|| invalid("unknown native record family"))?;
        let array = |start: usize| -> VersionResult<[u8; 16]> {
            bytes
                .get(start..start + 16)
                .and_then(|slice| slice.try_into().ok())
                .ok_or_else(|| invalid("truncated native record owner"))
        };
        let owner = match bytes.get(2) {
            Some(0) => NativeRecordOwner::Database(DatabaseId::from_bytes(array(3)?)),
            Some(1) => NativeRecordOwner::Object {
                identity: array(3)?,
                generation: array(19)?,
            },
            _ => return Err(invalid("unknown native record owner kind")),
        };
        Self::new(family, owner)
    }

    /// Encode a complete primary key. Integer order is signed `SQLite` order; TEXT and BLOB order is binary byte order. NULL/REAL primary keys require a different equality contract and are rejected.
    pub fn encode_key(
        self,
        components: &[ValueRef<'_>],
        control: &StorageReadControl,
    ) -> VersionResult<BudgetedVec<u8>> {
        if components.len() != self.family.layout().identity_columns.len() {
            return Err(invalid("native primary key does not match its layout"));
        }
        self.encode_prefix(components, control)
    }

    /// Encode an owner/component prefix for a bounded scan over this family and generation.
    pub fn encode_prefix(
        self,
        components: &[ValueRef<'_>],
        control: &StorageReadControl,
    ) -> VersionResult<BudgetedVec<u8>> {
        control.cancellation().check()?;
        if components.len() > self.family.layout().identity_columns.len() {
            return Err(invalid("native primary key prefix exceeds its layout"));
        }
        let mut key = Self::family_prefix(self.family, control)?;
        match self.owner {
            NativeRecordOwner::Database(database) => {
                key.push(0)?;
                key.extend_from_slice(&database.as_bytes())?;
            }
            NativeRecordOwner::Object {
                identity,
                generation,
            } => {
                key.push(1)?;
                key.extend_from_slice(&identity)?;
                key.extend_from_slice(&generation)?;
            }
        }
        for (component, &column) in components.iter().zip(self.family.layout().identity_columns) {
            control.cancellation().check()?;
            let expected = self.family.layout().column_types[column];
            if !expected.accepts(*component) {
                return Err(invalid(
                    "native primary key storage class does not match its layout",
                ));
            }
            match component {
                ValueRef::Integer(value) => {
                    key.push(1)?;
                    let ordered = u64::from_be_bytes(value.to_be_bytes()) ^ (1 << 63);
                    key.extend_from_slice(&ordered.to_be_bytes())?;
                }
                ValueRef::Text(bytes) => {
                    std::str::from_utf8(bytes)
                        .map_err(|_| invalid("native text key is not UTF-8"))?;
                    key.push(2)?;
                    escaped_bytes(&mut key, bytes, control)?;
                }
                ValueRef::Blob(bytes) => {
                    key.push(3)?;
                    escaped_bytes(&mut key, bytes, control)?;
                }
                ValueRef::Null | ValueRef::Real(_) => {
                    return Err(invalid("native primary key must be INTEGER, TEXT or BLOB"));
                }
            }
        }
        Ok(key)
    }

    /// Encode a byte prefix within a single BLOB key, without its component terminator. Complete keys retain the existing escaped byte order.
    pub(crate) fn encode_blob_prefix(
        self,
        bytes: &[u8],
        control: &StorageReadControl,
    ) -> VersionResult<BudgetedVec<u8>> {
        self.encode_variable_prefix(bytes, super::NativeColumnType::Blob, 3, control)
    }

    /// Encode a UTF-8 prefix within one TEXT identity, without a component terminator.
    pub(crate) fn encode_text_prefix(
        self,
        bytes: &[u8],
        control: &StorageReadControl,
    ) -> VersionResult<BudgetedVec<u8>> {
        std::str::from_utf8(bytes).map_err(|_| invalid("native text prefix is not UTF-8"))?;
        self.encode_variable_prefix(bytes, super::NativeColumnType::Text, 2, control)
    }

    fn encode_variable_prefix(
        self,
        bytes: &[u8],
        kind: super::NativeColumnType,
        tag: u8,
        control: &StorageReadControl,
    ) -> VersionResult<BudgetedVec<u8>> {
        let layout = self.family.layout();
        if layout.identity_columns.len() != 1
            || layout.column_types[layout.identity_columns[0]] != kind
        {
            return Err(invalid(
                "native variable prefix requires one matching identity column",
            ));
        }
        let mut key = self.encode_prefix(&[], control)?;
        key.push(tag)?;
        escaped_prefix(&mut key, bytes, control)?;
        Ok(key)
    }

    /// Validate every ordered component, including keys for an absent tombstone.
    pub(super) fn decode_full(key: &[u8], control: &StorageReadControl) -> VersionResult<Self> {
        Self::visit_key_components(key, control, |_, _| Ok(()))
    }

    /// Decode ordered keys without loading row payloads. Borrowed component buffers are valid only during the callback.
    pub(crate) fn visit_key_components(
        key: &[u8],
        control: &StorageReadControl,
        visit: impl FnMut(usize, ValueRef<'_>) -> VersionResult<()>,
    ) -> VersionResult<Self> {
        let count = Self::decode(key)?.family.layout().identity_columns.len();
        Self::visit_prefix_components(key, count, control, visit)
    }

    /// Validate an exact number of complete primary-key components in an encoded prefix.
    pub(crate) fn visit_prefix_components(
        key: &[u8],
        count: usize,
        control: &StorageReadControl,
        mut visit: impl FnMut(usize, ValueRef<'_>) -> VersionResult<()>,
    ) -> VersionResult<Self> {
        let identity = Self::decode(key)?;
        if count > identity.family.layout().identity_columns.len() {
            return Err(invalid("native prefix has too many components"));
        }
        let header = identity.encode_prefix(&[], control)?;
        let mut input = &key[header.len()..];
        for (component, &column) in identity
            .family
            .layout()
            .identity_columns
            .iter()
            .take(count)
            .enumerate()
        {
            control.cancellation().check()?;
            let (&tag, rest) = input
                .split_first()
                .ok_or_else(|| invalid("truncated native key component"))?;
            input = rest;
            let mut bytes = BudgetedVec::new(control.memory());
            let value = match tag {
                1 => {
                    let (integer, rest) = input
                        .split_at_checked(8)
                        .ok_or_else(|| invalid("truncated native integer key"))?;
                    input = rest;
                    let ordered = u64::from_be_bytes(integer.try_into().expect("eight bytes"));
                    ValueRef::Integer(i64::from_be_bytes((ordered ^ (1 << 63)).to_be_bytes()))
                }
                2 | 3 => {
                    loop {
                        control.cancellation().check()?;
                        let (&byte, rest) = input
                            .split_first()
                            .ok_or_else(|| invalid("unterminated native binary key"))?;
                        input = rest;
                        if byte == 0 {
                            let (&escape, rest) = input
                                .split_first()
                                .ok_or_else(|| invalid("truncated native key escape"))?;
                            input = rest;
                            match escape {
                                0 => break,
                                255 => {}
                                _ => return Err(invalid("invalid native key escape")),
                            }
                        }
                        bytes.push(byte)?;
                    }
                    if tag == 2 {
                        std::str::from_utf8(&bytes)
                            .map_err(|_| invalid("native text key is not UTF-8"))?;
                        ValueRef::Text(&bytes)
                    } else {
                        ValueRef::Blob(&bytes)
                    }
                }
                _ => return Err(invalid("unknown native key component")),
            };
            if !identity.family.layout().column_types[column].accepts(value) {
                return Err(invalid("native key component does not match its layout"));
            }
            visit(component, value)?;
        }
        if !input.is_empty() {
            return Err(invalid("native key has trailing components"));
        }
        Ok(identity)
    }
}

fn consume(input: &mut &[u8], expected: &[u8]) -> bool {
    if let Some(rest) = input.strip_prefix(expected) {
        *input = rest;
        true
    } else {
        false
    }
}

fn matches_escaped(
    input: &mut &[u8],
    bytes: &[u8],
    control: &StorageReadControl,
) -> VersionResult<bool> {
    for (index, &byte) in bytes.iter().enumerate() {
        if index % 1024 == 0 {
            control.cancellation().check()?;
        }
        if !consume(input, &[byte]) || (byte == 0 && !consume(input, &[255])) {
            return Ok(false);
        }
    }
    Ok(consume(input, &[0, 0]))
}

fn escaped_bytes(
    output: &mut BudgetedVec<u8>,
    bytes: &[u8],
    control: &StorageReadControl,
) -> VersionResult<()> {
    escaped_prefix(output, bytes, control)?;
    output.extend_from_slice(&[0, 0])?;
    Ok(())
}

fn escaped_prefix(
    output: &mut BudgetedVec<u8>,
    bytes: &[u8],
    control: &StorageReadControl,
) -> VersionResult<()> {
    output.reserve(
        bytes
            .len()
            .checked_add(2)
            .ok_or(MemoryError::SizeOverflow)?,
    )?;
    for chunk in bytes.chunks(1024) {
        control.cancellation().check()?;
        for &byte in chunk {
            output.push(byte)?;
            if byte == 0 {
                output.push(255)?;
            }
        }
    }
    Ok(())
}