bgpkit-snap 0.2.0

Versioned single-file container for prefix tries, with an append-only journal
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
//! bgpkit-snap v4: an immutable container for prefix tries.
//!
//! The container owns framing, identity and integrity. It knows nothing about
//! what a trie holds beyond the value type's `TYPE_ID`, and nothing about what a
//! client section means. See `docs/format.md`.

#![deny(missing_docs)]
// The only unsafe in this crate is the two archived-trie accessors, which read bytes
// `open` already byte-checked; everything else must stay safe.
#![deny(unsafe_code)]

use std::fmt;

use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use prefix_trie::rkyv::ArchivedPrefixMap;
use prefix_trie::{AsView, PrefixMap, TrieView};
use rkyv::rancor::Error as RkyvError;

/// The address types this crate takes and returns, re-exported so a caller cannot
/// end up on another version of `ipnet` than the one in its signatures. Unlike the
/// archive re-export this one is complete: no macro expands `::ipnet::` paths.
pub use ipnet;

/// The archived trie types `Snapshot::v4`/`v6` return, re-exported so a caller that
/// names them does not have to pin this crate's `prefix-trie` version itself.
pub use prefix_trie;

/// The archive library this crate builds value types on, re-exported so a
/// caller can name the same version in a signature.
///
/// It does not remove the dependency: `#[derive(rkyv::Archive)]` expands to paths
/// rooted at `rkyv`, so a crate that derives needs `rkyv` in its own manifest even
/// though this re-export exists.
pub use rkyv;
use rkyv::util::AlignedVec;
use std::io::Write;

pub mod header;
pub mod journal;
pub mod profile;

pub use header::{
    Entry, Header, StreamRef, ENTRY_LEN, FLAG_STREAM, FLAG_WINDOW, HEADER_LEN, ID_V4, ID_V6,
    KIND_PAYLOAD, KIND_V4, KIND_V6,
};

/// The serializer `rkyv::to_bytes` builds, so the value bound can name it.
pub type HighSer<'a> =
    rkyv::api::high::HighSerializer<AlignedVec, rkyv::ser::allocator::ArenaHandle<'a>, RkyvError>;

/// The developer guide ships inside the crate so its examples are doctests: a
/// signature change breaks `cargo test --doc` instead of leaving the page wrong.
/// The module is private, so the guide is not rendered in the API docs.
#[doc = include_str!("../docs/bgpkit-snap-developer-guide.md")]
mod guide {}

// ---------------------------------------------------------------- errors

/// Every way this crate refuses.
///
/// `#[non_exhaustive]`: a new refusal is a change to the crate, not to the caller's
/// match, so a caller needs a catch-all arm.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// An operating-system call failed; `source()` gives the original.
    Io(std::io::Error),
    /// The first eight bytes are not this container's magic.
    BadMagic,
    /// The layout version in the header is not one this build reads.
    BadVersion(u16),
    /// A header field is out of range or contradicts another.
    BadHeader(&'static str),
    /// The header's checksum does not match its contents.
    BadHeaderCrc,
    /// The file ends before the structure it declares.
    Truncated {
        /// Bytes the structure needs.
        need: usize,
        /// Bytes present.
        have: usize,
    },
    /// The section table is not a table this format allows.
    BadSectionTable(&'static str),
    /// Section ids are not strictly ascending, or a section is missing.
    SectionOrder,
    /// The object carries a required section this reader does not implement.
    UnknownRequiredSection(u16),
    /// A section's four-byte kind tag does not belong to its id.
    BadSectionKind(u16),
    /// The object holds another value type than the one this reader asked for.
    TypeMismatch {
        /// `TYPE_ID` recorded in the header.
        expected: String,
        /// `TYPE_ID` of the reader.
        found: String,
    },
    /// The content id in the header does not match the bytes.
    ContentId,
    /// Another writer holds this journal's lock. Retry later; this is not damage.
    Locked,
    /// A caller-supplied option is not something a writer can emit.
    BadOpts(&'static str),
    /// A section id the caller supplied is not usable here.
    BadSectionId(u16),
    /// A prefix the caller supplied has host bits set.
    BadPrefix(String),
    /// A section's body is not a valid archive of the type this reader asked for.
    BadArchive(u16),
    /// The directory or object was written under a different configuration than the
    /// one this call was made with, and adopting it would reinterpret the bytes.
    ConfigMismatch(String),
    /// A value could not be encoded, or an archived value could not be decoded.
    BadValue,
    /// A journal record or its state is not a record this build can act on.
    BadRecord(&'static str),
    /// The object a journal was attached to is gone or is no longer that object. A
    /// journal is a log of changes to one object, so this is not damage to the log:
    /// point the journal at the object it was attached to, or the log means nothing.
    BaseLost(String),
    /// Records were lost: the next sequence number on disk is not the expected one.
    SequenceGap {
        /// The sequence number the journal expected next.
        expected: u64,
        /// The sequence number the segment holds.
        found: u64,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Io(e) => write!(f, "io: {e}"),
            Error::BadMagic => write!(f, "not a snap object"),
            Error::BadVersion(v) => write!(f, "unsupported format version {v}"),
            Error::BadHeader(why) => write!(f, "bad header: {why}"),
            Error::BadHeaderCrc => write!(f, "header crc mismatch"),
            Error::Truncated { need, have } => write!(f, "truncated: need {need}, have {have}"),
            Error::BadSectionTable(why) => write!(f, "bad section table: {why}"),
            Error::SectionOrder => write!(f, "sections are not in ascending id order"),
            Error::UnknownRequiredSection(id) => write!(
                f,
                "section {id} is required but this reader does not know it; \
                 open the object with a validator that implements it"
            ),
            Error::BadSectionKind(id) => write!(f, "section {id} has an unexpected kind tag"),
            Error::TypeMismatch { expected, found } => {
                write!(
                    f,
                    "value type mismatch: expected {expected}, found {found:?}"
                )
            }
            Error::ContentId => write!(f, "content id mismatch"),
            Error::Locked => write!(f, "another writer holds this journal"),
            Error::BadOpts(what) => write!(f, "bad writer option: {what}"),
            Error::BadSectionId(id) => write!(f, "section id {id} cannot be used here"),
            Error::BadPrefix(p) => write!(f, "prefix {p} has host bits set"),
            Error::BadArchive(id) => {
                write!(f, "section {id} is not an archive of this value type")
            }
            Error::ConfigMismatch(why) => write!(f, "{why}"),
            Error::BadValue => write!(f, "value could not be decoded"),
            Error::BadRecord(what) => write!(f, "journal record: {what}"),
            Error::BaseLost(what) => write!(f, "journal base: {what}"),
            Error::SequenceGap { expected, found } => {
                write!(
                    f,
                    "journal sequence gap: expected {expected}, found {found}"
                )
            }
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(e) => Some(e),
            _ => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Io(e)
    }
}

// ---------------------------------------------------------------- coverage

/// How much of an address space a tree's entries cover.
///
/// A tuple of options could not tell "this family has no entries" from "this family
/// is covered end to end" (an IPv6 tree holding `::/0` covers 2^128 addresses, which
/// does not fit in the count).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Count {
    /// The tree holds no entry for this family.
    NoEntries,
    /// The whole address space is claimed.
    All,
    /// That many distinct addresses are covered; overlapping entries count once.
    Exact(u128),
}

impl Count {
    /// The number of addresses covered, when it is a number. `All` is the whole
    /// space, which no `u128` holds for IPv6; `NoEntries` is zero.
    pub fn exact(self) -> Option<u128> {
        match self {
            Count::NoEntries => Some(0),
            Count::Exact(n) => Some(n),
            Count::All => None,
        }
    }
}

impl std::fmt::Display for Count {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Count::NoEntries => f.write_str("0"),
            Count::Exact(n) => write!(f, "{n}"),
            Count::All => f.write_str("all"),
        }
    }
}

