hawdb-storage 0.3.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::super::{RelationalIndexRangeScan, RelationalIndexScanDirection};
use super::{
    encode_relational_key, IndexLeafPosting, IndexPageId, RelationalIndexRootDescriptor,
    RelationalIndexShadowError, RelationalIndexShadowReader,
};
use crate::{ImmutableIndexPage, ImmutableIndexPageBody, IndexPostingPage};
use std::collections::BTreeMap;
use std::num::{NonZeroU32, NonZeroUsize};

pub const DEFAULT_RELATIONAL_INDEX_READ_PAGES: usize = 256;
pub const DEFAULT_RELATIONAL_INDEX_READ_ROWS: usize = 4096;
pub const DEFAULT_RELATIONAL_INDEX_READ_BYTES: usize = 16 * 1024 * 1024;
pub const DEFAULT_RELATIONAL_INDEX_READ_TREE_HEIGHT: u32 = 64;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RelationalIndexReadLimits {
    /// Maximum logical index pages traversed, including cache hits.
    pub max_pages: NonZeroUsize,
    /// Maximum index rows visited.
    pub max_rows: NonZeroUsize,
    /// Maximum logical index bytes traversed, including cache hits.
    pub max_bytes: NonZeroUsize,
    /// Maximum bytes fetched from index files. A zero budget permits cache hits only.
    pub max_file_bytes: usize,
    pub max_tree_height: NonZeroU32,
}

