uqa-storage 0.4.5

Document store, inverted index, IVF/HNSW vectors, B-tree, R*Tree, catalog
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

use super::{
    identity::{row, Resolver},
    setup, FIELD, TABLE,
};
use crate::{
    diskann_index::{
        build::{
            DiskANNGenerationOptions, DiskANNMergeOptions, DiskANNPartitionOptions,
            DiskANNTemporaryBudget,
        },
        format::{DiskANNGeneration, PAGE_BYTES},
        pages::{DiskANNPageSource, DiskANNReadLimits},
        DiskANNIndexOptions, DiskANNPersistentOwner, DiskANNQueryMetadata, PQTrainingOptions,
        PersistentDiskANNIndex,
    },
    key_value::{
        conformance::{expect, expect_eq},
        KeyValueDiskANNCanonical, KeyValueVectorIndex,
    },
    read_control::StorageReadControl,
    vector_index::DiskANNIndexParams,
    CatalogFacade, KeyValueCatalog, KeyValueStore, StorageBackendResult, VectorIndex,
};
use std::sync::Arc;

mod statistics;
pub use statistics::verify_diskann_vector_statistics;

mod populations;
pub use populations::{
    seed_diskann_population_upgrade, verify_diskann_population_late_publication,
    verify_diskann_population_lifetimes, verify_diskann_population_upgrade,
    verify_diskann_population_upgrade_reopen, verify_diskann_population_writers,
};
mod retirement;
pub use retirement::{verify_diskann_runtime_retirement, verify_diskann_runtime_retirement_reopen};
mod reclamation;
pub use reclamation::{
    verify_diskann_runtime_reclaimed_reopen, verify_diskann_runtime_reclamation,
};

/// Small deterministic provider acceptance settings, never runtime defaults.
pub fn diskann_runtime_fixture_options(
    dimensions: u32,
) -> StorageBackendResult<DiskANNIndexOptions> {
    let training = PQTrainingOptions {
        max_samples: 8,
        max_centroids: 2,
        max_iterations: 2,
        seed: 42,
    };
    Ok(DiskANNIndexOptions {
        parameters: DiskANNIndexParams::for_dimensions(dimensions)?,
        read: DiskANNReadLimits {
            resident_bytes: 65_536,
            cache_bytes: PAGE_BYTES,
            max_in_flight_page_bytes: 2 * PAGE_BYTES,
            max_record_bytes: 8192,
        },
        partitions: DiskANNPartitionOptions {
            max_partition_points: 8,
            coarse_training: PQTrainingOptions {
                max_centroids: 3,
                ..training
            },
            max_depth: 0,
        },
        merge: DiskANNMergeOptions {
            sort_buffer_records: 8,
        },
        generation: DiskANNGenerationOptions {
            training,
            code_batch_nodes: 4,
            side_batch_entries: 4,
            max_record_bytes: 8192,
        },
    })
}

fn canonical(store: &Arc<dyn KeyValueStore>) -> StorageBackendResult<KeyValueDiskANNCanonical> {
    KeyValueDiskANNCanonical::new(store.clone(), TABLE, FIELD, 2)
}

pub(super) fn runtime(
    store: &Arc<dyn KeyValueStore>,
    temporary: &DiskANNTemporaryBudget,
    control: &StorageReadControl,
) -> StorageBackendResult<impl VectorIndex> {
    let options = diskann_runtime_fixture_options(2)?;
    PersistentDiskANNIndex::new(
        canonical(store)?.bind(
            row([91; 16])?.relation,
            Arc::new(Resolver),
            options.read,
            control,
        )?,
        options,
        temporary,
    )
}

fn generation(
    store: &Arc<dyn KeyValueStore>,
    control: &StorageReadControl,
) -> StorageBackendResult<DiskANNGeneration> {
    canonical(store)?
        .retain_for_index(&row([91; 16])?.relation, control)?
        .selected_source(&Resolver, control)?
        .map(|source| source.generation())
        .ok_or_else(|| {
            crate::StorageBackendError::Other(
                "runtime fixture has no selected DiskANN generation".into(),
            )
        })
}