/// The answer to `Snapshot::address_counts`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AddressCounts {
    /// Coverage of the IPv4 tree.
    pub v4: Count,
    /// Coverage of the IPv6 tree.
    pub v6: Count,
}

// ---------------------------------------------------------------- values

/// The one-attribute way to make a value type: it adds `rkyv`'s derives for you.
#[cfg(feature = "derive")]
pub use bgpkit_snap_derive::snap_value;
/// The derive, re-exported so a caller writes `bgpkit_snap::SnapValue` for both the
/// trait and `#[derive(SnapValue)]`. The macro lives in a namespace of its own, so the
/// name is not taken twice.
#[cfg(feature = "derive")]
pub use bgpkit_snap_derive::SnapValue;

/// A value type that can live in a trie.
///
/// `#[derive(SnapValue)]` only writes the id; the archive impls come from `rkyv`. The
/// diagnostics below say that once, where the compiler would otherwise print rkyv's
/// strategy types.
#[diagnostic::on_unimplemented(
    message = "`{Self}` cannot be stored in a snapshot",
    label = "not a value type",
    note = "a value type needs `Clone`, rkyv's `Archive` and `Serialize`, and `SnapValue` for its id:",
    note = "#[derive(Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, bgpkit_snap::SnapValue)]",
    note = "#[snap(type_id = \"your.type:v1\")]"
)]
pub trait SnapValue: rkyv::Archive + Clone + for<'a> rkyv::Serialize<HighSer<'a>> {
    /// Stable identifier, e.g. `snap.u32:v1`.
    ///
    /// At most 31 bytes of printable ASCII, no space, quote or backslash, and not
    /// empty: it is stored verbatim in the header and compared on every open, so a
    /// value that cannot round-trip through those bytes is rejected at `finish`.
    const TYPE_ID: &'static str;

    /// Bring the value into canonical form before it is written. The default is
    /// a no-op; override it when the value has an order that does not come from
    /// its own fields.
    fn canonicalize(&mut self) {}
}

impl SnapValue for u8 {
    const TYPE_ID: &str = "snap.u8:v1";
}

impl SnapValue for u16 {
    const TYPE_ID: &str = "snap.u16:v1";
}

impl SnapValue for u32 {
    const TYPE_ID: &str = "snap.u32:v1";
}

impl SnapValue for u64 {
    const TYPE_ID: &str = "snap.u64:v1";
}

impl SnapValue for u128 {
    const TYPE_ID: &str = "snap.u128:v1";
}

/// Decode a value from archived bytes, used by the journal.
pub trait Codec: SnapValue + Sized {
    /// Encode a value the way this container stores it.
    fn encode(value: &Self) -> Result<Vec<u8>, Error>;
    /// Decode a value that was stored this way.
    fn decode(bytes: &[u8]) -> Result<Self, Error>;
}

impl<T> Codec for T
where
    T: SnapValue,
    T::Archived: for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>
        + rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
{
    fn encode(value: &Self) -> Result<Vec<u8>, Error> {
        rkyv::to_bytes::<RkyvError>(value)
            .map(|b| b.to_vec())
            .map_err(|_| Error::BadValue)
    }

    fn decode(bytes: &[u8]) -> Result<Self, Error> {
        rkyv::from_bytes::<T, RkyvError>(bytes).map_err(|_| Error::BadValue)
    }
}

// ---------------------------------------------------------------- writer

/// The identity of the tree a journal writes.
///
/// A journal refuses a checkpoint that belongs to another tree, so the id is what
/// stops two feeds from writing into one directory. `from_name` exists because
/// hand-writing sixteen bytes is the kind of detail a caller gets wrong silently.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct TreeId([u8; 16]);

impl TreeId {
    /// The id as the sixteen bytes a header carries.
    pub const fn from_bytes(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }

    /// A stable id for a human name: the first 16 bytes of its BLAKE3 hash.
    ///
    /// The same name always gives the same id, so a service that starts with
    /// `TreeId::from_name("wayback-roas")` reopens its own journal and a typo in
    /// the name gives a different journal instead of silently adopting this one.
    pub fn from_name(name: &str) -> Self {
        let mut out = [0u8; 16];
        out.copy_from_slice(&blake3::hash(name.as_bytes()).as_bytes()[..16]);
        Self(out)
    }

    /// The same sixteen bytes, for a caller comparing it with a header.
    pub const fn as_bytes(&self) -> &[u8; 16] {
        &self.0
    }
}

impl From<[u8; 16]> for TreeId {
    fn from(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }
}

impl std::fmt::Display for TreeId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for b in self.0 {
            write!(f, "{b:02x}")?;
        }
        Ok(())
    }
}

/// One section the caller supplies next to the tries: an arena, a sidecar, any
/// bytes the profile owns. `id` is 16 or higher, `kind` is a four-byte tag, and
/// `required` makes a reader that does not implement the id refuse the object.
#[derive(Clone, Copy)]
pub struct ExtraSection<'a> {
    /// Section id; 16 or higher for a profile section.
    pub id: u16,
    /// Four-byte kind tag a validator can branch on.
    pub kind: [u8; 4],
    /// A reader that does not implement this id must refuse the object.
    pub required: bool,
    /// The section body, exactly as it will be written.
    pub bytes: &'a [u8],
}

/// Everything a writer declares about the object.
pub struct Opts<'a> {
    /// unix seconds UTC, written into the header. `created_at` is deliberately
    /// outside the content id, so this is provenance rather than identity.
    pub created_at: i64,
    /// Stream coordinates, written as given: `Writer::finish` sets the flag and stores
    /// the `tree_id`, `generation` and `base_seq` the caller passed. A journal
    /// overwrites them with its own, because a checkpoint belongs to that stream.
    pub stream: Option<StreamRef>,
    /// Coverage claim, `(first_ts, last_ts)` in unix seconds.
    pub window: Option<(i64, i64)>,
    /// Sections your profile owns; the tries and the id are the writer's.
    pub extras: &'a [ExtraSection<'a>],
}

