uqa-storage 0.5.3

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

use super::*;
use crate::mvcc::commit::RecordWriteKind;
use crate::mvcc::{MemoryVersionStore, MergedRecordSnapshot, RecordMetadata};

#[test]
fn revision_reads_stream_each_spilled_entry_once_without_payloads() {
    use crate::key_value::{KeyValueRead, KeyValueReadRevision};
    use crate::mvcc::{DatabaseId, RecordRead};

    for count in [128, 512] {
        let control = StorageReadControl::with_limit(128 << 10);
        let changes = PrivateRecordChanges::new(control.memory());
        for id in 0..count {
            stage(
                &changes,
                &mut Model::new(),
                id,
                Some("x".repeat(1024)),
                &control,
            );
        }
        let view = merged(&changes);
        let expected = view.private_revision();
        let database = DatabaseId::from_bytes([7; 16]);
        let read = RecordRead::new(&view, database, &control);
        super::super::run::read_counts::take();
        assert!(
            read.revision(&[b"k"]).unwrap()
                == KeyValueReadRevision::records(database, view.sequence(), expected)
        );
        let counts = super::super::run::read_counts::take();
        assert!(counts.entries > 0);
        assert!(counts.entries <= count, "{counts:?}");
        assert_eq!(counts.values, 0);
        stage(&changes, &mut Model::new(), 0, None, &control);
        assert!(
            read.revision(&[b"k"]).unwrap()
                == KeyValueReadRevision::records(database, view.sequence(), expected)
        );
        let newer = merged(&changes);
        assert!(
            RecordRead::new(&newer, database, &control)
                .revision(&[b"k"])
                .unwrap()
                != read.revision(&[b"k"]).unwrap()
        );
        control.cancellation().cancel();
        assert!(read.revision(&[b"k"]).is_err());
    }
}

#[test]
fn prepared_private_roots_share_sorted_spill_and_preserve_undo() {
    for count in [4, 128, 512] {
        for grouped in [false, true] {
            let control = StorageReadControl::with_limit(128 << 10);
            let source = PrivateRecordChanges::new(control.memory());
            let mut model = Model::new();
            for id in 0..count {
                stage(&source, &mut model, id, Some("x".repeat(1024)), &control);
            }
            let prepared = source.prepare(&control).unwrap();
            let scope: Option<PrivateRevisionScope> = grouped.then_some(|key| Ok(key.get(..1)));
            super::super::run::write_counts::take();
            super::super::run::read_counts::take();
            let changes = PrivateRecordChanges::from_prepared(&prepared, scope, &control).unwrap();
            let written = super::super::run::write_counts::take();
            assert_eq!(
                written.bytes, 0,
                "immutable prepared values must not be rewritten"
            );
            assert_eq!(written.copied, 0);
            assert_eq!(super::super::run::read_counts::take().values, 0);
            let again = changes.prepare(&control).unwrap();
            assert_eq!(again.fingerprint(), prepared.fingerprint());
            assert_eq!(super::super::run::write_counts::take().bytes, 0);
            drop(again);
            let retained = changes.snapshot().unwrap();
            assert_streamed_matches(&retained, &model, &control);
            if grouped {
                assert_eq!(
                    retained.scope_revision(b"k", &control).unwrap(),
                    retained.revision()
                );
            }
            let savepoint = StorageSavepointId::allocate();
            changes.savepoint(savepoint).unwrap();
            stage(&changes, &mut Model::new(), 0, None, &control);
            changes.rollback_to_savepoint(savepoint).unwrap();
            assert_eq!(changes.snapshot().unwrap().revision(), retained.revision());
            assert_streamed_matches(&changes.snapshot().unwrap(), &model, &control);
            assert_streamed_matches(&retained, &model, &control);
            stage(
                &source,
                &mut Model::new(),
                0,
                Some("changed".into()),
                &control,
            );
            assert_streamed_matches(&changes.snapshot().unwrap(), &model, &control);
            control.cancellation().cancel();
            assert!(PrivateRecordChanges::from_prepared(&prepared, scope, &control).is_err());
            drop((retained, changes, prepared, source));
            assert_eq!(control.memory().used(), 0);
        }
    }
}

