moraine 0.6.3

A SlateDB-backed catalog for DuckLake
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
//! Index entry maintenance: turning writer-supplied entries into staged
//! `index` writes, with commit-time uniqueness enforcement. A unique
//! entry's store key is the value, so two commits inserting the same value
//! collide on write-write detection.

use std::{
    collections::{HashMap, HashSet, VecDeque},
    ops::Bound,
    time::{Duration, Instant},
};

use bytes::Bytes;
use futures::{Stream, StreamExt, TryStreamExt, stream};
use slatedb::DbTransaction;
use tracing::warn;

use crate::{
    data_file::ScopedReadTally,
    error::{Error, Result},
    store::{
        StagedBytes,
        handle::{ReadHandle, ScanOrder, ScanShape},
        index_encoding::{CanonicalKey, NullOrder, non_null_flag_key},
        key::{
            IndexKey, IndexKind, Key, encode_index_entry, index_index_prefix,
            index_multi_value_prefix, index_value_above, index_value_body, index_value_suffix,
        },
    },
};

/// One index-entry mutation accumulated during a commit closure, resolved
/// against the store when the batch is staged.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct StagedIndexEntry {
    /// The index this entry belongs to.
    pub(crate) index_id: u64,
    /// Whether the index is unique.
    pub(crate) unique: bool,
    /// The final encoded SlateDB entry key.
    pub(crate) key: Bytes,
    /// The row the entry points at.
    pub(crate) row_id: u64,
    /// Whether this removes the entry rather than adds it.
    pub(crate) delete: bool,
    /// Whether the index is still building; a collision then poisons it
    /// instead of failing the commit.
    pub(crate) building: bool,
}

/// A unique entry's value is the holding row id, big-endian.
fn decode_row_id(bytes: &[u8]) -> Result<u64> {
    let array: [u8; 8] = bytes.try_into().map_err(|_| {
        Error::Corruption(format!(
            "index entry value is {} bytes, expected 8",
            bytes.len()
        ))
    })?;
    Ok(u64::from_be_bytes(array))
}

/// Uniqueness probes in flight at once; sized for a remote object store.
const UNIQUENESS_PROBE_CONCURRENCY: usize = 1024;

/// Ready keys sharing one transactional scan; pending sources flush partial
/// batches.
const PROBE_BATCH_SIZE: usize = 128;

/// Additions derived while the deletion phase is still draining. Once full,
/// backpressure pauses addition sources without holding up deletion staging.
const ADDITION_PREFETCH: usize = 512;

/// The most equality-index entries one commit may derive.
const MAX_INDEX_ENTRIES_PER_COMMIT: usize = 1_000_000;

/// The most encoded equality-index key and value bytes one commit may stage.
const MAX_INDEX_STAGED_BYTES_PER_COMMIT: u64 = 64 * 1024 * 1024;

#[derive(Default)]
struct IndexCommitBudget {
    entries: usize,
    staged_bytes: u64,
}

impl IndexCommitBudget {
    fn with_entries(entries: usize) -> Result<Self> {
        let mut budget = Self::default();
        budget.observe_entry(entries)?;
        Ok(budget)
    }

    fn observe_entry(&mut self, additional: usize) -> Result<()> {
        self.entries = self.entries.saturating_add(additional);
        if self.entries > MAX_INDEX_ENTRIES_PER_COMMIT {
            return Err(oversized_commit(self.entries));
        }
        Ok(())
    }

    fn stage_bytes(&mut self, additional: u64) -> Result<()> {
        let staged = self.staged_bytes.saturating_add(additional);
        if staged > MAX_INDEX_STAGED_BYTES_PER_COMMIT {
            return Err(oversized_staged_bytes(staged));
        }
        self.staged_bytes = staged;
        Ok(())
    }
}

/// What staging a batch's entries produced: the indexes a collision
/// poisoned, and the bytes the entries put on the batch.
pub(crate) struct StagedEntries {
    /// Definitions a duplicate poisoned, sorted and deduplicated.
    pub(crate) poisoned: Vec<u64>,
    /// Deferred definitions whose SQL additions committed without entries.
    pub(crate) deferred: Vec<u64>,
    /// Key and value bytes staged, deletes included.
    pub(crate) bytes: u64,
    /// Inline chunk-directory repairs derived along the way, handed back
    /// rather than staged so the caller can order them below the batch's
    /// own directory writes.
    pub(crate) locator_writes: Vec<crate::transaction::commit::StagedWrite>,
    pub(crate) metrics: IndexMaintenanceMetrics,
}

/// Per-commit equality-index work. Derivation and probe windows overlap.
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct IndexMaintenanceMetrics {
    pub(crate) deletion_derivation: Duration,
    pub(crate) addition_derivation: Duration,
    pub(crate) probe_window: Duration,
    pub(crate) probe_service: Duration,
    pub(crate) staging: Duration,
    pub(crate) scoped_read: ScopedReadTally,
    pub(crate) additions: u64,
    pub(crate) deletions: u64,
    pub(crate) unique_probes: u64,
    pub(crate) probe_hits: u64,
    pub(crate) probe_misses: u64,
    pub(crate) probe_peak_in_flight: u64,
    pub(crate) probes_completed_during_deletions: u64,
}

/// One unique put awaiting its committed-state probe.
struct PendingProbe {
    /// The entry's encoded store key.
    key: Bytes,
    /// The row claiming the value.
    row_id: u64,
    /// The index the claim belongs to.
    index_id: u64,
    /// Whether that index is still building.
    building: bool,
}

