itzma-core 0.2.1

Vector search on object storage: an embedded library, no server, just your bucket
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! The k-way merge of a streaming merge.
//!
//! Every input is sorted by id: a version 2 or 3 segment read forward a window at
//! a time, a version 1 segment or the WAL tail as a whole `Layer`. For each
//! id the newest input's entry wins, a document or a tombstone, which is
//! `Layer::overlay`'s rule, and the winners go to a [`SegmentWriter`] in id
//! order. Nothing needs to be held but each input's current window and the
//! output.

use std::collections::VecDeque;
use std::iter::Peekable;

use object_store::ObjectStore;

use crate::budget::ReadBudget;
use crate::cpu::{self, Cost, Lane};
use crate::document::{Document, SparseVector};
use crate::error::{ItzmaError, Result};
use crate::fetch::FetchPolicy;
use crate::layer::{Layer, NewestWins};
use crate::segment::{self, AttrIn, RangedSegment, SegmentWriter};
use crate::sparse::{self, SparseRows};

/// Bytes one input reads ahead (a guess, measured only on `InMemory` so
/// far). A window is at least one block, so a block
/// bigger than this is read on its own.
pub(crate) const MERGE_WINDOW: u64 = 8 * 1024 * 1024;

/// A segment at or below this size (or the window, if smaller) is read whole:
/// one request charged its exact size, where opening it for ranged reads
/// charges a 64 KiB suffix guess up front. Small enough that holding it
/// decoded costs little.
const WHOLE_BELOW: u64 = 1024 * 1024;

/// Entries handed to the writer per trip to the compute pool.
const WRITE_BATCH: usize = 4096;

/// What one input holds for one id.
pub(crate) enum Entry {
    Doc(Document),
    Tombstone(String),
}

impl Entry {
    fn id(&self) -> &str {
        match self {
            Entry::Doc(doc) => &doc.id,
            Entry::Tombstone(id) => id,
        }
    }

    /// Roughly what writing it costs, for the compute pool's threshold.
    fn weight(&self) -> usize {
        match self {
            Entry::Doc(doc) => doc.vector.len() * 4 + doc.id.len() + 64,
            Entry::Tombstone(id) => id.len(),
        }
    }
}

fn corrupt(label: &str, reason: &str) -> ItzmaError {
    ItzmaError::CorruptSegment {
        path: label.to_owned(),
        reason: reason.to_owned(),
    }
}

/// The next of two sorted runs, documents and tombstones, by id. One id in
/// both is damage: a segment never holds one.
fn smaller(label: &str, doc: Option<&str>, tombstone: Option<&str>) -> Result<Option<bool>> {
    match (doc, tombstone) {
        (None, None) => Ok(None),
        (Some(_), None) => Ok(Some(true)),
        (None, Some(_)) => Ok(Some(false)),
        (Some(d), Some(t)) if d == t => {
            Err(corrupt(label, "an id is both a document and a tombstone"))
        }
        (Some(d), Some(t)) => Ok(Some(d < t)),
    }
}

/// An input held whole: a version 1 segment or the WAL tail.
pub(crate) struct WholeInput {
    label: String,
    docs: Peekable<std::vec::IntoIter<Document>>,
    tombstones: Peekable<std::vec::IntoIter<String>>,
}

impl WholeInput {
    fn new(label: String, layer: Layer) -> Self {
        let (docs, tombstones) = layer.into_sorted_parts();
        Self::from_parts_unchecked(&label, docs, tombstones)
    }

    /// Parts taken as given, in the order given. Tests use it to forge an
    /// input out of order.
    pub(crate) fn from_parts_unchecked(
        label: &str,
        docs: Vec<Document>,
        tombstones: Vec<String>,
    ) -> Self {
        Self {
            label: label.to_owned(),
            docs: docs.into_iter().peekable(),
            tombstones: tombstones.into_iter().peekable(),
        }
    }

    fn next(&mut self) -> Result<Option<Entry>> {
        let doc = self.docs.peek().map(|d| d.id.as_str());
        let tombstone = self.tombstones.peek().map(String::as_str);
        Ok(match smaller(&self.label, doc, tombstone)? {
            None => None,
            Some(true) => self.docs.next().map(Entry::Doc),
            Some(false) => self.tombstones.next().map(Entry::Tombstone),
        })
    }
}