#[test]
fn prepared_spill_adoption_preserves_independent_allowance_ownership() {
    for count in [128, 512] {
        let control = StorageReadControl::with_limit(128 << 10);
        let source = PrivateRecordChanges::new(control.memory());
        let mut model = Model::new();
        for id in 0..count {
            stage(&source, &mut model, id, Some("x".repeat(1024)), &control);
        }
        let prepared = source.prepare(&control).unwrap();
        assert!(prepared.resident().is_none());
        let independent = StorageReadControl::with_limit(128 << 10);
        super::super::run::write_counts::take();
        let changes = PrivateRecordChanges::from_prepared(&prepared, None, &independent).unwrap();
        assert_eq!(
            super::super::run::write_counts::take().bytes,
            count as u64 * 1024
        );
        let undersized = StorageReadControl::with_limit(1024);
        assert!(PrivateRecordChanges::from_prepared(&prepared, None, &undersized).is_err());
        assert_eq!(undersized.memory().used(), 0);
        drop((source, prepared));
        assert_eq!(control.memory().used(), 0);
        assert!(independent.memory().used() > 0);
        assert_streamed_matches(&changes.snapshot().unwrap(), &model, &independent);
        drop(changes);
        assert_eq!(independent.memory().used(), 0);
    }
}

fn assert_streamed_matches(
    snapshot: &PrivateRecordSnapshot,
    model: &Model,
    control: &StorageReadControl,
) {
    let mut cursor = snapshot.cursor(b"k", None, control).unwrap();
    for (key, (expected, value)) in model {
        let entry = cursor.next(control).unwrap().unwrap();
        let write = entry.read(control).unwrap();
        assert_eq!(write.key(), key.as_slice());
        assert_eq!(write.expected(), *expected);
        assert_eq!(write.value(), value.as_deref());
    }
    assert!(cursor.next(control).unwrap().is_none());
}

fn merged(changes: &PrivateRecordChanges) -> MergedRecordSnapshot {
    let store = MemoryVersionStore::new(&MemoryBudget::new(1 << 20));
    MergedRecordSnapshot::new(
        std::sync::Arc::new(store.snapshot().unwrap()),
        changes.snapshot().unwrap(),
    )
}

#[test]
fn disjoint_private_prefixes_do_not_open_spill_readers() {
    for count in [128, 512] {
        let control = StorageReadControl::with_limit(128 << 10);
        let changes = PrivateRecordChanges::new(control.memory());
        let mut model = Model::new();
        for id in 0..count {
            stage(&changes, &mut model, id, Some("x".repeat(1024)), &control);
        }
        assert!(spilled_runs(&changes) > 0);
        let snapshot = changes.snapshot().unwrap();
        let read = StorageReadControl::with_limit(32 << 10);
        let retained = control.memory().used();
        for prefix in [b"a".as_slice(), b"j\xff", b"l", b"\xff"] {
            super::super::run::read_counts::take();
            let mut cursor = snapshot.cursor(prefix, None, &read).unwrap();
            assert!(cursor.next(&read).unwrap().is_none());
            drop(cursor);
            assert!(snapshot.scan(prefix, None, 1, &read).unwrap().is_empty());
            assert!(snapshot
                .scan_keys(prefix, None, 1, &read)
                .unwrap()
                .is_empty());
            let counts = super::super::run::read_counts::take();
            assert_eq!(counts.blocks, 0, "prefix={prefix:?}: {counts:?}");
            assert_eq!(counts.entries, 0, "prefix={prefix:?}: {counts:?}");
            assert_eq!(counts.values, 0);
            assert_eq!(read.memory().used(), 0);
            assert_eq!(control.memory().used(), retained);
        }
        assert_streamed_matches(&snapshot, &model, &read);
        read.cancellation().cancel();
        assert!(snapshot.cursor(b"a", None, &read).is_err());
        drop((snapshot, changes));
        assert_eq!(control.memory().used(), 0);
    }
}