/// Work one streamed index entry requires.
enum ProbePlan {
    /// Stage a non-unique put without a read.
    Put(Bytes),
    /// Read committed state before deciding whether to stage the put.
    Probe(PendingProbe),
    /// A repeated claim by the same row needs no write.
    Noop,
    /// Two rows in this batch claim one unique value.
    Collision {
        index_id: u64,
        building: bool,
        claimant: u64,
        holder: u64,
    },
    /// Input, limit, or derivation failure at this stream position.
    Failure(Error),
}

/// A completed plan, carrying a probe's value when it needed a read.
enum ReadyAddition {
    Put(Bytes),
    Probed(CompletedProbe),
    Poison(u64),
}

struct CompletedProbe {
    probe: PendingProbe,
    present: Option<Bytes>,
    service: Duration,
    completed: Instant,
}

/// Sequential planning state. Claims are recorded before probes start, so
/// concurrent reads cannot admit duplicate values from the same commit.
#[derive(Default)]
struct ProbePlanner {
    claimed: HashMap<Bytes, u64>,
    span: Option<KeySpan>,
}

#[derive(Clone)]
struct KeySpan {
    first: Bytes,
    last: Bytes,
}

impl KeySpan {
    fn include(span: &mut Option<Self>, key: &Bytes) {
        match span {
            Some(span) => {
                if *key < span.first {
                    span.first = key.clone();
                }
                if *key > span.last {
                    span.last = key.clone();
                }
            }
            None => {
                *span = Some(Self {
                    first: key.clone(),
                    last: key.clone(),
                });
            }
        }
    }

    fn overlaps(&self, other: &Self) -> bool {
        self.first <= other.last && other.first <= self.last
    }
}

struct ProbeBatch {
    probes: Vec<PendingProbe>,
    shared_scan: bool,
}

impl ProbePlanner {
    fn plan(
        &mut self,
        entry: Result<StagedIndexEntry>,
        budget: &mut IndexCommitBudget,
    ) -> ProbePlan {
        let entry = match entry {
            Ok(entry) => entry,
            Err(error) => return ProbePlan::Failure(error),
        };
        if let Err(error) = budget.observe_entry(1) {
            return ProbePlan::Failure(error);
        }
        let value_bytes = usize::from(entry.unique) * size_of::<u64>();
        if let Err(error) = budget.stage_bytes(
            u64::try_from(entry.key.len().saturating_add(value_bytes)).unwrap_or(u64::MAX),
        ) {
            return ProbePlan::Failure(error);
        }
        if entry.delete {
            return ProbePlan::Failure(Error::Corruption(
                "index put stream contains a deletion".to_owned(),
            ));
        }
        if !entry.unique {
            return ProbePlan::Put(entry.key);
        }

        if let Some(&holder) = self.claimed.get(&entry.key) {
            return if holder == entry.row_id {
                ProbePlan::Noop
            } else {
                ProbePlan::Collision {
                    index_id: entry.index_id,
                    building: entry.building,
                    claimant: entry.row_id,
                    holder,
                }
            };
        }
        self.claimed.insert(entry.key.clone(), entry.row_id);
        KeySpan::include(&mut self.span, &entry.key);

        ProbePlan::Probe(PendingProbe {
            key: entry.key,
            row_id: entry.row_id,
            index_id: entry.index_id,
            building: entry.building,
        })
    }
}

/// Which state a rejected claim collided with: the commit's own additions,
/// or a row the index already held.
#[derive(Clone, Copy)]
enum Collided {
    InBatch,
    Committed,
}

/// A collision's verdict: fail the commit, or poison the building index.
fn collision(
    index_id: u64,
    building: bool,
    claimant: u64,
    holder: u64,
    collided: Collided,
) -> Result<Option<u64>> {
    if building {
        Ok(Some(index_id))
    } else {
        Err(unique_violation(index_id, claimant, holder, collided))
    }
}

async fn resolve_probe(transaction: &DbTransaction, probe: PendingProbe) -> Result<CompletedProbe> {
    let started = Instant::now();
    let present = transaction.get(&probe.key).await?;
    Ok(CompletedProbe {
        probe,
        present,
        service: started.elapsed(),
        completed: Instant::now(),
    })
}

async fn resolve_probes(
    transaction: &DbTransaction,
    batch: ProbeBatch,
) -> Result<Vec<CompletedProbe>> {
    let mut probes = batch.probes;
    if probes.len() == 1
        && let Some(probe) = probes.pop()
    {
        return Ok(vec![resolve_probe(transaction, probe).await?]);
    }
    if !batch.shared_scan {
        let reads: Vec<_> = probes
            .into_iter()
            .map(|probe| resolve_probe(transaction, probe))
            .collect();
        return stream::iter(reads)
            .buffer_unordered(PROBE_BATCH_SIZE)
            .try_collect()
            .await;
    }
    let started = Instant::now();
    let keys: Vec<_> = probes.iter().map(|probe| probe.key.clone()).collect();
    let values = crate::store::handle::probes::get_many(transaction, &keys).await?;
    let completed = Instant::now();
    let mut resolutions: Vec<_> = probes
        .into_iter()
        .zip(values)
        .map(|(probe, present)| CompletedProbe {
            probe,
            present,
            service: Duration::ZERO,
            completed,
        })
        .collect();
    // Count the shared read interval once rather than once per key.
    if let Some(first) = resolutions.first_mut() {
        first.service = completed.saturating_duration_since(started);
    }
    Ok(resolutions)
}

