hawdb-storage 0.10.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
// 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::{
    durability, manifest, relational_row_page_artifact_file,
    relational_row_page_manifest_generation_file, relational_row_page_root_descriptor_file,
    relational_row_page_root_key_file, root, RelationalRowPagePublicationConfig,
    RelationalRowPagePublicationError, RelationalRowPageRootDescriptor,
    RelationalRowPageRootManifest, RelationalRowPageTableRoot, RELATIONAL_ROW_PAGE_MANIFEST_FILE,
};
use crate::cache::{
    ContentDigest, ManifestGeneration, RepresentationKind, SegmentCache, SegmentCacheError,
    SegmentCacheKey, StoreId,
};
use crate::file_io::{self as fs, File};
use crate::relational::row_page::{VerifiedRowPage, VerifiedRowPageMetadata};
use crate::relational::{
    ordered_key::encode_ordered_relational_key, ImmutableRelationalRowPage, RelationalKey,
    RelationalOverflowRootBinding, RelationalOverflowRootReader, RelationalRowPageView,
};
use hawdb_integrity::integrity_digest;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct RelationalRowPageRootReader {
    directory: PathBuf,
    pub(super) manifest: Arc<RelationalRowPageRootManifest>,
    config: RelationalRowPagePublicationConfig,
}

#[derive(Debug)]
pub(crate) struct RelationalRowPageSlotRead {
    pub page: VerifiedRowPage,
    pub cache_hit: bool,
    pub cache_miss: bool,
    pub cache_admission_rejected: bool,
}

pub(crate) struct CheckpointRootDescriptorRead {
    pub(crate) descriptor: RelationalRowPageRootDescriptor,
    // The descriptor's two Vec backings die before their retained permits.
    _memory: crate::background::CheckpointAllocationOwner,
}

impl std::ops::Deref for CheckpointRootDescriptorRead {
    type Target = RelationalRowPageRootDescriptor;
    fn deref(&self) -> &Self::Target {
        &self.descriptor
    }
}

