armdb 0.15.0

sharded bitcask key-value storage optimized for NVMe
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
//! The sidecar of a sealed segment, and the file header both segment files
//! share.
//!
//! Every integer is little-endian. The offsets below are pinned by the
//! golden-bytes tests of this module.
//!
//! File header of `.sdat` and `.sidx`, 64 bytes:
//!
//! ```text
//! [0..4]   magic              "ASD1" (.sdat) or "ASX1" (.sidx)
//! [4..6]   format_version     1
//! [6]      byte_order         1 = little-endian entries, 2 = big-endian
//! [7]      order_kind         0 = ByHash, 1 = ByKey
//! [8..16]  segment_id
//! [16..20] version
//! [20..22] key_len
//! [22..24] reserved           must be 0
//! [24..32] seed
//! [32..36] block_size
//! [36..60] reserved           must be 0
//! [60..64] crc32              over bytes 0..60
//! ```
//!
//! A sidecar is the header, then the sections back to back, then the section
//! table, then the footer:
//!
//! ```text
//! section table: one 32-byte record per section
//!   [0..4]   kind               below 0x1000 required, from 0x1000 optional
//!   [4..8]   reserved           must be 0
//!   [8..16]  offset             from the start of the file
//!   [16..24] len
//!   [24..28] crc32              of the section bytes
//!   [28..32] reserved           must be 0
//! footer, the last 24 bytes of the file:
//!   [0..4]   section_count
//!   [4..8]   crc32              of the section table
//!   [8..16]  table_offset
//!   [16..20] magic              "ASXF"
//!   [20..24] reserved           must be 0
//! ```
//!
//! Block index section (kind 1): one 24-byte record per block, then the end
//! offset of the last block.
//!
//! ```text
//! [0..8]   first_h            hash of the block's first entry
//! [8..16]  offset             of the block in .sdat
//! [16..20] block_len          header included
//! [20..24] entries
//! ...
//! [n*24 .. n*24+8]  end offset of the last block
//! ```
//!
//! Filter section (kind 2): see [`super::filter`].
//!
//! Key-ordered segments use the separator index (kind 4) and fixed-width
//! key range (kind 5) from [`super::key_index`] instead of kind 1. Their
//! optional prefix filters (kind 0x1001) start with a u16 prefix length,
//! six zero reserved bytes, and a normal partitioned filter body.
//! Hash-ordered readers CRC-check and skip these optional prefix sections.
//!
//! Properties section (kind 3):
//!
//! ```text
//! [0..8]   entries            [48..56] raw_bytes
//! [8..16]  tombstones         [56..64] opened_at_ms
//! [16..24] min_gsn            [64..72] frozen_at_ms
//! [24..32] max_gsn            [72..80] sealed_at_ms
//! [32..40] min_h              [80..88] typ_hash
//! [40..48] max_h              [88..90] value_version
//! [90..92] writer_len
//! [92..]   writer             UTF-8, writer_len bytes
//! ```

use std::sync::Arc;

use super::FormatFault;
use super::block::BLOCK_HEADER_LEN;
use super::filter::PartitionedFilter;
use super::key_index::{KeyRange, SegmentIndex, SeparatorIndex};
use super::page_index::PageIndex;
use crate::{DbError, DbResult};

pub(crate) const SDAT_MAGIC: [u8; 4] = *b"ASD1";
pub(crate) const SIDX_MAGIC: [u8; 4] = *b"ASX1";
pub(crate) const SIDX_FOOTER_MAGIC: [u8; 4] = *b"ASXF";
pub(crate) const FORMAT_VERSION: u16 = 1;
pub(crate) const FILE_HEADER_LEN: usize = 64;
pub(crate) const SIDX_FOOTER_LEN: usize = 24;
pub(crate) const SECTION_RECORD_LEN: usize = 32;

pub(crate) const SECTION_BLOCK_INDEX: u32 = 1;
pub(crate) const SECTION_FILTER: u32 = 2;
pub(crate) const SECTION_PROPERTIES: u32 = 3;
pub(crate) const SECTION_SEPARATOR_INDEX: u32 = 4;
pub(crate) const SECTION_KEY_RANGE: u32 = 5;
pub(crate) const SECTION_PAGE_INDEX: u32 = 0x1002;
pub(crate) const SECTION_PREFIX_FILTER: u32 = 0x1001;
/// A reader skips a section kind from here up when it does not know it.
pub(crate) const OPTIONAL_SECTION_MIN: u32 = 0x1000;

const INDEX_RECORD_LEN: usize = 24;
const HEADER_CRC_AT: usize = 60;

#[cfg(target_endian = "little")]
pub(crate) const NATIVE_BYTE_ORDER: u8 = 1;
#[cfg(target_endian = "big")]
pub(crate) const NATIVE_BYTE_ORDER: u8 = 2;

fn u16_at(b: &[u8], at: usize) -> u16 {
    u16::from_le_bytes(b[at..at + 2].try_into().expect("2 bytes"))
}

fn u32_at(b: &[u8], at: usize) -> u32 {
    u32::from_le_bytes(b[at..at + 4].try_into().expect("4 bytes"))
}

fn u64_at(b: &[u8], at: usize) -> u64 {
    u64::from_le_bytes(b[at..at + 8].try_into().expect("8 bytes"))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub(crate) enum OrderKind {
    /// Entries ordered by `(h, key bytes)`. The Map shape.
    ByHash = 0,
    /// Entries ordered by key bytes. The Tree shape.
    ByKey = 1,
}

impl OrderKind {
    pub(crate) fn from_byte(b: u8) -> Option<Self> {
        match b {
            0 => Some(Self::ByHash),
            1 => Some(Self::ByKey),
            _ => None,
        }
    }
}

/// The header `.sdat` and `.sidx` both start with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct SegHeader {
    pub segment_id: u64,
    pub version: u32,
    pub order: OrderKind,
    pub key_len: u16,
    pub seed: u64,
    /// Target block size the segment was written with.
    pub block_size: u32,
}

impl SegHeader {
    pub(crate) fn encode(&self, magic: [u8; 4]) -> [u8; FILE_HEADER_LEN] {
        let mut b = [0u8; FILE_HEADER_LEN];
        b[0..4].copy_from_slice(&magic);
        b[4..6].copy_from_slice(&FORMAT_VERSION.to_le_bytes());
        b[6] = NATIVE_BYTE_ORDER;
        b[7] = self.order as u8;
        b[8..16].copy_from_slice(&self.segment_id.to_le_bytes());
        b[16..20].copy_from_slice(&self.version.to_le_bytes());
        b[20..22].copy_from_slice(&self.key_len.to_le_bytes());
        b[24..32].copy_from_slice(&self.seed.to_le_bytes());
        b[32..36].copy_from_slice(&self.block_size.to_le_bytes());
        let crc = crc32fast::hash(&b[..HEADER_CRC_AT]);
        b[HEADER_CRC_AT..].copy_from_slice(&crc.to_le_bytes());
        b
    }