fn plan_probe_batch(
    additions: Vec<Result<StagedIndexEntry>>,
    planner: &mut ProbePlanner,
    budget: &mut IndexCommitBudget,
    ready: &mut VecDeque<ReadyAddition>,
    metrics: &mut IndexMaintenanceMetrics,
    in_flight: &mut usize,
    first_probe: &mut Option<Instant>,
) -> Result<ProbeBatch> {
    let occupied = planner.span.clone();
    let capacity = additions.len();
    let mut span = None;
    let mut probes = Vec::new();
    for addition in additions {
        metrics.additions = metrics.additions.saturating_add(1);
        match planner.plan(addition, budget) {
            ProbePlan::Put(key) => ready.push_back(ReadyAddition::Put(key)),
            ProbePlan::Probe(probe) => {
                if probes.is_empty() {
                    probes.reserve_exact(capacity);
                }
                KeySpan::include(&mut span, &probe.key);
                probes.push(probe);
            }
            ProbePlan::Noop => {}
            ProbePlan::Collision {
                index_id,
                building,
                claimant,
                holder,
            } => {
                if let Some(index_id) =
                    collision(index_id, building, claimant, holder, Collided::InBatch)?
                {
                    ready.push_back(ReadyAddition::Poison(index_id));
                }
            }
            ProbePlan::Failure(error) => return Err(error),
        }
    }
    if !probes.is_empty() {
        first_probe.get_or_insert_with(Instant::now);
        let count = u64::try_from(probes.len()).unwrap_or(u64::MAX);
        metrics.unique_probes = metrics.unique_probes.saturating_add(count);
        *in_flight += probes.len();
        metrics.probe_peak_in_flight = metrics
            .probe_peak_in_flight
            .max(u64::try_from(*in_flight).unwrap_or(u64::MAX));
    }
    let shared_scan = match (&occupied, &span) {
        (Some(occupied), Some(span)) => !occupied.overlaps(span),
        _ => true,
    };
    Ok(ProbeBatch {
        probes,
        shared_scan,
    })
}

fn complete_probes(
    resolutions: Result<Vec<CompletedProbe>>,
    ready: &mut VecDeque<ReadyAddition>,
    in_flight: &mut usize,
) -> Result<usize> {
    let resolutions = resolutions?;
    let count = resolutions.len();
    *in_flight -= count;
    ready.extend(resolutions.into_iter().map(ReadyAddition::Probed));
    Ok(count)
}

/// Drops a unique deletion whose entry is held by some other row.
///
/// A unique entry's key is the value alone, so deleting it by key removes
/// whichever row holds that value — not necessarily the row being removed.
/// The stored row id is what says the two are the same, and a derivation
/// that names an already-removed row must not take the entry a later
/// insert of the same value put there.
fn guarded_deletions<'a, D>(
    reader: ReadHandle<'a>,
    deletes: D,
) -> impl Stream<Item = Result<StagedIndexEntry>> + 'a
where
    D: Stream<Item = Result<StagedIndexEntry>> + 'a,
{
    deletes
        .map(move |entry| async move {
            let entry = entry?;
            if !entry.unique {
                return Ok(Some(entry));
            }
            let Some(bytes) = reader.get(entry.key.clone()).await.map_err(Error::from)? else {
                return Ok(None);
            };
            Ok((decode_row_id(&bytes)? == entry.row_id).then_some(entry))
        })
        .buffer_unordered(UNIQUENESS_PROBE_CONCURRENCY)
        .try_filter_map(|entry| std::future::ready(Ok(entry)))
}

fn stage_probe_put(
    db_tx: &DbTransaction,
    staged: &mut StagedBytes,
    key: Bytes,
    row_id: Option<u64>,
) -> Result<()> {
    let value_bytes = row_id.map_or(0, |_| size_of::<u64>());
    staged.add(key.len(), value_bytes);
    match row_id {
        Some(row_id) => db_tx.put(key, row_id.to_be_bytes()),
        None => db_tx.put(key, []),
    }
    .map_err(Error::from)
}

/// Resolves accumulated entries onto `db_tx`, enforcing uniqueness at
/// commit. Deletes are staged first. For each unique put: present with a
/// different row id → [`Error::Constraint`]; present with the same row id
/// → no-op; absent → staged. Duplicates within the commit are caught in
/// memory.
pub(crate) async fn stage_index_entries(
    db_tx: &DbTransaction,
    entries: Vec<StagedIndexEntry>,
    shared_scan: bool,
) -> Result<StagedEntries> {
    let entry_count = entries.len();
    if entry_count > MAX_INDEX_ENTRIES_PER_COMMIT {
        warn!(
            staged = entry_count,
            limit = MAX_INDEX_ENTRIES_PER_COMMIT,
            "refusing an oversized commit"
        );
        return Err(oversized_commit(entry_count));
    }
    let encoded_bytes = entries.iter().fold(0_u64, |total, entry| {
        let value_bytes = usize::from(entry.unique && !entry.delete) * size_of::<u64>();
        total.saturating_add(
            u64::try_from(entry.key.len().saturating_add(value_bytes)).unwrap_or(u64::MAX),
        )
    });
    if encoded_bytes > MAX_INDEX_STAGED_BYTES_PER_COMMIT {
        return Err(oversized_staged_bytes(encoded_bytes));
    }

    let (deletes, puts): (Vec<_>, Vec<_>) = entries.into_iter().partition(|entry| entry.delete);
    let deletes = stream::iter(deletes.into_iter().map(Ok));
    let puts = stream::iter(puts.into_iter().map(Ok));
    stage_index_entry_stream(db_tx, deletes, puts, 0, shared_scan).await
}