/// A version 2 or 3 segment read forward, one window of documents and one of
/// tombstones at a time.
pub(crate) struct RangedInput<'a> {
    label: String,
    segment: RangedSegment<'a>,
    sparse: Option<SparseRows<'a>>,
    window: u64,
    next_doc_block: usize,
    next_tombstone_block: usize,
    docs: VecDeque<Document>,
    tombstones: VecDeque<String>,
}

impl RangedInput<'_> {
    /// Blocks from `start` whose bytes fit the window, at least one.
    fn window_end(&self, start: usize, count: usize, bytes_of: impl Fn(usize) -> u64) -> usize {
        let mut end = start;
        let mut bytes = 0u64;
        while end < count {
            let next = bytes_of(end);
            if end > start && bytes.saturating_add(next) > self.window {
                break;
            }
            bytes = bytes.saturating_add(next);
            end += 1;
        }
        end
    }

    fn rows_of_block(&self, block: usize) -> std::ops::Range<u64> {
        let rows = segment::BLOCK_ROWS as u64;
        let start = block as u64 * rows;
        start..(start + rows).min(self.segment.doc_count())
    }

    /// The next window, fetched only once the last one is drained; fetching
    /// it in the background is
    async fn refill_docs(&mut self) -> Result<()> {
        let count = self.segment.doc_block_count();
        let start = self.next_doc_block;
        if !self.docs.is_empty() || start >= count {
            return Ok(());
        }
        let end = self.window_end(start, count, |b| {
            let sparse = self
                .sparse
                .as_ref()
                .map_or(0, |s| s.bytes_of(self.rows_of_block(b)));
            // A damaged sidecar directory can claim nearly u64::MAX bytes;
            // reading it is then an error, not an overflow here.
            self.segment.doc_window_bytes(b).saturating_add(sparse)
        });
        let rows = self.rows_of_block(start).start..self.rows_of_block(end - 1).end;
        let sidecar = async {
            match &self.sparse {
                Some(sparse) => sparse.rows(rows).await.map(Some),
                None => Ok(None),
            }
        };
        let (window, sparse) = futures::try_join!(self.segment.doc_window(start..end), sidecar)?;
        let sparse = sparse.unwrap_or_else(|| vec![None; window.ids.len()]);
        let label = self.label.clone();
        let cost = Cost::Bytes(window.vectors.values.len() * 4 + window.attrs.stored_bytes());
        let docs = cpu::run(Lane::Maintenance, cost, move || {
            documents(&label, window, sparse)
        })
        .await?;
        // The queue is empty here, so the decoded window becomes it as is.
        self.docs = VecDeque::from(docs);
        self.next_doc_block = end;
        Ok(())
    }

    async fn refill_tombstones(&mut self) -> Result<()> {
        let count = self.segment.tombstone_block_count();
        let start = self.next_tombstone_block;
        if !self.tombstones.is_empty() || start >= count {
            return Ok(());
        }
        let end = self.window_end(start, count, |b| self.segment.tombstone_window_bytes(b));
        self.tombstones = VecDeque::from(self.segment.tombstone_window(start..end).await?);
        self.next_tombstone_block = end;
        Ok(())
    }

    async fn next(&mut self) -> Result<Option<Entry>> {
        self.refill_docs().await?;
        self.refill_tombstones().await?;
        let doc = self.docs.front().map(|d| d.id.as_str());
        let tombstone = self.tombstones.front().map(String::as_str);
        Ok(match smaller(&self.label, doc, tombstone)? {
            None => None,
            Some(true) => self.docs.pop_front().map(Entry::Doc),
            Some(false) => self.tombstones.pop_front().map(Entry::Tombstone),
        })
    }
}

/// A window's rows as documents, attributes parsed from their JSON.
fn documents(
    label: &str,
    window: segment::DocWindow,
    sparse: Vec<Option<SparseVector>>,
) -> Result<Vec<Document>> {
    let dim = window.vectors.dim;
    window
        .ids
        .into_iter()
        .zip(sparse)
        .enumerate()
        .map(|(i, (id, sparse))| {
            let attrs = window.attrs.get(i).value(label)?;
            Ok(Document {
                id,
                vector: window.vectors.values[i * dim..(i + 1) * dim].to_vec(),
                attrs,
                sparse,
            })
        })
        .collect()
}