fn scores(index: &dyn VectorIndex, expected: &[(u64, f64)]) -> StorageBackendResult<()> {
    let postings = index.search_knn(&[1.0, 0.0], 10)?;
    expect_eq(
        &postings
            .iter()
            .map(|entry| (entry.doc_id, entry.payload.score.to_bits()))
            .collect::<Vec<_>>(),
        &expected
            .iter()
            .map(|&(id, score)| (id, score.to_bits()))
            .collect::<Vec<_>>(),
        "runtime literal complete tensor scores",
    )
}

fn population(
    index: &dyn VectorIndex,
    current: u64,
    changed: u64,
    control: &StorageReadControl,
) -> StorageBackendResult<()> {
    expect_eq(
        &index
            .diskann_query_metadata(control)?
            .expect("DiskANN metadata")
            .canonical_counts,
        &Some(crate::diskann_index::DiskANNCanonicalCounts::new(
            current, changed,
        )?),
        "runtime metadata preserves complete canonical populations",
    )
}

fn capture_metadata(
    store: &Arc<dyn KeyValueStore>,
    index: &dyn VectorIndex,
    held: &dyn VectorIndex,
    temporary: &DiskANNTemporaryBudget,
    control: &StorageReadControl,
) -> StorageBackendResult<DiskANNQueryMetadata> {
    let expected = index
        .diskann_query_metadata(control)?
        .expect("DiskANN metadata");
    require_metadata(held, &expected, control)?;
    expect_eq(
        &expected.manifest.input().coverage.vector_count(),
        &4,
        "metadata complete base tensor count",
    )?;
    let mut options = diskann_runtime_fixture_options(2)?;
    options.read.resident_bytes = 0;
    let handle = canonical(store)?.bind(
        row([91; 16])?.relation,
        Arc::new(Resolver),
        options.read,
        control,
    )?;
    let before = control.memory().used();
    let metadata = handle.query_metadata()?;
    expect_eq(
        &metadata.manifest,
        &expected.manifest,
        "bounded metadata selects the actual manifest",
    )?;
    expect_eq(
        &metadata.corpus_fingerprint,
        &expected.corpus_fingerprint,
        "bounded metadata selects the actual private field",
    )?;
    expect_eq(
        &metadata.read_limits,
        &options.read,
        "metadata retains requested read limits",
    )?;
    expect_eq(
        &control.memory().used(),
        &before,
        "metadata releases temporary provider readers",
    )?;
    expect(
        PersistentDiskANNIndex::new(handle, options, temporary).is_err(),
        "executable restore still requires resident PQ admission",
    )?;
    Ok(expected)
}

fn require_metadata(
    index: &dyn VectorIndex,
    expected: &DiskANNQueryMetadata,
    control: &StorageReadControl,
) -> StorageBackendResult<()> {
    expect_eq(
        &index.diskann_query_metadata(control)?.as_ref(),
        &Some(expected),
        "selected metadata matches the original captured view",
    )
}

fn changed_metadata(
    index: &dyn VectorIndex,
    held: &dyn VectorIndex,
    expected: &DiskANNQueryMetadata,
    control: &StorageReadControl,
) -> StorageBackendResult<()> {
    let changed = index
        .diskann_query_metadata(control)?
        .expect("changed metadata");
    expect_eq(
        &changed.manifest,
        &expected.manifest,
        "row changes preserve physical metadata",
    )?;
    expect(
        changed.corpus_fingerprint != expected.corpus_fingerprint,
        "private changes update live canonical identity",
    )?;
    require_metadata(held, expected, control)
}