/// Consumes deletion entries, then additions, as streams. Unique probes form
/// one continuously replenished window; successful reads stage in
/// completion order, and the first fatal result aborts.
#[allow(clippy::too_many_lines)]
pub(crate) async fn stage_index_entry_stream<D, S>(
    db_tx: &DbTransaction,
    deletes: D,
    entries: S,
    prior_entry_count: usize,
    shared_scan: bool,
) -> Result<StagedEntries>
where
    D: Stream<Item = Result<StagedIndexEntry>>,
    S: Stream<Item = Result<StagedIndexEntry>>,
{
    let started = Instant::now();
    let mut staged = StagedBytes::default();
    let mut deleted_unique = HashSet::new();
    let mut deleted_span = None;
    let mut budget = IndexCommitBudget::with_entries(prior_entry_count)?;
    let reader = ReadHandle::Tx(db_tx);
    let mut deletes = std::pin::pin!(guarded_deletions(reader, deletes));
    let mut entries = std::pin::pin!(entries.ready_chunks(PROBE_BATCH_SIZE));
    let mut ready = VecDeque::with_capacity(ADDITION_PREFETCH);
    let mut additions_done = false;
    let mut planner = ProbePlanner::default();

    let mut probes = futures::stream::FuturesUnordered::new();
    let mut in_flight = 0;
    let mut metrics = IndexMaintenanceMetrics::default();
    let mut first_probe = None;
    let mut last_probe_completion = None;

    loop {
        let buffered = ready.len().saturating_add(in_flight);
        let deletion = if buffered > ADDITION_PREFETCH - PROBE_BATCH_SIZE {
            deletes.next().await
        } else {
            tokio::select! {
                biased;
                deletion = deletes.next() => deletion,
                resolution = probes.next(), if !probes.is_empty() => {
                    if let Some(resolution) = resolution {
                        let count = complete_probes(resolution, &mut ready, &mut in_flight)?;
                        metrics.probes_completed_during_deletions = metrics
                            .probes_completed_during_deletions
                            .saturating_add(u64::try_from(count).unwrap_or(u64::MAX));
                    }
                    continue;
                },
                addition = entries.next(), if !additions_done => if let Some(addition) = addition {
                    let mut pending = plan_probe_batch(
                        addition, &mut planner, &mut budget, &mut ready,
                        &mut metrics, &mut in_flight, &mut first_probe,
                    )?;
                    pending.shared_scan = false;
                    if !pending.probes.is_empty() {
                        probes.push(resolve_probes(db_tx, pending));
                    }
                    continue;
                } else {
                    additions_done = true;
                    metrics.addition_derivation = started.elapsed();
                        continue;
                }
            }
        };
        let Some(entry) = deletion else {
            break;
        };
        let entry = entry?;
        budget.observe_entry(1)?;
        budget.stage_bytes(u64::try_from(entry.key.len()).unwrap_or(u64::MAX))?;
        if !entry.delete {
            return Err(Error::Corruption(
                "index deletion stream contains a put".to_owned(),
            ));
        }
        if entry.unique {
            KeySpan::include(&mut deleted_span, &entry.key);
            deleted_unique.insert(entry.key.clone());
        }
        metrics.deletions = metrics.deletions.saturating_add(1);
        let stage_started = Instant::now();
        staged.add(entry.key.len(), 0);
        db_tx.delete(entry.key).map_err(Error::from)?;
        metrics.staging = metrics.staging.saturating_add(stage_started.elapsed());
    }
    metrics.deletion_derivation = started.elapsed();

    let mut poisoned = Vec::new();
    loop {
        let resolution = if let Some(resolution) = ready.pop_front() {
            Some(resolution)
        } else if additions_done || in_flight > UNIQUENESS_PROBE_CONCURRENCY - PROBE_BATCH_SIZE {
            if let Some(resolutions) = probes.next().await {
                complete_probes(resolutions, &mut ready, &mut in_flight)?;
                continue;
            }
            None
        } else {
            tokio::select! {
                biased;
                resolution = probes.next(), if !probes.is_empty() => {
                    if let Some(resolution) = resolution {
                        complete_probes(resolution, &mut ready, &mut in_flight)?;
                    }
                    continue;
                },
                addition = entries.next() => if let Some(addition) = addition {
                    let mut pending = plan_probe_batch(
                        addition, &mut planner, &mut budget, &mut ready,
                        &mut metrics, &mut in_flight, &mut first_probe,
                    )?;
                    if let Some(deleted) = &deleted_span {
                        // A batch spanning the deleted interval also overlaps its local writes.
                        if let (Some(first), Some(last)) = (
                            pending.probes.iter().map(|probe| &probe.key).min(),
                            pending.probes.iter().map(|probe| &probe.key).max(),
                        ) {
                            pending.shared_scan &= *last < deleted.first || *first > deleted.last;
                        }
                    }
                    pending.shared_scan &= shared_scan && prior_entry_count == 0;
                    if !pending.probes.is_empty() {
                        probes.push(resolve_probes(db_tx, pending));
                    }
                    continue;
                } else {
                    additions_done = true;
                    metrics.addition_derivation = started.elapsed();
                    continue;
                }
            }
        };

        let Some(resolution) = resolution else {
            break;
        };
        match resolution {
            ReadyAddition::Put(key) => {
                let stage_started = Instant::now();
                stage_probe_put(db_tx, &mut staged, key, None)?;
                metrics.staging = metrics.staging.saturating_add(stage_started.elapsed());
            }
            ReadyAddition::Probed(CompletedProbe {
                probe,
                present,
                service,
                completed,
            }) => {
                metrics.probe_service = metrics.probe_service.saturating_add(service);
                last_probe_completion = Some(completed);
                if present.is_some() {
                    metrics.probe_hits = metrics.probe_hits.saturating_add(1);
                } else {
                    metrics.probe_misses = metrics.probe_misses.saturating_add(1);
                }
                if deleted_unique.contains(&probe.key) {
                    let stage_started = Instant::now();
                    stage_probe_put(db_tx, &mut staged, probe.key, Some(probe.row_id))?;
                    metrics.staging = metrics.staging.saturating_add(stage_started.elapsed());
                } else if let Some(bytes) = present {
                    match decode_row_id(&bytes) {
                        Ok(holder) if holder == probe.row_id => {}
                        Ok(holder) => {
                            if let Some(index_id) = collision(
                                probe.index_id,
                                probe.building,
                                probe.row_id,
                                holder,
                                Collided::Committed,
                            )? {
                                poisoned.push(index_id);
                            }
                        }
                        Err(error) => return Err(error),
                    }
                } else {
                    let stage_started = Instant::now();
                    stage_probe_put(db_tx, &mut staged, probe.key, Some(probe.row_id))?;
                    metrics.staging = metrics.staging.saturating_add(stage_started.elapsed());
                }
            }
            ReadyAddition::Poison(index_id) => poisoned.push(index_id),
        }
    }

    if let (Some(first_probe), Some(last_probe_completion)) = (first_probe, last_probe_completion) {
        metrics.probe_window = last_probe_completion.saturating_duration_since(first_probe);
    }
    poisoned.sort_unstable();
    poisoned.dedup();

    Ok(StagedEntries {
        poisoned,
        deferred: Vec::new(),
        bytes: staged.0,
        locator_writes: Vec::new(),
        metrics,
    })
}