#[test]
fn absent_prefixes_inside_spill_blocks_do_not_read_unrelated_entries() {
    for count in [128_usize, 512] {
        let control = StorageReadControl::with_limit(128 << 10);
        let revision = PrivateRecordRevision::for_tests();
        let mut writer =
            run::SpilledRunWriter::new(count as u64 + 1, count as u64 * 16, control.memory())
                .unwrap();
        for id in 0..count {
            writer
                .push(
                    format!("a/{id:06}/vector").as_bytes(),
                    None,
                    RecordWriteKind::Canonical,
                    revision,
                    (!id.is_multiple_of(7)).then_some(b"value".as_slice()),
                    &control,
                )
                .unwrap();
        }
        writer
            .push(
                b"z/header",
                None,
                RecordWriteKind::Canonical,
                revision,
                Some(b"header"),
                &control,
            )
            .unwrap();
        let changes = PrivateRecordChanges::from_spilled_run(
            writer.finish().unwrap(),
            revision,
            None,
            &control,
        )
        .unwrap();
        let snapshot = changes.snapshot().unwrap();
        let baseline = control.memory().used();
        for prefix in [b"a/999999/".as_slice(), b"y/empty/"] {
            run::read_counts::take();
            let mut cursor = snapshot.cursor(prefix, None, &control).unwrap();
            assert!(cursor.next(&control).unwrap().is_none());
            drop(cursor);
            assert!(snapshot.scan(prefix, None, 1, &control).unwrap().is_empty());
            assert!(snapshot
                .scan_keys(prefix, None, 1, &control)
                .unwrap()
                .is_empty());
            let reads = run::read_counts::take();
            assert_eq!(
                reads.blocks, 0,
                "count={count}, prefix={prefix:?}: {reads:?}"
            );
            assert_eq!(reads.entries, 0);
            assert_eq!(reads.values, 0);
            assert_eq!(control.memory().used(), baseline);
        }
        for id in [0, count / 2, count - 1] {
            let key = format!("a/{id:06}/vector");
            for length in 0..=key.len() {
                let rows = snapshot
                    .scan_keys(&key.as_bytes()[..length], None, count + 1, &control)
                    .unwrap();
                assert!(rows.iter().any(|row| row.key() == key.as_bytes()));
            }
        }
        let savepoint = StorageSavepointId::allocate();
        changes.savepoint(savepoint).unwrap();
        let write =
            PreparedRecordWrite::copy_bytes(b"a/999999/vector", None, Some(b"new"), &control)
                .unwrap();
        changes.apply_owned(&[write], &control).unwrap();
        let newer = changes.snapshot().unwrap();
        assert_eq!(
            newer.scan(b"a/999999/", None, 1, &control).unwrap().len(),
            1
        );
        assert!(snapshot
            .scan(b"a/999999/", None, 1, &control)
            .unwrap()
            .is_empty());
        changes.rollback_to_savepoint(savepoint).unwrap();
        assert!(changes
            .snapshot()
            .unwrap()
            .scan(b"a/999999/", None, 1, &control)
            .unwrap()
            .is_empty());
        assert_eq!(
            newer.scan(b"a/999999/", None, 1, &control).unwrap().len(),
            1
        );
        control.cancellation().cancel();
        assert!(snapshot.cursor(b"y/empty/", None, &control).is_err());
        drop((newer, snapshot, changes));
        assert_eq!(control.memory().used(), 0);
    }
}

#[test]
fn empty_private_cursors_need_no_read_workspace_and_keep_their_snapshot() {
    let control = StorageReadControl::with_limit(1 << 20);
    let changes = PrivateRecordChanges::new(control.memory());
    let snapshot = changes.snapshot().unwrap();
    let exhausted = StorageReadControl::with_limit(0);
    let mut cursor = snapshot.cursor(b"k", Some(b"k000000"), &exhausted).unwrap();
    stage(
        &changes,
        &mut Model::new(),
        1,
        Some("later".into()),
        &control,
    );
    assert!(cursor.next(&exhausted).unwrap().is_none());
    assert!(cursor.next(&exhausted).unwrap().is_none());
    assert_eq!(exhausted.memory().used(), 0);
    exhausted.cancellation().cancel();
    assert!(cursor.next(&exhausted).is_err());
    assert!(snapshot.cursor(b"k", None, &exhausted).is_err());
}