fn create(
    store: &Arc<dyn KeyValueStore>,
    temporary: &DiskANNTemporaryBudget,
    control: &StorageReadControl,
) -> StorageBackendResult<()> {
    setup(store)?;
    let catalog = KeyValueCatalog::new(store.clone());
    let options = diskann_runtime_fixture_options(2)?;
    let definition = row([91; 16])?;
    catalog.save_catalog_index_row(&definition)?;
    let mut raw = KeyValueVectorIndex::new(store.clone(), TABLE, FIELD, 2);
    raw.add(1, vec![1.0, -0.0])?;
    raw.add_many(2, vec![vec![0.0, 1.0], vec![1.0, 0.0]])?;
    raw.add(3, vec![-1.0, 0.0])?;
    expect(
        runtime(store, temporary, control).is_err(),
        "restore never creates a missing head",
    )?;
    expect(
        canonical(store)?
            .create_index(&definition.relation, &Resolver, options, temporary, control)
            .is_err(),
        "creation requires the caller transaction",
    )?;
    store.begin_transaction()?;
    store.put(b"runtime-outer-write", b"kept")?;
    catalog.save_catalog_index_row(&definition)?;
    let mut bad = options;
    bad.generation.max_record_bytes = 1;
    expect(
        canonical(store)?
            .create_index(&definition.relation, &Resolver, bad, temporary, control)
            .is_err(),
        "failed construction rolls back adoption",
    )?;
    let mut unreadable = options;
    unreadable.read.resident_bytes = 1;
    expect(
        canonical(store)?
            .create_index(
                &definition.relation,
                &Resolver,
                unreadable,
                temporary,
                control,
            )
            .is_err(),
        "creation rejects a sealed generation that cannot be admitted for queries",
    )?;
    expect_eq(
        &raw.count()?,
        &4,
        "failed creation preserves original raw ordinals",
    )?;
    expect(
        canonical(store)?
            .retain(control)?
            .origin(1, control)
            .is_err(),
        "failed creation leaves the original unstamped raw field",
    )?;
    expect_eq(
        &store.get(b"runtime-outer-write")?,
        &Some(b"kept".to_vec()),
        "structural undo keeps earlier caller effects",
    )?;
    canonical(store)?.create_index(&definition.relation, &Resolver, options, temporary, control)?;
    expect(
        store.in_transaction(),
        "construction never commits the outer transaction",
    )?;
    Ok(())
}