impl Opts<'_> {
    /// The options most writers want: the current time, no window, no extras.
    ///
    /// There is no `Default`: a default would have to invent a creation time, and
    /// an invented one is a valid header with false provenance.
    pub fn now() -> Self {
        Self {
            created_at: chrono::Utc::now().timestamp(),
            stream: None,
            window: None,
            extras: &[],
        }
    }
}

/// Builds one object in memory, then writes it in one piece.
///
/// A writer is not a store: it holds the whole tree, so a build larger than RAM is
/// out of scope by design.
pub struct Writer<T: SnapValue> {
    v4: PrefixMap<Ipv4Net, T>,
    v6: PrefixMap<Ipv6Net, T>,
}

impl<T: SnapValue> Default for Writer<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: SnapValue> Writer<T> {
    /// An empty writer.
    pub fn new() -> Self {
        Self {
            v4: PrefixMap::new(),
            v6: PrefixMap::new(),
        }
    }

    /// Store `value` under `prefix`, replacing any value already there.
    ///
    /// The prefix must be canonical (`addr() == network()`); a prefix with host
    /// bits set is refused with `Error::BadPrefix` in every build, because silently
    /// storing it under its network address would make the key you asked for and
    /// the key you get back differ.
    pub fn insert(&mut self, prefix: IpNet, mut value: T) -> Result<(), Error> {
        if prefix.addr() != prefix.network() {
            // storing it under its network address would make the key you asked
            // for and the key you get back differ, and two inputs would collide
            return Err(Error::BadPrefix(prefix.to_string()));
        }
        value.canonicalize();
        match prefix {
            IpNet::V4(p) => {
                self.v4.insert(p, value);
            }
            IpNet::V6(p) => {
                self.v6.insert(p, value);
            }
        }
        Ok(())
    }

    /// Insert many entries, stopping at the first prefix that is refused.
    ///
    /// `Writer` deliberately does not implement `Extend`: a set that silently
    /// drops the entries it could not store is worse than a loop that stops.
    pub fn insert_many(
        &mut self,
        entries: impl IntoIterator<Item = (IpNet, T)>,
    ) -> Result<(), Error> {
        for (prefix, value) in entries {
            self.insert(prefix, value)?;
        }
        Ok(())
    }

    /// How many prefixes the writer holds.
    pub fn len(&self) -> usize {
        self.v4.len() + self.v6.len()
    }

    /// Whether the writer holds nothing.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Build the object. The caller writes the returned bytes wherever it likes.
    pub fn finish(self, opts: &Opts<'_>) -> Result<Vec<u8>, Error> {
        let created_at = opts.created_at;

        let v4_bytes = rkyv::to_bytes::<RkyvError>(&self.v4).map_err(|_| Error::BadValue)?;
        let v6_bytes = rkyv::to_bytes::<RkyvError>(&self.v6).map_err(|_| Error::BadValue)?;

        // sections in ascending id order, each starting on a 16-byte boundary
        let mut sections: Vec<(Entry, &[u8])> = Vec::new();
        // both family sections are written even when a family holds nothing: an object
        // whose shape depends on its contents makes every client carry the "which
        // families are in this file" question. A reader still has to accept a missing
        // family, because objects written before this rule exist.
        sections.push((
            Entry {
                id: ID_V4,
                kind: KIND_V4,
                required: false,
                len: v4_bytes.len() as u64,
            },
            &v4_bytes,
        ));
        sections.push((
            Entry {
                id: ID_V6,
                kind: KIND_V6,
                required: false,
                len: v6_bytes.len() as u64,
            },
            &v6_bytes,
        ));
        // the counts section carries no data the tries need, so it stays optional: a
        // reader that does not know it skips it, and one that does can report the entry
        // count without decoding a value
        let counts = header::Counts {
            entries: self.len() as u64,
            v4: self.v4.len() as u64,
            v6: self.v6.len() as u64,
        };
        let counts_bytes = counts.encode();
        sections.push((
            Entry {
                id: header::ID_STATS,
                kind: header::KIND_STATS,
                required: false,
                len: header::STATS_LEN as u64,
            },
            &counts_bytes,
        ));

        let mut extras: Vec<&ExtraSection<'_>> = opts.extras.iter().collect();
        extras.sort_by_key(|e| e.id);
        for extra in extras {
            if extra.id < header::FIRST_PROFILE_ID {
                return Err(Error::BadHeader("extra section id is below 16"));
            }
            // the family kinds live at ids 2 and 3 and nowhere else, so an extra
            // claiming one would build an object the reader refuses
            if extra.kind == KIND_V4 || extra.kind == KIND_V6 {
                return Err(Error::BadHeader("extra section uses a family kind"));
            }
            if sections.iter().any(|(e, _)| e.id == extra.id) {
                return Err(Error::BadHeader("duplicate section id"));
            }
            sections.push((
                Entry {
                    id: extra.id,
                    kind: extra.kind,
                    required: extra.required,
                    len: extra.bytes.len() as u64,
                },
                extra.bytes,
            ));
        }
        sections.sort_by_key(|(e, _)| e.id);

        let table_len = sections.len() * ENTRY_LEN;
        let mut body: Vec<u8> = Vec::new();
        let mut table: Vec<u8> = Vec::with_capacity(table_len);
        for (entry, bytes) in &sections {
            while !(HEADER_LEN + table_len + body.len()).is_multiple_of(16) {
                body.push(0);
            }
            table.extend_from_slice(&entry.encode());
            body.extend_from_slice(bytes);
        }
        let file_len = (HEADER_LEN + table_len + body.len()) as u64;

        let mut type_id = [0u8; 32];
        let id_bytes = T::TYPE_ID.as_bytes();
        if id_bytes.is_empty() {
            return Err(Error::BadHeader("type id is empty"));
        }
        if id_bytes.len() > 31 {
            return Err(Error::BadHeader("type id is longer than 31 bytes"));
        }
        type_id[..id_bytes.len()].copy_from_slice(id_bytes);

        let (flags, tree_id, generation, base_seq) = match opts.stream {
            Some(s) => (FLAG_STREAM, s.tree_id, s.generation, s.base_seq),
            None => (0, [0u8; 16], [0u8; 16], 0),
        };
        let (flags, window) = match opts.window {
            Some((from, to)) => (flags | FLAG_WINDOW, [from, to]),
            None => (flags, [0, 0]),
        };

        let mut head = Header {
            flags,
            tree_id,
            generation,
            base_seq,
            type_id,
            file_len,
            section_count: sections.len() as u32,
            section_table_len: table_len as u32,
            window,
            created_at,
            content_id: [0u8; 32],
        };
        // the hash covers header[0..HASH_END]; content_id cannot hash itself, so
        // it is filled in after the first encode
        let mut prefix = head.encode();
        let mut h = blake3::Hasher::new();
        h.update(b"bgpkit-snap-v4\0");
        h.update(&prefix[..header::HASH_END]);
        h.update(&table);
        for (_, bytes) in &sections {
            h.update(bytes);
        }
        head.content_id = *h.finalize().as_bytes();

        let mut out = Vec::with_capacity(file_len as usize);
        prefix = head.encode();
        // a writer must not produce an object its own reader rejects: the
        // semantic rules live in Header::parse, so run them on the bytes here
        Header::parse(&prefix)?;
        out.extend_from_slice(&prefix);
        out.extend_from_slice(&table);
        out.extend_from_slice(&body);
        debug_assert_eq!(out.len() as u64, file_len);
        Ok(out)
    }

