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
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
// 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::compaction::RowPageRewriteControls;
use super::publisher::{PreparedDirtyPage, PreparedTableDelta};
use super::{
    durability, RelationalRowPageArtifactMetadata, RelationalRowPagePhysicalGeneration,
    RelationalRowPagePublicationConfig, RelationalRowPagePublicationError,
    RelationalRowPageRootDescriptor, RelationalRowPageRootReader, RelationalRowPageSlotIntegrity,
    RelationalRowPageTableRoot,
};
use crate::file_io::File;
use crate::relational::{
    ordered_key::encode_ordered_relational_key, ImmutableRelationalRowPage,
    RelationalRowPageLimits, RelationalRowPageView,
};
use hawdb_integrity::{IntegrityHasher, Sha256Digest, SHA256_BYTES};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{BufWriter, Write};

pub(super) mod checkpoint;
mod codec;

pub(super) use codec::{read_descriptor, ROOT_DESCRIPTOR_BYTES};
use codec::{validate_descriptor, WireDescriptor};
const ROOT_DESCRIPTOR_BINDING_OFFSET: usize = 100;

pub(super) struct RootBuildOutput {
    pub tables: Vec<RelationalRowPageTableRoot>,
    pub root_page_count: u64,
    pub reused_page_count: u64,
    pub relocated_page_count: u64,
    pub descriptor_artifact: RelationalRowPageArtifactMetadata,
    pub key_artifact: RelationalRowPageArtifactMetadata,
    pub physical_generations: Vec<RelationalRowPagePhysicalGeneration>,
}

pub(super) fn prepare_dirty_page(
    page: ImmutableRelationalRowPage,
    limits: RelationalRowPageLimits,
) -> Result<PreparedDirtyPage, RelationalRowPagePublicationError> {
    prepare_dirty_page_with_work_context(page, limits, None)
}

pub(super) fn prepare_dirty_page_with_work_context(
    page: ImmutableRelationalRowPage,
    limits: RelationalRowPageLimits,
    work: Option<&crate::background::CheckpointWorkContext>,
) -> Result<PreparedDirtyPage, RelationalRowPagePublicationError> {
    if let Some(work) = work {
        work.checkpoint().map_err(checkpoint::work_error)?;
    }
    let first = page.rows.first().ok_or_else(|| {
        RelationalRowPagePublicationError::Admission("dirty row page contains no rows".to_string())
    })?;
    let last = page.rows.last().expect("dirty page has a first row");
    let encode_key = |key: &crate::relational::RelationalKey| match work {
        Some(work) => {
            crate::relational::row_page::checkpoint::ordered_key(key, work).map_err(|error| {
                match error {
                    crate::relational::RelationalRowPageError::Admission(message)
                    | crate::relational::RelationalRowPageError::Corrupt(message) => message,
                }
            })
        }
        None => encode_ordered_relational_key(key).map_err(|error| error.to_string()),
    };
    let lower_bound = encode_key(&first.primary_key).map_err(|error| {
        RelationalRowPagePublicationError::Admission(format!("dirty row-page lower bound: {error}"))
    })?;
    let upper_bound = encode_key(&last.primary_key).map_err(|error| {
        RelationalRowPagePublicationError::Admission(format!("dirty row-page upper bound: {error}"))
    })?;
    if lower_bound.len() > limits.max_key_bytes.get()
        || upper_bound.len() > limits.max_key_bytes.get()
    {
        return Err(RelationalRowPagePublicationError::Admission(format!(
            "dirty row-page bounds exceed key limit {}",
            limits.max_key_bytes
        )));
    }
    let row_count = u32::try_from(page.rows.len()).map_err(|_| {
        RelationalRowPagePublicationError::Admission(
            "row-page row count does not fit in u32".to_string(),
        )
    })?;
    let descriptor = RelationalRowPageRootDescriptor {
        logical_page_id: page.page_id,
        physical_generation: page.generation,
        physical_slot: 0,
        source_commit_epoch: page.source_commit_epoch,
        row_count,
        lower_bound,
        upper_bound,
        slot_integrity: RelationalRowPageSlotIntegrity {
            encoded_len: 0,
            slot_crc32c: 0,
            slot_sha256: Sha256Digest::from_bytes([0; SHA256_BYTES]),
        },
    };
    Ok(PreparedDirtyPage { page, descriptor })
}