/// Exercise real first construction, bounded legacy adoption, rebuild and empty-generation publication through the public `VectorIndex` interface, including private undo and failure atomicity.
pub fn verify_diskann_runtime_lifecycle(
    store: &Arc<dyn KeyValueStore>,
) -> StorageBackendResult<DiskANNGeneration> {
    let control = StorageReadControl::with_limit(1 << 21);
    let temporary = DiskANNTemporaryBudget::new(1 << 20);
    let options = diskann_runtime_fixture_options(2)?;
    let definition = row([91; 16])?;
    create(store, &temporary, &control)?;
    let mut index = runtime(store, &temporary, &control)?;
    scores(&index, &[(1, 1.0), (2, 1.0), (3, -1.0)])?;
    expect_eq(&index.count()?, &4, "adoption preserves complete tensors")?;
    let first = generation(store, &control)?;
    population(&index, 4, 0, &control)?;
    verify_diskann_vector_statistics(&index, first, (3, 4), (0, 0))?;
    let held = index.snapshot()?;
    let metadata = capture_metadata(store, &index, &*held, &temporary, &control)?;
    store.savepoint("runtime-user")?;
    index.add(1, vec![-1.0, 0.0])?;
    index.delete(2)?;
    index.add(4, vec![0.0, 1.0])?;
    changed_metadata(&index, &*held, &metadata, &control)?;
    population(&index, 3, 2, &control)?;
    verify_diskann_vector_statistics(&index, first, (1, 1), (2, 2))?;
    verify_diskann_vector_statistics(&*held, first, (3, 4), (0, 0))?;
    population(&*held, 4, 0, &control)?;
    expect_eq(
        &generation(store, &control)?,
        &first,
        "ordinary writes preserve the base",
    )?;
    index.initialize()?;
    expect(
        generation(store, &control)? != first,
        "initialize publishes a replacement base",
    )?;
    let rebuilt = index.snapshot()?;
    population(&index, 3, 0, &control)?;
    scores(&*rebuilt, &[(1, -1.0), (3, -1.0), (4, 0.0)])?;
    index.clear()?;
    let empty = index.snapshot()?;
    population(&index, 0, 0, &control)?;
    verify_diskann_vector_statistics(&*empty, generation(store, &control)?, (0, 0), (0, 0))?;
    expect_eq(
        &empty.count()?,
        &0,
        "clear publishes empty canonical membership",
    )?;
    let empty_source = canonical(store)?
        .retain_for_index(&definition.relation, &control)?
        .into_vector_index(&Resolver, options.read, &control)?
        .ok_or_else(|| crate::StorageBackendError::Other("missing empty runtime head".into()))?;
    expect_eq(
        &empty_source.manifest().input().nodes,
        &0,
        "clear publishes an actually empty graph",
    )?;
    store.rollback_to_savepoint("runtime-user")?;
    require_metadata(&index, &metadata, &control)?;
    population(&*rebuilt, 3, 0, &control)?;
    population(&*empty, 0, 0, &control)?;
    scores(&index, &[(1, 1.0), (2, 1.0), (3, -1.0)])?;
    scores(&*rebuilt, &[(1, -1.0), (3, -1.0), (4, 0.0)])?;
    expect_eq(&empty.count()?, &0, "undone empty snapshot remains valid")?;
    expect_eq(
        &generation(store, &control)?,
        &first,
        "savepoint restores the original head",
    )?;
    store.commit_transaction()?;
    expect(
        index.clear().is_err(),
        "clear requires a caller transaction",
    )?;
    expect(
        index.initialize().is_err(),
        "rebuild requires a caller transaction",
    )?;
    store.begin_transaction()?;
    reject_failed_clear(store, &temporary, &control)?;
    scores(&index, &[(1, 1.0), (2, 1.0), (3, -1.0)])?;
    expect_eq(
        &generation(store, &control)?,
        &first,
        "failed clear preserves the original head",
    )?;
    index.clear()?;
    index.add(5, vec![1.0, 0.0])?;
    index.initialize()?;
    store.commit_transaction()?;
    scores(&index, &[(5, 1.0)])?;
    population(&index, 1, 0, &control)?;
    scores(&*held, &[(1, 1.0), (2, 1.0), (3, -1.0)])?;
    let final_generation = generation(store, &control)?;
    verify_diskann_vector_statistics(&index, final_generation, (1, 1), (0, 0))?;
    verify_diskann_vector_statistics(&*held, first, (3, 4), (0, 0))?;
    expect_eq(
        &temporary.used(),
        &0,
        "finished builds release encrypted temporary input",
    )?;
    Ok(final_generation)
}

fn reject_failed_clear(
    store: &Arc<dyn KeyValueStore>,
    temporary: &DiskANNTemporaryBudget,
    control: &StorageReadControl,
) -> StorageBackendResult<()> {
    let mut bad = diskann_runtime_fixture_options(2)?;
    bad.generation.max_record_bytes = 1;
    let mut constrained = PersistentDiskANNIndex::new(
        canonical(store)?.bind(
            row([91; 16])?.relation,
            Arc::new(Resolver),
            bad.read,
            control,
        )?,
        bad,
        temporary,
    )?;
    expect(
        constrained.clear().is_err(),
        "failed empty build restores its own clear",
    )
}

/// Reopen the actual provider after all original owners have been dropped.
pub fn verify_diskann_runtime_reopen(
    store: &Arc<dyn KeyValueStore>,
    expected: DiskANNGeneration,
) -> StorageBackendResult<()> {
    let control = StorageReadControl::with_limit(1 << 20);
    let temporary = DiskANNTemporaryBudget::new(1 << 20);
    let mut index = runtime(store, &temporary, &control)?;
    expect_eq(
        &generation(store, &control)?,
        &expected,
        "cold restore keeps its actual generation",
    )?;
    scores(&index, &[(5, 1.0)])?;
    index.add(6, vec![0.0, 1.0])?;
    population(&index, 2, 1, &control)?;
    scores(&index, &[(5, 1.0), (6, 0.0)])?;
    expect_eq(
        &generation(store, &control)?,
        &expected,
        "cold writes do not rebuild",
    )
}