    /// Build the object and write it to `path` in one call, returning the number
    /// of bytes written. The file is written beside the target and renamed, so a
    /// reader never sees a half-written object.
    pub fn finish_to(
        self,
        path: impl AsRef<std::path::Path>,
        opts: &Opts<'_>,
    ) -> Result<u64, Error> {
        let bytes = self.finish(opts)?;
        let path = path.as_ref();
        // one temp name per call: two writers to one path must not share a temp
        static NEXT_TEMP: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let n = NEXT_TEMP.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let tmp = path.with_extension(format!("snap.{}.{n}.tmp", std::process::id()));
        let written = (|| -> std::io::Result<()> {
            let mut f = std::fs::File::create(&tmp)?;
            f.write_all(&bytes)?;
            f.sync_all()
        })();
        if let Err(e) = written {
            // the caller's existing object is still where it was; a temp file left
            // here would sit beside it forever
            let _ = std::fs::remove_file(&tmp);
            return Err(Error::Io(e));
        }
        if let Err(e) = std::fs::rename(&tmp, path) {
            // a complete object left beside the target would sit there forever
            let _ = std::fs::remove_file(&tmp);
            return Err(Error::Io(e));
        }
        if let Some(dir) = path.parent() {
            let _ = std::fs::File::open(dir).and_then(|d| d.sync_all());
        }
        Ok(bytes.len() as u64)
    }
}

// ---------------------------------------------------------------- merge

/// How many entries an object records, without naming a value type.
///
/// The writer records this in section 4 precisely so the number survives a reader that
/// does not know `T`: a trie's node layout depends on the value type, so counting from the
/// bytes needs the type, while reading three `u64`s does not. An object written before the
/// section existed answers `None`.
pub fn recorded_counts(raw: &[u8]) -> Result<Option<header::Counts>, Error> {
    let header = Header::parse(raw)?;
    header.check_layout(raw.len())?;
    let table_end = HEADER_LEN + header.section_table_len as usize;
    let sections = parse_sections(raw, &header, table_end)?;
    Ok(sections
        .iter()
        .find(|s| s.entry.id == header::ID_STATS)
        .and_then(|s| header::Counts::parse(&raw[s.offset..s.offset + s.len])))
}

/// The section table and each section's range in the file.
///
/// Split out of the typed reader because nothing here needs the value type: the layout,
/// the padding and the id/kind pairing are properties of the container, and a caller that
/// only wants the counts should not have to name a value type to get them.
fn parse_sections(raw: &[u8], header: &Header, table_end: usize) -> Result<Vec<SectionRef>, Error> {
    let table = &raw[HEADER_LEN..table_end];
    let mut sections: Vec<SectionRef> = Vec::new();
    let mut cursor = table_end;
    let mut prev_id = 0u16;
    for i in 0..header.section_count as usize {
        let entry = Entry::parse(&table[i * ENTRY_LEN..(i + 1) * ENTRY_LEN])?;
        if entry.id <= prev_id && i > 0 {
            return Err(Error::SectionOrder);
        }
        // every section starts at the next 16-byte boundary at or after the
        // previous end, so the offset is computed and never stored
        while !cursor.is_multiple_of(16) {
            let Some(byte) = raw.get(cursor) else {
                return Err(Error::Truncated {
                    need: cursor + 1,
                    have: raw.len(),
                });
            };
            if *byte != 0 {
                return Err(Error::BadSectionTable("padding is not zero"));
            }
            cursor += 1;
        }
        let len = entry.len as usize;
        let Some(end) = cursor.checked_add(len) else {
            return Err(Error::BadSectionTable("section length overflows"));
        };
        if end > raw.len() {
            return Err(Error::Truncated {
                need: end,
                have: raw.len(),
            });
        }
        sections.push(SectionRef {
            entry,
            offset: cursor,
            len,
        });
        prev_id = entry.id;
        cursor = end;
    }
    if cursor != raw.len() {
        return Err(Error::BadSectionTable("sections do not end the file"));
    }

    for SectionRef { entry, len, .. } in &sections {
        if entry.id == header::ID_STATS && *len != header::STATS_LEN {
            return Err(Error::BadSectionTable(
                "count section is not the fixed 32 bytes",
            ));
        }
    }

    // the family sections must be the kind they claim, which is the guard
    // against reading an IPv4 archive as IPv6
    for SectionRef { entry, .. } in &sections {
        match entry.id {
            ID_V4 if entry.kind != KIND_V4 => return Err(Error::BadSectionKind(entry.id)),
            ID_V6 if entry.kind != KIND_V6 => return Err(Error::BadSectionKind(entry.id)),
            header::ID_STATS if entry.kind != header::KIND_STATS => {
                return Err(Error::BadSectionKind(entry.id))
            }
            _ => {}
        }
        // and the reverse: a family kind lives at its normative id, so an
        // object cannot dodge a reader by putting it somewhere else
        match entry.kind {
            KIND_V4 if entry.id != ID_V4 => return Err(Error::BadSectionKind(entry.id)),
            KIND_V6 if entry.id != ID_V6 => return Err(Error::BadSectionKind(entry.id)),
            header::KIND_STATS if entry.id != header::ID_STATS => {
                return Err(Error::BadSectionKind(entry.id))
            }
            _ => {}
        }
    }
    Ok(sections)
}

