uqa-storage-sqlite 0.4.9

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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Bounded canonical reads borrow native rows and preserve their original visibility and controls.

mod changes;
mod populations;
mod pruning;
mod publication;
mod selection;

use super::invalid;
use crate::mvcc::native::{
    decode_record, NativeRecordFamily as Family, NativeRecordIdentity as Identity,
    NativeRecordOwner, NativeSnapshot,
};
use crate::vector_index::{encode_doc_id, native::records::Address, SQLiteVectorIndex};
use rusqlite::types::ValueRef;
use std::sync::Arc;
use uqa_core::{
    memory::{BudgetedVec, MemoryReservation},
    DocId,
};
use uqa_storage::{
    diskann_index::{
        format::{DiskANNCanonicalOrigin, DiskANNVectorVersion, CANONICAL_ORIGIN_BYTES},
        DiskANNCanonicalRead, DiskANNCanonicalVectorVisitor,
    },
    mvcc::VersionError,
    read_control::StorageReadControl,
    StorageBackendResult,
};

/// A fixed native canonical source. Each visitor borrows one vector; it must not reenter this source. Failure invalidates any partial output.
pub struct RetainedSQLiteDiskANNCanonical {
    snapshot: Arc<NativeSnapshot>,
    owner: Option<NativeRecordOwner>,
    table: BudgetedVec<u8>,
    field: BudgetedVec<u8>,
    dimensions: u32,
    control: StorageReadControl,
    binding: Option<crate::catalog::DiskANNCatalogBinding>,
    _memory: MemoryReservation,
}

impl RetainedSQLiteDiskANNCanonical {
    /// Resolve the index incarnation from this source's fixed native catalog view through the SQL owner.
    pub fn index_scope(
        &self,
        resolver: &dyn uqa_storage::diskann_index::catalog::DiskANNIndexResolver,
        control: &StorageReadControl,
    ) -> StorageBackendResult<uqa_storage::diskann_index::catalog::DiskANNIndexScope> {
        self.check(control)?;
        let scope = self
            .binding
            .as_ref()
            .ok_or_else(|| invalid("native canonical source has no index binding"))?
            .scope(&self.snapshot, resolver, &self.control, control)?;
        self.check(control)?;
        Ok(scope)
    }