#[test]
fn private_entry_cursor_preserves_retained_values_without_repeated_entry_scans() {
    let control = StorageReadControl::with_limit(512 << 10);
    let changes = PrivateRecordChanges::new(control.memory());
    let mut model = Model::new();
    for id in 0..256 {
        stage(
            &changes,
            &mut model,
            id,
            (id % 7 != 0).then(|| "x".repeat(4096)),
            &control,
        );
    }
    assert!(spilled_runs(&changes) > 0);
    let snapshot = changes.snapshot().unwrap();
    stage(&changes, &mut Model::new(), 16, None, &control);
    let read = StorageReadControl::with_limit(128 << 10);
    let retained_memory = control.memory().used();
    super::super::run::read_counts::take();
    let mut cursor = snapshot.cursor(b"k", Some(&key(15)), &read).unwrap();
    let mut entries = 0;
    let mut spilled = None;
    while let Some(entry) = cursor.next(&read).unwrap() {
        let id = entries + 16;
        assert_eq!(entry.key(), key(id));
        let write = entry.read(&read).unwrap();
        let (revision, value) = &model[entry.key()];
        assert_eq!(write.expected(), *revision);
        assert_eq!(write.value(), value.as_deref());
        assert_eq!(entry.metadata().live, value.is_some());
        assert!(entry.revision() <= snapshot.revision().unwrap());
        entries += 1;
        if spilled.is_none() && snapshot.records.get(entry.key()).is_none() {
            spilled = Some(entry);
        }
    }
    assert_eq!(entries, 240);
    assert!(cursor.next(&read).unwrap().is_none());
    assert_eq!(control.memory().used(), retained_memory);
    // An actual spilled entry still reads its selected value after all cursor readers close.
    let entry = spilled.unwrap();
    assert_eq!(
        entry.read(&read).unwrap().value(),
        model[entry.key()].1.as_deref()
    );
    drop(entry);
    let counts = super::super::run::read_counts::take();
    assert!(counts.entries > 0);
    assert!(counts.entries <= 512, "{counts:?}");
    drop(cursor);
    assert_eq!(read.memory().used(), 0);
    let mut outside = snapshot.cursor(b"k", Some(b"z"), &read).unwrap();
    assert!(outside.next(&read).unwrap().is_none());
    drop(outside);
    let mut prefix = snapshot.cursor(b"k00000", None, &read).unwrap();
    while prefix.next(&read).unwrap().is_some() {}
    assert_eq!(control.memory().used(), retained_memory);
    drop(prefix);

    let mut failed = snapshot.cursor(b"k", None, &read).unwrap();
    let exhausted = StorageReadControl::with_limit(0);
    assert!(failed.next(&exhausted).is_err());
    assert_eq!(control.memory().used(), retained_memory);
    assert!(
        failed.next(&read).is_err(),
        "an interrupted cursor cannot skip a consumed entry"
    );
    drop(failed);
    assert_eq!(read.memory().used(), 0);
}

#[test]
fn merged_key_scan_does_not_load_private_payloads_larger_than_its_allowance() {
    let control = StorageReadControl::with_limit(512 << 10);
    let changes = PrivateRecordChanges::new(control.memory());
    let mut model = Model::new();
    for id in 0..32 {
        stage(
            &changes,
            &mut model,
            id,
            (id != 1).then(|| "x".repeat(64 << 10)),
            &control,
        );
    }
    assert!(spilled_runs(&changes) > 0);
    let captured = merged(&changes);
    stage(&changes, &mut model, 0, None, &control);
    stage(&changes, &mut model, 1, Some("new".into()), &control);
    let current = merged(&changes);
    let read = StorageReadControl::with_limit(32 << 10);
    super::super::run::read_counts::take();
    for (view, deleted) in [(&captured, 1), (&current, 0)] {
        let mut visited = 0;
        view.visit_keys(b"k", None, usize::MAX, &read, &mut |selected, metadata| {
            assert_eq!(selected, key(visited));
            assert_eq!(
                metadata,
                RecordMetadata {
                    revision: expected(visited),
                    live: visited != deleted,
                }
            );
            visited += 1;
            Ok(true)
        })
        .unwrap();
        assert_eq!(visited, 32);
        assert_eq!(read.memory().used(), 0);
    }
    let counts = super::super::run::read_counts::take();
    assert!(counts.entries > 0);
    assert_eq!(counts.values, 0, "key scans must not load payloads");
}

#[test]
fn stopping_a_value_scan_does_not_load_the_next_private_payload() {
    let control = StorageReadControl::with_limit(512 << 10);
    let changes = PrivateRecordChanges::new(control.memory());
    let mut model = Model::new();
    stage(&changes, &mut model, 0, Some("small".into()), &control);
    for id in 1..16 {
        stage(
            &changes,
            &mut model,
            id,
            Some("x".repeat(64 << 10)),
            &control,
        );
    }
    assert!(spilled_runs(&changes) > 0);
    let view = merged(&changes);
    let read = StorageReadControl::with_limit(32 << 10);
    for (limit, more) in [(1, true), (usize::MAX, false)] {
        super::super::run::read_counts::take();
        let mut calls = 0;
        view.visit_prefix(b"k", None, limit, &read, &mut |selected, record| {
            assert_eq!(selected, key(0));
            assert_eq!(record.value, Some(b"small".as_slice()));
            calls += 1;
            Ok(more)
        })
        .unwrap();
        assert_eq!(calls, 1);
        assert_eq!(super::super::run::read_counts::take().values, 1);
        assert_eq!(read.memory().used(), 0);
    }
}