impl RelationalRowPageRootReader {
    pub(super) fn open_latest_with_work_context(
        directory: &Path,
        config: RelationalRowPagePublicationConfig,
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Result<Option<Self>, RelationalRowPagePublicationError> {
        let Some(work) = work else {
            return Self::open_latest(directory, config);
        };
        let path = directory.join(RELATIONAL_ROW_PAGE_MANIFEST_FILE);
        manifest::read_manifest_if_exists_with_work_context(&path, config, Some(work))?
            .map(|manifest| Self::from_manifest_inner(directory, manifest, config, Some(work)))
            .transpose()
    }

    pub fn open_latest(
        directory: &Path,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Option<Self>, RelationalRowPagePublicationError> {
        let path = directory.join(RELATIONAL_ROW_PAGE_MANIFEST_FILE);
        manifest::read_manifest_if_exists(&path, config)?
            .map(|manifest| Self::from_manifest(directory, manifest, config))
            .transpose()
    }

    pub fn open_generation(
        directory: &Path,
        generation: u64,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        Self::open_generation_with_work_context(directory, generation, config, None)
    }

    pub(crate) fn open_generation_with_work_context(
        directory: &Path,
        generation: u64,
        config: RelationalRowPagePublicationConfig,
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        let path = directory.join(relational_row_page_manifest_generation_file(generation));
        let manifest = manifest::read_manifest_with_work_context(&path, config, work)?;
        if manifest.generation != generation {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "generation manifest {generation} identifies generation {}",
                manifest.generation
            )));
        }
        Self::from_manifest_inner(directory, manifest, config, work)
    }

    pub fn open_bound_generation(
        directory: &Path,
        binding: super::RelationalRowPageGenerationArtifacts,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        Self::open_bound_generation_with_work_context(directory, binding, config, None)
    }

    pub(crate) fn open_bound_generation_with_work_context(
        directory: &Path,
        binding: super::RelationalRowPageGenerationArtifacts,
        config: RelationalRowPagePublicationConfig,
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        let path = directory.join(relational_row_page_manifest_generation_file(
            binding.generation,
        ));
        let manifest = manifest::read_bound_manifest_with_work_context(
            &path,
            config,
            binding.manifest_artifact,
            work,
        )?;
        if manifest.generation != binding.generation
            || manifest.source_commit_epoch != binding.source_commit_epoch
            || manifest.root_set_digest != binding.root_set_digest
        {
            return Err(RelationalRowPagePublicationError::Corrupt(
                "row-page generation identity does not match its canonical binding".to_string(),
            ));
        }
        Self::from_manifest_inner(directory, manifest, config, work)
    }

    fn from_manifest(
        directory: &Path,
        manifest: RelationalRowPageRootManifest,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        Self::from_manifest_inner(directory, manifest, config, None)
    }

    fn from_manifest_inner(
        directory: &Path,
        manifest: RelationalRowPageRootManifest,
        config: RelationalRowPagePublicationConfig,
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        super::publisher::checkpoint::io(work, || {
            validate_artifact_length(
                &directory.join(relational_row_page_artifact_file(manifest.generation)),
                manifest.page_artifact.encoded_len,
                "row-page artifact",
            )
        })?;
        super::publisher::checkpoint::io(work, || {
            validate_artifact_length(
                &directory.join(relational_row_page_root_descriptor_file(
                    manifest.generation,
                )),
                manifest.root_descriptor_artifact.encoded_len,
                "row-page root descriptor artifact",
            )
        })?;
        super::publisher::checkpoint::io(work, || {
            validate_artifact_length(
                &directory.join(relational_row_page_root_key_file(manifest.generation)),
                manifest.root_key_artifact.encoded_len,
                "row-page root key artifact",
            )
        })?;
        Ok(Self {
            directory: directory.to_path_buf(),
            manifest: Arc::new(manifest),
            config,
        })
    }

    pub fn manifest(&self) -> &RelationalRowPageRootManifest {
        &self.manifest
    }

    /// Verifies all referenced slots and their physical-generation accounting.
    /// Keeps one page and one counter per manifest inventory entry in memory;
    /// this explicit full scrub is not part of opening a reader.
    pub fn scrub_physical_pages(&self) -> Result<(), RelationalRowPagePublicationError> {
        let inventory = &self.manifest.physical_generations;
        let mut live_counts = vec![0u64; inventory.len()];
        for entry in inventory {
            validate_artifact_length(
                &self
                    .directory
                    .join(relational_row_page_artifact_file(entry.generation)),
                entry.allocated_pages * self.manifest.page_bytes,
                "row-page physical generation",
            )?;
        }
        for table in &self.manifest.tables {
            self.visit_table_pages(&table.table, |descriptor| {
                let index = inventory
                    .binary_search_by_key(&descriptor.physical_generation, |entry| entry.generation)
                    .map_err(|_| {
                        RelationalRowPagePublicationError::Corrupt(
                            "row-page descriptor references an unaccounted generation".to_string(),
                        )
                    })?;
                live_counts[index] += 1;
                self.read_page(descriptor)?;
                Ok(())
            })?;
        }
        for (entry, actual) in inventory.iter().zip(live_counts) {
            if entry.live_pages != actual {
                return Err(RelationalRowPagePublicationError::Corrupt(format!(
                    "row-page physical generation {} has {actual} live pages, expected {}",
                    entry.generation, entry.live_pages,
                )));
            }
        }
        Ok(())
    }

    pub(crate) const fn publication_config(&self) -> RelationalRowPagePublicationConfig {
        self.config
    }

    pub fn overflow_root_binding(&self) -> Option<RelationalOverflowRootBinding> {
        self.manifest.overflow_root
    }

    pub fn validate_overflow_root(
        &self,
        overflow_root: &RelationalOverflowRootReader,
    ) -> Result<(), RelationalRowPagePublicationError> {
        let Some(expected) = self.manifest.overflow_root else {
            return Ok(());
        };
        let actual = overflow_root.manifest().binding();
        if actual != expected {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "row-page overflow binding {expected:?} differs from selected root {actual:?}"
            )));
        }
        Ok(())
    }

    pub fn read_table_page_descriptor(
        &self,
        table: &str,
        ordinal: u64,
    ) -> Result<RelationalRowPageRootDescriptor, RelationalRowPagePublicationError> {
        let table_root = self.table_root(table)?;
        let mut descriptors = File::open(self.descriptor_path())
            .map_err(durability("open row-page root descriptor artifact"))?;
        let mut keys =
            File::open(self.key_path()).map_err(durability("open row-page root key artifact"))?;
        self.read_table_page_descriptor_from(
            &mut descriptors,
            &mut keys,
            table,
            table_root,
            ordinal,
        )
    }

    pub fn find_table_page_descriptor(
        &self,
        table: &str,
        primary_key: &RelationalKey,
    ) -> Result<Option<RelationalRowPageRootDescriptor>, RelationalRowPagePublicationError> {
        self.find_table_page_descriptor_accounted(table, primary_key)
            .map(|(descriptor, _)| descriptor.map(|(_, descriptor)| descriptor))
    }

    pub(crate) fn find_table_page_descriptor_accounted(
        &self,
        table: &str,
        primary_key: &RelationalKey,
    ) -> Result<
        (Option<(u64, RelationalRowPageRootDescriptor)>, usize),
        RelationalRowPagePublicationError,
    > {
        let encoded_key = encode_ordered_relational_key(primary_key).map_err(|error| {
            RelationalRowPagePublicationError::Admission(format!(
                "row-page lookup key cannot be encoded: {error}"
            ))
        })?;
        self.find_table_page_descriptor_accounted_encoded(table, &encoded_key)
    }

    pub(crate) fn find_table_page_descriptor_accounted_encoded(
        &self,
        table: &str,
        encoded_key: &[u8],
    ) -> Result<
        (Option<(u64, RelationalRowPageRootDescriptor)>, usize),
        RelationalRowPagePublicationError,
    > {
        self.find_table_page_descriptor_accounted_encoded_with_work_context(
            table,
            encoded_key,
            None,
        )
        .map(|(found, reads)| {
            (
                found.map(|(ordinal, owned)| {
                    debug_assert!(owned._memory.is_empty());
                    (ordinal, owned.descriptor)
                }),
                reads,
            )
        })
    }

    pub(crate) fn find_table_page_descriptor_accounted_encoded_with_work_context(
        &self,
        table: &str,
        encoded_key: &[u8],
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Result<
        (Option<(u64, CheckpointRootDescriptorRead)>, usize),
        RelationalRowPagePublicationError,
    > {
        if let Some(work) = work {
            work.checkpoint().map_err(root::checkpoint::work_error)?;
        }
        if encoded_key.len() > self.config.page_limits.max_key_bytes.get() {
            return Err(RelationalRowPagePublicationError::Admission(format!(
                "row-page lookup key contains {} bytes, exceeding limit {}",
                encoded_key.len(),
                self.config.page_limits.max_key_bytes
            )));
        }
        let table_root = self.table_root(table)?;
        if table_root.page_count == 0 {
            return Ok((None, 0));
        }
        let open_wave = work
            .map(|work| work.io_wave())
            .transpose()
            .map_err(root::checkpoint::work_error)?;
        let mut descriptors = File::open(self.descriptor_path())
            .map_err(durability("open row-page root descriptor artifact"))?;
        let mut keys =
            File::open(self.key_path()).map_err(durability("open row-page root key artifact"))?;
        drop(open_wave);

        let mut lower = 0u64;
        let mut upper = table_root.page_count;
        let mut descriptor_reads = 0usize;
        let mut candidate = None;
        while lower < upper {
            let middle = lower + (upper - lower) / 2;
            let allocations = work.map(|work| crate::background::CheckpointDecodeContext {
                work: work.clone(),
                memory: Default::default(),
            });
            let descriptor = self.read_table_page_descriptor_from_with_allocations(
                &mut descriptors,
                &mut keys,
                table,
                table_root,
                middle,
                allocations.as_ref(),
            )?;
            let descriptor = CheckpointRootDescriptorRead {
                descriptor,
                _memory: allocations
                    .map_or_else(Default::default, |context| context.memory.into_inner()),
            };
            descriptor_reads = descriptor_reads.checked_add(1).ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "row-page descriptor read counter overflow".to_string(),
                )
            })?;
            let compare = |bound: &[u8]| match work {
                Some(work) => root::checkpoint::compare(bound, encoded_key, work),
                None => Ok(bound.cmp(encoded_key)),
            };
            if compare(&descriptor.upper_bound)? == std::cmp::Ordering::Less {
                lower = middle.checked_add(1).ok_or_else(|| {
                    RelationalRowPagePublicationError::Corrupt(
                        "row-page descriptor search overflow".to_string(),
                    )
                })?;
                if lower == table_root.page_count {
                    // Preserve the last-page result for keys above the table.
                    candidate = Some((middle, descriptor));
                }
            } else if compare(&descriptor.lower_bound)? != std::cmp::Ordering::Greater {
                // Published page bounds do not overlap. The descriptor and
                // both keys have already passed their binding checks, so an
                // in-range key needs neither more probes nor a second read.
                return Ok((Some((middle, descriptor)), descriptor_reads));
            } else {
                upper = middle;
                candidate = Some((middle, descriptor));
            }
        }
        let candidate = candidate.ok_or_else(|| {
            RelationalRowPagePublicationError::Corrupt(
                "row-page descriptor search did not select a page".to_string(),
            )
        })?;
        Ok((Some(candidate), descriptor_reads))
    }

    pub fn read_page(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
    ) -> Result<ImmutableRelationalRowPage, RelationalRowPagePublicationError> {
        let slot = self.read_page_slot_bytes(descriptor)?;
        let view = self.validate_page_slot(descriptor, &slot)?;
        ImmutableRelationalRowPage::decode_view(view).map_err(Into::into)
    }

    pub(crate) fn read_page_slot_accounted(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        cache: &SegmentCache,
        store_id: StoreId,
    ) -> Result<RelationalRowPageSlotRead, RelationalRowPagePublicationError> {
        self.read_page_slot_accounted_with_work_context(descriptor, cache, store_id, None)
    }

    pub(crate) fn read_page_slot_accounted_with_work_context(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        cache: &SegmentCache,
        store_id: StoreId,
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Result<RelationalRowPageSlotRead, RelationalRowPagePublicationError> {
        if let Some(work) = work {
            return self.read_checkpoint_page_slot(descriptor, work);
        }
        let cache_key = SegmentCacheKey {
            store_id,
            manifest_generation: ManifestGeneration(descriptor.physical_generation),
            segment_id: descriptor.physical_slot,
            content_digest: ContentDigest(descriptor.slot_integrity.slot_crc32c as u64),
            representation: RepresentationKind::RelationalRowPageSlot,
        };
        let verification_tag = *descriptor.slot_integrity.slot_sha256.as_bytes();
        if let Some(lease) = cache.get_verified(&cache_key, verification_tag) {
            let bytes = lease.into_bytes();
            let metadata = self.validate_verified_page(descriptor, &bytes)?;
            return Ok(RelationalRowPageSlotRead {
                page: VerifiedRowPage::new(bytes, metadata),
                cache_hit: true,
                cache_miss: false,
                cache_admission_rejected: false,
            });
        }
        let mut slot = self.read_page_slot_bytes(descriptor)?;
        let view = self.validate_page_slot(descriptor, &slot)?;
        let metadata = VerifiedRowPageMetadata::from_view(&view);
        let encoded_len = view.encoded_len();
        slot.truncate(encoded_len);
        match cache.insert_verified(cache_key, verification_tag, slot) {
            Ok(lease) => Ok(RelationalRowPageSlotRead {
                page: VerifiedRowPage::new(lease.into_bytes(), metadata),
                cache_hit: false,
                cache_miss: true,
                cache_admission_rejected: false,
            }),
            Err(rejection) => {
                let (error, bytes) = rejection.into_parts();
                match error {
                    SegmentCacheError::EntryTooLarge { .. }
                    | SegmentCacheError::PinnedCapacity { .. } => Ok(RelationalRowPageSlotRead {
                        page: VerifiedRowPage::new(bytes.into(), metadata),
                        cache_hit: false,
                        cache_miss: true,
                        cache_admission_rejected: true,
                    }),
                    error => Err(RelationalRowPagePublicationError::Corrupt(format!(
                        "row-page cache rejected immutable slot identity: {error}"
                    ))),
                }
            }
        }
    }

    fn read_checkpoint_page_slot(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        work: &crate::background::CheckpointWorkContext,
    ) -> Result<RelationalRowPageSlotRead, RelationalRowPagePublicationError> {
        let page_bytes = self.config.page_limits.max_page_bytes.get();
        let offset = descriptor
            .physical_slot
            .checked_mul(page_bytes as u64)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Corrupt(
                    "row-page physical offset overflow".into(),
                )
            })?;
        let mut scratch =
            crate::background::CheckpointBytes::zeroed(page_bytes.min(64 * 1024), work)
                .map_err(root::checkpoint::work_error)?;
        let bytes = crate::background::CheckpointSharedBytes::build_with_error(
            page_bytes,
            work,
            root::checkpoint::work_error,
            |bytes| -> Result<(), RelationalRowPagePublicationError> {
                let unit = work.start_unit().map_err(root::checkpoint::work_error)?;
                let wave = work.io_wave().map_err(root::checkpoint::work_error)?;
                let mut artifact = File::open(self.directory.join(
                    relational_row_page_artifact_file(descriptor.physical_generation),
                ))
                .map_err(durability("open row-page generation artifact"))?;
                artifact
                    .seek(SeekFrom::Start(offset))
                    .map_err(durability("seek row-page generation slot"))?;
                drop(wave);
                unit.finish();
                let mut remaining = page_bytes;
                while remaining != 0 {
                    let count = remaining.min(scratch.len());
                    let unit = work.start_unit().map_err(root::checkpoint::work_error)?;
                    let wave = work.io_wave().map_err(root::checkpoint::work_error)?;
                    artifact
                        .read_exact(&mut scratch.as_mut_slice()[..count])
                        .map_err(durability("read row-page generation slot"))?;
                    drop(wave);
                    unit.finish();
                    bytes
                        .append(&scratch[..count], work)
                        .map_err(root::checkpoint::work_error)?;
                    remaining -= count;
                }
                Ok(())
            },
        )?;
        let view = self.validate_page_slot_with_work_context(descriptor, &bytes, Some(work))?;
        let metadata = VerifiedRowPageMetadata::from_view(&view);
        Ok(RelationalRowPageSlotRead {
            page: VerifiedRowPage::new_checkpoint(bytes, metadata),
            // Private reads bypass serving-cache ownership and publication.
            cache_hit: false,
            cache_miss: false,
            cache_admission_rejected: false,
        })
    }

    fn read_page_slot_bytes(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
    ) -> Result<Vec<u8>, RelationalRowPagePublicationError> {
        let page_bytes = self.config.page_limits.max_page_bytes.get() as u64;
        let offset = descriptor
            .physical_slot
            .checked_mul(page_bytes)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Corrupt(
                    "row-page physical offset overflow".to_string(),
                )
            })?;
        let mut artifact = File::open(self.directory.join(relational_row_page_artifact_file(
            descriptor.physical_generation,
        )))
        .map_err(durability("open row-page generation artifact"))?;
        artifact
            .seek(SeekFrom::Start(offset))
            .map_err(durability("seek row-page generation slot"))?;
        let mut slot = vec![0u8; self.config.page_limits.max_page_bytes.get()];
        artifact
            .read_exact(&mut slot)
            .map_err(durability("read row-page generation slot"))?;
        Ok(slot)
    }

    fn validate_page_slot<'a>(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        slot: &'a [u8],
    ) -> Result<RelationalRowPageView<'a>, RelationalRowPagePublicationError> {
        self.validate_page_slot_with_work_context(descriptor, slot, None)
    }

    fn validate_page_slot_with_work_context<'a>(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        slot: &'a [u8],
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Result<RelationalRowPageView<'a>, RelationalRowPagePublicationError> {
        let digest = match work {
            Some(work) => work.integrity(slot).map_err(root::checkpoint::work_error)?,
            None => integrity_digest(slot),
        };
        if digest.crc32c.get() != descriptor.slot_integrity.slot_crc32c
            || digest.sha256 != descriptor.slot_integrity.slot_sha256
        {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "row-page {} slot checksum mismatch",
                descriptor.logical_page_id.get()
            )));
        }
        let view = RelationalRowPageView::open_slot(slot, self.config.page_limits)?;
        self.validate_page_identity(descriptor, &view)?;
        Ok(view)
    }

    fn validate_verified_page(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        encoded: &[u8],
    ) -> Result<VerifiedRowPageMetadata, RelationalRowPagePublicationError> {
        let view = RelationalRowPageView::open_verified(encoded, self.config.page_limits)?;
        self.validate_page_identity(descriptor, &view)?;
        Ok(VerifiedRowPageMetadata::from_view(&view))
    }

    fn validate_page_identity(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        view: &RelationalRowPageView<'_>,
    ) -> Result<(), RelationalRowPagePublicationError> {
        if view.page_id() != descriptor.logical_page_id
            || view.generation() != descriptor.physical_generation
            || view.source_commit_epoch() != descriptor.source_commit_epoch
            || view.row_count() != descriptor.row_count as usize
            || view.encoded_len() != descriptor.slot_integrity.encoded_len as usize
            || view.lower_bound_bytes() != descriptor.lower_bound
            || view.upper_bound_bytes() != descriptor.upper_bound
        {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "row-page {} content does not match its root descriptor",
                descriptor.logical_page_id.get()
            )));
        }
        Ok(())
    }

    pub fn visit_table_pages<F>(
        &self,
        table: &str,
        mut visitor: F,
    ) -> Result<(), RelationalRowPagePublicationError>
    where
        F: FnMut(&RelationalRowPageRootDescriptor) -> Result<(), RelationalRowPagePublicationError>,
    {
        let table_root = self.table_root(table)?;
        let mut descriptors = File::open(self.descriptor_path())
            .map_err(durability("open row-page root descriptor artifact"))?;
        let mut keys =
            File::open(self.key_path()).map_err(durability("open row-page root key artifact"))?;
        let mut previous_upper: Option<Vec<u8>> = None;
        for offset in 0..table_root.page_count {
            let ordinal = table_root
                .first_descriptor
                .checked_add(offset)
                .ok_or_else(|| {
                    RelationalRowPagePublicationError::Corrupt(
                        "row-page descriptor ordinal overflow".to_string(),
                    )
                })?;
            let descriptor = root::read_descriptor(
                &mut descriptors,
                &mut keys,
                ordinal,
                &self.manifest,
                self.config,
            )?;
            if previous_upper
                .as_ref()
                .is_some_and(|upper| upper.as_slice() >= descriptor.lower_bound.as_slice())
            {
                return Err(RelationalRowPagePublicationError::Corrupt(format!(
                    "table {table} row-page bounds overlap or are unordered"
                )));
            }
            previous_upper = Some(descriptor.upper_bound.clone());
            visitor(&descriptor)?;
        }
        Ok(())
    }

    pub(super) fn visit_table_pages_with_work_context<F>(
        &self,
        table: &str,
        work: &crate::background::CheckpointWorkContext,
        mut visitor: F,
    ) -> Result<(), RelationalRowPagePublicationError>
    where
        F: FnMut(&RelationalRowPageRootDescriptor) -> Result<(), RelationalRowPagePublicationError>,
    {
        let unit = work.start_unit().map_err(root::checkpoint::work_error)?;
        let table_root = self.table_root(table)?;
        unit.finish();
        let unit = work.start_unit().map_err(root::checkpoint::work_error)?;
        let wave = work.io_wave().map_err(root::checkpoint::work_error)?;
        let mut descriptors = File::open(self.descriptor_path())
            .map_err(durability("open row-page root descriptor artifact"))?;
        drop(wave);
        unit.finish();
        let unit = work.start_unit().map_err(root::checkpoint::work_error)?;
        let wave = work.io_wave().map_err(root::checkpoint::work_error)?;
        let mut keys =
            File::open(self.key_path()).map_err(durability("open row-page root key artifact"))?;
        drop(wave);
        unit.finish();
        let mut previous_upper: Option<Vec<u8>> = None;
        for offset in 0..table_root.page_count {
            let unit = work.start_unit().map_err(root::checkpoint::work_error)?;
            let ordinal = table_root
                .first_descriptor
                .checked_add(offset)
                .ok_or_else(|| {
                    RelationalRowPagePublicationError::Corrupt(
                        "row-page descriptor ordinal overflow".to_string(),
                    )
                })?;
            unit.finish();
            let descriptor = root::checkpoint::read_descriptor(
                &mut descriptors,
                &mut keys,
                ordinal,
                &self.manifest,
                self.config,
                work,
            )?;
            if previous_upper
                .as_ref()
                .map(|upper| root::checkpoint::compare(upper, &descriptor.lower_bound, work))
                .transpose()?
                .is_some_and(|ordering| ordering != std::cmp::Ordering::Less)
            {
                return Err(RelationalRowPagePublicationError::Corrupt(format!(
                    "table {table} row-page bounds overlap or are unordered"
                )));
            }
            previous_upper = Some(root::checkpoint::clone_bytes(
                &descriptor.upper_bound,
                work,
            )?);
            // No read, CPU or I/O lease crosses the callback into nested writers.
            visitor(&descriptor)?;
        }
        work.checkpoint().map_err(root::checkpoint::work_error)
    }

    pub fn table_root(
        &self,
        table: &str,
    ) -> Result<&RelationalRowPageTableRoot, RelationalRowPagePublicationError> {
        self.manifest
            .tables
            .binary_search_by(|candidate| candidate.table.as_str().cmp(table))
            .map(|index| &self.manifest.tables[index])
            .map_err(|_| RelationalRowPagePublicationError::MissingTable(table.to_string()))
    }

    fn descriptor_path(&self) -> PathBuf {
        self.directory
            .join(relational_row_page_root_descriptor_file(
                self.manifest.generation,
            ))
    }

    fn key_path(&self) -> PathBuf {
        self.directory
            .join(relational_row_page_root_key_file(self.manifest.generation))
    }

    fn read_table_page_descriptor_from(
        &self,
        descriptors: &mut File,
        keys: &mut File,
        table: &str,
        table_root: &RelationalRowPageTableRoot,
        ordinal: u64,
    ) -> Result<RelationalRowPageRootDescriptor, RelationalRowPagePublicationError> {
        self.read_table_page_descriptor_from_with_allocations(
            descriptors,
            keys,
            table,
            table_root,
            ordinal,
            None,
        )
    }

    fn read_table_page_descriptor_from_with_allocations(
        &self,
        descriptors: &mut File,
        keys: &mut File,
        table: &str,
        table_root: &RelationalRowPageTableRoot,
        ordinal: u64,
        allocations: Option<&crate::background::CheckpointDecodeContext>,
    ) -> Result<RelationalRowPageRootDescriptor, RelationalRowPagePublicationError> {
        if ordinal >= table_root.page_count {
            return Err(RelationalRowPagePublicationError::Admission(format!(
                "row-page ordinal {ordinal} exceeds table {table} page count {}",
                table_root.page_count
            )));
        }
        let descriptor_ordinal = table_root
            .first_descriptor
            .checked_add(ordinal)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Corrupt(
                    "row-page descriptor ordinal overflow".to_string(),
                )
            })?;
        let descriptor = match allocations {
            Some(context) => root::checkpoint::read_descriptor_with_allocations(
                descriptors,
                keys,
                descriptor_ordinal,
                &self.manifest,
                self.config,
                context,
                Some(context),
            ),
            None => root::read_descriptor(
                descriptors,
                keys,
                descriptor_ordinal,
                &self.manifest,
                self.config,
            ),
        }?;
        if descriptor.logical_page_id.get() >= table_root.next_page_id.get() {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "table {table} descriptor page id {} is not below next page id {}",
                descriptor.logical_page_id.get(),
                table_root.next_page_id
            )));
        }
        Ok(descriptor)
    }
}

fn validate_artifact_length(
    path: &Path,
    expected: u64,
    context: &str,
) -> Result<(), RelationalRowPagePublicationError> {
    let actual = fs::metadata(path)
        .map_err(durability("read row-page artifact metadata"))?
        .len();
    if actual != expected {
        return Err(RelationalRowPagePublicationError::Corrupt(format!(
            "{context} contains {actual} bytes, expected {expected}"
        )));
    }
    Ok(())
}