/// Records `poisoned` on the working state's definitions, so the commit's
/// ordinary entity diff stages the flag. Poisoning is terminal.
pub(crate) fn apply_poison(state: &mut crate::catalog::CatalogSnapshot, poisoned: &[u64]) {
    for index_id in poisoned {
        let Some(table_id) = state.index_tables.get(index_id).copied() else {
            continue;
        };
        if let Some(value) = state
            .indexes
            .get_mut(&table_id)
            .and_then(|indexes| indexes.get_mut(index_id))
        {
            value.poisoned = Some(true);
        }
    }
}

/// Marks ready deferred definitions as awaiting repair. A definition already
/// being repaired keeps its cursor.
pub(crate) fn apply_deferred_maintenance(
    base: &crate::catalog::CatalogSnapshot,
    state: &mut crate::catalog::CatalogSnapshot,
    deferred: &[u64],
    new_snapshot: u64,
) {
    for index_id in deferred {
        let Some(table_id) = state.index_tables.get(index_id).copied() else {
            continue;
        };
        let Some(value) = state
            .indexes
            .get_mut(&table_id)
            .and_then(|indexes| indexes.get_mut(index_id))
        else {
            continue;
        };
        if value.build_state.is_none() {
            value.begin_snapshot = new_snapshot;
            value.build_state = Some("maintaining".to_owned());
            value.build_cursor_row_id = base
                .table_stats
                .get(&table_id)
                .and_then(|stats| stats.next_row_id.checked_sub(1));
            let tail = base
                .data_files
                .get(&table_id)
                .and_then(|files| files.get_max())
                .map(|(_, file)| file);
            value.build_cursor_file = Some(tail.map_or(0, |file| file.data_file_id));
            value.build_cursor_position =
                Some(tail.map_or(0, |file| file.record_count.saturating_sub(1)));
        }
    }
}

/// The refusal for a commit deriving too many equality-index entries. The
/// text must avoid DuckLake's retry
/// substrings (`conflict`, `concurrent`, `unique`, `primary key`).
fn oversized_commit(staged: usize) -> Error {
    Error::Constraint(format!(
        "commit stages {staged} equality-index entries, above the \
         {MAX_INDEX_ENTRIES_PER_COMMIT} a single commit allows. Split the work into \
         several smaller commits."
    ))
}

fn oversized_staged_bytes(staged: u64) -> Error {
    Error::Constraint(format!(
        "commit stages {staged} encoded equality-index bytes, above the \
         {MAX_INDEX_STAGED_BYTES_PER_COMMIT} a single commit allows. Split the work into \
         several smaller commits."
    ))
}

/// A uniqueness error, naming both rows and which state the claim collided
/// with. The text must avoid DuckLake's retry substrings (`conflict`,
/// `concurrent`, `unique`, `primary key`), and names row ids rather than the
/// value, which no error on this path carries.
fn unique_violation(index_id: u64, claimant: u64, holder: u64, collided: Collided) -> Error {
    Error::Constraint(match collided {
        Collided::InBatch => format!(
            "duplicate value violates equality index {index_id}: rows {claimant} and {holder} in \
             one commit claim the same value"
        ),
        Collided::Committed => format!(
            "duplicate value violates equality index {index_id}: row {claimant} claims a value \
             held by committed row {holder}"
        ),
    })
}

/// Deletes up to `limit` orphaned entries of one dropped index inside an
/// open transaction, returning how many deletes were staged.
pub(crate) async fn reclaim_entries(
    tx: &slatedb::DbTransaction,
    index_id: u64,
    limit: usize,
    staged: &mut StagedBytes,
) -> Result<usize> {
    let mut deleted = 0;
    for kind in [IndexKind::Unique, IndexKind::Multi] {
        if deleted >= limit {
            break;
        }
        let (batch, _) =
            reclaim_entries_from(tx, kind, index_id, limit - deleted, None, staged).await?;
        deleted += batch;
    }

    Ok(deleted)
}