    pub(super) fn capture(
        index: &SQLiteVectorIndex,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Self> {
        Self::capture_selected(index, None, control)
    }

    pub(super) fn capture_for_index(
        index: &SQLiteVectorIndex,
        catalog_index: &uqa_storage::RelationIdentity,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Self> {
        Self::capture_selected(index, Some(catalog_index), control)
    }

    fn capture_selected(
        index: &SQLiteVectorIndex,
        catalog_index: Option<&uqa_storage::RelationIdentity>,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Self> {
        control.check()?;
        let memory = control
            .memory()
            .reserve(std::mem::size_of::<Self>() + std::mem::size_of::<NativeSnapshot>())?;
        let snapshot = index
            .conn
            .native_snapshot()?
            .ok_or_else(|| invalid("native canonical source requires a bound session"))?;
        let binding = catalog_index
            .map(|catalog_index| {
                crate::catalog::DiskANNCatalogBinding::capture(
                    &snapshot,
                    &index.table,
                    &index.field,
                    index.dimensions,
                    catalog_index,
                    control,
                )
            })
            .transpose()?;
        let owner = if let Some(binding) = &binding {
            Some(binding.owner)
        } else {
            snapshot.table_owner_controlled(&index.table, control)?
        };
        let mut table = BudgetedVec::new(control.memory());
        table.extend_from_slice(index.table.as_bytes())?;
        let mut field = BudgetedVec::new(control.memory());
        field.extend_from_slice(index.field.as_bytes())?;
        control.check()?;
        Ok(Self {
            snapshot,
            owner,
            table,
            field,
            dimensions: index.dimensions,
            control: control.clone(),
            binding,
            _memory: memory,
        })
    }

    pub fn index_parameters(&self) -> Option<uqa_storage::vector_index::DiskANNIndexParams> {
        self.binding.as_ref().map(|binding| binding.parameters)
    }

    /// Recheck the captured native definitions and stage committed requirements on one publication command's raw record reader/batch. This does not publish a generation or complete the caller's transaction.
    pub fn require_current_index(
        &self,
        read: &dyn uqa_storage::key_value::KeyValueRead,
        batch: &mut dyn uqa_storage::KeyValueBatch,
        control: &StorageReadControl,
    ) -> StorageBackendResult<()> {
        self.check(control)?;
        self.binding
            .as_ref()
            .ok_or_else(|| invalid("native canonical source has no index binding"))?
            .require_current(read, batch, control)?;
        self.check(control)
    }

    /// Return an origin only after verifying its complete contiguous native canonical ordinal set. An origin with zero ordinals denotes an explicit empty replacement.
    pub fn origin(
        &self,
        document: DocId,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<DiskANNVectorVersion>> {
        self.record(document, control)
            .map(|record| record.map(DiskANNCanonicalOrigin::version))
    }

    /// Stream all visible ordinals with their original coordinate bits, independently of navigation, scoring or document candidate selection.
    pub fn visit_document(
        &self,
        document: DocId,
        control: &StorageReadControl,
        visit: &mut DiskANNCanonicalVectorVisitor<'_>,
    ) -> StorageBackendResult<Option<DiskANNVectorVersion>> {
        let Some(record) = self.record(document, control)? else {
            return Ok(None);
        };
        if record.count() == 0 {
            return Ok(Some(record.version()));
        }
        let document = encode_doc_id(document)?;
        let dimensions = usize::try_from(self.dimensions)
            .map_err(|_| invalid("canonical dimensions exceed platform size"))?;
        let bytes = dimensions
            .checked_mul(4)
            .ok_or(uqa_core::memory::MemoryError::SizeOverflow)?;
        let mut vector = BudgetedVec::new(control.memory());
        vector.reserve(dimensions)?;
        for ordinal in 0..record.count() {
            self.read_payload(document, Some(ordinal), bytes, control, &mut |value| {
                let value = value.ok_or_else(|| invalid("missing native canonical ordinal"))?;
                if value.len() != bytes {
                    return Err(invalid("native canonical vector width mismatch"));
                }
                vector.clear();
                for (coordinate, chunk) in value.chunks_exact(4).enumerate() {
                    if coordinate.is_multiple_of(1024) {
                        self.check(control)?;
                    }
                    vector.push(f32::from_le_bytes(chunk.try_into().expect("fixed width")))?;
                }
                uqa_storage::vector_index::validate_vector_values_controlled(
                    self.dimensions,
                    &vector,
                    Some(control),
                )?;
                visit(ordinal as u32, record.version(), &vector)
            })?;
        }
        self.check(control)?;
        Ok(Some(record.version()))
    }

    fn record(
        &self,
        document: DocId,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<DiskANNCanonicalOrigin>> {
        self.record_on(&self.snapshot, document, control)
    }

    fn record_on(
        &self,
        snapshot: &NativeSnapshot,
        document: DocId,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<DiskANNCanonicalOrigin>> {
        self.check(control)?;
        let document = encode_doc_id(document)?;
        let Some(owner) = self.owner else {
            return Ok(None);
        };
        let mut record = None;
        self.read_payload_on(
            snapshot,
            document,
            None,
            CANONICAL_ORIGIN_BYTES,
            control,
            &mut |bytes| {
                record = bytes
                    .map(|bytes| DiskANNCanonicalOrigin::decode(bytes, self.dimensions))
                    .transpose()?;
                Ok(())
            },
        )?;
        let identity =
            Identity::new(Family::Vectors, owner).map_err(VersionError::into_storage_error)?;
        let prefix = identity
            .encode_prefix(
                &[ValueRef::Text(&self.field), ValueRef::Integer(document)],
                control,
            )
            .map_err(VersionError::into_storage_error)?;
        let expected = record.map_or(0, DiskANNCanonicalOrigin::count);
        let mut count = 0;
        snapshot
            .view
            .visit_keys(&prefix, None, usize::MAX, control, &mut |key, metadata| {
                self.check(control).map_err(VersionError::Storage)?;
                if metadata.live {
                    let address = Address::decode(key, control)?;
                    if count >= expected
                        || address.identity != identity
                        || *address.field != *self.field
                        || address.numbers[..2] != [document, count as i64]
                    {
                        return Err(VersionError::InvalidEncoding(
                            "native canonical origin or ordinal coverage mismatch",
                        ));
                    }
                    count += 1;
                }
                Ok(true)
            })
            .map_err(VersionError::into_storage_error)?;
        if count != expected {
            return Err(invalid("native canonical replacement count mismatch"));
        }
        self.check(control)?;
        Ok(record)
    }

    fn read_payload(
        &self,
        document: i64,
        ordinal: Option<u64>,
        max_bytes: usize,
        control: &StorageReadControl,
        visit: &mut dyn FnMut(Option<&[u8]>) -> StorageBackendResult<()>,
    ) -> StorageBackendResult<()> {
        self.read_payload_on(&self.snapshot, document, ordinal, max_bytes, control, visit)
    }

    fn read_payload_on(
        &self,
        snapshot: &NativeSnapshot,
        document: i64,
        ordinal: Option<u64>,
        max_bytes: usize,
        control: &StorageReadControl,
        visit: &mut dyn FnMut(Option<&[u8]>) -> StorageBackendResult<()>,
    ) -> StorageBackendResult<()> {
        self.check(control)?;
        let Some(owner) = self.owner else {
            return visit(None);
        };
        let family = if ordinal.is_some() {
            Family::Vectors
        } else {
            Family::VectorOrigins
        };
        let components = [
            ValueRef::Text(&self.field),
            ValueRef::Integer(document),
            ValueRef::Integer(ordinal.unwrap_or(0) as i64),
        ];
        let identity = Identity::new(family, owner).map_err(VersionError::into_storage_error)?;
        let key = identity
            .encode_key(
                &components[..if ordinal.is_some() { 3 } else { 2 }],
                control,
            )
            .map_err(VersionError::into_storage_error)?;
        let limit = crate::mvcc::native::vector_row_limit(
            self.table.len(),
            self.field.len(),
            max_bytes,
            ordinal.is_some(),
        )
        .map_err(VersionError::into_storage_error)?;
        snapshot
            .view
            .visit_value_bounded(&key, limit, control, &mut |record| {
                self.check(control).map_err(VersionError::Storage)?;
                let Some(bytes) = record.and_then(|record| record.value) else {
                    return visit(None).map_err(VersionError::Storage);
                };
                let (actual, row) = decode_record(&key, bytes, control)?;
                if actual != identity || row[0] != ValueRef::Text(&self.table) {
                    return Err(VersionError::InvalidEncoding(
                        "native canonical row scope mismatch",
                    ));
                }
                let payload = row[if ordinal.is_some() { 4 } else { 3 }]
                    .as_blob()
                    .map_err(|_| {
                        VersionError::InvalidEncoding("invalid native canonical payload")
                    })?;
                control.check_value_size(payload.len(), max_bytes)?;
                visit(Some(payload)).map_err(VersionError::Storage)
            })
            .map_err(VersionError::into_storage_error)?;
        self.check(control)
    }

    fn check(&self, control: &StorageReadControl) -> StorageBackendResult<()> {
        self.snapshot.control.check()?;
        self.control.check()?;
        control.check()
    }

    fn next_key_document(
        &self,
        family: Family,
        after: Option<i64>,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<DocId>> {
        let Some(owner) = self.owner else {
            return Ok(None);
        };
        let identity = Identity::new(family, owner).map_err(VersionError::into_storage_error)?;
        let prefix = identity
            .encode_prefix(&[ValueRef::Text(&self.field)], control)
            .map_err(VersionError::into_storage_error)?;
        let after_key = after
            .map(|document| {
                let components = [
                    ValueRef::Text(&self.field),
                    ValueRef::Integer(document),
                    ValueRef::Integer(i64::MAX),
                ];
                identity.encode_key(
                    &components[..if family == Family::Vectors { 3 } else { 2 }],
                    control,
                )
            })
            .transpose()
            .map_err(VersionError::into_storage_error)?;
        let mut selected = None;
        self.snapshot
            .view
            .visit_keys(
                &prefix,
                after_key.as_deref(),
                usize::MAX,
                control,
                &mut |key, metadata| {
                    self.check(control).map_err(VersionError::Storage)?;
                    if !metadata.live {
                        return Ok(true);
                    }
                    let address = Address::decode(key, control)?;
                    let document = address.numbers[0];
                    if address.identity != identity
                        || *address.field != *self.field
                        || document < 0
                        || after.is_some_and(|after| document <= after)
                        || (family == Family::Vectors && u32::try_from(address.numbers[1]).is_err())
                    {
                        return Err(VersionError::InvalidEncoding(
                            "invalid native canonical corpus identity",
                        ));
                    }
                    selected = Some(document as DocId);
                    Ok(false)
                },
            )
            .map_err(VersionError::into_storage_error)?;
        Ok(selected)
    }
}

impl DiskANNCanonicalRead for RetainedSQLiteDiskANNCanonical {
    fn population_counts(
        &self,
        generation: uqa_storage::diskann_index::format::DiskANNGeneration,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<uqa_storage::diskann_index::DiskANNCanonicalCounts>> {
        self.read_population(generation, control)
    }

    fn corpus_fingerprint(
        &self,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<[u8; 32]>> {
        self.check(control)?;
        self.owner
            .map(|owner| {
                self.snapshot
                    .vector_field_fingerprint(owner, &self.field, control)
            })
            .transpose()
    }
    fn read_vector(
        &self,
        document: DocId,
        ordinal: u32,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<BudgetedVec<f32>>> {
        self.check(control)?;
        let bytes = usize::try_from(self.dimensions)
            .map_err(|_| uqa_core::memory::MemoryError::SizeOverflow)?
            .checked_mul(4)
            .ok_or(uqa_core::memory::MemoryError::SizeOverflow)?;
        let mut result = None;
        self.read_payload(
            encode_doc_id(document)?,
            Some(u64::from(ordinal)),
            bytes,
            control,
            &mut |value| {
                result = value
                    .map(|value| {
                        uqa_storage::vector_index::decode_vector_bytes(
                            value,
                            self.dimensions,
                            control,
                        )
                    })
                    .transpose()?;
                Ok(())
            },
        )?;
        self.check(control)?;
        Ok(result)
    }
    fn check_control(&self, control: &StorageReadControl) -> StorageBackendResult<()> {
        self.check(control)
    }

    fn dimensions(&self) -> u32 {
        self.dimensions
    }

    fn next_document_after(
        &self,
        after: Option<DocId>,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<DocId>> {
        self.check(control)?;
        if after.is_some_and(|document| document >= i64::MAX as DocId) {
            return Ok(None);
        }
        let after = after.map(|document| document as i64);
        let origin = self.next_key_document(Family::VectorOrigins, after, control)?;
        let vector = self.next_key_document(Family::Vectors, after, control)?;
        self.check(control)?;
        Ok(origin.into_iter().chain(vector).min())
    }

    fn origin(
        &self,
        document: DocId,
        control: &StorageReadControl,
    ) -> StorageBackendResult<Option<DiskANNVectorVersion>> {
        self.origin(document, control)
    }

    fn visit_document(
        &self,
        document: DocId,
        control: &StorageReadControl,
        visit: &mut DiskANNCanonicalVectorVisitor<'_>,
    ) -> StorageBackendResult<Option<DiskANNVectorVersion>> {
        self.visit_document(document, control, visit)
    }
}