/// One sorted input of a merge. A merge holds a handful, so the size of the
/// bigger variant costs nothing worth a box.
#[allow(clippy::large_enum_variant)]
pub(crate) enum Input<'a> {
    /// A version 2 or 3 segment, read forward.
    Ranged(RangedInput<'a>),
    /// A version 1 segment or the WAL tail, decoded whole.
    Whole(WholeInput),
}

impl<'a> Input<'a> {
    /// Open segment `id` of `namespace`, whose `data.bin` the caller's
    /// listing says is `size` bytes: read forward when it is version 2 and
    /// bigger than [`WHOLE_BELOW`] (or `window`, if smaller), else decoded
    /// whole with its sparse vectors. Everything is charged to `budget`, and
    /// nothing goes through the decoded cache.
    ///
    /// # Errors
    ///
    /// What opening or reading the segment gives: [`segment::missing`] for one
    /// that is gone, [`ItzmaError::CorruptSegment`], a budget refusal.
    #[allow(clippy::too_many_arguments)] // the handle's read settings and the listing's size
    pub(crate) async fn segment(
        store: &'a dyn ObjectStore,
        namespace: &str,
        id: &str,
        dim: usize,
        budget: &'a ReadBudget,
        policy: &'a FetchPolicy,
        size: u64,
        window: u64,
    ) -> Result<Input<'a>> {
        let path = segment::segment_path(namespace, id);
        let label = path.to_string();
        if size > window.min(WHOLE_BELOW) {
            let segment =
                RangedSegment::open(store, namespace, id, dim, budget, policy, None).await?;
            let sparse =
                SparseRows::open(store, namespace, id, segment.doc_count(), budget).await?;
            return Ok(Input::Ranged(RangedInput {
                label,
                segment,
                sparse,
                window,
                next_doc_block: 0,
                next_tombstone_block: 0,
                docs: VecDeque::new(),
                tombstones: VecDeque::new(),
            }));
        }
        let layer =
            sparse::fetch_with_sparse(store, namespace, id, dim, budget, policy, Lane::Maintenance)
                .await?;
        Ok(Input::Whole(WholeInput::new(label, layer)))
    }

    /// The WAL tail, or any layer, as an input.
    pub(crate) fn layer(layer: Layer) -> Input<'a> {
        Input::Whole(WholeInput::new("the WAL tail".into(), layer))
    }

    fn label(&self) -> &str {
        match self {
            Input::Ranged(input) => &input.label,
            Input::Whole(input) => &input.label,
        }
    }

    /// The next entry in id order, or `None` once drained.
    pub(crate) async fn next(&mut self) -> Result<Option<Entry>> {
        match self {
            Input::Ranged(input) => input.next().await,
            Input::Whole(input) => input.next(),
        }
    }
}