/// Deletes up to `limit` entries of one dropped index of one kind,
/// resuming at `start_from` when given, and returns how many were staged
/// alongside the last key deleted.
pub(crate) async fn reclaim_entries_from(
    tx: &slatedb::DbTransaction,
    kind: IndexKind,
    index_id: u64,
    limit: usize,
    start_from: Option<&[u8]>,
    staged: &mut StagedBytes,
) -> Result<(usize, Option<Vec<u8>>)> {
    let prefix = index_index_prefix(kind, index_id);
    // `scan_prefix` takes its bounds as a suffix of the prefix.
    let suffix = match start_from {
        Some(key) if key.len() >= prefix.len() => key[prefix.len()..].to_vec(),
        _ => Vec::new(),
    };

    let mut iter = ReadHandle::Tx(tx)
        .scan_prefix(prefix, suffix.., ScanShape::Bulk)
        .await?;
    let mut deleted = 0;
    let mut last: Option<Bytes> = None;
    while deleted < limit {
        match iter.next().await? {
            Some(entry) => {
                staged.add(entry.key.len(), 0);
                last = Some(entry.key.clone());
                tx.delete(entry.key)?;
                deleted += 1;
            }
            None => break,
        }
    }

    Ok((deleted, last.map(|key| key.to_vec())))
}

/// Drains a scan over `multi` entries into their row ids, which live in
/// the key. `context` names the scan in a corruption error.
async fn collect_multi_row_ids(iter: &mut slatedb::DbIterator, context: &str) -> Result<Vec<u64>> {
    let mut row_ids = Vec::new();
    while let Some(entry) = iter.next().await.map_err(Error::from)? {
        match Key::decode(&entry.key)? {
            Key::Index(IndexKey::Multi { row_id, .. }) => row_ids.push(row_id),
            other => {
                return Err(Error::Corruption(format!(
                    "non-multi key in {context}: {other:?}"
                )));
            }
        }
    }

    Ok(row_ids)
}

/// The row ids holding one indexed value: a point-get for a unique index,
/// an ascending prefix scan for a non-unique one.
pub(crate) async fn lookup_row_ids(
    reader: ReadHandle<'_>,
    index_id: u64,
    unique: bool,
    key: &CanonicalKey,
) -> Result<Vec<u64>> {
    if unique {
        let entry_key = encode_index_entry(index_id, true, key, 0);
        return match reader.get(entry_key).await.map_err(Error::from)? {
            Some(bytes) => Ok(vec![decode_row_id(&bytes)?]),
            None => Ok(Vec::new()),
        };
    }

    let prefix = index_multi_value_prefix(index_id, key);
    let mut iter = reader
        .scan_prefix(prefix, .., ScanShape::Probe)
        .await
        .map_err(Error::from)?;

    collect_multi_row_ids(&mut iter, "index scan").await
}

/// The row ids whose indexed value falls between `lower` and `upper`, in the
/// requested scan order. The bounds are canonical values, already encoded
/// in the columns' declared directions.
pub(crate) async fn range_row_ids(
    reader: ReadHandle<'_>,
    index_id: u64,
    unique: bool,
    leading_nulls: NullOrder,
    lower: Bound<CanonicalKey>,
    upper: Bound<CanonicalKey>,
    order: ScanOrder,
) -> Result<Vec<u64>> {
    let kind = if unique {
        IndexKind::Unique
    } else {
        IndexKind::Multi
    };
    let prefix = index_index_prefix(kind, index_id);

    let suffix = |canon: &CanonicalKey| index_value_suffix(kind, index_id, canon);
    let above = |canon: &CanonicalKey| index_value_above(kind, index_id, canon);

    // A comparison never matches a NULL, so an open side stops at the
    // leading column's non-null region.
    let non_null = non_null_flag_key(leading_nulls);
    let past_non_null = above(&non_null).map_or(Bound::Unbounded, Bound::Excluded);

    let start = match lower {
        Bound::Included(canon) => Bound::Included(suffix(&canon)),
        // Skip every entry sharing the bound value: start above them all.
        Bound::Excluded(canon) => match above(&canon) {
            Some(above) => Bound::Included(above),
            None => Bound::Excluded(suffix(&canon)),
        },
        Bound::Unbounded => Bound::Included(index_value_body(kind, index_id, &non_null)),
    };
    let end = match upper {
        // Include every entry sharing the bound value: end above them all.
        Bound::Included(canon) => match above(&canon) {
            Some(above) => Bound::Excluded(above),
            None => past_non_null,
        },
        Bound::Excluded(canon) => Bound::Excluded(suffix(&canon)),
        Bound::Unbounded => past_non_null,
    };

    let mut iter = reader
        .scan_prefix_ordered(prefix, (start, end), ScanShape::Probe, order)
        .await
        .map_err(Error::from)?;
    if !unique {
        return collect_multi_row_ids(&mut iter, "index range scan").await;
    }
    let mut row_ids = Vec::new();
    while let Some(entry) = iter.next().await.map_err(Error::from)? {
        row_ids.push(decode_row_id(entry.value.as_ref())?);
    }

    Ok(row_ids)
}