impl<T: SnapValue> Snapshot<T> {
    /// Merge several objects into one, later inputs winning where they hold the same
    /// prefix.
    ///
    /// This is what a daily merge is: build one object per collector or per hour,
    /// then fold them. "Overlap" means the same key: a later `/8` and an earlier
    /// `/16` are different entries and both survive, because a merge cannot know
    /// whether you meant one to cover the other.
    ///
    /// What a merge refuses, and why:
    ///
    /// - an input carrying a section of its own (id 16 or above). The values may
    ///   point into that section, and a merged object that drops it would open
    ///   cleanly while its handles pointed at nothing.
    /// - `opts.extras`, for the same reason; a profile builds its own object.
    ///
    /// The stream coordinates are dropped: several streams are not one stream. A
    /// window is claimed only when every input declares one and the intervals form a
    /// continuous chain; disjoint inputs claim nothing, because a single `window`
    /// is a validity claim over its whole span.
    pub fn merge_last_wins(inputs: &[&Snapshot<T>], opts: &Opts<'_>) -> Result<Vec<u8>, Error>
    where
        T::Archived: rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
    {
        Self::merge_with(inputs, opts, |_, _, later| Some(later))
    }

    /// The same, with `combine` deciding a collision on the same prefix.
    ///
    /// `combine(prefix, earlier, later)` returns the value to keep, or `None` to
    /// leave the prefix out of the result *of this fold*. That is not a withdrawal:
    /// absence in a later input never reaches `combine`, and a third input holding
    /// the prefix puts it back. A tombstone would have to be a value the caller
    /// understands, which is what a value type is for.
    ///
    /// Both values are owned: a merge that has to look at two archived values and
    /// then write one cannot avoid building at least one of them. The prefix is
    /// passed because a resolution that depends on the key (interning a handle into
    /// a rebuilt arena, say) cannot be written without it.
    pub fn merge_with(
        inputs: &[&Snapshot<T>],
        opts: &Opts<'_>,
        mut combine: impl FnMut(IpNet, T, T) -> Option<T>,
    ) -> Result<Vec<u8>, Error>
    where
        T::Archived: rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
    {
        if !opts.extras.is_empty() {
            return Err(Error::BadOpts(
                "a merge cannot carry a caller section: merge refuses inputs that hold \
                 sections of their own, so there is nothing to keep it consistent with",
            ));
        }
        for snap in inputs {
            for entry in snap.entries() {
                if entry.id >= crate::header::FIRST_PROFILE_ID {
                    return Err(Error::BadOpts(
                        "a merge cannot take an object that carries its own section: the \
                         values may point into it, and the merged object would not have it",
                    ));
                }
            }
        }

        let mut merged: std::collections::HashMap<IpNet, T> = std::collections::HashMap::new();
        for snap in inputs {
            for (prefix, value) in snap.iter() {
                let value =
                    rkyv::deserialize::<T, RkyvError>(value).map_err(|_| Error::BadValue)?;
                match merged.remove(&prefix) {
                    None => {
                        merged.insert(prefix, value);
                    }
                    Some(earlier) => {
                        if let Some(next) = combine(prefix, earlier, value) {
                            merged.insert(prefix, next);
                        }
                    }
                }
            }
        }

        let window = match opts.window {
            Some(w) => Some(w),
            None => Self::covers_one_span(inputs),
        };

        let mut writer = Writer::<T>::new();
        writer.insert_many(merged)?;
        let opts = Opts {
            created_at: opts.created_at,
            stream: None,
            window,
            extras: &[],
        };
        writer.finish(&opts)
    }

    /// The union of the inputs' windows, or `None` when they do not form one
    /// continuous span.
    ///
    /// `window` says "this value is valid across this range", so two inputs covering
    /// `(100,200)` and `(300,400)` cannot claim `(100,400)`: nobody observed the
    /// hole between them.
    fn covers_one_span(inputs: &[&Snapshot<T>]) -> Option<(i64, i64)> {
        if inputs.is_empty() {
            return None;
        }
        let mut spans = Vec::with_capacity(inputs.len());
        for snap in inputs {
            spans.push(snap.header().window()?);
        }
        spans.sort_unstable();
        let (mut from, mut to) = spans[0];
        for (a, b) in spans.into_iter().skip(1) {
            if a > to {
                return None;
            }
            to = to.max(b);
            from = from.min(a);
        }
        Some((from, to))
    }
}

// ---------------------------------------------------------------- owned view

/// The same queries `Snapshot` answers, returning values you own.
///
/// `snap.lpm(..)` hands back `&T::Archived`, which is the point of the format but
/// forces every caller to name the archived type. `snap.owned()` answers the same
/// questions with `T`. Both views exist for every value-returning query, which is
/// what a pair of `*_owned` methods got wrong: they covered two queries out of five.
pub struct Owned<'a, T: SnapValue> {
    snap: &'a Snapshot<T>,
}

impl<T: SnapValue> Snapshot<T> {
    /// Every value-returning query, with the value deserialized.
    pub fn owned(&self) -> Owned<'_, T> {
        Owned { snap: self }
    }
}

impl<T: SnapValue> Owned<'_, T>
where
    T::Archived: rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
{
    /// A value whose `Deserialize` refuses is an error, not an absence: returning
    /// `None` would make `len()` disagree with `iter().count()` and hide a broken
    /// value type behind a lookup miss.
    fn decode(value: &T::Archived) -> Result<T, Error> {
        rkyv::deserialize::<T, RkyvError>(value).map_err(|_| Error::BadValue)
    }

    /// How many prefixes the object holds.
    ///
    /// The table's section count is `entries().len()`; this is the same number
    /// `Snapshot::len` gives, because an owned view holds no sections of its own.
    pub fn len(&self) -> usize {
        self.snap.len()
    }

    /// Whether the object holds no prefixes.
    pub fn is_empty(&self) -> bool {
        self.snap.is_empty()
    }

    /// The entry stored exactly at `prefix`.
    pub fn get_exact(&self, prefix: IpNet) -> Result<Option<T>, Error> {
        match self.snap.get_exact(prefix) {
            Some(value) => Ok(Some(Self::decode(value)?)),
            None => Ok(None),
        }
    }

    /// The most specific stored entry covering `addr`.
    pub fn search(&self, addr: std::net::IpAddr) -> Result<Option<(IpNet, T)>, Error> {
        match self.snap.search(addr) {
            Some((prefix, value)) => Ok(Some((prefix, Self::decode(value)?))),
            None => Ok(None),
        }
    }

    /// The most specific stored entry covering `prefix`.
    pub fn lpm(&self, prefix: IpNet) -> Result<Option<(IpNet, T)>, Error> {
        match self.snap.lpm(prefix) {
            Some((prefix, value)) => Ok(Some((prefix, Self::decode(value)?))),
            None => Ok(None),
        }
    }

    /// The least specific stored entry covering `prefix`.
    pub fn spm(&self, prefix: IpNet) -> Result<Option<(IpNet, T)>, Error> {
        match self.snap.spm(prefix) {
            Some((prefix, value)) => Ok(Some((prefix, Self::decode(value)?))),
            None => Ok(None),
        }
    }

    /// Every stored entry covering `prefix`, in trie order (least specific first).
    pub fn cover(&self, prefix: IpNet) -> Result<Vec<(IpNet, T)>, Error> {
        self.snap
            .cover(prefix)
            .into_iter()
            .map(|(p, v)| Ok((p, Self::decode(v)?)))
            .collect()
    }

    /// `prefix` itself when stored, plus every stored entry under it.
    pub fn children(&self, prefix: IpNet) -> Result<Vec<(IpNet, T)>, Error> {
        self.snap
            .children(prefix)
            .into_iter()
            .map(|(p, v)| Ok((p, Self::decode(v)?)))
            .collect()
    }

    /// The whole object, IPv4 then IPv6.
    ///
    /// Materialised, unlike the archived view: an owned value has to be built, so a
    /// lazy iterator would only move that allocation into the loop body.
    pub fn iter(&self) -> Result<Vec<(IpNet, T)>, Error> {
        self.snap
            .iter()
            .map(|(p, v)| Ok((p, Self::decode(v)?)))
            .collect()
    }

    /// The values alone, IPv4 then IPv6. Materialised, like `iter`.
    pub fn values(&self) -> Result<Vec<T>, Error> {
        self.snap.values().map(|v| Self::decode(v)).collect()
    }

    /// Entries at or after `prefix` in trie order, within `prefix`'s family.
    pub fn iter_from(&self, prefix: IpNet, inclusive: bool) -> Result<Vec<(IpNet, T)>, Error> {
        self.snap
            .iter_from(prefix, inclusive)
            .map(|(p, v)| Ok((p, Self::decode(v)?)))
            .collect()
    }
}