/// Take input `at`'s next entry into `heads` and `wins`, checking that the
/// input's ids rise strictly.
async fn advance(
    inputs: &mut [Input<'_>],
    heads: &mut [Option<Entry>],
    wins: &mut NewestWins<String>,
    at: usize,
    last: Option<&str>,
) -> Result<()> {
    let Some(entry) = inputs[at].next().await? else {
        return Ok(());
    };
    if last.is_some_and(|last| entry.id() <= last) {
        return Err(corrupt(
            inputs[at].label(),
            "ids are not in strictly ascending order",
        ));
    }
    wins.push(at, entry.id().to_owned());
    heads[at] = Some(entry);
    Ok(())
}

/// Merge `inputs`, oldest first, into `writer`: for each id the newest
/// input's entry, tombstones left out when `drop_tombstones` (a group that
/// starts at WAL seq 1). The writer is fed on the maintenance lane in batches.
///
/// # Errors
///
/// What reading an input gives, [`ItzmaError::CorruptSegment`] for an input
/// out of order, and what the writer gives.
pub(crate) async fn merge_into(
    mut inputs: Vec<Input<'_>>,
    drop_tombstones: bool,
    mut writer: SegmentWriter,
) -> Result<SegmentWriter> {
    let mut heads: Vec<Option<Entry>> = inputs.iter().map(|_| None).collect();
    let mut wins = NewestWins::with_capacity(inputs.len());
    for at in 0..inputs.len() {
        advance(&mut inputs, &mut heads, &mut wins, at, None).await?;
    }
    let mut batch = Vec::with_capacity(WRITE_BATCH);
    let mut batch_weight = 0;
    while let Some((id, at)) = wins.pop() {
        let winner = heads[at].take();
        advance(&mut inputs, &mut heads, &mut wins, at, Some(&id)).await?;
        while let Some(older) = wins.pop_older(&id) {
            heads[older] = None;
            advance(&mut inputs, &mut heads, &mut wins, older, Some(&id)).await?;
        }
        match winner {
            Some(Entry::Tombstone(_)) if drop_tombstones => {}
            Some(entry) => {
                batch_weight += entry.weight();
                batch.push(entry);
            }
            None => {}
        }
        if batch.len() == WRITE_BATCH {
            writer = write(writer, std::mem::take(&mut batch), batch_weight).await?;
            batch_weight = 0;
        }
    }
    if !batch.is_empty() {
        writer = write(writer, batch, batch_weight).await?;
    }
    Ok(writer)
}

/// Feed `batch` to `writer` on the maintenance lane and hand it back.
async fn write(
    mut writer: SegmentWriter,
    batch: Vec<Entry>,
    weight: usize,
) -> Result<SegmentWriter> {
    cpu::run(Lane::Maintenance, Cost::Bytes(weight), move || {
        for entry in batch {
            match entry {
                Entry::Doc(doc) => writer.doc(
                    &doc.id,
                    &doc.vector,
                    AttrIn::Owned(doc.attrs),
                    doc.sparse.as_ref(),
                )?,
                Entry::Tombstone(id) => writer.tombstone(&id)?,
            }
        }
        Ok(writer)
    })
    .await
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use object_store::memory::InMemory;
    use object_store::{ObjectStore, ObjectStoreExt};

    use super::*;
    use crate::attrs::AttrSchema;
    use crate::document::WalOp;
    use crate::segment::{encode_as, encode_parts, segment_id, segment_path};

    struct Rng(u64);

    impl Rng {
        fn next(&mut self) -> u64 {
            self.0 ^= self.0 << 13;
            self.0 ^= self.0 >> 7;
            self.0 ^= self.0 << 17;
            self.0
        }

        fn below(&mut self, n: u64) -> u64 {
            self.next() % n.max(1)
        }
    }

    const DIM: usize = 3;

    /// One input: upserts and deletes over a shared id space, so ids repeat
    /// across inputs, deletes hit ids a later input inserts again, and some
    /// ids are only ever tombstones.
    fn input(rng: &mut Rng, space: u64) -> Layer {
        let mut l = Layer::new();
        for _ in 0..rng.below(space + 1) {
            let n = rng.below(space);
            let id = format!("id{n:04}");
            if rng.below(4) == 0 {
                l.apply(WalOp::Delete(id));
            } else {
                let v = rng.below(1000) as f32;
                l.apply(WalOp::Upsert(Document {
                    id,
                    vector: vec![v, -v, 0.5],
                    attrs: (rng.below(2) == 0).then(|| serde_json::json!({ "v": v, "s": "x" })),
                    sparse: (rng.below(3) == 0).then(|| vec![(n as u32, v)]),
                }));
            }
        }
        l
    }

    /// Cases of 1 to 6 inputs, oldest first, and whether tombstones go.
    fn cases(count: usize) -> Vec<(Vec<Layer>, bool)> {
        let mut rng = Rng(0x00ab_0037);
        (0..count)
            .map(|_| {
                let space = rng.below(600) + 1;
                let inputs = (0..rng.below(6) + 1)
                    .map(|_| input(&mut rng, space))
                    .collect();
                (inputs, rng.below(2) == 0)
            })
            .collect()
    }

    fn expected(inputs: &[Layer], drop: bool) -> (Vec<u8>, Option<Vec<u8>>) {
        let mut all = Layer::new();
        for layer in inputs {
            all.overlay(layer.clone());
        }
        if drop {
            all.drop_tombstones();
        }
        let (data, sparse) = encode_parts(&all, DIM, 2, &AttrSchema::free_form()).unwrap();
        (
            Bytes::from(data).to_vec(),
            sparse.map(|s| Bytes::from(s).to_vec()),
        )
    }

    async fn merged(inputs: Vec<Input<'_>>, drop: bool) -> Result<(Vec<u8>, Option<Vec<u8>>)> {
        let writer = merge_into(
            inputs,
            drop,
            SegmentWriter::new(DIM, 2, &AttrSchema::free_form())?,
        )
        .await?;
        let (data, sparse) = writer.finish()?;
        Ok((
            Bytes::from(data).to_vec(),
            sparse.map(|s| Bytes::from(s).to_vec()),
        ))
    }

    #[tokio::test]
    async fn merge_equals_overlay_on_random_inputs() {
        for (case, (inputs, drop)) in cases(500).into_iter().enumerate() {
            let want = expected(&inputs, drop);
            let got = merged(inputs.into_iter().map(Input::layer).collect(), drop)
                .await
                .unwrap();
            assert!(got == want, "case {case}");
        }
    }

    /// Each input as a segment of `version`, with its `sparse.bin`.
    async fn store_of(inputs: &[Layer], versions: &[u32]) -> InMemory {
        let store = InMemory::new();
        for (i, layer) in inputs.iter().enumerate() {
            let id = segment_id(i as u64 + 1, i as u64 + 1);
            let version = versions[i % versions.len()];
            let (_, sparse) = encode_parts(layer, DIM, version, &AttrSchema::free_form()).unwrap();
            let data = encode_as(layer, DIM, version).unwrap();
            store
                .put(&segment_path("ns", &id), data.into())
                .await
                .unwrap();
            if let Some(sparse) = sparse {
                store
                    .put(&crate::sparse::sparse_path("ns", &id), sparse)
                    .await
                    .unwrap();
            }
        }
        store
    }

    async fn open_all<'a>(
        store: &'a dyn ObjectStore,
        count: usize,
        budget: &'a ReadBudget,
        policy: &'a FetchPolicy,
        window: u64,
    ) -> Vec<Input<'a>> {
        let mut inputs = Vec::new();
        for i in 0..count {
            let id = segment_id(i as u64 + 1, i as u64 + 1);
            let size = store.head(&segment_path("ns", &id)).await.unwrap().size;
            let input = Input::segment(store, "ns", &id, DIM, budget, policy, size, window);
            inputs.push(input.await.unwrap());
        }
        inputs
    }

    async fn ranged_cases(versions: &[u32], window: u64, count: usize) {
        for (case, (inputs, drop)) in cases(count).into_iter().enumerate() {
            let want = expected(&inputs, drop);
            let store = store_of(&inputs, versions).await;
            let budget = ReadBudget::unlimited();
            let policy = FetchPolicy::local();
            let opened = open_all(&store, inputs.len(), &budget, &policy, window).await;
            let got = merged(opened, drop).await.unwrap();
            assert!(
                got == want,
                "case {case}, versions {versions:?}, window {window}"
            );
        }
    }

    /// A window smaller than any segment here, so every version 2 input is
    /// read forward rather than whole.
    const SMALL_WINDOW: u64 = 64;

    #[tokio::test]
    async fn ranged_inputs_merge_the_same_as_layers() {
        ranged_cases(&[2], SMALL_WINDOW, 120).await;
    }

    #[tokio::test]
    async fn small_segments_read_whole_merge_the_same() {
        ranged_cases(&[2], MERGE_WINDOW, 60).await;
    }

    #[tokio::test]
    async fn a_one_block_window_merges_the_same() {
        ranged_cases(&[2], 1, 60).await;
    }

    #[tokio::test]
    async fn a_window_bounds_what_one_refill_reads() {
        let mut layer = Layer::new();
        for n in 0..2_000 {
            layer.apply(WalOp::Upsert(Document {
                id: format!("d{n:05}"),
                vector: vec![n as f32; DIM],
                attrs: Some(serde_json::json!({ "n": n })),
                sparse: Some(vec![(n, 1.0)]),
            }));
        }
        for n in 0..700 {
            layer.apply(WalOp::Delete(format!("t{n:05}")));
        }
        let store = store_of(std::slice::from_ref(&layer), &[2]).await;
        let budget = ReadBudget::unlimited();
        let policy = FetchPolicy::local();
        let window = 16 * 1024;
        let mut input = open_all(&store, 1, &budget, &policy, window)
            .await
            .remove(0);
        let mut largest = 0;
        let mut entries = 0;
        loop {
            let before = budget.used();
            let Some(_) = input.next().await.unwrap() else {
                break;
            };
            entries += 1;
            largest = largest.max(budget.used() - before);
        }
        assert_eq!(entries, 2_700);
        // One window, plus one block that crossed it, plus the planner's
        // gaps (64 KiB each under `local()`, at most two per window).
        let block = 256 * (DIM as u64 * 4 + 64);
        assert!(
            largest <= window + block + 2 * 64 * 1024,
            "a refill read {largest} bytes"
        );
        let stored: u64 = {
            let data = encode_as(&layer, DIM, 2).unwrap().len() as u64;
            let (_, sparse) = encode_parts(&layer, DIM, 2, &AttrSchema::free_form()).unwrap();
            data + sparse.map_or(0, |s| s.content_length() as u64)
        };
        assert!(
            budget.used() <= stored + 64 * 1024 * 4,
            "read {}",
            budget.used()
        );
    }

    #[tokio::test]
    async fn a_segment_collected_after_it_was_opened_is_missing() {
        let mut layer = Layer::new();
        for n in 0..600 {
            layer.apply(WalOp::Upsert(Document {
                id: format!("d{n:04}"),
                vector: vec![n as f32; DIM],
                attrs: None,
                sparse: None,
            }));
        }
        let store = store_of(std::slice::from_ref(&layer), &[2]).await;
        let budget = ReadBudget::unlimited();
        let policy = FetchPolicy::local();
        let mut input = open_all(&store, 1, &budget, &policy, SMALL_WINDOW)
            .await
            .remove(0);
        assert!(matches!(input, Input::Ranged(_)));
        store
            .delete(&segment_path("ns", &segment_id(1, 1)))
            .await
            .unwrap();
        let err = input.next().await.err().expect("the window is gone");
        assert!(segment::is_missing(&err), "{err:?}");
    }

    #[tokio::test]
    async fn a_sparse_directory_with_a_huge_offset_is_an_error_not_a_panic() {
        let mut layer = Layer::new();
        for n in 0..600u32 {
            layer.apply(WalOp::Upsert(Document {
                id: format!("d{n:04}"),
                vector: vec![n as f32; DIM],
                attrs: None,
                sparse: Some(vec![(n, 1.0)]),
            }));
        }
        let store = store_of(std::slice::from_ref(&layer), &[2]).await;
        let path = crate::sparse::sparse_path("ns", &segment_id(1, 1));
        let mut bytes = store
            .get(&path)
            .await
            .unwrap()
            .bytes()
            .await
            .unwrap()
            .to_vec();
        // Row 255's directory entry, the last of the first block: a term
        // count of 1 and an offset near the end of the u64 range.
        let entry = 12 + 255 * 10;
        bytes[entry + 2..entry + 10].copy_from_slice(&(u64::MAX - 100).to_le_bytes());
        store.put(&path, bytes.into()).await.unwrap();

        let budget = ReadBudget::unlimited();
        let policy = FetchPolicy::local();
        let mut input = open_all(&store, 1, &budget, &policy, SMALL_WINDOW)
            .await
            .remove(0);
        let err = input.next().await.err().expect("damage is an error");
        assert!(matches!(err, ItzmaError::CorruptSegment { .. }), "{err:?}");
    }

    #[tokio::test]
    async fn an_input_out_of_order_is_refused() {
        let docs: Vec<Document> = ["b", "a"]
            .iter()
            .map(|id| Document {
                id: (*id).into(),
                vector: vec![0.0; DIM],
                attrs: None,
                sparse: None,
            })
            .collect();
        let input = Input::Whole(WholeInput::from_parts_unchecked("forged", docs, vec![]));
        let err = merged(vec![input], false).await.unwrap_err();
        assert!(
            matches!(&err, ItzmaError::CorruptSegment { path, .. } if path == "forged"),
            "{err:?}"
        );
    }

    #[tokio::test]
    async fn an_id_both_document_and_tombstone_in_one_input_is_refused() {
        let docs = vec![Document {
            id: "a".into(),
            vector: vec![0.0; DIM],
            attrs: None,
            sparse: None,
        }];
        let input = Input::Whole(WholeInput::from_parts_unchecked(
            "forged",
            docs,
            vec!["a".into()],
        ));
        let err = merged(vec![input], false).await.unwrap_err();
        assert!(
            matches!(&err, ItzmaError::CorruptSegment { reason, .. }
                if reason.contains("both a document and a tombstone")),
            "{err:?}"
        );
    }
}