/// The row ids whose leading indexed columns match `prefix`, a canonical
/// key over a leading run of `= value` and `IS NULL` predicates. A row with
/// any NULL indexed column is stored multi-shaped, so the `multi` subrange
/// is scanned.
pub(crate) async fn null_prefix_row_ids(
    reader: ReadHandle<'_>,
    index_id: u64,
    prefix: &CanonicalKey,
    order: ScanOrder,
) -> Result<Vec<u64>> {
    let mut scan_prefix = index_multi_value_prefix(index_id, prefix);
    // Dropping the exact-value terminator turns the bytes into a true
    // leading prefix.
    scan_prefix.pop();

    let mut iter = reader
        .scan_prefix_ordered(scan_prefix, .., ScanShape::Probe, order)
        .await
        .map_err(Error::from)?;

    collect_multi_row_ids(&mut iter, "null-prefix scan").await
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use object_store::memory::InMemory;
    use slatedb::IsolationLevel;

    use super::*;
    use crate::store::open::StoreBuilder;

    #[test]
    fn overlapping_probe_batches_keep_point_reads() {
        let mut planner = ProbePlanner::default();
        let mut budget = IndexCommitBudget::default();
        let mut ready = VecDeque::new();
        let mut metrics = IndexMaintenanceMetrics::default();
        let mut in_flight = 0;
        let mut first = None;
        for (keys, shared) in [
            (vec![10u64, 20], true),
            (vec![0, 5], true),
            (vec![7, 30], false),
            (vec![31, 40], true),
        ] {
            let additions = keys
                .into_iter()
                .map(|key| {
                    Ok(StagedIndexEntry {
                        key: Bytes::copy_from_slice(&key.to_be_bytes()),
                        index_id: 1,
                        unique: true,
                        row_id: key,
                        delete: false,
                        building: false,
                    })
                })
                .collect();
            let batch = plan_probe_batch(
                additions,
                &mut planner,
                &mut budget,
                &mut ready,
                &mut metrics,
                &mut in_flight,
                &mut first,
            )
            .unwrap();
            assert_eq!(batch.shared_scan, shared);
        }
    }

    /// A normal fact flush carries 250 source keys plus a handful of newly
    /// discovered reference keys. They must all enter the first probe window:
    /// leaving one or two for a second window charges a full remote-read tail
    /// for almost no work.
    #[test]
    fn normal_flush_unique_probes_share_one_window() {
        const FACTS_AND_REFERENCES: usize = 258;
        let concurrency = std::hint::black_box(UNIQUENESS_PROBE_CONCURRENCY);

        assert!(
            concurrency >= FACTS_AND_REFERENCES,
            "{FACTS_AND_REFERENCES} probes exceed the concurrency window of \
             {concurrency}"
        );
    }

    /// A stream larger than the former 1,024-entry group stays one rolling
    /// probe window rather than draining at an artificial boundary.
    #[tokio::test]
    async fn unique_probe_stream_crosses_the_old_group_boundary() {
        const ENTRIES: usize = 1_025;
        let (db, _) = StoreBuilder::new("continuous-probes", Arc::new(InMemory::new()))
            .open_writer()
            .await
            .unwrap();
        let tx = db.begin(IsolationLevel::Snapshot).await.unwrap();
        let entries = stream::iter((0..ENTRIES).map(|position| {
            let row_id = u64::try_from(position).unwrap();
            Ok(StagedIndexEntry {
                index_id: 1,
                unique: true,
                key: Bytes::copy_from_slice(&row_id.to_be_bytes()),
                row_id,
                delete: false,
                building: false,
            })
        }));

        let staged = stage_index_entry_stream(&tx, stream::empty(), entries, 0, true)
            .await
            .unwrap();

        assert_eq!(staged.bytes, u64::try_from(ENTRIES * 16).unwrap());
        tx.rollback();
        db.close().await.unwrap();
    }

    /// A unique deletion removes the entry of the row it names, and only
    /// that row's.
    ///
    /// The store key is the value alone, so a deletion derived for a row
    /// that no longer holds the value would otherwise take the entry from
    /// whichever row does.
    #[tokio::test]
    async fn a_unique_deletion_removes_only_the_named_row_s_entry() {
        let (db, _) = StoreBuilder::new("guarded-deletions", Arc::new(InMemory::new()))
            .open_writer()
            .await
            .unwrap();
        let key = Bytes::from_static(b"one value");

        let tx = db.begin(IsolationLevel::Snapshot).await.unwrap();
        tx.put(key.clone(), 3u64.to_be_bytes()).unwrap();
        tx.commit().await.unwrap();

        let deletion = |row_id: u64, key: Bytes| {
            stream::once(async move {
                Ok(StagedIndexEntry {
                    index_id: 1,
                    unique: true,
                    key,
                    row_id,
                    delete: true,
                    building: false,
                })
            })
        };

        let tx = db.begin(IsolationLevel::Snapshot).await.unwrap();
        let staged =
            stage_index_entry_stream(&tx, deletion(0, key.clone()), stream::empty(), 0, true)
                .await
                .unwrap();
        assert_eq!(staged.metrics.deletions, 0);
        tx.commit().await.unwrap();

        let tx = db.begin(IsolationLevel::Snapshot).await.unwrap();
        assert!(
            ReadHandle::Tx(&tx)
                .get(key.clone())
                .await
                .unwrap()
                .is_some(),
            "row 3's entry survives a deletion derived for row 0"
        );
        let staged =
            stage_index_entry_stream(&tx, deletion(3, key.clone()), stream::empty(), 0, true)
                .await
                .unwrap();
        assert_eq!(staged.metrics.deletions, 1);
        tx.commit().await.unwrap();

        let tx = db.begin(IsolationLevel::Snapshot).await.unwrap();
        assert!(ReadHandle::Tx(&tx).get(key).await.unwrap().is_none());
        tx.rollback();

        db.close().await.unwrap();
    }

    /// Addition sources may fill the bounded prefetch while a deletion is
    /// pending, but the transaction must still stage the delete first.
    #[tokio::test]
    async fn addition_prefetch_preserves_the_deletion_phase_boundary() {
        let (db, _) = StoreBuilder::new("addition-prefetch", Arc::new(InMemory::new()))
            .open_writer()
            .await
            .unwrap();
        let key = Bytes::from_static(b"delete-before-add");
        // The entry the deletion removes: a unique deletion is staged only
        // for the row the committed entry names.
        let seed = db.begin(IsolationLevel::Snapshot).await.unwrap();
        seed.put(key.clone(), 7_u64.to_be_bytes()).unwrap();
        seed.commit().await.unwrap();

        let tx = db.begin(IsolationLevel::Snapshot).await.unwrap();
        let (release_deletion, deletion_released) = tokio::sync::oneshot::channel();
        let deletion_key = key.clone();
        let deletions = stream::once(async move {
            deletion_released.await.unwrap();
            Ok(StagedIndexEntry {
                index_id: 1,
                unique: true,
                key: deletion_key,
                row_id: 7,
                delete: true,
                building: false,
            })
        });
        let (addition_polled, observed_addition) = tokio::sync::oneshot::channel();
        let addition_key = key.clone();
        let additions = stream::once(async move {
            addition_polled.send(()).unwrap();
            Ok(StagedIndexEntry {
                index_id: 1,
                unique: true,
                key: addition_key,
                row_id: 7,
                delete: false,
                building: false,
            })
        });

        {
            let staging = stage_index_entry_stream(&tx, deletions, additions, 0, true);
            let mut staging = std::pin::pin!(staging);
            tokio::select! {
                _ = &mut staging => panic!("staging finished before deletion release"),
                observed = observed_addition => observed.unwrap(),
            }
            tokio::time::sleep(std::time::Duration::from_millis(25)).await;
            release_deletion.send(()).unwrap();
            let staged = staging.as_mut().await.unwrap();
            assert_eq!(staged.metrics.unique_probes, 1);
            assert_eq!(staged.metrics.probes_completed_during_deletions, 1);
            assert_eq!(staged.metrics.additions, 1);
            assert_eq!(staged.metrics.deletions, 1);
        }

        assert_eq!(
            tx.get(&key).await.unwrap(),
            Some(Bytes::copy_from_slice(&7_u64.to_be_bytes()))
        );
        tx.rollback();
        db.close().await.unwrap();
    }

    /// The oversized-commit refusal must be terminal: DuckLake re-runs a
    /// commit whose error text carries any of four substrings, and
    /// re-running this one only reaches the same answer more slowly.
    #[test]
    fn oversized_commit_refusal_avoids_ducklake_retry_substrings() {
        for text in [
            oversized_commit(1_000_001).to_string(),
            oversized_staged_bytes(64 * 1024 * 1024 + 1).to_string(),
            unique_violation(24, 900, 17, Collided::Committed).to_string(),
            unique_violation(24, 900, 901, Collided::InBatch).to_string(),
        ] {
            for substring in ["conflict", "concurrent", "unique", "primary key"] {
                assert!(
                    !text.contains(substring),
                    "{text:?} contains DuckLake's retry substring {substring:?}"
                );
            }
        }
    }

    /// A rejection names both rows and the state it collided with, so an
    /// entry naming a row that is no longer live is one query away.
    #[test]
    fn unique_violation_names_both_rows_and_the_state_it_collided_with() {
        let committed = unique_violation(24, 900, 17, Collided::Committed).to_string();
        assert!(
            committed.contains("index 24"),
            "names the index: {committed}"
        );
        assert!(
            committed.contains("row 900"),
            "names the claimant: {committed}"
        );
        assert!(
            committed.contains("row 17"),
            "names the holder: {committed}"
        );
        assert!(
            committed.contains("committed"),
            "names the state: {committed}"
        );

        let in_batch = unique_violation(24, 900, 901, Collided::InBatch).to_string();
        assert!(in_batch.contains("900"), "names the claimant: {in_batch}");
        assert!(in_batch.contains("901"), "names the holder: {in_batch}");
        assert!(
            in_batch.contains("one commit"),
            "names the state: {in_batch}"
        );
    }

    /// It names the count, the limit, the memory it would have needed, and
    /// the one thing the caller can do about it.
    #[test]
    fn oversized_commit_refusal_is_actionable() {
        let text = oversized_commit(1_000_001).to_string();
        assert!(text.contains("1000001"), "names the count: {text}");
        assert!(text.contains("1000000"), "names the limit: {text}");
        assert!(text.contains("smaller commits"), "names the remedy: {text}");
    }

    /// The entry ceiling bounds ordinary narrow keys, while the byte
    /// ceiling stops a smaller number of unusually wide keys.
    #[test]
    fn index_commit_budget_enforces_both_dimensions() {
        let mut budget = IndexCommitBudget::default();
        assert!(budget.observe_entry(MAX_INDEX_ENTRIES_PER_COMMIT).is_ok());
        assert!(budget.observe_entry(1).is_err());

        let mut budget = IndexCommitBudget::default();
        assert!(
            budget
                .stage_bytes(MAX_INDEX_STAGED_BYTES_PER_COMMIT)
                .is_ok()
        );
        let error = budget.stage_bytes(1).unwrap_err().to_string();
        assert!(error.contains("67108864"), "names the byte limit: {error}");
        assert!(
            error.contains("smaller commits"),
            "names the remedy: {error}"
        );
    }
}