pub(super) struct PageArtifactWriter {
    writer: BufWriter<File>,
    hasher: IntegrityHasher,
    page_count: u64,
    limits: RelationalRowPageLimits,
    work: Option<crate::background::CheckpointWorkContext>,
}

impl PageArtifactWriter {
    pub(super) fn new(file: File, limits: RelationalRowPageLimits) -> Self {
        Self {
            writer: BufWriter::new(file),
            hasher: IntegrityHasher::new(),
            page_count: 0,
            limits,
            work: None,
        }
    }

    pub(super) fn with_work_context(
        mut self,
        work: Option<&crate::background::CheckpointWorkContext>,
    ) -> Self {
        self.work = work.cloned();
        self
    }

    pub(super) fn write(
        &mut self,
        page: &mut PreparedDirtyPage,
    ) -> Result<(), RelationalRowPagePublicationError> {
        let next_page_count = self.page_count.checked_add(1).ok_or_else(|| {
            RelationalRowPagePublicationError::Admission("row-page slot count overflow".to_string())
        })?;
        next_page_count
            .checked_mul(self.limits.max_page_bytes.get() as u64)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "row-page artifact length overflow".to_string(),
                )
            })?;
        let mut encoded_slot = match &self.work {
            Some(work) => {
                crate::relational::row_page::checkpoint::encode(&page.page, self.limits, work)?
            }
            None => page.page.encode(self.limits)?,
        };
        let encoded_page_len = u32::try_from(encoded_slot.len()).map_err(|_| {
            RelationalRowPagePublicationError::Admission(
                "encoded row page length does not fit in u32".to_string(),
            )
        })?;
        let view = RelationalRowPageView::open(&encoded_slot, self.limits)?;
        if view.lower_bound_bytes() != page.descriptor.lower_bound
            || view.upper_bound_bytes() != page.descriptor.upper_bound
            || view.row_count() as u32 != page.descriptor.row_count
            || page.page.page_id != page.descriptor.logical_page_id
            || page.page.generation != page.descriptor.physical_generation
            || page.page.source_commit_epoch != page.descriptor.source_commit_epoch
        {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "row page {} metadata changed during encoding",
                page.descriptor.logical_page_id.get()
            )));
        }
        let slot_digest = if let Some(work) = &self.work {
            checkpoint::pad_slot(&mut encoded_slot, self.limits.max_page_bytes.get(), work)?;
            let digest = work
                .integrity(&encoded_slot)
                .map_err(checkpoint::work_error)?;
            checkpoint::write_slot(&mut self.writer, &mut self.hasher, &encoded_slot, work)?;
            RelationalRowPageArtifactMetadata {
                encoded_len: encoded_slot.len() as u64,
                encoded_crc32c: digest.crc32c.get(),
                encoded_sha256: digest.sha256,
            }
        } else {
            encoded_slot.resize(self.limits.max_page_bytes.get(), 0);
            let digest = digest_bytes(&encoded_slot);
            self.writer
                .write_all(&encoded_slot)
                .map_err(durability("write row-page slot"))?;
            self.hasher.update(&encoded_slot);
            digest
        };
        page.descriptor.physical_slot = self.page_count;
        page.descriptor.slot_integrity = RelationalRowPageSlotIntegrity {
            encoded_len: encoded_page_len,
            slot_crc32c: slot_digest.encoded_crc32c,
            slot_sha256: slot_digest.encoded_sha256,
        };
        self.page_count = next_page_count;
        Ok(())
    }

    pub(super) fn finish(
        mut self,
    ) -> Result<(RelationalRowPageArtifactMetadata, u64), RelationalRowPagePublicationError> {
        if let Some(work) = &self.work {
            checkpoint::finish(&mut self.writer, work)?;
        } else {
            self.writer
                .flush()
                .map_err(durability("flush row-page artifact"))?;
            self.writer
                .get_ref()
                .sync_all()
                .map_err(durability("sync row-page artifact"))?;
        }
        let encoded_len = self.page_count * self.limits.max_page_bytes.get() as u64;
        let digest = self.hasher.finish();
        Ok((
            RelationalRowPageArtifactMetadata {
                encoded_len,
                encoded_crc32c: digest.crc32c.get(),
                encoded_sha256: digest.sha256,
            },
            self.page_count,
        ))
    }
}