// ---------------------------------------------------------------- reader

/// One section as a validator sees it: the table row and where its bytes are.
#[derive(Debug, Clone, Copy)]
pub struct SectionRef {
    /// The table row: id, kind, required flag and body length.
    pub entry: Entry,
    /// Byte offset of the section body in the whole object.
    pub offset: usize,
    /// Length of the section body.
    pub len: usize,
}

/// What a validator may inspect when an object is opened.
pub struct SnapshotView<'a> {
    /// The parsed header: type, identity, stream coordinates, coverage window.
    pub header: &'a Header,
    /// One row per section, in table order.
    pub sections: &'a [SectionRef],
    bytes: &'a [u8],
    table: &'a [u8],
}

impl SnapshotView<'_> {
    /// The raw bytes of section `id`, exactly as written.
    pub fn section_bytes(&self, id: u16) -> Option<&[u8]> {
        self.sections
            .iter()
            .find(|s| s.entry.id == id)
            .map(|s| &self.bytes[s.offset..s.offset + s.len])
    }

    /// The table entry for section `id`: its id, kind, required flag and length.
    /// `knows` only runs for required sections, so a profile that also wants to
    /// check the kind of an optional one does it here.
    pub fn entry(&self, id: u16) -> Option<&Entry> {
        self.sections
            .iter()
            .find(|s| s.entry.id == id)
            .map(|s| &s.entry)
    }

    /// Raw table bytes, for a reader that only wants to enumerate sections.
    pub fn table(&self) -> &[u8] {
        self.table
    }
}

/// Container-level verification that needs no value type: header, layout,
/// section table, trailing bytes and the content id. What it cannot check is
/// that a section body is a valid archive of its declared type; a reader with
/// the type does that on open.
pub fn verify_bytes(raw: &[u8]) -> Result<Header, Error> {
    let header = Header::parse(raw)?;
    header.check_layout(raw.len())?;

    let table_start = HEADER_LEN;
    let table_end = table_start + header.section_table_len as usize;
    let table = &raw[table_start..table_end];

    let mut cursor = table_end;
    let mut prev_id = 0u16;
    for i in 0..header.section_count as usize {
        let entry = Entry::parse(&table[i * ENTRY_LEN..(i + 1) * ENTRY_LEN])?;
        if entry.id <= prev_id && i > 0 {
            return Err(Error::SectionOrder);
        }
        match entry.id {
            ID_V4 if entry.kind != KIND_V4 => return Err(Error::BadSectionKind(entry.id)),
            ID_V6 if entry.kind != KIND_V6 => return Err(Error::BadSectionKind(entry.id)),
            _ => {}
        }
        match entry.kind {
            KIND_V4 if entry.id != ID_V4 => return Err(Error::BadSectionKind(entry.id)),
            KIND_V6 if entry.id != ID_V6 => return Err(Error::BadSectionKind(entry.id)),
            _ => {}
        }
        while !cursor.is_multiple_of(16) {
            // untrusted bytes: the alignment walk must not run off the end of the
            // file, which a crafted object can aim at
            let Some(byte) = raw.get(cursor) else {
                return Err(Error::Truncated {
                    need: cursor + 1,
                    have: raw.len(),
                });
            };
            if *byte != 0 {
                return Err(Error::BadSectionTable("padding is not zero"));
            }
            cursor += 1;
        }
        let Some(end) = cursor.checked_add(entry.len as usize) else {
            return Err(Error::BadSectionTable("section length overflows"));
        };
        if end > raw.len() {
            return Err(Error::Truncated {
                need: end,
                have: raw.len(),
            });
        }
        cursor = end;
        prev_id = entry.id;
    }
    if cursor != raw.len() {
        return Err(Error::BadSectionTable("sections do not end the file"));
    }

    let mut h = blake3::Hasher::new();
    h.update(b"bgpkit-snap-v4\0");
    h.update(&raw[..header::HASH_END]);
    h.update(table);
    let mut cursor = table_end;
    for i in 0..header.section_count as usize {
        let entry = Entry::parse(&table[i * ENTRY_LEN..(i + 1) * ENTRY_LEN])?;
        while !cursor.is_multiple_of(16) {
            cursor += 1;
        }
        if cursor + entry.len as usize > raw.len() {
            return Err(Error::Truncated {
                need: cursor + entry.len as usize,
                have: raw.len(),
            });
        }
        h.update(&raw[cursor..cursor + entry.len as usize]);
        cursor += entry.len as usize;
    }
    if h.finalize().as_bytes() != &header.content_id {
        return Err(Error::ContentId);
    }
    Ok(header)
}

/// Profile-side validation, run once at open before any value is handed out.
pub trait Validator {
    /// Does this validator implement the section behind `entry`? A required
    /// section that nobody knows is refused with its id.
    fn knows(&self, _entry: &Entry) -> bool {
        false
    }

    /// Cross-section checks (for example handle ranges) run here.
    fn validate(&self, _view: &SnapshotView<'_>) -> Result<(), Error> {
        Ok(())
    }
}

struct NoValidator;
impl Validator for NoValidator {}

/// An immutable object, validated once and then queried in memory.
///
/// The bytes are owned, not mapped: `open` reads the file, checks the container and
/// both trie archives, and hands back something that answers lookups without further
/// parsing.
pub struct Snapshot<T: SnapValue> {
    bytes: AlignedVec,
    header: Header,
    sections: Vec<SectionRef>,
    table_range: (usize, usize),
    _t: std::marker::PhantomData<T>,
}

impl<T: SnapValue> fmt::Debug for Snapshot<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Snapshot")
            .field("content_id", &self.header.content_id_hex())
            .field("sections", &self.sections.len())
            .finish()
    }
}

impl<T: SnapValue> Snapshot<T> {
    /// Read and validate an object from a path.
    pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self, Error>
    where
        T::Archived:
            for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
    {
        Self::from_bytes(&std::fs::read(path.as_ref())?)
    }