    /// The CRC is checked before `format_version` is read, so the magic, the
    /// `format_version` field and the CRC position (bytes 60..64, over
    /// 0..60) are frozen across format versions: a later version may change
    /// any other byte, but not these, or an older build reads its files as
    /// damaged instead of as unsupported.
    pub(crate) fn decode(bytes: &[u8], magic: [u8; 4]) -> Result<Self, FormatFault> {
        if bytes.len() < FILE_HEADER_LEN {
            return Err(FormatFault::corrupt("file is shorter than its header"));
        }
        let b = &bytes[..FILE_HEADER_LEN];
        if b[0..4] != magic {
            return Err(FormatFault::corrupt(format!(
                "bad magic {:?}, expected {:?}",
                &b[0..4],
                magic
            )));
        }
        let stored = u32_at(b, HEADER_CRC_AT);
        let computed = crc32fast::hash(&b[..HEADER_CRC_AT]);
        if stored != computed {
            return Err(FormatFault::corrupt(format!(
                "header CRC mismatch: stored {stored:#x}, computed {computed:#x}"
            )));
        }
        let version = u16_at(b, 4);
        if version != FORMAT_VERSION {
            return Err(FormatFault::unsupported(format!(
                "format version {version}"
            )));
        }
        if b[6] != NATIVE_BYTE_ORDER {
            return Err(FormatFault::unsupported(format!(
                "entries were written in byte order {}, this build reads {NATIVE_BYTE_ORDER}",
                b[6]
            )));
        }
        let order = match OrderKind::from_byte(b[7]) {
            Some(order) => order,
            None => {
                return Err(FormatFault::unsupported(format!("entry order {}", b[7])));
            }
        };
        if u16_at(b, 22) != 0 || b[36..HEADER_CRC_AT].iter().any(|&x| x != 0) {
            return Err(FormatFault::unsupported("header reserved bytes are set"));
        }
        Ok(Self {
            segment_id: u64_at(b, 8),
            version: u32_at(b, 16),
            order,
            key_len: u16_at(b, 20),
            seed: u64_at(b, 24),
            block_size: u32_at(b, 32),
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct BlockIndexRecord {
    pub first_h: u64,
    pub offset: u64,
    /// Length of the whole block in `.sdat`, header included.
    pub block_len: u32,
    pub entries: u32,
}

/// Section bytes: one 24-byte record per block, then the end offset of the
/// last block.
pub(crate) struct BlockIndexBuilder {
    records: Vec<u8>,
    end: u64,
    count: usize,
}

impl BlockIndexBuilder {
    pub(crate) fn new() -> Self {
        Self {
            records: Vec::new(),
            end: FILE_HEADER_LEN as u64,
            count: 0,
        }
    }

    /// Record the block that starts where the previous one ended.
    pub(crate) fn push(&mut self, first_h: u64, block_len: u32, entries: u32) {
        self.records.extend_from_slice(&first_h.to_le_bytes());
        self.records.extend_from_slice(&self.end.to_le_bytes());
        self.records.extend_from_slice(&block_len.to_le_bytes());
        self.records.extend_from_slice(&entries.to_le_bytes());
        self.end += u64::from(block_len);
        self.count += 1;
    }

    pub(crate) fn end_offset(&self) -> u64 {
        self.end
    }

    pub(crate) fn finish(mut self) -> Vec<u8> {
        self.records.extend_from_slice(&self.end.to_le_bytes());
        self.records
    }
}

/// Blocks are numbered with a `u32` in cache keys and errors, so an index
/// may not list more.
fn check_block_count(count: usize) -> Result<(), FormatFault> {
    if u32::try_from(count).is_err() {
        return Err(FormatFault::corrupt(format!(
            "block index lists {count} blocks, more than u32::MAX"
        )));
    }
    Ok(())
}

/// Records of a block index section. At most `u32::MAX` of them, so a block
/// number always fits a `u32`.
pub(crate) fn parse_index_records(
    section: &[u8],
) -> Result<(Vec<BlockIndexRecord>, u64), FormatFault> {
    if section.len() < 8 || !(section.len() - 8).is_multiple_of(INDEX_RECORD_LEN) {
        return Err(FormatFault::corrupt(format!(
            "block index section of {} bytes is not a whole number of records",
            section.len()
        )));
    }
    let count = (section.len() - 8) / INDEX_RECORD_LEN;
    check_block_count(count)?;
    let end = u64_at(section, count * INDEX_RECORD_LEN);

    let records = parse_index_run(&section[..count * INDEX_RECORD_LEN], 0, end)?;
    Ok((records, end))
}

/// Decode and validate one contiguous record run, closed by `end`.
/// Full indexes and cold groups share these structural invariants.
pub(crate) fn parse_index_run(
    section: &[u8],
    first_block: usize,
    end: u64,
) -> Result<Vec<BlockIndexRecord>, FormatFault> {
    if !section.len().is_multiple_of(INDEX_RECORD_LEN) {
        return Err(FormatFault::corrupt("block index run has a partial record"));
    }
    let count = section.len() / INDEX_RECORD_LEN;
    let mut records = Vec::with_capacity(count);
    let mut expected_offset: Option<u64> = None;
    let mut previous_h: Option<u64> = None;
    for i in 0..count {
        let at = i * INDEX_RECORD_LEN;
        let rec = BlockIndexRecord {
            first_h: u64_at(section, at),
            offset: u64_at(section, at + 8),
            block_len: u32_at(section, at + 16),
            entries: u32_at(section, at + 20),
        };
        if rec.offset < FILE_HEADER_LEN as u64 {
            return Err(FormatFault::corrupt(format!(
                "block {i} starts at {} inside the file header",
                rec.offset
            )));
        }
        if first_block == 0 && i == 0 && rec.offset != FILE_HEADER_LEN as u64 {
            return Err(FormatFault::corrupt(format!(
                "first block starts at {}, expected {} after the file header",
                rec.offset, FILE_HEADER_LEN
            )));
        }
        if let Some(expected) = expected_offset
            && rec.offset != expected
        {
            return Err(FormatFault::corrupt(format!(
                "block {i} starts at {}, blocks are not contiguous (expected {expected})",
                rec.offset
            )));
        }
        if previous_h.is_some_and(|h| h >= rec.first_h) {
            return Err(FormatFault::corrupt(format!(
                "block {i} is out of order: first hash {:#x}",
                rec.first_h
            )));
        }
        if (rec.block_len as usize) <= BLOCK_HEADER_LEN || rec.entries == 0 {
            return Err(FormatFault::corrupt(format!("block {i} is empty")));
        }
        expected_offset = Some(
            rec.offset
                .checked_add(u64::from(rec.block_len))
                .ok_or_else(|| FormatFault::corrupt("block index range overflows u64"))?,
        );
        previous_h = Some(rec.first_h);
        records.push(rec);
    }
    let expected_end = expected_offset.unwrap_or(FILE_HEADER_LEN as u64);
    if end != expected_end {
        return Err(FormatFault::corrupt(format!(
            "block index ends at {end}, its blocks end at {expected_end}"
        )));
    }
    Ok(records)
}

/// The block index as it stays in RAM: 16 bytes per block plus the end
/// offset. Block `i` occupies `offsets[i] .. offsets[i + 1]`.
pub(crate) struct BlockIndex {
    first_h: Vec<u64>,
    offsets: Vec<u64>,
}

impl BlockIndex {
    pub(crate) fn parse(section: &[u8]) -> Result<Self, FormatFault> {
        let (records, end) = parse_index_records(section)?;
        let mut first_h = Vec::with_capacity(records.len());
        let mut offsets = Vec::with_capacity(records.len() + 1);
        for rec in &records {
            first_h.push(rec.first_h);
            offsets.push(rec.offset);
        }
        offsets.push(end);
        Ok(Self { first_h, offsets })
    }

    pub(crate) fn len(&self) -> usize {
        self.first_h.len()
    }

    #[cfg(test)]
    pub(crate) fn is_empty(&self) -> bool {
        self.first_h.is_empty()
    }

    /// The only block that can hold `h`: the last one that starts at or
    /// before it.
    pub(crate) fn locate(&self, h: u64) -> Option<usize> {
        self.first_h
            .partition_point(|&first| first <= h)
            .checked_sub(1)
    }

    /// Offset and length of a block in `.sdat`.
    pub(crate) fn range(&self, block: usize) -> (u64, usize) {
        let start = self.offsets[block];
        (start, (self.offsets[block + 1] - start) as usize)
    }

    pub(crate) fn first_h(&self, block: usize) -> u64 {
        self.first_h[block]
    }

    pub(crate) fn end_offset(&self) -> u64 {
        *self
            .offsets
            .last()
            .expect("the end offset is always present")
    }

    pub(crate) fn resident_bytes(&self) -> usize {
        (self.first_h.len() + self.offsets.len()) * size_of::<u64>()
    }
}

const PROPS_FIXED_LEN: usize = 11 * 8 + 2 + 2;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Properties {
    pub entries: u64,
    pub tombstones: u64,
    pub min_gsn: u64,
    pub max_gsn: u64,
    pub min_h: u64,
    pub max_h: u64,
    /// Payload bytes before compression, all blocks.
    pub raw_bytes: u64,
    pub opened_at_ms: u64,
    pub frozen_at_ms: u64,
    pub sealed_at_ms: u64,
    pub typ_hash: u64,
    pub value_version: u16,
    /// Crate and version that wrote the segment.
    pub writer: String,
}

impl Properties {
    pub(crate) fn encode(&self) -> Vec<u8> {
        let writer = self.writer.as_bytes();
        let writer_len = u16::try_from(writer.len()).expect("properties writer exceeds u16 length");
        let mut out = Vec::with_capacity(PROPS_FIXED_LEN + writer.len());
        for v in [
            self.entries,
            self.tombstones,
            self.min_gsn,
            self.max_gsn,
            self.min_h,
            self.max_h,
            self.raw_bytes,
            self.opened_at_ms,
            self.frozen_at_ms,
            self.sealed_at_ms,
            self.typ_hash,
        ] {
            out.extend_from_slice(&v.to_le_bytes());
        }
        out.extend_from_slice(&self.value_version.to_le_bytes());
        out.extend_from_slice(&writer_len.to_le_bytes());
        out.extend_from_slice(writer);
        out
    }

    pub(crate) fn decode(section: &[u8]) -> Result<Self, FormatFault> {
        if section.len() < PROPS_FIXED_LEN {
            return Err(FormatFault::corrupt("properties section is truncated"));
        }
        let writer_len = u16_at(section, 90) as usize;
        if section.len() != PROPS_FIXED_LEN + writer_len {
            return Err(FormatFault::corrupt(format!(
                "properties section is {} bytes, expected {}",
                section.len(),
                PROPS_FIXED_LEN + writer_len
            )));
        }
        let writer = String::from_utf8(section[PROPS_FIXED_LEN..].to_vec())
            .map_err(|_| FormatFault::corrupt("properties writer is not UTF-8"))?;
        Ok(Self {
            entries: u64_at(section, 0),
            tombstones: u64_at(section, 8),
            min_gsn: u64_at(section, 16),
            max_gsn: u64_at(section, 24),
            min_h: u64_at(section, 32),
            max_h: u64_at(section, 40),
            raw_bytes: u64_at(section, 48),
            opened_at_ms: u64_at(section, 56),
            frozen_at_ms: u64_at(section, 64),
            sealed_at_ms: u64_at(section, 72),
            typ_hash: u64_at(section, 80),
            value_version: u16_at(section, 88),
            writer,
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct SectionRecord {
    pub kind: u32,
    pub offset: u64,
    pub len: u64,
    pub crc32: u32,
}

/// Header, sections in the order given, section table, footer.
pub(crate) fn encode_sidecar(header: &SegHeader, sections: &[(u32, Vec<u8>)]) -> Vec<u8> {
    let bodies: usize = sections.iter().map(|(_, s)| s.len()).sum();
    let mut out = Vec::with_capacity(
        FILE_HEADER_LEN + bodies + sections.len() * SECTION_RECORD_LEN + SIDX_FOOTER_LEN,
    );
    out.extend_from_slice(&header.encode(SIDX_MAGIC));

    let mut table = Vec::with_capacity(sections.len() * SECTION_RECORD_LEN);
    for (kind, body) in sections {
        table.extend_from_slice(&kind.to_le_bytes());
        table.extend_from_slice(&0u32.to_le_bytes());
        table.extend_from_slice(&(out.len() as u64).to_le_bytes());
        table.extend_from_slice(&(body.len() as u64).to_le_bytes());
        table.extend_from_slice(&crc32fast::hash(body).to_le_bytes());
        table.extend_from_slice(&0u32.to_le_bytes());
        out.extend_from_slice(body);
    }

    let table_offset = out.len() as u64;
    out.extend_from_slice(&table);
    out.extend_from_slice(&(sections.len() as u32).to_le_bytes());
    out.extend_from_slice(&crc32fast::hash(&table).to_le_bytes());
    out.extend_from_slice(&table_offset.to_le_bytes());
    out.extend_from_slice(&SIDX_FOOTER_MAGIC);
    out.extend_from_slice(&0u32.to_le_bytes());
    out
}

/// Append a page index while retaining the offsets of all described sections.
pub(crate) fn encode_sidecar_paged(
    header: &SegHeader,
    sections: &[(u32, Vec<u8>)],
) -> DbResult<Vec<u8>> {
    let derive = |bytes: &[u8]| -> Result<PageIndex, FormatFault> {
        let footer_at = bytes.len() - SIDX_FOOTER_LEN;
        let (at, count, crc) = parse_footer(&bytes[footer_at..], bytes.len() as u64)?;
        let records = parse_table(&bytes[at as usize..footer_at], count, crc, at)?;
        PageIndex::derive(bytes, &records, header)
    };
    let bytes = encode_sidecar(header, sections);
    let page_index = derive(&bytes).map_err(|_| DbError::Internal("segment writer: page index"))?;
    let mut sections = sections.to_vec();
    sections.push((SECTION_PAGE_INDEX, page_index.encode()));
    let bytes = encode_sidecar(header, &sections);
    debug_assert_eq!(derive(&bytes), Ok(page_index));
    Ok(bytes)
}

/// `(table_offset, section_count, table_crc)`, checked against the file
/// length.
///
/// The footer magic and its position at the end of the file are frozen
/// across format versions, like the file header's magic and CRC.
pub(crate) fn parse_footer(footer: &[u8], file_len: u64) -> Result<(u64, u32, u32), FormatFault> {
    if footer.len() != SIDX_FOOTER_LEN {
        return Err(FormatFault::corrupt("sidecar footer is truncated"));
    }
    if footer[16..20] != SIDX_FOOTER_MAGIC {
        return Err(FormatFault::corrupt("bad sidecar footer magic"));
    }
    // No CRC covers the footer, so a set reserved byte cannot be told apart
    // from damage. The section table's reserved fields, which its CRC does
    // cover, are the place for a future version to signal a new layout.
    if u32_at(footer, 20) != 0 {
        return Err(FormatFault::corrupt(
            "sidecar footer reserved bytes are set",
        ));
    }
    let count = u32_at(footer, 0);
    let table_crc = u32_at(footer, 4);
    let table_offset = u64_at(footer, 8);
    let expected_len = table_offset
        .checked_add(u64::from(count) * SECTION_RECORD_LEN as u64)
        .and_then(|n| n.checked_add(SIDX_FOOTER_LEN as u64));
    if table_offset < FILE_HEADER_LEN as u64 || expected_len != Some(file_len) {
        return Err(FormatFault::corrupt(format!(
            "sidecar footer places {count} sections at {table_offset} in a file of {file_len} bytes"
        )));
    }
    Ok((table_offset, count, table_crc))
}

/// Section records, checked to lie between the header and the table and not
/// to overlap.
pub(crate) fn parse_table(
    table: &[u8],
    count: u32,
    table_crc: u32,
    table_offset: u64,
) -> Result<Vec<SectionRecord>, FormatFault> {
    if table.len() != count as usize * SECTION_RECORD_LEN {
        return Err(FormatFault::corrupt("sidecar section table is truncated"));
    }
    let computed = crc32fast::hash(table);
    if computed != table_crc {
        return Err(FormatFault::corrupt(format!(
            "sidecar section table CRC mismatch: stored {table_crc:#x}, computed {computed:#x}"
        )));
    }
    let mut records = Vec::with_capacity(count as usize);
    for i in 0..count as usize {
        let rec = &table[i * SECTION_RECORD_LEN..(i + 1) * SECTION_RECORD_LEN];
        if u32_at(rec, 4) != 0 || u32_at(rec, 28) != 0 {
            return Err(FormatFault::unsupported(format!(
                "section {i} reserved bytes are set"
            )));
        }
        let record = SectionRecord {
            kind: u32_at(rec, 0),
            offset: u64_at(rec, 8),
            len: u64_at(rec, 16),
            crc32: u32_at(rec, 24),
        };
        if record.offset < FILE_HEADER_LEN as u64 {
            return Err(FormatFault::corrupt(format!(
                "section {i} starts at {} inside the file header",
                record.offset
            )));
        }
        match record.offset.checked_add(record.len) {
            Some(end) if end <= table_offset => {}
            _ => {
                return Err(FormatFault::corrupt(format!(
                    "section {i} ({} bytes at {}) runs past the section table at {table_offset}",
                    record.len, record.offset
                )));
            }
        }
        records.push(record);
    }
    let mut by_offset: Vec<&SectionRecord> = records.iter().collect();
    by_offset.sort_by_key(|r| r.offset);
    for pair in by_offset.windows(2) {
        if pair[0].offset + pair[0].len > pair[1].offset {
            return Err(FormatFault::corrupt(format!(
                "sections of kind {} and {} overlap",
                pair[0].kind, pair[1].kind
            )));
        }
    }
    Ok(records)
}

/// Validate table-level structure without reading pageable section bodies.
/// Prefix identities and parameters are checked by `PageIndex::decode` or the
/// full parser, because prefix lengths live in the section body.
pub(crate) fn check_section_table(
    records: &[SectionRecord],
    header: &SegHeader,
) -> Result<(), FormatFault> {
    let mut seen = std::collections::HashSet::new();
    for rec in records {
        if rec.kind == SECTION_PREFIX_FILTER {
            if header.order == OrderKind::ByKey && rec.len < 8 {
                return Err(FormatFault::corrupt("invalid prefix filter header"));
            }
            continue;
        }
        if rec.kind >= OPTIONAL_SECTION_MIN && rec.kind != SECTION_PAGE_INDEX {
            continue;
        }
        if !seen.insert(rec.kind) {
            return Err(FormatFault::corrupt(format!(
                "duplicate {} section",
                section_name(rec.kind)
            )));
        }
        match rec.kind {
            SECTION_BLOCK_INDEX if header.order == OrderKind::ByHash => {
                if rec.len < 8 || !(rec.len - 8).is_multiple_of(24) {
                    return Err(FormatFault::corrupt("invalid block index length"));
                }
            }
            SECTION_SEPARATOR_INDEX | SECTION_KEY_RANGE if header.order == OrderKind::ByKey => {}
            SECTION_BLOCK_INDEX | SECTION_SEPARATOR_INDEX | SECTION_KEY_RANGE => {
                return Err(FormatFault::corrupt(
                    "sidecar sections differ from entry order",
                ));
            }
            SECTION_FILTER | SECTION_PROPERTIES | SECTION_PAGE_INDEX => {}
            other => {
                return Err(FormatFault::unsupported(format!(
                    "required section kind {other}"
                )));
            }
        }
    }
    let index = if header.order == OrderKind::ByHash {
        SECTION_BLOCK_INDEX
    } else {
        SECTION_SEPARATOR_INDEX
    };
    for kind in [index, SECTION_FILTER, SECTION_PROPERTIES] {
        if !seen.contains(&kind) {
            return Err(FormatFault::corrupt(format!(
                "sidecar has no {} section",
                section_name(kind)
            )));
        }
    }
    if header.order == OrderKind::ByKey && !seen.contains(&SECTION_KEY_RANGE) {
        return Err(FormatFault::corrupt("sidecar has no key range section"));
    }
    Ok(())
}

fn section_name(kind: u32) -> &'static str {
    match kind {
        SECTION_BLOCK_INDEX => "block index",
        SECTION_PAGE_INDEX => "page index",
        SECTION_FILTER => "filter",
        SECTION_PROPERTIES => "properties",
        SECTION_SEPARATOR_INDEX => "separator index",
        SECTION_KEY_RANGE => "key range",
        SECTION_PREFIX_FILTER => "prefix filter",
        _ => "unknown",
    }
}

/// Everything a reader keeps from a sidecar.
pub(crate) struct Sidecar {
    pub header: SegHeader,
    pub index: SegmentIndex,
    pub key_range: Option<KeyRange>,
    pub prefix_filters: Vec<(u16, PartitionedFilter)>,
    pub filter: PartitionedFilter,
    pub props: Properties,
    pub page_index: Option<PageIndex>,
}

impl Sidecar {
    pub(crate) fn parse(bytes: &[u8]) -> Result<Self, FormatFault> {
        let header = SegHeader::decode(bytes, SIDX_MAGIC)?;
        if bytes.len() < FILE_HEADER_LEN + SIDX_FOOTER_LEN {
            return Err(FormatFault::corrupt("sidecar has no footer"));
        }
        let footer_at = bytes.len() - SIDX_FOOTER_LEN;
        let (table_offset, count, table_crc) =
            parse_footer(&bytes[footer_at..], bytes.len() as u64)?;
        let table = &bytes[table_offset as usize..footer_at];
        let records = parse_table(table, count, table_crc, table_offset)?;
        check_section_table(&records, &header)?;

        let mut index = None;
        let mut key_range = None;
        let mut prefix_filters: Vec<(u16, PartitionedFilter)> = Vec::new();
        let mut filter = None;
        let mut props = None;
        let mut page_index_body = None;
        for rec in &records {
            let body = &bytes[rec.offset as usize..(rec.offset + rec.len) as usize];
            let computed = crc32fast::hash(body);
            if computed != rec.crc32 {
                return Err(FormatFault::corrupt(format!(
                    "{} section CRC mismatch: stored {:#x}, computed {computed:#x}",
                    section_name(rec.kind),
                    rec.crc32
                )));
            }
            if rec.kind == SECTION_PREFIX_FILTER && header.order == OrderKind::ByKey {
                if body.len() < 8 || body[2..8].iter().any(|&b| b != 0) {
                    return Err(FormatFault::corrupt("invalid prefix filter header"));
                }
                let len = u16_at(body, 0);
                if len == 0
                    || len >= header.key_len
                    || prefix_filters.iter().any(|(p, _)| *p == len)
                {
                    return Err(FormatFault::corrupt("invalid or duplicate prefix length"));
                }
                prefix_filters.push((len, PartitionedFilter::parse(Arc::from(&body[8..]))?));
                continue;
            }
            if rec.kind == SECTION_PAGE_INDEX {
                if page_index_body.replace(body).is_some() {
                    return Err(FormatFault::corrupt("duplicate page index section"));
                }
                continue;
            }
            if rec.kind >= OPTIONAL_SECTION_MIN {
                continue;
            }
            if (header.order == OrderKind::ByHash
                && matches!(rec.kind, SECTION_SEPARATOR_INDEX | SECTION_KEY_RANGE))
                || (header.order == OrderKind::ByKey && rec.kind == SECTION_BLOCK_INDEX)
            {
                return Err(FormatFault::corrupt(
                    "sidecar sections differ from entry order",
                ));
            }
            match rec.kind {
                SECTION_BLOCK_INDEX if index.is_none() => {
                    index = Some(SegmentIndex::Hash(BlockIndex::parse(body)?))
                }
                SECTION_SEPARATOR_INDEX if index.is_none() => {
                    index = Some(SegmentIndex::Key(SeparatorIndex::parse(
                        body,
                        header.key_len as usize,
                    )?))
                }
                SECTION_KEY_RANGE if key_range.is_none() => {
                    key_range = Some(KeyRange::decode(body, header.key_len as usize)?)
                }
                SECTION_FILTER if filter.is_none() => {
                    filter = Some(PartitionedFilter::parse(Arc::from(body))?);
                }
                SECTION_PROPERTIES if props.is_none() => props = Some(Properties::decode(body)?),
                SECTION_BLOCK_INDEX
                | SECTION_SEPARATOR_INDEX
                | SECTION_KEY_RANGE
                | SECTION_FILTER
                | SECTION_PROPERTIES => {
                    return Err(FormatFault::corrupt(format!(
                        "duplicate {} section",
                        section_name(rec.kind)
                    )));
                }
                other => {
                    return Err(FormatFault::unsupported(format!(
                        "required section kind {other}"
                    )));
                }
            }
        }
        let page_index = page_index_body
            .map(|body| {
                let Some(decoded) = PageIndex::decode_known(body, &records, &header)? else {
                    return Ok(None);
                };
                if decoded != PageIndex::derive(bytes, &records, &header)? {
                    return Err(FormatFault::corrupt(
                        "page index disagrees with the sections it describes",
                    ));
                }
                Ok(Some(decoded))
            })
            .transpose()?
            .flatten();
        let missing = |name: &str| FormatFault::corrupt(format!("sidecar has no {name} section"));
        if header.order == OrderKind::ByKey && key_range.is_none() {
            return Err(missing("key range"));
        }
        Ok(Self {
            header,
            page_index,
            key_range,
            prefix_filters,
            index: index.ok_or_else(|| missing("block index"))?,
            filter: filter.ok_or_else(|| missing("filter"))?,
            props: props.ok_or_else(|| missing("properties"))?,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::seg::filter::PartitionedFilterBuilder;

    #[test]
    fn a_page_index_that_disagrees_with_its_sections_fails_the_parse() {
        use crate::seg::page_index::{GroupSamples, PageIndex};
        let bytes = sidecar_bytes();
        let footer_at = bytes.len() - SIDX_FOOTER_LEN;
        let (at, count, crc) = parse_footer(&bytes[footer_at..], bytes.len() as u64).unwrap();
        let records = parse_table(&bytes[at as usize..footer_at], count, crc, at).unwrap();
        let mut page_index = PageIndex::derive(&bytes, &records, &header()).unwrap();
        let GroupSamples::Hash(samples) = &mut page_index.index.samples else {
            panic!("hash fixture")
        };
        samples[0] += 1;
        let mut sections: Vec<_> = records
            .iter()
            .map(|r| {
                (
                    r.kind,
                    bytes[r.offset as usize..(r.offset + r.len) as usize].to_vec(),
                )
            })
            .collect();
        sections.push((SECTION_PAGE_INDEX, page_index.encode()));
        let bytes = encode_sidecar(&header(), &sections);
        assert!(matches!(
            Sidecar::parse(&bytes),
            Err(FormatFault::Corrupt(_))
        ));
    }

    #[test]
    fn a_sidecar_without_a_page_index_still_parses() {
        use crate::seg::{seal::sidx_name, segment::Segment, testkit};
        let dir = tempfile::tempdir().unwrap();
        for rec in [
            testkit::write_legacy_segment(
                crate::seg::fs::real(),
                dir.path(),
                1,
                &testkit::entries(3_000, 1),
            ),
            testkit::write_legacy_key_segment(dir.path(), &testkit::key_entries()),
        ] {
            let bytes =
                std::fs::read(dir.path().join(sidx_name(rec.segment_id, rec.version))).unwrap();
            let sidecar = Sidecar::parse(&bytes).unwrap();
            assert!(sidecar.page_index.is_none());
            assert_eq!(rec.sidx_len, bytes.len() as u64);
            assert_eq!(rec.sidx_checksum, xxhash_rust::xxh3::xxh3_64(&bytes));
            let segment = Segment::open(
                crate::seg::fs::real(),
                dir.path(),
                &rec,
                testkit::KEY_LEN as u16,
                testkit::SEED,
                &std::sync::Arc::new(crate::seg::core::SegShared::new()),
            )
            .unwrap();
            let mut report = crate::seg::verify::VerifyReport::default();
            crate::seg::verify::verify_segment(&segment, &mut report).unwrap();
            assert!(report.is_clean(), "{:?}", report.problems);
        }
    }

    #[test]
    fn rebuilt_crcs_do_not_hide_a_malformed_page_index() {
        use crate::seg::{seal::sidx_name, testkit};
        for ordered in [false, true] {
            for mutation in [0, 1, 2] {
                let dir = tempfile::tempdir().unwrap();
                let rec = if ordered {
                    testkit::write_key_segment(dir.path(), &testkit::key_entries())
                } else {
                    testkit::write_segment(
                        crate::seg::fs::real(),
                        dir.path(),
                        1,
                        &testkit::entries(3_000, 1),
                    )
                };
                let rec = testkit::rewrite_section(dir.path(), &rec, SECTION_PAGE_INDEX, |body| {
                    match mutation {
                        0 => {
                            let at = body.len() - 1;
                            body[at] ^= 1;
                        }
                        1 => body[48] ^= 1, // First section's first chunk CRC, not its section CRC.
                        _ => {
                            body.pop();
                        }
                    }
                });
                let bytes =
                    std::fs::read(dir.path().join(sidx_name(rec.segment_id, rec.version))).unwrap();
                assert!(
                    matches!(Sidecar::parse(&bytes), Err(FormatFault::Corrupt(_))),
                    "ordered={ordered}, mutation={mutation}"
                );
            }
        }
    }

    #[test]
    fn duplicate_page_index_sections_are_corrupt() {
        let bytes = sidecar_bytes();
        let footer_at = bytes.len() - SIDX_FOOTER_LEN;
        let (at, count, crc) = parse_footer(&bytes[footer_at..], bytes.len() as u64).unwrap();
        let records = parse_table(&bytes[at as usize..footer_at], count, crc, at).unwrap();
        let page_index = PageIndex::derive(&bytes, &records, &header()).unwrap();
        let mut sections: Vec<_> = records
            .iter()
            .map(|r| {
                (
                    r.kind,
                    bytes[r.offset as usize..(r.offset + r.len) as usize].to_vec(),
                )
            })
            .collect();
        sections.push((SECTION_PAGE_INDEX, page_index.encode()));
        sections.push((SECTION_PAGE_INDEX, page_index.encode()));
        assert!(matches!(
            Sidecar::parse(&encode_sidecar(&header(), &sections)),
            Err(FormatFault::Corrupt(_))
        ));
    }

    #[test]
    fn paged_sidecar_writer_returns_an_internal_error_for_invalid_metadata() {
        assert!(matches!(
            encode_sidecar_paged(
                &header(),
                &[
                    (SECTION_BLOCK_INDEX, vec![0]),
                    (SECTION_FILTER, filter_section()),
                    (SECTION_PROPERTIES, props().encode()),
                ]
            ),
            Err(DbError::Internal("segment writer: page index"))
        ));
    }

    fn header() -> SegHeader {
        SegHeader {
            segment_id: 0x2a,
            version: 1,
            order: OrderKind::ByHash,
            key_len: 32,
            seed: 0x1122_3344_5566_7788,
            block_size: 16 * 1024,
        }
    }

    fn props() -> Properties {
        Properties {
            entries: 3,
            tombstones: 1,
            min_gsn: 10,
            max_gsn: 12,
            min_h: 100,
            max_h: 300,
            raw_bytes: 456,
            opened_at_ms: 1_000,
            frozen_at_ms: 2_000,
            sealed_at_ms: 3_000,
            typ_hash: 0xabcd,
            value_version: 7,
            writer: "armdb test".into(),
        }
    }

    fn index_section() -> Vec<u8> {
        let mut b = BlockIndexBuilder::new();
        b.push(100, 200, 2);
        b.push(300, 150, 1);
        b.finish()
    }

    fn filter_section() -> Vec<u8> {
        let mut b = PartitionedFilterBuilder::new(0);
        for h in [100u64, 200, 300] {
            b.push(h).unwrap();
        }
        b.finish().unwrap()
    }

    fn sidecar_bytes() -> Vec<u8> {
        encode_sidecar(
            &header(),
            &[
                (SECTION_BLOCK_INDEX, index_section()),
                (SECTION_FILTER, filter_section()),
                (SECTION_PROPERTIES, props().encode()),
            ],
        )
    }

    fn key_sections() -> Vec<(u32, Vec<u8>)> {
        let mut b = crate::seg::key_index::SeparatorIndexBuilder::new();
        b.push(b"", 100, 3).unwrap();
        vec![
            (SECTION_SEPARATOR_INDEX, b.finish().unwrap()),
            (SECTION_KEY_RANGE, vec![0; 64]),
            (SECTION_FILTER, filter_section()),
            (SECTION_PROPERTIES, props().encode()),
        ]
    }

    #[test]
    fn sidecar_sections_must_match_order() {
        let mut h = header();
        h.order = OrderKind::ByKey;
        let sections = key_sections();
        let parsed = Sidecar::parse(&encode_sidecar(&h, &sections)).unwrap();
        assert!(matches!(
            parsed.index,
            crate::seg::key_index::SegmentIndex::Key(_)
        ));
        assert!(parsed.key_range.is_some());
        assert!(Sidecar::parse(&encode_sidecar(&header(), &sections)).is_err());
        assert!(
            Sidecar::parse(&encode_sidecar(
                &h,
                &[
                    (SECTION_BLOCK_INDEX, index_section()),
                    (SECTION_FILTER, filter_section()),
                    (SECTION_PROPERTIES, props().encode())
                ]
            ))
            .is_err()
        );
        for missing in 0..sections.len() {
            let mut bad = sections.clone();
            bad.remove(missing);
            assert!(Sidecar::parse(&encode_sidecar(&h, &bad)).is_err());
            let mut bad = sections.clone();
            bad.push(sections[missing].clone());
            assert!(Sidecar::parse(&encode_sidecar(&h, &bad)).is_err());
        }
    }

    #[test]
    fn prefix_sections_are_crc_checked_and_unique() {
        let mut h = header();
        h.order = OrderKind::ByKey;
        let mut prefix = vec![16, 0, 0, 0, 0, 0, 0, 0];
        prefix.extend(filter_section());
        let mut sections = key_sections();
        sections.push((SECTION_PREFIX_FILTER, prefix.clone()));
        let good = encode_sidecar(&h, &sections);
        let parsed = Sidecar::parse(&good).unwrap();
        assert_eq!(parsed.prefix_filters[0].0, 16);
        assert_eq!(prefix[..8], [16, 0, 0, 0, 0, 0, 0, 0]);
        let mut other = prefix.clone();
        other[0] = 8;
        sections.push((SECTION_PREFIX_FILTER, other));
        assert_eq!(
            Sidecar::parse(&encode_sidecar(&h, &sections))
                .unwrap()
                .prefix_filters
                .len(),
            2
        );
        sections.pop();
        sections.push((SECTION_PREFIX_FILTER, prefix.clone()));
        assert!(Sidecar::parse(&encode_sidecar(&h, &sections)).is_err());
        sections.pop();
        for (at, value) in [(0, 0), (0, 33), (2, 1)] {
            let mut bad = prefix.clone();
            bad[at] = value;
            *sections.last_mut().unwrap() = (SECTION_PREFIX_FILTER, bad);
            assert!(Sidecar::parse(&encode_sidecar(&h, &sections)).is_err());
        }
        for cut in 0..8 {
            *sections.last_mut().unwrap() = (SECTION_PREFIX_FILTER, prefix[..cut].to_vec());
            assert!(Sidecar::parse(&encode_sidecar(&h, &sections)).is_err());
        }
        let prefix_at = FILE_HEADER_LEN
            + key_sections()
                .iter()
                .map(|(_, body)| body.len())
                .sum::<usize>();
        let mut corrupt = good;
        corrupt[prefix_at] ^= 1;
        assert!(Sidecar::parse(&corrupt).is_err());
        // Hash readers ignore prefix semantics while still checking its CRC.
        let bytes = encode_sidecar(
            &header(),
            &[
                (SECTION_BLOCK_INDEX, index_section()),
                (SECTION_FILTER, filter_section()),
                (SECTION_PROPERTIES, props().encode()),
                (SECTION_PREFIX_FILTER, vec![255]),
            ],
        );
        assert!(Sidecar::parse(&bytes).unwrap().prefix_filters.is_empty());
        let prefix_at = FILE_HEADER_LEN
            + index_section().len()
            + filter_section().len()
            + props().encode().len();
        let mut bad = bytes;
        bad[prefix_at] ^= 1;
        assert!(matches!(Sidecar::parse(&bad),Err(FormatFault::Corrupt(m)) if m.contains("CRC")));
    }

    #[test]
    fn the_file_header_has_the_documented_layout() {
        let bytes = header().encode(SDAT_MAGIC);
        assert_eq!(&bytes[0..4], b"ASD1");
        assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 1);
        assert_eq!(bytes[6], NATIVE_BYTE_ORDER);
        assert_eq!(bytes[7], 0);
        assert_eq!(u64::from_le_bytes(bytes[8..16].try_into().unwrap()), 0x2a);
        assert_eq!(u32::from_le_bytes(bytes[16..20].try_into().unwrap()), 1);
        assert_eq!(u16::from_le_bytes([bytes[20], bytes[21]]), 32);
        assert_eq!(
            u64::from_le_bytes(bytes[24..32].try_into().unwrap()),
            0x1122_3344_5566_7788
        );
        assert_eq!(
            u32::from_le_bytes(bytes[32..36].try_into().unwrap()),
            16 * 1024
        );
        assert!(bytes[36..60].iter().all(|&b| b == 0));
        assert_eq!(
            u32::from_le_bytes(bytes[60..64].try_into().unwrap()),
            crc32fast::hash(&bytes[..60])
        );
        assert_eq!(SegHeader::decode(&bytes, SDAT_MAGIC).unwrap(), header());
    }

    fn reseal(bytes: &mut [u8; 64]) {
        let crc = crc32fast::hash(&bytes[..60]);
        bytes[60..64].copy_from_slice(&crc.to_le_bytes());
    }

    #[test]
    fn the_file_header_fails_closed() {
        let good = header().encode(SIDX_MAGIC);

        assert!(matches!(
            SegHeader::decode(&good, SDAT_MAGIC),
            Err(FormatFault::Corrupt(m)) if m.contains("magic")
        ));
        assert!(matches!(
            SegHeader::decode(&good[..63], SIDX_MAGIC),
            Err(FormatFault::Corrupt(_))
        ));

        let mut flipped = good;
        flipped[10] ^= 1;
        assert!(matches!(
            SegHeader::decode(&flipped, SIDX_MAGIC),
            Err(FormatFault::Corrupt(m)) if m.contains("CRC")
        ));

        let mut version = good;
        version[4] = 2;
        reseal(&mut version);
        assert!(matches!(
            SegHeader::decode(&version, SIDX_MAGIC),
            Err(FormatFault::Unsupported(m)) if m.contains("version")
        ));

        let mut byte_order = good;
        byte_order[6] = 3 - NATIVE_BYTE_ORDER;
        reseal(&mut byte_order);
        assert!(matches!(
            SegHeader::decode(&byte_order, SIDX_MAGIC),
            Err(FormatFault::Unsupported(m)) if m.contains("byte order")
        ));

        let mut by_key = good;
        by_key[7] = 1;
        reseal(&mut by_key);
        assert_eq!(
            SegHeader::decode(&by_key, SIDX_MAGIC).unwrap().order,
            OrderKind::ByKey
        );

        let mut reserved = good;
        reserved[40] = 1;
        reseal(&mut reserved);
        assert!(matches!(
            SegHeader::decode(&reserved, SIDX_MAGIC),
            Err(FormatFault::Unsupported(_))
        ));
    }

    #[test]
    fn the_block_index_locates_blocks_by_hash() {
        let index = BlockIndex::parse(&index_section()).unwrap();
        assert_eq!(index.len(), 2);
        assert_eq!(index.locate(0), None);
        assert_eq!(index.locate(99), None);
        assert_eq!(index.locate(100), Some(0));
        assert_eq!(index.locate(299), Some(0));
        assert_eq!(index.locate(300), Some(1));
        assert_eq!(index.locate(u64::MAX), Some(1));
        assert_eq!(index.range(0), (64, 200));
        assert_eq!(index.range(1), (264, 150));
        assert_eq!(index.end_offset(), 414);
        assert_eq!(index.first_h(1), 300);
        // 16 bytes per block plus the end offset.
        assert_eq!(index.resident_bytes(), 2 * 16 + 8);
    }

    #[test]
    fn an_empty_block_index_is_valid() {
        let section = BlockIndexBuilder::new().finish();
        let index = BlockIndex::parse(&section).unwrap();
        assert!(index.is_empty());
        assert_eq!(index.locate(5), None);
        assert_eq!(index.end_offset(), 64);
    }

    #[test]
    fn a_damaged_block_index_is_rejected() {
        let good = index_section();

        assert!(matches!(
            BlockIndex::parse(&good[..good.len() - 1]),
            Err(FormatFault::Corrupt(_))
        ));
        assert!(matches!(
            BlockIndex::parse(&[]),
            Err(FormatFault::Corrupt(_))
        ));

        // Second record's first_h at [24..32]: make it equal to the first.
        let mut unordered = good.clone();
        unordered[24..32].copy_from_slice(&100u64.to_le_bytes());
        assert!(matches!(
            BlockIndex::parse(&unordered),
            Err(FormatFault::Corrupt(m)) if m.contains("order")
        ));

        // Second record's offset at [32..40]: leave a gap.
        let mut gap = good.clone();
        gap[32..40].copy_from_slice(&265u64.to_le_bytes());
        assert!(matches!(
            BlockIndex::parse(&gap),
            Err(FormatFault::Corrupt(m)) if m.contains("contiguous")
        ));

        // First record's offset at [8..16]: inside the file header.
        let mut early = good.clone();
        early[8..16].copy_from_slice(&10u64.to_le_bytes());
        assert!(matches!(
            BlockIndex::parse(&early),
            Err(FormatFault::Corrupt(_))
        ));

        // First record's entries at [20..24]: a block with no entries.
        let mut hollow = good.clone();
        hollow[20..24].copy_from_slice(&0u32.to_le_bytes());
        assert!(matches!(
            BlockIndex::parse(&hollow),
            Err(FormatFault::Corrupt(_))
        ));

        // A one-byte hole between the data header and the first block.
        let mut leading_gap = good.clone();
        leading_gap[8..16].copy_from_slice(&65u64.to_le_bytes());
        leading_gap[32..40].copy_from_slice(&265u64.to_le_bytes());
        leading_gap[good.len() - 8..].copy_from_slice(&415u64.to_le_bytes());
        assert!(matches!(
            BlockIndex::parse(&leading_gap),
            Err(FormatFault::Corrupt(_))
        ));
    }

    #[test]
    fn properties_round_trip() {
        let p = props();
        assert_eq!(Properties::decode(&p.encode()).unwrap(), p);
        let bytes = p.encode();
        assert!(matches!(
            Properties::decode(&bytes[..bytes.len() - 1]),
            Err(FormatFault::Corrupt(_))
        ));
        assert!(matches!(
            Properties::decode(&bytes[..20]),
            Err(FormatFault::Corrupt(_))
        ));
    }

    #[test]
    fn a_sidecar_round_trips() {
        let sidecar = Sidecar::parse(&sidecar_bytes()).unwrap();
        assert_eq!(sidecar.header, header());
        assert_eq!(sidecar.props, props());
        let SegmentIndex::Hash(index) = sidecar.index else {
            panic!("expected hash index");
        };
        assert_eq!(index.len(), 2);
        assert!(sidecar.filter.may_contain(200));
    }

    #[test]
    fn an_unknown_optional_section_is_skipped() {
        let bytes = encode_sidecar(
            &header(),
            &[
                (SECTION_BLOCK_INDEX, index_section()),
                (OPTIONAL_SECTION_MIN + 5, b"from the future".to_vec()),
                (SECTION_FILTER, filter_section()),
                (SECTION_PROPERTIES, props().encode()),
            ],
        );
        assert!(Sidecar::parse(&bytes).is_ok());
    }

    #[test]
    fn an_unknown_required_section_fails_the_parse() {
        let bytes = encode_sidecar(
            &header(),
            &[
                (SECTION_BLOCK_INDEX, index_section()),
                (SECTION_FILTER, filter_section()),
                (SECTION_PROPERTIES, props().encode()),
                (9, b"required and unknown".to_vec()),
            ],
        );
        assert!(matches!(
            Sidecar::parse(&bytes),
            Err(FormatFault::Unsupported(m)) if m.contains("section kind 9")
        ));
    }

    #[test]
    fn a_missing_required_section_fails_the_parse() {
        let bytes = encode_sidecar(
            &header(),
            &[
                (SECTION_BLOCK_INDEX, index_section()),
                (SECTION_PROPERTIES, props().encode()),
            ],
        );
        assert!(matches!(
            Sidecar::parse(&bytes),
            Err(FormatFault::Corrupt(m)) if m.contains("filter")
        ));
    }

    /// Rewrites one field of the first section record and reseals the table,
    /// so that only the field under test is wrong.
    fn with_first_record(mut bytes: Vec<u8>, edit: impl FnOnce(&mut [u8])) -> Vec<u8> {
        let footer_at = bytes.len() - SIDX_FOOTER_LEN;
        let count = u32::from_le_bytes(bytes[footer_at..footer_at + 4].try_into().unwrap());
        let table_at = footer_at - count as usize * SECTION_RECORD_LEN;
        edit(&mut bytes[table_at..table_at + SECTION_RECORD_LEN]);
        let crc = crc32fast::hash(&bytes[table_at..footer_at]);
        bytes[footer_at + 4..footer_at + 8].copy_from_slice(&crc.to_le_bytes());
        bytes
    }

    #[test]
    fn a_section_table_that_points_outside_the_file_is_rejected() {
        let good = sidecar_bytes();

        let past_end = with_first_record(good.clone(), |rec| {
            rec[8..16].copy_from_slice(&u64::MAX.to_le_bytes());
        });
        assert!(matches!(
            Sidecar::parse(&past_end),
            Err(FormatFault::Corrupt(_))
        ));

        let huge = with_first_record(good.clone(), |rec| {
            rec[16..24].copy_from_slice(&u64::MAX.to_le_bytes());
        });
        assert!(matches!(
            Sidecar::parse(&huge),
            Err(FormatFault::Corrupt(_))
        ));

        let in_header = with_first_record(good.clone(), |rec| {
            rec[8..16].copy_from_slice(&8u64.to_le_bytes());
        });
        assert!(matches!(
            Sidecar::parse(&in_header),
            Err(FormatFault::Corrupt(m)) if m.contains("header")
        ));

        // The first section now starts where the second one does.
        let second_offset = {
            let footer_at = good.len() - SIDX_FOOTER_LEN;
            let table_at = footer_at - 3 * SECTION_RECORD_LEN;
            let rec = &good[table_at + SECTION_RECORD_LEN..table_at + 2 * SECTION_RECORD_LEN];
            u64::from_le_bytes(rec[8..16].try_into().unwrap())
        };
        let overlapping = with_first_record(good.clone(), |rec| {
            rec[8..16].copy_from_slice(&second_offset.to_le_bytes());
        });
        assert!(matches!(
            Sidecar::parse(&overlapping),
            Err(FormatFault::Corrupt(_))
        ));
    }

    #[test]
    fn a_damaged_tail_is_rejected() {
        let good = sidecar_bytes();

        for cut in [0, 10, FILE_HEADER_LEN, good.len() - 1] {
            assert!(
                matches!(Sidecar::parse(&good[..cut]), Err(FormatFault::Corrupt(_))),
                "cut at {cut}"
            );
        }

        let mut magic = good.clone();
        let at = magic.len() - 8;
        magic[at] = b'Z';
        assert!(matches!(
            Sidecar::parse(&magic),
            Err(FormatFault::Corrupt(m)) if m.contains("footer")
        ));

        let mut table = good.clone();
        let at = table.len() - SIDX_FOOTER_LEN - 1;
        table[at] ^= 1;
        assert!(matches!(
            Sidecar::parse(&table),
            Err(FormatFault::Corrupt(m)) if m.contains("table")
        ));

        // A byte inside the first section.
        let mut section = good.clone();
        section[FILE_HEADER_LEN + 3] ^= 1;
        assert!(matches!(
            Sidecar::parse(&section),
            Err(FormatFault::Corrupt(m)) if m.contains("section")
        ));
    }

    #[test]
    fn reserved_section_table_fields_are_rejected() {
        for field in [4, 28] {
            let bytes = with_first_record(sidecar_bytes(), |rec| rec[field] = 1);
            assert!(matches!(
                Sidecar::parse(&bytes),
                Err(FormatFault::Unsupported(m)) if m.contains("reserved")
            ));
        }
    }

    #[test]
    fn duplicate_required_sections_are_rejected() {
        let bytes = encode_sidecar(
            &header(),
            &[
                (SECTION_BLOCK_INDEX, index_section()),
                (SECTION_FILTER, filter_section()),
                (SECTION_PROPERTIES, props().encode()),
                (SECTION_PROPERTIES, props().encode()),
            ],
        );
        assert!(matches!(
            Sidecar::parse(&bytes),
            Err(FormatFault::Corrupt(_))
        ));
    }

    #[test]
    fn damaged_optional_sections_are_rejected() {
        let mut bytes = encode_sidecar(
            &header(),
            &[
                (SECTION_BLOCK_INDEX, index_section()),
                (OPTIONAL_SECTION_MIN, b"future".to_vec()),
                (SECTION_FILTER, filter_section()),
                (SECTION_PROPERTIES, props().encode()),
            ],
        );
        let at = FILE_HEADER_LEN + index_section().len();
        bytes[at] ^= 1;
        assert!(matches!(
            Sidecar::parse(&bytes),
            Err(FormatFault::Corrupt(_))
        ));
    }

    #[test]
    fn set_footer_reserved_bytes_are_damage() {
        let mut bytes = sidecar_bytes();
        let at = bytes.len() - 1;
        bytes[at] = 1;
        assert!(matches!(
            Sidecar::parse(&bytes),
            Err(FormatFault::Corrupt(m)) if m.contains("footer reserved")
        ));
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn a_block_index_may_not_list_more_than_u32_max_blocks() {
        assert!(check_block_count(0).is_ok());
        assert!(check_block_count(u32::MAX as usize).is_ok());
        assert!(matches!(
            check_block_count(u32::MAX as usize + 1),
            Err(FormatFault::Corrupt(m)) if m.contains("u32::MAX")
        ));
    }

    /// One optional section of two bytes, so every field of its table
    /// record and of the footer has a distinct, hand-computed value.
    #[test]
    fn the_section_table_and_footer_have_the_documented_layout() {
        #[rustfmt::skip]
        const RECORD: [u8; 32] = [
            0x00, 0x10, 0x00, 0x00,                         // kind 0x1000
            0x00, 0x00, 0x00, 0x00,                         // reserved
            0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // offset 64
            0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // len 2
            0x6d, 0x48, 0x83, 0x9e,                         // crc32("ab")
            0x00, 0x00, 0x00, 0x00,                         // reserved
        ];
        #[rustfmt::skip]
        const FOOTER: [u8; 24] = [
            0x01, 0x00, 0x00, 0x00,                         // section_count 1
            0x18, 0x76, 0x10, 0x9c,                         // crc32 of RECORD
            0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // table_offset 66
            0x41, 0x53, 0x58, 0x46,                         // "ASXF"
            0x00, 0x00, 0x00, 0x00,                         // reserved
        ];
        let bytes = encode_sidecar(&header(), &[(OPTIONAL_SECTION_MIN, b"ab".to_vec())]);
        assert_eq!(bytes.len(), FILE_HEADER_LEN + 2 + 32 + 24);
        assert_eq!(&bytes[FILE_HEADER_LEN..FILE_HEADER_LEN + 2], b"ab");
        assert_eq!(bytes[66..98], RECORD);
        assert_eq!(bytes[98..], FOOTER);

        assert_eq!(parse_footer(&FOOTER, 122).unwrap(), (66, 1, 0x9c10_7618));
        assert_eq!(
            parse_table(&RECORD, 1, 0x9c10_7618, 66).unwrap(),
            [SectionRecord {
                kind: OPTIONAL_SECTION_MIN,
                offset: 64,
                len: 2,
                crc32: 0x9e83_486d,
            }]
        );
    }

    #[test]
    fn the_block_index_section_has_the_documented_layout() {
        #[rustfmt::skip]
        const INDEX: [u8; 56] = [
            // block 0: first_h 100, offset 64, block_len 200, entries 2
            0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0xc8, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
            // block 1: first_h 300, offset 264, block_len 150, entries 1
            0x2c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x96, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
            // end offset 414
            0x9e, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ];
        assert_eq!(index_section(), INDEX);
        let (records, end) = parse_index_records(&INDEX).unwrap();
        assert_eq!(
            records,
            [
                BlockIndexRecord {
                    first_h: 100,
                    offset: 64,
                    block_len: 200,
                    entries: 2,
                },
                BlockIndexRecord {
                    first_h: 300,
                    offset: 264,
                    block_len: 150,
                    entries: 1,
                },
            ]
        );
        assert_eq!(end, 414);
    }

    #[test]
    fn the_properties_section_has_the_documented_layout() {
        #[rustfmt::skip]
        const PROPS: [u8; 102] = [
            0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // entries 3
            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // tombstones 1
            0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // min_gsn 10
            0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // max_gsn 12
            0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // min_h 100
            0x2c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // max_h 300
            0xc8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // raw_bytes 456
            0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // opened_at_ms 1000
            0xd0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // frozen_at_ms 2000
            0xb8, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sealed_at_ms 3000
            0xcd, 0xab, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // typ_hash 0xabcd
            0x07, 0x00,                                     // value_version 7
            0x0a, 0x00,                                     // writer_len 10
            b'a', b'r', b'm', b'd', b'b', b' ', b't', b'e', b's', b't',
        ];
        assert_eq!(props().encode(), PROPS);
        assert_eq!(Properties::decode(&PROPS).unwrap(), props());
    }
}