pub(super) struct RootBuildRequest<'a> {
    pub descriptor_file: File,
    pub key_file: File,
    pub base: Option<&'a RelationalRowPageRootReader>,
    pub deltas: &'a mut BTreeMap<String, PreparedTableDelta>,
    pub generation: u64,
    pub source_commit_epoch: u64,
    pub config: RelationalRowPagePublicationConfig,
}

pub(super) fn write_root_artifacts(
    request: RootBuildRequest<'_>,
    pages: &mut PageArtifactWriter,
    rewrite: Option<RowPageRewriteControls<'_>>,
) -> Result<RootBuildOutput, RelationalRowPagePublicationError> {
    let RootBuildRequest {
        descriptor_file,
        key_file,
        base,
        deltas,
        generation,
        source_commit_epoch,
        config,
    } = request;

    let mut writer = RootWriter {
        descriptors: BufWriter::new(descriptor_file),
        keys: BufWriter::new(key_file),
        descriptor_hasher: IntegrityHasher::new(),
        key_hasher: IntegrityHasher::new(),
        descriptor_count: 0,
        key_bytes: 0,
        generation,
        source_commit_epoch,
        config,
        work: pages.work.clone(),
        physical_generations: Vec::with_capacity(
            base.map_or(1, |base| base.manifest.physical_generations.len() + 1),
        ),
        pages,
        rewrite,
        relocated_page_count: 0,
    };
    // Occupancy is bounded by the selected manifest, not by the number of pages.
    // Recount live descriptors while merging, retaining each file's allocation.
    if let Some(base) = base {
        for entry in &base.manifest.physical_generations {
            let unit = writer.start_unit()?;
            writer
                .physical_generations
                .push(RelationalRowPagePhysicalGeneration {
                    generation: entry.generation,
                    allocated_pages: entry.allocated_pages,
                    live_pages: 0,
                });
            if let Some(unit) = unit {
                unit.finish();
            }
        }
    }
    let unit = writer.start_unit()?;
    writer
        .physical_generations
        .push(RelationalRowPagePhysicalGeneration {
            generation,
            allocated_pages: 0,
            live_pages: 0,
        });
    if let Some(unit) = unit {
        unit.finish();
    }
    let mut table_names = BTreeSet::new();
    for name in base
        .into_iter()
        .flat_map(|base| {
            base.manifest
                .tables
                .iter()
                .map(|table| table.table.as_str())
        })
        .chain(deltas.keys().map(String::as_str))
    {
        let name = writer.clone_name(name)?;
        let unit = writer.start_unit()?;
        table_names.insert(name);
        if let Some(unit) = unit {
            unit.finish();
        }
    }
    if table_names.len() > config.max_tables.get() {
        return Err(RelationalRowPagePublicationError::Admission(format!(
            "row-page root contains {} tables, exceeding limit {}",
            table_names.len(),
            config.max_tables
        )));
    }

    let mut tables = Vec::with_capacity(table_names.len());
    let mut reused_page_count = 0u64;
    for table_name in table_names {
        writer.checkpoint()?;
        let unit = writer.start_unit()?;
        let base_table = base.and_then(|reader| {
            reader
                .manifest
                .tables
                .binary_search_by(|table| table.table.cmp(&table_name))
                .ok()
                .map(|index| &reader.manifest.tables[index])
        });
        let delta = deltas.remove(&table_name);
        if let Some(unit) = unit {
            unit.finish();
        }
        let schema_source = match (
            base_table,
            delta.as_ref().and_then(|delta| delta.schema.as_ref()),
        ) {
            (Some(base_table), Some(schema)) => {
                let same = match &writer.work {
                    Some(work) => checkpoint::same_schema(&base_table.schema, schema, work)?,
                    None => base_table.schema == *schema,
                };
                if !same {
                    return Err(RelationalRowPagePublicationError::Admission(format!(
                        "table {table_name} schema changed during incremental row-page publication"
                    )));
                }
                &base_table.schema
            }
            (Some(base_table), _) => &base_table.schema,
            (None, Some(schema)) => schema,
            (None, None) => {
                return Err(RelationalRowPagePublicationError::Admission(format!(
                    "new row-page table {table_name} is missing its schema"
                )));
            }
        };
        let schema = match &writer.work {
            Some(work) => checkpoint::clone_schema(schema_source, work)?,
            None => schema_source.clone(),
        };
        let schema_digest = match (base_table, delta.as_ref()) {
            (Some(base_table), Some(delta)) if base_table.schema_digest != delta.schema_digest => {
                return Err(RelationalRowPagePublicationError::Admission(format!(
                    "table {table_name} schema digest changed during incremental row-page publication"
                )));
            }
            (Some(base_table), _) => base_table.schema_digest,
            (None, Some(delta)) => delta.schema_digest,
            (None, None) => unreachable!("table name originated from base or delta"),
        };
        let column_count = match (base_table, delta.as_ref()) {
            (Some(base_table), Some(delta)) if base_table.column_count != delta.column_count => {
                return Err(RelationalRowPagePublicationError::Admission(format!(
                    "table {table_name} column count changed during incremental row-page publication"
                )));
            }
            (Some(base_table), _) => base_table.column_count,
            (None, Some(delta)) => delta.column_count,
            (None, None) => unreachable!("table name originated from base or delta"),
        };
        let next_page_id = match (base_table, delta.as_ref()) {
            (Some(base_table), Some(delta)) => {
                if delta.next_page_id < base_table.next_page_id {
                    return Err(RelationalRowPagePublicationError::Admission(format!(
                        "table {table_name} next page id {} precedes published allocator {}",
                        delta.next_page_id, base_table.next_page_id
                    )));
                }
                delta.next_page_id
            }
            (Some(base_table), None) => base_table.next_page_id,
            (None, Some(delta)) => delta.next_page_id,
            (None, None) => unreachable!("table name originated from base or delta"),
        };
        let first_descriptor = writer.descriptor_count;
        let mut bounds = TableBounds::default();
        match (base, base_table, delta) {
            (Some(base), Some(_), Some(delta)) => {
                reused_page_count = reused_page_count
                    .checked_add(write_merged_table(
                        &mut writer,
                        base,
                        &table_name,
                        delta,
                        &mut bounds,
                    )?)
                    .ok_or_else(|| {
                        RelationalRowPagePublicationError::Admission(
                            "reused row-page count overflow".to_string(),
                        )
                    })?;
            }
            (Some(base), Some(_), None) => {
                let relocated_before = writer.relocated_page_count;
                let work = writer.work.clone();
                visit_base(base, &table_name, work.as_ref(), |descriptor| {
                    writer
                        .write_base_descriptor(base, descriptor, &mut bounds)
                        .map(|_| ())
                })?;
                reused_page_count = reused_page_count
                    .checked_add(
                        writer.descriptor_count
                            - first_descriptor
                            - (writer.relocated_page_count - relocated_before),
                    )
                    .ok_or_else(|| {
                        RelationalRowPagePublicationError::Admission(
                            "reused row-page count overflow".to_string(),
                        )
                    })?;
            }
            (_, None, Some(delta)) => {
                if !delta.deleted_page_ids.is_empty() {
                    return Err(RelationalRowPagePublicationError::Admission(format!(
                        "new table {table_name} deletes pages from a missing base"
                    )));
                }
                for page in &delta.dirty_pages {
                    writer.write_descriptor(&page.descriptor, &mut bounds)?;
                }
            }
            _ => unreachable!("base table and delta combination was exhausted"),
        }
        let page_count = writer.descriptor_count - first_descriptor;
        let unit = writer.start_unit()?;
        tables.push(RelationalRowPageTableRoot {
            table: table_name,
            schema,
            schema_digest,
            column_count,
            row_count: bounds.row_count,
            next_page_id,
            first_descriptor,
            page_count,
            lower_bound: bounds.lower.unwrap_or_default(),
            upper_bound: bounds.upper.unwrap_or_default(),
        });
        if let Some(unit) = unit {
            unit.finish();
        }
    }
    if !deltas.is_empty() {
        return Err(RelationalRowPagePublicationError::Corrupt(
            "row-page root builder left unprocessed table deltas".to_string(),
        ));
    }
    let finished = writer.finish()?;
    Ok(RootBuildOutput {
        tables,
        root_page_count: finished.root_page_count,
        reused_page_count,
        relocated_page_count: finished.relocated_page_count,
        descriptor_artifact: finished.descriptor_artifact,
        key_artifact: finished.key_artifact,
        physical_generations: finished.physical_generations,
    })
}