    /// The same, for a profile whose sections this reader implements.
    pub fn open_with(
        path: impl AsRef<std::path::Path>,
        validator: &dyn Validator,
    ) -> Result<Self, Error>
    where
        T::Archived:
            for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
    {
        Self::from_bytes_with(&std::fs::read(path.as_ref())?, validator)
    }

    /// Validate an object already in memory.
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error>
    where
        T::Archived:
            for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
    {
        Self::from_bytes_with(raw, &NoValidator)
    }

    /// The same, for a profile whose sections this reader implements.
    pub fn from_bytes_with(raw: &[u8], validator: &dyn Validator) -> Result<Self, Error>
    where
        T::Archived:
            for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
    {
        let header = Header::parse(raw)?;
        header.check_layout(raw.len())?;

        let mut aligned = AlignedVec::with_capacity(raw.len());
        aligned.extend_from_slice(raw);

        let table_start = HEADER_LEN;
        let table_end = table_start + header.section_table_len as usize;
        let table = &aligned[table_start..table_end];

        let sections = parse_sections(&aligned, &header, table_end)?;

        // identity: header prefix, table, every section
        let mut h = blake3::Hasher::new();
        h.update(b"bgpkit-snap-v4\0");
        h.update(&aligned[..header::HASH_END]);
        h.update(table);
        for SectionRef {
            offset: off, len, ..
        } in &sections
        {
            h.update(&aligned[*off..*off + *len]);
        }
        if *h.finalize().as_bytes() != header.content_id {
            return Err(Error::ContentId);
        }

        let declared = header.type_id_str();
        if declared != T::TYPE_ID {
            return Err(Error::TypeMismatch {
                expected: T::TYPE_ID.to_string(),
                found: declared,
            });
        }

        let snap = Snapshot {
            bytes: aligned,
            header,
            sections,
            table_range: (table_start, table_end),
            _t: std::marker::PhantomData,
        };

        let view = snap.view();
        for SectionRef { entry, .. } in &snap.sections {
            let known = matches!(entry.id, ID_V4 | ID_V6 | header::ID_STATS);
            if entry.required && !known && !validator.knows(entry) {
                return Err(Error::UnknownRequiredSection(entry.id));
            }
        }
        snap.validate_archives()?;
        validator.validate(&view)?;
        Ok(snap)
    }

    /// The table bytes, for a reader that wants to list sections without
    /// touching the body.
    pub fn table_bytes(&self) -> &[u8] {
        &self.bytes[self.table_range.0..self.table_range.1]
    }

    /// The section table, in file order.
    pub fn entries(&self) -> Vec<Entry> {
        self.sections.iter().map(|s| s.entry).collect()
    }

    /// The parsed header, including the claims `open` verified.
    pub fn header(&self) -> &Header {
        &self.header
    }

    /// The content id as hex: what names this object's content, not its provenance.
    pub fn content_id(&self) -> String {
        self.header.content_id_hex()
    }

    /// The raw bytes of a section, exactly as written.
    pub fn section_bytes(&self, id: u16) -> Option<&[u8]> {
        self.sections
            .iter()
            .find(|s| s.entry.id == id)
            .map(|s| &self.bytes[s.offset..s.offset + s.len])
    }