impl Default for RelationalIndexReadLimits {
    fn default() -> Self {
        Self {
            max_pages: NonZeroUsize::new(DEFAULT_RELATIONAL_INDEX_READ_PAGES)
                .expect("default relational index page limit is non-zero"),
            max_rows: NonZeroUsize::new(DEFAULT_RELATIONAL_INDEX_READ_ROWS)
                .expect("default relational index row limit is non-zero"),
            max_bytes: NonZeroUsize::new(DEFAULT_RELATIONAL_INDEX_READ_BYTES)
                .expect("default relational index byte limit is non-zero"),
            max_file_bytes: DEFAULT_RELATIONAL_INDEX_READ_BYTES,
            max_tree_height: NonZeroU32::new(DEFAULT_RELATIONAL_INDEX_READ_TREE_HEIGHT)
                .expect("default relational index tree-height limit is non-zero"),
        }
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RelationalIndexReadReport {
    pub pages_read: usize,
    pub bytes_read: usize,
    pub file_pages_read: usize,
    pub file_bytes_read: usize,
    pub cache_hits: usize,
    pub cache_misses: usize,
    pub cache_admission_rejections: usize,
    pub leaf_entries_visited: usize,
    pub matched_index_keys: usize,
    pub rows_visited: usize,
    pub stopped_early: bool,
}

impl RelationalIndexShadowReader {
    /// Visits the postings for one complete encoded index key.
    ///
    /// Values observed by `visit` are provisional until this method returns
    /// `Ok`; callers must discard them when traversal fails. Returning
    /// `false` stops before reading the remainder of a posting chain.
    pub fn visit_exact_postings(
        &self,
        table: &str,
        index: &str,
        key: &super::RelationalKey,
        limits: RelationalIndexReadLimits,
        mut visit: impl FnMut(&super::RelationalKey) -> bool,
    ) -> Result<RelationalIndexReadReport, RelationalIndexShadowError> {
        let descriptor = self.root_descriptor(table, index)?.clone();
        let encoded = self.encode_lookup_key(key)?;
        let mut context = ReadContext::new(self, limits);
        let root = context.read_root(&descriptor)?;
        let Some(leaf) = context.find_leaf(root.child, root.height, &encoded)? else {
            return Ok(context.report);
        };
        let ImmutableIndexPageBody::Leaf(leaf) = leaf.body else {
            return Err(context.corrupt("index traversal ended on a non-leaf page"));
        };
        let mut comparisons = 0usize;
        let position = leaf.entries.binary_search_by(|entry| {
            comparisons = comparisons.saturating_add(1);
            entry.key.as_slice().cmp(&encoded)
        });
        context.report.leaf_entries_visited = context
            .report
            .leaf_entries_visited
            .checked_add(comparisons)
            .ok_or_else(|| context.admission("leaf-entry counter overflow"))?;
        if let Ok(position) = position {
            context.report.matched_index_keys = 1;
            let outcome = context.visit_posting(&leaf.entries[position].posting, &mut visit)?;
            context.report.stopped_early = outcome == VisitOutcome::Stopped;
        }
        Ok(context.report)
    }

    /// Visits postings whose complete index key has the supplied leading
    /// relational-key prefix. Only root-to-leaf paths and selected posting
    /// pages are read; opening the reader and unrelated subtrees remain cold.
    ///
    /// Values observed by `visit` are provisional until this method returns
    /// `Ok`; callers must discard them on error.
    pub fn visit_prefix_postings(
        &self,
        table: &str,
        index: &str,
        prefix: &super::RelationalKey,
        limits: RelationalIndexReadLimits,
        mut visit: impl FnMut(&super::RelationalKey) -> bool,
    ) -> Result<RelationalIndexReadReport, RelationalIndexShadowError> {
        self.visit_prefix_entries(table, index, prefix, limits, |_, primary_key| {
            visit(primary_key)
        })
    }

    /// Visits ordered `(index_key, primary_key)` entries whose complete index
    /// key has the supplied leading relational-key prefix.
    ///
    /// Values observed by `visit` are provisional until this method returns
    /// `Ok`; callers must discard them on error.
    pub fn visit_prefix_entries(
        &self,
        table: &str,
        index: &str,
        prefix: &super::RelationalKey,
        limits: RelationalIndexReadLimits,
        mut visit: impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<RelationalIndexReadReport, RelationalIndexShadowError> {
        let descriptor = self.root_descriptor(table, index)?.clone();
        let encoded = self.encode_lookup_key(prefix)?;
        let mut context = ReadContext::new(self, limits);
        let root = context.read_root(&descriptor)?;
        let outcome =
            context.visit_prefix_subtree(root.child, root.height, None, &encoded, &mut visit)?;
        context.report.stopped_early = outcome == VisitOutcome::Stopped;
        Ok(context.report)
    }

    /// Visits entries for a deduplicated set of equally wide index-key
    /// prefixes under one immutable root read, cache scope, and read budget.
    ///
    /// Values observed by `visit` are provisional until this method returns
    /// `Ok`; callers must discard them on error. The first callback argument
    /// identifies the requested prefix that selected the entry.
    pub fn visit_prefix_entries_many(
        &self,
        table: &str,
        index: &str,
        prefixes: &[super::RelationalKey],
        limits: RelationalIndexReadLimits,
        mut visit: impl FnMut(
            &super::RelationalKey,
            &super::RelationalKey,
            &super::RelationalKey,
        ) -> bool,
    ) -> Result<RelationalIndexReadReport, RelationalIndexShadowError> {
        let mut encoded_prefixes = BTreeMap::new();
        let mut prefix_width = None;
        for prefix in prefixes {
            if let Some(width) = prefix_width {
                if width != prefix.0.len() {
                    return Err(RelationalIndexShadowError::Admission(
                        "batch index prefixes must have one common key width".to_string(),
                    ));
                }
            } else {
                prefix_width = Some(prefix.0.len());
            }
            encoded_prefixes
                .entry(self.encode_lookup_key(prefix)?)
                .or_insert_with(|| prefix.clone());
        }
        if encoded_prefixes.is_empty() {
            return Ok(RelationalIndexReadReport::default());
        }

        let descriptor = self.root_descriptor(table, index)?.clone();
        let mut context = ReadContext::new(self, limits);
        let root = context.read_root(&descriptor)?;
        for (encoded_prefix, prefix) in encoded_prefixes {
            let outcome = context.visit_prefix_subtree(
                root.child,
                root.height,
                None,
                &encoded_prefix,
                &mut |index_key, primary_key| visit(&prefix, index_key, primary_key),
            )?;
            if outcome == VisitOutcome::Stopped {
                context.report.stopped_early = true;
                break;
            }
        }
        Ok(context.report)
    }

    /// Visits an ordered exclusive range within one leading index-key prefix.
    pub fn visit_range_entries(
        &self,
        table: &str,
        index: &str,
        scan: &RelationalIndexRangeScan,
        limits: RelationalIndexReadLimits,
        mut visit: impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<RelationalIndexReadReport, RelationalIndexShadowError> {
        let descriptor = self.root_descriptor(table, index)?.clone();
        let encoded_prefix = self.encode_lookup_key(&scan.prefix)?;
        let encoded_bound = scan
            .exclusive_bound
            .as_ref()
            .map(|bound| self.encode_lookup_key(bound))
            .transpose()?;
        let mut context = ReadContext::new(self, limits);
        let root = context.read_root(&descriptor)?;
        let outcome = match scan.direction {
            RelationalIndexScanDirection::Forward => context.visit_forward_range_subtree(
                root.child,
                root.height,
                None,
                &encoded_prefix,
                encoded_bound.as_deref(),
                &mut visit,
            )?,
            RelationalIndexScanDirection::Backward => {
                let prefix_successor;
                let upper = match encoded_bound.as_deref() {
                    Some(bound) => bound,
                    None => {
                        prefix_successor =
                            encoded_prefix_successor(&encoded_prefix).ok_or_else(|| {
                                RelationalIndexShadowError::Admission(
                                    "relational index prefix has no finite exclusive upper bound"
                                        .to_string(),
                                )
                            })?;
                        &prefix_successor
                    }
                };
                context.visit_backward_range_subtree(
                    root.child,
                    root.height,
                    None,
                    &encoded_prefix,
                    upper,
                    &mut visit,
                )?
            }
        };
        context.report.stopped_early = outcome == VisitOutcome::Stopped;
        Ok(context.report)
    }

    fn root_descriptor(
        &self,
        table: &str,
        index: &str,
    ) -> Result<&RelationalIndexRootDescriptor, RelationalIndexShadowError> {
        self.manifest()
            .root(table, index)
            .ok_or_else(|| RelationalIndexShadowError::MissingIndex {
                table: table.to_string(),
                index: index.to_string(),
            })
    }

    fn encode_lookup_key(
        &self,
        key: &super::RelationalKey,
    ) -> Result<Vec<u8>, RelationalIndexShadowError> {
        let encoded = encode_relational_key(key).map_err(|error| match error {
            RelationalIndexShadowError::Corrupt(message) => RelationalIndexShadowError::Admission(
                format!("relational index lookup key is not encodable: {message}"),
            ),
            error => error,
        })?;
        if encoded.len() > self.config.page_limits.max_key_bytes.get() {
            return Err(RelationalIndexShadowError::Admission(format!(
                "relational index lookup key contains {} encoded bytes, exceeding limit {}",
                encoded.len(),
                self.config.page_limits.max_key_bytes
            )));
        }
        Ok(encoded)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum VisitOutcome {
    Continue,
    PastPrefix,
    Stopped,
}

struct ReadContext<'a> {
    reader: &'a RelationalIndexShadowReader,
    limits: RelationalIndexReadLimits,
    report: RelationalIndexReadReport,
}

impl<'a> ReadContext<'a> {
    const fn new(
        reader: &'a RelationalIndexShadowReader,
        limits: RelationalIndexReadLimits,
    ) -> Self {
        Self {
            reader,
            limits,
            report: RelationalIndexReadReport {
                pages_read: 0,
                bytes_read: 0,
                file_pages_read: 0,
                file_bytes_read: 0,
                cache_hits: 0,
                cache_misses: 0,
                cache_admission_rejections: 0,
                leaf_entries_visited: 0,
                matched_index_keys: 0,
                rows_visited: 0,
                stopped_early: false,
            },
        }
    }

    fn read_root(
        &mut self,
        descriptor: &RelationalIndexRootDescriptor,
    ) -> Result<crate::IndexRootPage, RelationalIndexShadowError> {
        if descriptor.height > self.limits.max_tree_height.get() {
            return Err(self.admission(format!(
                "index tree height {} exceeds read limit {}",
                descriptor.height, self.limits.max_tree_height
            )));
        }
        let page = self.read_page(descriptor.root_page_id)?;
        let ImmutableIndexPageBody::Root(root) = page.body else {
            return Err(self.corrupt("manifest root descriptor references a non-root page"));
        };
        if root.identity != descriptor.identity
            || root.schema_digest != descriptor.schema_digest
            || root.height != descriptor.height
        {
            return Err(self.corrupt("root page disagrees with its manifest descriptor"));
        }
        Ok(root)
    }

    fn find_leaf(
        &mut self,
        mut page_id: IndexPageId,
        mut height: u32,
        key: &[u8],
    ) -> Result<Option<ImmutableIndexPage>, RelationalIndexShadowError> {
        let mut expected_upper_bound: Option<Vec<u8>> = None;
        while height > 1 {
            let page = self.read_page(page_id)?;
            if let Some(expected) = &expected_upper_bound {
                self.validate_page_upper_bound(&page, expected)?;
            }
            let ImmutableIndexPageBody::Interior(interior) = page.body else {
                return Err(self.corrupt("index traversal expected an interior page"));
            };
            let position = interior
                .entries
                .partition_point(|entry| entry.upper_bound.as_slice() < key);
            let Some(entry) = interior.entries.get(position) else {
                return Ok(None);
            };
            page_id = entry.child;
            expected_upper_bound = Some(entry.upper_bound.clone());
            height -= 1;
        }
        let page = self.read_page(page_id)?;
        if let Some(expected) = &expected_upper_bound {
            self.validate_page_upper_bound(&page, expected)?;
        }
        Ok(Some(page))
    }

    fn visit_prefix_subtree(
        &mut self,
        page_id: IndexPageId,
        height: u32,
        expected_upper_bound: Option<&[u8]>,
        prefix: &[u8],
        visit: &mut impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        let page = self.read_page(page_id)?;
        if let Some(expected) = expected_upper_bound {
            self.validate_page_upper_bound(&page, expected)?;
        }
        if height == 1 {
            let ImmutableIndexPageBody::Leaf(leaf) = page.body else {
                return Err(self.corrupt("index traversal expected a leaf page"));
            };
            return self.visit_leaf_prefix(leaf.entries, prefix, visit);
        }
        let ImmutableIndexPageBody::Interior(interior) = page.body else {
            return Err(self.corrupt("index traversal expected an interior page"));
        };
        let start = interior
            .entries
            .partition_point(|entry| entry.upper_bound.as_slice() < prefix);
        for entry in interior.entries.into_iter().skip(start) {
            match self.visit_prefix_subtree(
                entry.child,
                height - 1,
                Some(&entry.upper_bound),
                prefix,
                visit,
            )? {
                VisitOutcome::Continue => {}
                outcome => return Ok(outcome),
            }
        }
        Ok(VisitOutcome::Continue)
    }

    fn visit_forward_range_subtree(
        &mut self,
        page_id: IndexPageId,
        height: u32,
        expected_upper_bound: Option<&[u8]>,
        prefix: &[u8],
        exclusive_bound: Option<&[u8]>,
        visit: &mut impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        let page = self.read_page(page_id)?;
        if let Some(expected) = expected_upper_bound {
            self.validate_page_upper_bound(&page, expected)?;
        }
        if height == 1 {
            let ImmutableIndexPageBody::Leaf(leaf) = page.body else {
                return Err(self.corrupt("index traversal expected a leaf page"));
            };
            let start = match exclusive_bound {
                Some(bound) => leaf
                    .entries
                    .partition_point(|entry| entry.key.as_slice() <= bound),
                None => leaf
                    .entries
                    .partition_point(|entry| entry.key.as_slice() < prefix),
            };
            return self.visit_forward_range_leaf(leaf.entries, start, prefix, visit);
        }
        let ImmutableIndexPageBody::Interior(interior) = page.body else {
            return Err(self.corrupt("index traversal expected an interior page"));
        };
        let start = match exclusive_bound {
            Some(bound) => interior
                .entries
                .partition_point(|entry| entry.upper_bound.as_slice() <= bound),
            None => interior
                .entries
                .partition_point(|entry| entry.upper_bound.as_slice() < prefix),
        };
        for entry in interior.entries.into_iter().skip(start) {
            match self.visit_forward_range_subtree(
                entry.child,
                height - 1,
                Some(&entry.upper_bound),
                prefix,
                exclusive_bound,
                visit,
            )? {
                VisitOutcome::Continue => {}
                outcome => return Ok(outcome),
            }
        }
        Ok(VisitOutcome::Continue)
    }

    fn visit_forward_range_leaf(
        &mut self,
        entries: Vec<crate::IndexLeafEntry>,
        start: usize,
        prefix: &[u8],
        visit: &mut impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        for entry in entries.into_iter().skip(start) {
            self.report.leaf_entries_visited = self
                .report
                .leaf_entries_visited
                .checked_add(1)
                .ok_or_else(|| self.admission("leaf-entry counter overflow"))?;
            if !entry.key.starts_with(prefix) {
                return Ok(VisitOutcome::PastPrefix);
            }
            self.report.matched_index_keys = self
                .report
                .matched_index_keys
                .checked_add(1)
                .ok_or_else(|| self.admission("matched-key counter overflow"))?;
            let index_key = decode_relational_key(&entry.key).inspect_err(|_| {
                self.reader.poison();
            })?;
            if self.visit_ordered_posting(&index_key, &entry.posting, visit)?
                == VisitOutcome::Stopped
            {
                return Ok(VisitOutcome::Stopped);
            }
        }
        Ok(VisitOutcome::Continue)
    }

    fn visit_backward_range_subtree(
        &mut self,
        page_id: IndexPageId,
        height: u32,
        expected_upper_bound: Option<&[u8]>,
        prefix: &[u8],
        exclusive_upper: &[u8],
        visit: &mut impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        let page = self.read_page(page_id)?;
        if let Some(expected) = expected_upper_bound {
            self.validate_page_upper_bound(&page, expected)?;
        }
        if height == 1 {
            let ImmutableIndexPageBody::Leaf(leaf) = page.body else {
                return Err(self.corrupt("index traversal expected a leaf page"));
            };
            let end = leaf
                .entries
                .partition_point(|entry| entry.key.as_slice() < exclusive_upper);
            for entry in leaf.entries.into_iter().take(end).rev() {
                self.report.leaf_entries_visited = self
                    .report
                    .leaf_entries_visited
                    .checked_add(1)
                    .ok_or_else(|| self.admission("leaf-entry counter overflow"))?;
                if !entry.key.starts_with(prefix) {
                    if entry.key.as_slice() < prefix {
                        return Ok(VisitOutcome::PastPrefix);
                    }
                    continue;
                }
                self.report.matched_index_keys = self
                    .report
                    .matched_index_keys
                    .checked_add(1)
                    .ok_or_else(|| self.admission("matched-key counter overflow"))?;
                let index_key = decode_relational_key(&entry.key).inspect_err(|_| {
                    self.reader.poison();
                })?;
                if self.visit_ordered_posting(&index_key, &entry.posting, visit)?
                    == VisitOutcome::Stopped
                {
                    return Ok(VisitOutcome::Stopped);
                }
            }
            return Ok(VisitOutcome::Continue);
        }
        let ImmutableIndexPageBody::Interior(interior) = page.body else {
            return Err(self.corrupt("index traversal expected an interior page"));
        };
        let end = interior
            .entries
            .partition_point(|entry| entry.upper_bound.as_slice() < exclusive_upper);
        let inclusive_end = end.saturating_add(1).min(interior.entries.len());
        for entry in interior.entries.into_iter().take(inclusive_end).rev() {
            match self.visit_backward_range_subtree(
                entry.child,
                height - 1,
                Some(&entry.upper_bound),
                prefix,
                exclusive_upper,
                visit,
            )? {
                VisitOutcome::Continue => {}
                outcome => return Ok(outcome),
            }
        }
        Ok(VisitOutcome::Continue)
    }

    fn validate_page_upper_bound(
        &self,
        page: &ImmutableIndexPage,
        expected: &[u8],
    ) -> Result<(), RelationalIndexShadowError> {
        let actual = match &page.body {
            ImmutableIndexPageBody::Interior(interior) => interior
                .entries
                .last()
                .map(|entry| entry.upper_bound.as_slice()),
            ImmutableIndexPageBody::Leaf(leaf) => {
                leaf.entries.last().map(|entry| entry.key.as_slice())
            }
            _ => None,
        };
        if actual != Some(expected) {
            return Err(self.corrupt("parent separator disagrees with child page upper bound"));
        }
        Ok(())
    }

    fn visit_leaf_prefix(
        &mut self,
        entries: Vec<crate::IndexLeafEntry>,
        prefix: &[u8],
        visit: &mut impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        let start = entries.partition_point(|entry| entry.key.as_slice() < prefix);
        for entry in entries.into_iter().skip(start) {
            self.report.leaf_entries_visited = self
                .report
                .leaf_entries_visited
                .checked_add(1)
                .ok_or_else(|| self.admission("leaf-entry counter overflow"))?;
            if !entry.key.starts_with(prefix) {
                return Ok(VisitOutcome::PastPrefix);
            }
            self.report.matched_index_keys = self
                .report
                .matched_index_keys
                .checked_add(1)
                .ok_or_else(|| self.admission("matched-key counter overflow"))?;
            let index_key = decode_relational_key(&entry.key).inspect_err(|_| {
                self.reader.poison();
            })?;
            if self.visit_ordered_posting(&index_key, &entry.posting, visit)?
                == VisitOutcome::Stopped
            {
                return Ok(VisitOutcome::Stopped);
            }
        }
        Ok(VisitOutcome::Continue)
    }

    fn visit_ordered_posting(
        &mut self,
        index_key: &super::RelationalKey,
        posting: &IndexLeafPosting,
        visit: &mut impl FnMut(&super::RelationalKey, &super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        self.visit_posting(posting, &mut |primary_key| visit(index_key, primary_key))
    }

    fn visit_posting(
        &mut self,
        posting: &IndexLeafPosting,
        visit: &mut impl FnMut(&super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        match posting {
            IndexLeafPosting::Inline(row_ids) => {
                for row_id in row_ids {
                    if !self.visit_row(row_id.as_bytes(), visit)? {
                        return Ok(VisitOutcome::Stopped);
                    }
                }
            }
            IndexLeafPosting::Page { first, total_rows } => {
                return self.visit_posting_chain(*first, *total_rows, visit);
            }
        }
        Ok(VisitOutcome::Continue)
    }

    fn visit_posting_chain(
        &mut self,
        first: IndexPageId,
        total_rows: u64,
        visit: &mut impl FnMut(&super::RelationalKey) -> bool,
    ) -> Result<VisitOutcome, RelationalIndexShadowError> {
        let mut next = Some(first);
        let mut remaining = total_rows;
        let mut previous_row_id: Option<Vec<u8>> = None;
        while let Some(page_id) = next {
            let page = self.read_page(page_id)?;
            let ImmutableIndexPageBody::Posting(IndexPostingPage {
                next: following,
                row_ids,
            }) = page.body
            else {
                return Err(self.corrupt("posting reference points to a non-posting page"));
            };
            let row_count = u64::try_from(row_ids.len())
                .map_err(|_| self.corrupt("posting row count does not fit u64"))?;
            if row_count > remaining {
                return Err(self.corrupt("posting chain exceeds its declared cardinality"));
            }
            for row_id in row_ids {
                if previous_row_id
                    .as_ref()
                    .is_some_and(|previous| previous.as_slice() >= row_id.as_bytes())
                {
                    return Err(self.corrupt("posting chain row locators are not strictly ordered"));
                }
                previous_row_id = Some(row_id.as_bytes().to_vec());
                if !self.visit_row(row_id.as_bytes(), visit)? {
                    return Ok(VisitOutcome::Stopped);
                }
            }
            remaining -= row_count;
            if remaining == 0 && following.is_some() {
                return Err(self.corrupt("posting chain continues past its declared cardinality"));
            }
            if remaining > 0 && following.is_none() {
                return Err(self.corrupt("posting chain ends before its declared cardinality"));
            }
            next = following;
        }
        if remaining != 0 {
            return Err(self.corrupt("posting chain has an incomplete declared cardinality"));
        }
        Ok(VisitOutcome::Continue)
    }

    fn visit_row(
        &mut self,
        encoded: &[u8],
        visit: &mut impl FnMut(&super::RelationalKey) -> bool,
    ) -> Result<bool, RelationalIndexShadowError> {
        if self.report.rows_visited >= self.limits.max_rows.get() {
            return Err(self.admission(format!(
                "index lookup exceeds row limit {}",
                self.limits.max_rows
            )));
        }
        let key = decode_relational_key(encoded).inspect_err(|_| {
            self.reader.poison();
        })?;
        self.report.rows_visited += 1;
        Ok(visit(&key))
    }

    fn read_page(
        &mut self,
        page_id: IndexPageId,
    ) -> Result<ImmutableIndexPage, RelationalIndexShadowError> {
        if self.report.pages_read >= self.limits.max_pages.get() {
            return Err(self.admission(format!(
                "index lookup exceeds page limit {}",
                self.limits.max_pages
            )));
        }
        let page_bytes = usize::try_from(self.reader.manifest().page_bytes)
            .map_err(|_| self.corrupt("manifest page size overflows usize"))?;
        let next_bytes = self
            .report
            .bytes_read
            .checked_add(page_bytes)
            .ok_or_else(|| self.admission("index lookup byte counter overflow"))?;
        if next_bytes > self.limits.max_bytes.get() {
            return Err(self.admission(format!(
                "index lookup needs {next_bytes} bytes, exceeding byte limit {}",
                self.limits.max_bytes
            )));
        }
        let remaining_file_bytes = self
            .limits
            .max_file_bytes
            .checked_sub(self.report.file_bytes_read)
            .ok_or_else(|| self.admission("index file byte counter exceeds its limit"))?;
        let read = self
            .reader
            .read_page_accounted(page_id, remaining_file_bytes)?;
        self.report.pages_read += 1;
        self.report.bytes_read = next_bytes;
        self.report.cache_hits += usize::from(read.cache_hit);
        self.report.cache_misses += usize::from(read.cache_miss);
        self.report.cache_admission_rejections += usize::from(read.cache_admission_rejected);
        if !read.cache_hit {
            self.report.file_pages_read += 1;
            self.report.file_bytes_read = self
                .report
                .file_bytes_read
                .checked_add(page_bytes)
                .ok_or_else(|| self.admission("index file byte counter overflow"))?;
        }
        Ok(read.page)
    }

    fn corrupt(&self, message: impl Into<String>) -> RelationalIndexShadowError {
        self.reader.poison();
        RelationalIndexShadowError::Corrupt(message.into())
    }

    fn admission(&self, message: impl Into<String>) -> RelationalIndexShadowError {
        RelationalIndexShadowError::Admission(message.into())
    }
}

fn encoded_prefix_successor(prefix: &[u8]) -> Option<Vec<u8>> {
    let mut successor = prefix.to_vec();
    for index in (0..successor.len()).rev() {
        if successor[index] != u8::MAX {
            successor[index] += 1;
            successor.truncate(index + 1);
            return Some(successor);
        }
    }
    None
}

pub(super) fn decode_relational_key(
    encoded: &[u8],
) -> Result<super::RelationalKey, RelationalIndexShadowError> {
    let mut values = Vec::new();
    let mut offset = 0usize;
    while offset < encoded.len() {
        let tag = encoded[offset];
        offset += 1;
        let value = match tag {
            0 => super::RelationalValue::Null,
            1 => {
                let value = *encoded.get(offset).ok_or_else(|| {
                    RelationalIndexShadowError::Corrupt("truncated boolean row locator".to_string())
                })?;
                offset += 1;
                match value {
                    0 => super::RelationalValue::Boolean(false),
                    1 => super::RelationalValue::Boolean(true),
                    _ => {
                        return Err(RelationalIndexShadowError::Corrupt(
                            "invalid boolean row locator".to_string(),
                        ));
                    }
                }
            }
            2 => {
                let ordered = read_ordered_u64(encoded, &mut offset, "bigint row locator")?;
                super::RelationalValue::BigInt((ordered ^ (1_u64 << 63)) as i64)
            }
            3 => {
                let ordered = read_ordered_u64(encoded, &mut offset, "double row locator")?;
                let bits = if ordered >> 63 == 0 {
                    !ordered
                } else {
                    ordered ^ (1_u64 << 63)
                };
                super::RelationalValue::DoublePrecision(f64::from_bits(bits))
            }
            4 => {
                let bytes = decode_escaped_bytes(encoded, &mut offset, "text row locator")?;
                super::RelationalValue::Text(String::from_utf8(bytes).map_err(|_| {
                    RelationalIndexShadowError::Corrupt(
                        "text row locator is not valid UTF-8".to_string(),
                    )
                })?)
            }
            5 => super::RelationalValue::Bytea(decode_escaped_bytes(
                encoded,
                &mut offset,
                "bytea row locator",
            )?),
            _ => {
                return Err(RelationalIndexShadowError::Corrupt(format!(
                    "unknown relational row-locator tag {tag}"
                )));
            }
        };
        values.push(value);
    }
    if values.is_empty() {
        return Err(RelationalIndexShadowError::Corrupt(
            "relational row locator must contain at least one value".to_string(),
        ));
    }
    Ok(super::RelationalKey(values))
}

fn read_ordered_u64(
    encoded: &[u8],
    offset: &mut usize,
    context: &str,
) -> Result<u64, RelationalIndexShadowError> {
    let end = offset
        .checked_add(8)
        .ok_or_else(|| RelationalIndexShadowError::Corrupt(format!("{context} length overflow")))?;
    let bytes = encoded
        .get(*offset..end)
        .ok_or_else(|| RelationalIndexShadowError::Corrupt(format!("truncated {context}")))?;
    *offset = end;
    Ok(u64::from_be_bytes(
        bytes.try_into().expect("ordered u64 length was checked"),
    ))
}

fn decode_escaped_bytes(
    encoded: &[u8],
    offset: &mut usize,
    context: &str,
) -> Result<Vec<u8>, RelationalIndexShadowError> {
    let mut decoded = Vec::new();
    loop {
        let byte = *encoded.get(*offset).ok_or_else(|| {
            RelationalIndexShadowError::Corrupt(format!("unterminated {context}"))
        })?;
        *offset += 1;
        if byte != 0 {
            decoded.push(byte);
            continue;
        }
        let escape = *encoded.get(*offset).ok_or_else(|| {
            RelationalIndexShadowError::Corrupt(format!("truncated {context} escape"))
        })?;
        *offset += 1;
        match escape {
            0 => return Ok(decoded),
            255 => decoded.push(0),
            _ => {
                return Err(RelationalIndexShadowError::Corrupt(format!(
                    "invalid {context} escape"
                )));
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ordered_row_locator_round_trips_all_inline_value_kinds() {
        let key = super::super::RelationalKey(vec![
            super::super::RelationalValue::Null,
            super::super::RelationalValue::Boolean(true),
            super::super::RelationalValue::BigInt(i64::MIN),
            super::super::RelationalValue::DoublePrecision(-0.0),
            super::super::RelationalValue::DoublePrecision(f64::NAN),
            super::super::RelationalValue::Text("a\0b".to_string()),
            super::super::RelationalValue::Bytea(vec![0, 1, 255]),
        ]);
        let encoded = super::super::encode_relational_key(&key).unwrap();
        assert_eq!(decode_relational_key(&encoded).unwrap(), key);
    }

    #[test]
    fn malformed_row_locator_fails_closed() {
        assert!(matches!(
            decode_relational_key(&[4, b'a', 0, 1]),
            Err(RelationalIndexShadowError::Corrupt(_))
        ));
        assert!(matches!(
            decode_relational_key(&[99]),
            Err(RelationalIndexShadowError::Corrupt(_))
        ));
    }
}