fn visit_base<F>(
    base: &RelationalRowPageRootReader,
    table: &str,
    work: Option<&crate::background::CheckpointWorkContext>,
    visitor: F,
) -> Result<(), RelationalRowPagePublicationError>
where
    F: FnMut(&RelationalRowPageRootDescriptor) -> Result<(), RelationalRowPagePublicationError>,
{
    match work {
        Some(work) => base.visit_table_pages_with_work_context(table, work, visitor),
        None => base.visit_table_pages(table, visitor),
    }
}

fn write_merged_table(
    writer: &mut RootWriter<'_>,
    base: &RelationalRowPageRootReader,
    table: &str,
    delta: PreparedTableDelta,
    bounds: &mut TableBounds,
) -> Result<u64, RelationalRowPagePublicationError> {
    debug_assert_eq!(delta.table, table);
    let mut dirty_page_ids = BTreeSet::new();
    for page in &delta.dirty_pages {
        let unit = writer.start_unit()?;
        dirty_page_ids.insert(page.descriptor.logical_page_id);
        if let Some(unit) = unit {
            unit.finish();
        }
    }
    let mut remaining_deleted = delta.deleted_page_ids;
    let mut dirty_index = 0usize;
    let mut reused = 0u64;
    let work = writer.work.clone();
    visit_base(base, table, work.as_ref(), |base_descriptor| {
        writer.checkpoint()?;
        let unit = writer.start_unit()?;
        let replaced_or_deleted = remaining_deleted.remove(&base_descriptor.logical_page_id)
            || dirty_page_ids.contains(&base_descriptor.logical_page_id);
        if let Some(unit) = unit {
            unit.finish();
        }
        if replaced_or_deleted {
            return Ok(());
        }
        while let Some(dirty) = delta.dirty_pages.get(dirty_index) {
            if writer.compare(&dirty.descriptor.lower_bound, &base_descriptor.lower_bound)?
                != std::cmp::Ordering::Less
            {
                break;
            }
            writer.write_descriptor(&dirty.descriptor, bounds)?;
            dirty_index += 1;
        }
        if writer.write_base_descriptor(base, base_descriptor, bounds)? {
            reused = reused.checked_add(1).ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "reused row-page count overflow".to_string(),
                )
            })?;
        }
        Ok(())
    })?;
    if !remaining_deleted.is_empty() {
        return Err(RelationalRowPagePublicationError::Admission(format!(
            "table {table} deletes {} page ids absent from the selected base",
            remaining_deleted.len()
        )));
    }
    for dirty in &delta.dirty_pages[dirty_index..] {
        writer.write_descriptor(&dirty.descriptor, bounds)?;
    }
    Ok(reused)
}