    /// What a `Validator` is handed: the header, the section table and the raw
    /// bytes, after the container and both trie archives have been checked.
    pub fn view(&self) -> SnapshotView<'_> {
        SnapshotView {
            header: &self.header,
            sections: &self.sections,
            bytes: &self.bytes,
            table: &self.bytes[self.table_range.0..self.table_range.1],
        }
    }

    fn validate_archives(&self) -> Result<(), Error>
    where
        T::Archived:
            for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
    {
        for SectionRef {
            entry,
            offset: off,
            len,
        } in &self.sections
        {
            let slice = &self.bytes[*off..*off + *len];
            match entry.id {
                ID_V4 => {
                    rkyv::access::<ArchivedPrefixMap<Ipv4Net, T>, RkyvError>(slice)
                        .map_err(|_| Error::BadArchive(ID_V4))?;
                }
                ID_V6 => {
                    rkyv::access::<ArchivedPrefixMap<Ipv6Net, T>, RkyvError>(slice)
                        .map_err(|_| Error::BadArchive(ID_V6))?;
                }
                _ => {}
            }
        }
        Ok(())
    }

    /// The IPv4 trie, if the object has one.
    ///
    /// # Safety of the unchecked access
    ///
    /// The bytes were checked by `validate_archives` during `from_bytes`, and this
    /// method is only reachable through a value that construction produced. Doing the
    /// checked access here instead would re-validate the whole archive on every
    /// lookup.
    #[allow(unsafe_code)]
    pub fn v4(&self) -> Option<&ArchivedPrefixMap<Ipv4Net, T>> {
        // SAFETY: see the doc comment; the archive was byte-checked at open.
        self.section_bytes(ID_V4)
            .map(|s| unsafe { rkyv::access_unchecked::<ArchivedPrefixMap<Ipv4Net, T>>(s) })
    }

    /// The IPv6 trie, if the object has one. Same unchecked access as `v4`.
    #[allow(unsafe_code)]
    pub fn v6(&self) -> Option<&ArchivedPrefixMap<Ipv6Net, T>> {
        // SAFETY: see `v4`; the archive was byte-checked at open.
        self.section_bytes(ID_V6)
            .map(|s| unsafe { rkyv::access_unchecked::<ArchivedPrefixMap<Ipv6Net, T>>(s) })
    }

    /// How many prefixes the object holds.
    pub fn len(&self) -> usize {
        self.v4().map(|m| m.len()).unwrap_or(0) + self.v6().map(|m| m.len()).unwrap_or(0)
    }

    /// Whether the object holds nothing.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Longest-prefix match for a single address.
    pub fn search(&self, addr: std::net::IpAddr) -> Option<(IpNet, &T::Archived)> {
        match addr {
            std::net::IpAddr::V4(a) => {
                let p = Ipv4Net::new(a, 32).ok()?;
                let map = self.v4()?;
                let (lpm, value) = map.get_lpm(&p)?;
                Some((IpNet::V4(lpm), value))
            }
            std::net::IpAddr::V6(a) => {
                let p = Ipv6Net::new(a, 128).ok()?;
                let map = self.v6()?;
                let (lpm, value) = map.get_lpm(&p)?;
                Some((IpNet::V6(lpm), value))
            }
        }
    }

    /// Longest prefix that covers `prefix`, and its value.
    ///
    /// `prefix` is a prefix, not an address: for one address use `search`, which
    /// builds the `/32` or `/128` for you.
    pub fn lpm(&self, prefix: IpNet) -> Option<(IpNet, &T::Archived)> {
        match prefix {
            IpNet::V4(p) => {
                let map = self.v4()?;
                let (lpm, value) = map.get_lpm(&p)?;
                Some((IpNet::V4(lpm), value))
            }
            IpNet::V6(p) => {
                let map = self.v6()?;
                let (lpm, value) = map.get_lpm(&p)?;
                Some((IpNet::V6(lpm), value))
            }
        }
    }

    // ---- the read side of prefix_trie, with IpNet keys ----
    //
    // A snapshot holds two tries, one per family. These forward the map's own
    // read API so a caller does not have to match on the family and hold two
    // code paths for every query.

    /// The value stored exactly at `prefix`, with no ancestor fallback.
    pub fn get_exact(&self, prefix: IpNet) -> Option<&T::Archived> {
        match prefix {
            IpNet::V4(p) => self.v4().and_then(|m| m.get(&p)),
            IpNet::V6(p) => self.v6().and_then(|m| m.get(&p)),
        }
    }

    /// Whether an entry is stored exactly at `prefix`.
    pub fn contains_key(&self, prefix: IpNet) -> bool {
        match prefix {
            IpNet::V4(p) => self.v4().is_some_and(|m| m.contains_key(&p)),
            IpNet::V6(p) => self.v6().is_some_and(|m| m.contains_key(&p)),
        }
    }

    /// The least specific stored entry covering `prefix`.
    pub fn spm(&self, prefix: IpNet) -> Option<(IpNet, &T::Archived)> {
        match prefix {
            IpNet::V4(p) => self
                .v4()
                .and_then(|m| m.get_spm(&p))
                .map(|(k, v)| (IpNet::V4(k), v)),
            IpNet::V6(p) => self
                .v6()
                .and_then(|m| m.get_spm(&p))
                .map(|(k, v)| (IpNet::V6(k), v)),
        }
    }

    /// Whether any stored entry covers `prefix`.
    pub fn is_covered(&self, prefix: IpNet) -> bool {
        match prefix {
            IpNet::V4(p) => self.v4().is_some_and(|m| m.is_covered(&p)),
            IpNet::V6(p) => self.v6().is_some_and(|m| m.is_covered(&p)),
        }
    }

    /// Whether `prefix` is covered by an aggregate entry, that is a stored
    /// prefix carrying its whole subtree.
    pub fn is_covered_in_aggregate(&self, prefix: IpNet) -> bool {
        match prefix {
            IpNet::V4(p) => self.v4().is_some_and(|m| m.is_covered_in_aggregate(&p)),
            IpNet::V6(p) => self.v6().is_some_and(|m| m.is_covered_in_aggregate(&p)),
        }
    }

    /// Every entry, IPv4 first then IPv6, in trie order.
    pub fn iter(&self) -> Box<dyn Iterator<Item = (IpNet, &T::Archived)> + '_> {
        let v4 = self
            .v4()
            .into_iter()
            .flat_map(|m| m.iter())
            .map(|(p, v)| (IpNet::V4(p), v));
        let v6 = self
            .v6()
            .into_iter()
            .flat_map(|m| m.iter())
            .map(|(p, v)| (IpNet::V6(p), v));
        Box::new(v4.chain(v6))
    }

    /// Every key, IPv4 first then IPv6.
    pub fn keys(&self) -> Box<dyn Iterator<Item = IpNet> + '_> {
        Box::new(self.iter().map(|(p, _)| p))
    }

    /// Every entry at or after `prefix` in trie order, within `prefix`'s family;
    /// an IPv4 start does not continue into the IPv6 trie. Use `iter()` for both.
    pub fn iter_from(
        &self,
        prefix: IpNet,
        inclusive: bool,
    ) -> Box<dyn Iterator<Item = (IpNet, &T::Archived)> + '_> {
        match prefix {
            IpNet::V4(p) => {
                let it = self
                    .v4()
                    .into_iter()
                    .flat_map(move |m| m.iter_from(&p, inclusive))
                    .map(|(k, v)| (IpNet::V4(k), v));
                Box::new(it)
            }
            IpNet::V6(p) => {
                let it = self
                    .v6()
                    .into_iter()
                    .flat_map(move |m| m.iter_from(&p, inclusive))
                    .map(|(k, v)| (IpNet::V6(k), v));
                Box::new(it)
            }
        }
    }

    /// Every value, IPv4 first then IPv6.
    pub fn values(&self) -> Box<dyn Iterator<Item = &T::Archived> + '_> {
        let v4 = self.v4().into_iter().flat_map(|m| m.values());
        let v6 = self.v6().into_iter().flat_map(|m| m.values());
        Box::new(v4.chain(v6))
    }

    /// How many addresses the entries cover, per family.
    pub fn address_counts(&self) -> AddressCounts {
        let count = |m: Option<Option<u128>>| match m {
            None => Count::NoEntries,
            Some(None) => Count::All,
            Some(Some(n)) => Count::Exact(n),
        };
        AddressCounts {
            v4: count(self.v4().map(|m| m.address_count().map(u128::from))),
            v6: count(self.v6().map(|m| m.address_count())),
        }
    }

    /// The prefixes that cover `prefix`, each with its value.
    pub fn cover(&self, prefix: IpNet) -> Vec<(IpNet, &T::Archived)> {
        match prefix {
            IpNet::V4(p) => self
                .v4()
                .map(|m| m.cover(&p).map(|(k, v)| (IpNet::V4(k), v)).collect())
                .unwrap_or_default(),
            IpNet::V6(p) => self
                .v6()
                .map(|m| m.cover(&p).map(|(k, v)| (IpNet::V6(k), v)).collect())
                .unwrap_or_default(),
        }
    }

    /// The prefix itself when it is stored, plus everything under it, in trie
    /// order. A query for a block that is not stored returns only what is under
    /// it, never a sibling.
    ///
    /// The query prefix is read as its network address, and every key returned is a
    /// key the object contains: a query for `10.1.2.3/16` answers for `10.1.0.0/16`
    /// and returns that key, never the caller's spelling of it.
    pub fn children(&self, prefix: IpNet) -> Vec<(IpNet, &T::Archived)> {
        let mut out: Vec<(IpNet, &T::Archived)> = Vec::new();
        match prefix {
            IpNet::V4(p) => {
                let p = p.trunc();
                let Some(map) = self.v4() else { return out };
                if let Some((k, v)) = map.get_key_value(&p) {
                    out.push((IpNet::V4(k), v));
                }
                if let Some(view) = map.view_at(&p) {
                    for (k, v) in view.iter() {
                        if k != p {
                            out.push((IpNet::V4(k), v));
                        }
                    }
                }
            }
            IpNet::V6(p) => {
                let p = p.trunc();
                let Some(map) = self.v6() else { return out };
                if let Some((k, v)) = map.get_key_value(&p) {
                    out.push((IpNet::V6(k), v));
                }
                if let Some(view) = map.view_at(&p) {
                    for (k, v) in view.iter() {
                        if k != p {
                            out.push((IpNet::V6(k), v));
                        }
                    }
                }
            }
        }
        out
    }
}