/// A constructor adopts one complete raw namespace. A racing legacy insertion must conflict even when its key did not exist in the captured source.
pub fn verify_diskann_runtime_adoption_conflicts(
    store: &Arc<dyn KeyValueStore>,
) -> StorageBackendResult<()> {
    setup(store)?;
    let catalog = KeyValueCatalog::new(store.clone());
    let definition = row([91; 16])?;
    catalog.save_catalog_index_row(&definition)?;
    let peer = store.open_session()?;
    let options = diskann_runtime_fixture_options(2)?;
    let temporary = DiskANNTemporaryBudget::new(1 << 20);
    let control = StorageReadControl::with_limit(1 << 21);
    verify_adoption_gaps(store, &control)?;
    let mut raw = KeyValueVectorIndex::new(store.clone(), TABLE, FIELD, 2);
    let mut late = KeyValueVectorIndex::new(peer.clone(), TABLE, FIELD, 2);
    for creator_first in [false, true] {
        raw.clear()?;
        raw.add(1, vec![1.0, 0.0])?;
        peer.begin_transaction()?;
        late.add(99, vec![0.0, 1.0])?;
        store.begin_transaction()?;
        canonical(store)?.create_index(
            &definition.relation,
            &Resolver,
            options,
            &temporary,
            &control,
        )?;
        if creator_first {
            store.commit_transaction()?;
            expect(
                peer.commit_transaction().is_err(),
                "older unstamped insertion conflicts with completed adoption",
            )?;
            peer.rollback_transaction()?;
            scores(&runtime(store, &temporary, &control)?, &[(1, 1.0)])?;
        } else {
            peer.commit_transaction()?;
            expect(
                store.commit_transaction().is_err(),
                "adoption conflicts with a newly committed raw ordinal",
            )?;
            store.rollback_transaction()?;
            expect_eq(
                &raw.count()?,
                &2,
                "failed adoption preserves the winning raw insertion",
            )?;
            expect(
                canonical(store)?
                    .retain_for_index(&definition.relation, &control)?
                    .selected_source(&Resolver, &control)?
                    .is_none(),
                "failed adoption publishes no head",
            )?;
        }
    }
    Ok(())
}

fn verify_adoption_gaps(
    store: &Arc<dyn KeyValueStore>,
    control: &StorageReadControl,
) -> StorageBackendResult<()> {
    let mut raw = KeyValueVectorIndex::new(store.clone(), TABLE, FIELD, 2);
    raw.add(1, vec![1.0, 0.0])?;
    raw.add_many(2, vec![vec![1.0, 0.0], vec![0.0, 1.0], vec![-1.0, 0.0]])?;
    let peer = store.open_session()?;
    for (missing, expected) in [
        (
            0,
            "invalid persisted vector ordinal sequence for document 2: expected 0, found 1",
        ),
        (
            1,
            "invalid persisted vector ordinal sequence for document 2: expected 1, found 2",
        ),
    ] {
        let key = crate::key_value::codec::vector_key(TABLE, FIELD, 2, missing)?;
        let value = store.get(&key)?.expect("corrupt fixture starts complete");
        store.delete(&key)?;
        store.begin_transaction()?;
        store.put(b"before-rejected-adoption", b"kept")?;
        let before = store.scan_prefix(b"")?;
        let result = canonical(store)?.create_index(
            &row([91; 16])?.relation,
            &Resolver,
            diskann_runtime_fixture_options(2)?,
            &DiskANNTemporaryBudget::new(1 << 20),
            control,
        );
        expect_eq(
            &result.as_ref().err().map(std::string::ToString::to_string),
            &Some(expected.to_owned()),
            "the shared canonical decoder rejects ordinal gaps during adoption",
        )?;
        expect_eq(
            &store.scan_prefix(b"")?,
            &before,
            "failed adoption preserves all outer private records",
        )?;
        expect(
            peer.scan_prefix_keys_after(b"\0uqa-diskann-v1\0", None, 1)?
                .is_empty(),
            "rejected adoption allocates no physical generation or data identity",
        )?;
        store.rollback_transaction()?;
        store.put(&key, &value)?;
    }
    Ok(())
}