#[derive(Default)]
struct TableBounds {
    lower: Option<Vec<u8>>,
    upper: Option<Vec<u8>>,
    row_count: u64,
}

struct RootWriter<'a> {
    descriptors: BufWriter<File>,
    keys: BufWriter<File>,
    descriptor_hasher: IntegrityHasher,
    key_hasher: IntegrityHasher,
    descriptor_count: u64,
    key_bytes: u64,
    generation: u64,
    source_commit_epoch: u64,
    config: RelationalRowPagePublicationConfig,
    physical_generations: Vec<RelationalRowPagePhysicalGeneration>,
    pages: &'a mut PageArtifactWriter,
    work: Option<crate::background::CheckpointWorkContext>,
    rewrite: Option<RowPageRewriteControls<'a>>,
    relocated_page_count: u64,
}

impl RootWriter<'_> {
    fn checkpoint(&self) -> Result<(), RelationalRowPagePublicationError> {
        if let Some(work) = &self.work {
            work.checkpoint().map_err(checkpoint::work_error)?;
        }
        if let Some(rewrite) = self.rewrite {
            rewrite.checkpoint()?;
        }
        Ok(())
    }

    fn start_unit(
        &self,
    ) -> Result<Option<crate::background::CheckpointWorkUnit>, RelationalRowPagePublicationError>
    {
        self.work
            .as_ref()
            .map(|work| work.start_unit().map_err(checkpoint::work_error))
            .transpose()
    }

    fn clone_name(&self, name: &str) -> Result<String, RelationalRowPagePublicationError> {
        match &self.work {
            Some(work) => crate::relational::codec::clone_string_with_work_context(name, work)
                .map_err(|error| RelationalRowPagePublicationError::Admission(error.to_string())),
            None => Ok(name.to_string()),
        }
    }

    fn compare(
        &self,
        left: &[u8],
        right: &[u8],
    ) -> Result<std::cmp::Ordering, RelationalRowPagePublicationError> {
        match &self.work {
            Some(work) => checkpoint::compare(left, right, work),
            None => Ok(left.cmp(right)),
        }
    }

    fn clone_bound(&self, bytes: &[u8]) -> Result<Vec<u8>, RelationalRowPagePublicationError> {
        match &self.work {
            Some(work) => checkpoint::clone_bytes(bytes, work),
            None => Ok(bytes.to_vec()),
        }
    }

    fn write_base_descriptor(
        &mut self,
        base: &RelationalRowPageRootReader,
        descriptor: &RelationalRowPageRootDescriptor,
        bounds: &mut TableBounds,
    ) -> Result<bool, RelationalRowPagePublicationError> {
        self.checkpoint()?;
        let selected = self.rewrite.filter(|rewrite| {
            base.manifest
                .physical_generations
                .binary_search_by_key(&descriptor.physical_generation, |entry| entry.generation)
                .is_ok_and(|index| rewrite.selects(&base.manifest.physical_generations[index]))
        });
        let Some(rewrite) = selected else {
            self.write_descriptor(descriptor, bounds)?;
            return Ok(true);
        };
        let next_count = self.relocated_page_count.checked_add(1).ok_or_else(|| {
            RelationalRowPagePublicationError::Admission(
                "row-page relocation count overflow".to_string(),
            )
        })?;
        let rewrite_bytes = next_count
            .checked_mul(self.config.page_limits.max_page_bytes.get() as u64)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "row-page relocation byte count overflow".to_string(),
                )
            })?;
        if rewrite_bytes > rewrite.config.max_rewrite_bytes.get() {
            return Err(RelationalRowPagePublicationError::Admission(
                "row-page relocation exceeds the rewrite byte limit".to_string(),
            ));
        }
        let mut page = base.read_page(descriptor)?;
        page.generation = self.generation;
        let mut page = prepare_dirty_page_with_work_context(
            page,
            self.config.page_limits,
            self.pages.work.as_ref(),
        )?;
        self.pages.write(&mut page)?;
        self.write_descriptor(&page.descriptor, bounds)?;
        self.relocated_page_count = next_count;
        Ok(false)
    }

    fn write_descriptor(
        &mut self,
        descriptor: &RelationalRowPageRootDescriptor,
        table_bounds: &mut TableBounds,
    ) -> Result<(), RelationalRowPagePublicationError> {
        self.checkpoint()?;
        match &self.work {
            Some(work) => checkpoint::validate_descriptor(
                descriptor,
                self.generation,
                self.source_commit_epoch,
                self.config,
                work,
            )?,
            None => validate_descriptor(
                descriptor,
                self.generation,
                self.source_commit_epoch,
                self.config,
            )?,
        }
        let unit = self.start_unit()?;
        let index = self
            .physical_generations
            .binary_search_by_key(&descriptor.physical_generation, |entry| entry.generation)
            .map_err(|_| {
                RelationalRowPagePublicationError::Corrupt(
                    "row-page descriptor references an unaccounted physical generation".to_string(),
                )
            })?;
        let occupancy = &mut self.physical_generations[index];
        occupancy.live_pages = occupancy.live_pages.checked_add(1).ok_or_else(|| {
            RelationalRowPagePublicationError::Admission(
                "row-page live-page count overflow".to_string(),
            )
        })?;
        if occupancy.generation == self.generation {
            occupancy.allocated_pages = occupancy.live_pages;
        } else if occupancy.live_pages > occupancy.allocated_pages
            || descriptor.physical_slot >= occupancy.allocated_pages
        {
            return Err(RelationalRowPagePublicationError::Corrupt(
                "row-page root exceeds the accounted physical allocation".to_string(),
            ));
        }
        if let Some(unit) = unit {
            unit.finish();
        }
        if table_bounds
            .upper
            .as_ref()
            .map(|upper| self.compare(upper, &descriptor.lower_bound))
            .transpose()?
            .is_some_and(|ordering| ordering != std::cmp::Ordering::Less)
        {
            return Err(RelationalRowPagePublicationError::Admission(
                "row-page root bounds overlap or are unordered".to_string(),
            ));
        }
        if self.descriptor_count >= self.config.max_root_pages.get() {
            return Err(RelationalRowPagePublicationError::Admission(format!(
                "row-page root exceeds page limit {}",
                self.config.max_root_pages
            )));
        }
        let lower_offset = self.key_bytes;
        self.write_key(&descriptor.lower_bound)?;
        let upper_offset = self.key_bytes;
        self.write_key(&descriptor.upper_bound)?;
        let unit = self.start_unit()?;
        let wire = WireDescriptor {
            logical_page_id: descriptor.logical_page_id.get(),
            physical_generation: descriptor.physical_generation,
            physical_slot: descriptor.physical_slot,
            source_commit_epoch: descriptor.source_commit_epoch,
            row_count: descriptor.row_count,
            encoded_len: descriptor.slot_integrity.encoded_len,
            lower_offset,
            lower_len: descriptor.lower_bound.len() as u32,
            upper_offset,
            upper_len: descriptor.upper_bound.len() as u32,
            slot_crc32c: descriptor.slot_integrity.slot_crc32c,
            slot_sha256: descriptor.slot_integrity.slot_sha256,
            binding_crc32c: 0,
            binding_sha256: Sha256Digest::from_bytes([0; SHA256_BYTES]),
        };
        let mut encoded = wire.encode();
        if let Some(unit) = unit {
            unit.finish();
        }
        let mut hasher = IntegrityHasher::new();
        for bytes in [
            &self.generation.to_le_bytes()[..],
            &self.descriptor_count.to_le_bytes()[..],
            &encoded[..ROOT_DESCRIPTOR_BINDING_OFFSET],
            &descriptor.lower_bound,
            &descriptor.upper_bound,
        ] {
            match &self.work {
                Some(work) => checkpoint::hash(&mut hasher, bytes, work)?,
                None => hasher.update(bytes),
            }
        }
        let unit = self.start_unit()?;
        let digest = hasher.finish();
        encoded[100..104].copy_from_slice(&digest.crc32c.get().to_le_bytes());
        encoded[104..136].copy_from_slice(digest.sha256.as_bytes());
        if let Some(unit) = unit {
            unit.finish();
        }
        if let Some(work) = &self.work {
            checkpoint::write_bytes(
                &mut self.descriptors,
                &mut self.descriptor_hasher,
                &encoded,
                "write row-page root descriptor",
                work,
            )?;
        } else {
            self.descriptors
                .write_all(&encoded)
                .map_err(durability("write row-page root descriptor"))?;
            self.descriptor_hasher.update(&encoded);
        }
        let unit = self.start_unit()?;
        self.descriptor_count += 1;
        table_bounds.row_count = table_bounds
            .row_count
            .checked_add(u64::from(descriptor.row_count))
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "row-page table row count overflow".to_string(),
                )
            })?;
        if let Some(unit) = unit {
            unit.finish();
        }
        if table_bounds.lower.is_none() {
            table_bounds.lower = Some(self.clone_bound(&descriptor.lower_bound)?);
        }
        table_bounds.upper = Some(self.clone_bound(&descriptor.upper_bound)?);
        Ok(())
    }

    fn write_key(&mut self, key: &[u8]) -> Result<(), RelationalRowPagePublicationError> {
        let next = self
            .key_bytes
            .checked_add(key.len() as u64)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "row-page root key byte count overflow".to_string(),
                )
            })?;
        if next > self.config.max_root_key_bytes.get() {
            return Err(RelationalRowPagePublicationError::Admission(format!(
                "row-page root keys contain {next} bytes, exceeding limit {}",
                self.config.max_root_key_bytes
            )));
        }
        if let Some(work) = &self.work {
            checkpoint::write_bytes(
                &mut self.keys,
                &mut self.key_hasher,
                key,
                "write row-page root key",
                work,
            )?;
        } else {
            self.keys
                .write_all(key)
                .map_err(durability("write row-page root key"))?;
            self.key_hasher.update(key);
        }
        self.key_bytes = next;
        Ok(())
    }

    fn finish(mut self) -> Result<FinishedRootWriter, RelationalRowPagePublicationError> {
        if let Some(work) = &self.work {
            checkpoint::flush(
                &mut self.descriptors,
                "flush row-page root descriptors",
                work,
            )?;
            checkpoint::flush(&mut self.keys, "flush row-page root keys", work)?;
            checkpoint::sync(&self.descriptors, "sync row-page root descriptors", work)?;
            checkpoint::sync(&self.keys, "sync row-page root keys", work)?;
        } else {
            self.descriptors
                .flush()
                .map_err(durability("flush row-page root descriptors"))?;
            self.keys
                .flush()
                .map_err(durability("flush row-page root keys"))?;
            self.descriptors
                .get_ref()
                .sync_all()
                .map_err(durability("sync row-page root descriptors"))?;
            self.keys
                .get_ref()
                .sync_all()
                .map_err(durability("sync row-page root keys"))?;
        }
        let descriptor_len = self
            .descriptor_count
            .checked_mul(ROOT_DESCRIPTOR_BYTES as u64)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "row-page descriptor byte count overflow".to_string(),
                )
            })?;
        let mut retained = 0;
        for index in 0..self.physical_generations.len() {
            let unit = self.start_unit()?;
            if self.physical_generations[index].live_pages != 0 {
                self.physical_generations.swap(retained, index);
                retained += 1;
            }
            if let Some(unit) = unit {
                unit.finish();
            }
        }
        self.physical_generations.truncate(retained);
        self.checkpoint()?;
        let descriptor_digest = self.descriptor_hasher.finish();
        let key_digest = self.key_hasher.finish();
        Ok(FinishedRootWriter {
            root_page_count: self.descriptor_count,
            descriptor_artifact: RelationalRowPageArtifactMetadata {
                encoded_len: descriptor_len,
                encoded_crc32c: descriptor_digest.crc32c.get(),
                encoded_sha256: descriptor_digest.sha256,
            },
            key_artifact: RelationalRowPageArtifactMetadata {
                encoded_len: self.key_bytes,
                encoded_crc32c: key_digest.crc32c.get(),
                encoded_sha256: key_digest.sha256,
            },
            physical_generations: self.physical_generations,
            relocated_page_count: self.relocated_page_count,
        })
    }
}

struct FinishedRootWriter {
    root_page_count: u64,
    descriptor_artifact: RelationalRowPageArtifactMetadata,
    key_artifact: RelationalRowPageArtifactMetadata,
    physical_generations: Vec<RelationalRowPagePhysicalGeneration>,
    relocated_page_count: u64,
}

fn digest_bytes(bytes: &[u8]) -> RelationalRowPageArtifactMetadata {
    let mut hasher = IntegrityHasher::new();
    hasher.update(bytes);
    let digest = hasher.finish();
    RelationalRowPageArtifactMetadata {
        encoded_len: bytes.len() as u64,
        encoded_crc32c: digest.crc32c.get(),
        encoded_sha256: digest.sha256,
    }
}