littlegraph-core 0.2.1

Embedded openCypher graph database backed by Neo4j record storage
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
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
use std::collections::BTreeSet;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};

use crate::error::{Neo4jStoreError, Result};

const META_SIZE: usize = 40;
const STATE_SIZE: usize = 114;
const STATE_FIELDS_SIZE: usize = STATE_SIZE / 2;
const HEADER_LENGTH_SIZE: usize = 4;
const ID_HEADER_SIZE: usize = 36;
const EXPECTED_IDS_PER_ENTRY: u32 = 128;
const PAGE_SIZE: usize = 8192;
const BASE_HEADER_SIZE: usize = 82;
const KEY_SIZE: usize = 8;
const LONGS_PER_ENTRY: usize = EXPECTED_IDS_PER_ENTRY as usize / u64::BITS as usize;
const BITSET_COUNT: usize = 3;
const VALUE_SIZE: usize = 8 + LONGS_PER_ENTRY * 8 * BITSET_COUNT;
const MAX_KEY_COUNT: usize = (PAGE_SIZE - BASE_HEADER_SIZE) / (KEY_SIZE + VALUE_SIZE);
const VALUE_AREA_OFFSET: usize = BASE_HEADER_SIZE + MAX_KEY_COUNT * KEY_SIZE;
const ID_RANGE_LAYOUT_IDENTIFIER: u64 = 3_735_929_054 + EXPECTED_IDS_PER_ENTRY as u64;
const TREE_NODE: u8 = 1;
const LEAF_NODE: u8 = 1;
const INTERNAL_NODE: u8 = 0;
const DATA_LAYER: u8 = 0;
const PAGE_REFERENCE_SIZE: usize = 24;
const GENERATION_SAFE_POINTER_SIZE: usize = 12;
const INTERNAL_MAX_KEY_COUNT: usize =
    (PAGE_SIZE - BASE_HEADER_SIZE - PAGE_REFERENCE_SIZE) / (KEY_SIZE + PAGE_REFERENCE_SIZE);
const INTERNAL_CHILDREN_OFFSET: usize = BASE_HEADER_SIZE + INTERNAL_MAX_KEY_COUNT * KEY_SIZE;
const BITSET_COMMIT: usize = 0;
const BITSET_REUSE: usize = 1;
const BITSET_RESERVED: usize = 2;

pub(super) fn empty_file(high_id: u64) -> Vec<u8> {
    let highest_written_id = high_id.checked_sub(1).unwrap_or(u64::MAX);
    let mut bytes = vec![0_u8; PAGE_SIZE * 4];
    bytes[4..8].copy_from_slice(&(PAGE_SIZE as u32).to_be_bytes());
    bytes[8..16].copy_from_slice(&ID_RANGE_LAYOUT_IDENTIFIER.to_be_bytes());
    write_empty_state(&mut bytes, 1, 5, high_id, highest_written_id);
    write_empty_state(&mut bytes, 2, 4, high_id, highest_written_id);
    let root = &mut bytes[3 * PAGE_SIZE..4 * PAGE_SIZE];
    root[0] = TREE_NODE;
    root[1] = LEAF_NODE;
    root[2..6].copy_from_slice(&3_u32.to_be_bytes());
    bytes
}

fn write_empty_state(
    bytes: &mut [u8],
    page_id: usize,
    unstable: u32,
    high_id: u64,
    highest_written_id: u64,
) {
    let offset = PAGE_SIZE * page_id;
    let mut fields = [0_u8; STATE_FIELDS_SIZE];
    fields[0..4].copy_from_slice(&4_u32.to_be_bytes());
    fields[4..8].copy_from_slice(&unstable.to_be_bytes());
    fields[8..16].copy_from_slice(&3_u64.to_be_bytes());
    fields[16..24].copy_from_slice(&3_u64.to_be_bytes());
    fields[24..32].copy_from_slice(&3_u64.to_be_bytes());
    fields[56] = 1;
    bytes[offset..offset + STATE_FIELDS_SIZE].copy_from_slice(&fields);
    bytes[offset + STATE_FIELDS_SIZE..offset + STATE_SIZE].copy_from_slice(&fields);
    let header_offset = offset + STATE_SIZE;
    bytes[header_offset..header_offset + HEADER_LENGTH_SIZE]
        .copy_from_slice(&(ID_HEADER_SIZE as u32).to_be_bytes());
    let header = &mut bytes
        [header_offset + HEADER_LENGTH_SIZE..header_offset + HEADER_LENGTH_SIZE + ID_HEADER_SIZE];
    header[0..8].copy_from_slice(&high_id.to_be_bytes());
    header[8..16].copy_from_slice(&highest_written_id.to_be_bytes());
    header[16..24].copy_from_slice(&2_u64.to_be_bytes());
    header[24..28].copy_from_slice(&EXPECTED_IDS_PER_ENTRY.to_be_bytes());
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct IdGeneratorHeader {
    pub(super) high_id: u64,
    pub(super) highest_written_id: u64,
    pub(super) generation: u64,
    pub(super) ids_per_entry: u32,
    pub(super) num_unused_ids: u64,
    pub(super) state_header_offsets: [u64; 2],
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct TreeState {
    page_id: u64,
    stable_generation: u32,
    unstable_generation: u32,
    root_id: u64,
    root_generation: u64,
    last_id: u64,
    clean: bool,
    valid: bool,
}

#[allow(dead_code)]
pub(super) struct IdAllocation {
    pub(super) first_id: u64,
    pub(super) header_patches: [(u64, Vec<u8>); 2],
}

#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
pub(super) struct IdFreeUpdate {
    pub(super) root_page_offset: u64,
    pub(super) root_page: Vec<u8>,
    pub(super) header_patches: [(u64, Vec<u8>); 2],
}

#[derive(Debug, PartialEq, Eq)]
pub(super) struct IdReuseUpdate {
    pub(super) ids: Vec<u64>,
    pub(super) root_page_offset: u64,
    pub(super) root_page: Vec<u8>,
    pub(super) header_patches: [(u64, Vec<u8>); 2],
}

#[derive(Debug, PartialEq, Eq)]
pub(super) struct IdTransactionUpdate {
    pub(super) reused_ids: Vec<u64>,
    pub(super) root_page_offset: u64,
    pub(super) root_page: Vec<u8>,
    pub(super) header_patches: [(u64, Vec<u8>); 2],
}

#[cfg(test)]
fn read(path: &Path) -> Result<IdGeneratorHeader> {
    let (_, payload_size, _, header) = read_tree_header(path, false)?;
    let offsets = [
        state_header_offset(payload_size, 1) as u64,
        state_header_offset(payload_size, 2) as u64,
    ];
    Ok(IdGeneratorHeader {
        state_header_offsets: offsets,
        ..header
    })
}

pub(super) fn validate_writable(
    database_directory: &Path,
    store_name: &str,
    physical_record_count: u64,
) -> Result<IdGeneratorHeader> {
    let path = database_directory.join(format!("{store_name}.id"));
    let (mut file, payload_size, selected, mut header) = read_tree_header(&path, true)?;
    header.state_header_offsets = [
        state_header_offset(payload_size, 1) as u64,
        state_header_offset(payload_size, 2) as u64,
    ];
    if header.generation <= 1 || header.ids_per_entry != EXPECTED_IDS_PER_ENTRY {
        return Err(unsupported(
            database_directory,
            format!(
                "ID generator {} requires rebuild or uses an unsupported range size",
                path.display()
            ),
        ));
    }
    if header.high_id > physical_record_count {
        return Err(unsupported(
            database_directory,
            format!(
                "ID generator {} has high ID {}, beyond physical record capacity {physical_record_count}",
                path.display(),
                header.high_id
            ),
        ));
    }
    if header.high_id == 0 {
        if header.highest_written_id != u64::MAX {
            return Err(invalid(
                &path,
                0,
                "zero high ID has a non-empty highest-written ID",
            ));
        }
    } else if header.highest_written_id >= header.high_id {
        return Err(invalid(&path, 0, "highest-written ID is not below high ID"));
    }
    let persisted_unused_ids = validate_range_tree(&mut file, &path, selected, header.high_id)?;
    validate_unused_id_count(&path, selected.page_id, header, persisted_unused_ids)?;
    Ok(header)
}

#[allow(dead_code)]
pub(super) fn allocate_high_ids(
    header: IdGeneratorHeader,
    count: u64,
) -> std::result::Result<IdAllocation, String> {
    if count == 0 {
        return Err("ID allocation count must be greater than zero".to_owned());
    }
    let first_id = header.high_id;
    let next_high_id = first_id
        .checked_add(count)
        .ok_or_else(|| "ID generator high ID overflow".to_owned())?;
    let highest_written_id = next_high_id - 1;
    let mut values = Vec::with_capacity(16);
    values.extend_from_slice(&next_high_id.to_be_bytes());
    values.extend_from_slice(&highest_written_id.to_be_bytes());
    Ok(IdAllocation {
        first_id,
        header_patches: header
            .state_header_offsets
            .map(|offset| (offset, values.clone())),
    })
}

#[allow(dead_code)]
pub(super) fn prepare_mark_deleted_and_free(path: &Path, ids: &[u64]) -> Result<IdFreeUpdate> {
    if ids.is_empty() {
        return Err(invalid(path, 0, "ID free list cannot be empty"));
    }
    let mut ids = ids.to_vec();
    ids.sort_unstable();
    if ids.windows(2).any(|pair| pair[0] == pair[1]) {
        return Err(invalid(path, 0, "ID free list contains a duplicate ID"));
    }

    let (mut file, _, selected, header) = read_tree_header(path, true)?;
    if header.ids_per_entry != EXPECTED_IDS_PER_ENTRY {
        return Err(invalid(path, 0, "unsupported ID range size"));
    }
    if ids.iter().any(|id| *id >= header.high_id) {
        return Err(invalid(
            path,
            0,
            "cannot free an ID at or above the high ID",
        ));
    }

    let root_offset = usize::try_from(selected.root_id)
        .ok()
        .and_then(|id| id.checked_mul(PAGE_SIZE))
        .ok_or_else(|| invalid(path, 0, "ID range root page offset overflow"))?;
    let mut root = read_region(
        &mut file,
        path,
        root_offset as u64,
        PAGE_SIZE,
        root_offset,
        "ID range root page is truncated",
    )?;
    let persisted_unused_ids = validate_range_root(
        path,
        root_offset,
        &root,
        selected.root_generation,
        header.high_id,
    )?;
    validate_unused_id_count(path, selected.page_id, header, persisted_unused_ids)?;
    let key_count = read_u32_from(&root, 6) as usize;
    let mut entries = (0..key_count)
        .map(|position| {
            let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
            let value_offset = VALUE_AREA_OFFSET + position * VALUE_SIZE;
            let key = read_u64_from(&root, key_offset);
            let value: [u8; VALUE_SIZE] = root[value_offset..value_offset + VALUE_SIZE]
                .try_into()
                .expect("validated fixed-size ID range value");
            (key, value)
        })
        .collect::<Vec<_>>();

    for id in &ids {
        let range_index = *id / EXPECTED_IDS_PER_ENTRY as u64;
        let offset_in_range = (*id % EXPECTED_IDS_PER_ENTRY as u64) as usize;
        let position = entries.iter().position(|(key, _)| *key == range_index);
        let position = if let Some(position) = position {
            position
        } else {
            let mut value = [0_u8; VALUE_SIZE];
            value[..8].copy_from_slice(&header.generation.to_be_bytes());
            entries.push((range_index, value));
            entries.len() - 1
        };
        let value = &mut entries[position].1;
        let existing_generation = read_u64_from(value, 0);
        if existing_generation != header.generation {
            for long_index in 0..LONGS_PER_ENTRY {
                let commit = read_bitset_long(value, BITSET_COMMIT, long_index);
                write_bitset_long(value, BITSET_REUSE, long_index, commit);
                write_bitset_long(value, BITSET_RESERVED, long_index, 0);
            }
            value[..8].copy_from_slice(&header.generation.to_be_bytes());
        }
        let long_index = offset_in_range / u64::BITS as usize;
        let bit_index = offset_in_range % u64::BITS as usize;
        let mask = 1_u64 << bit_index;
        let commit = read_bitset_long(value, BITSET_COMMIT, long_index);
        if commit & mask != 0 {
            return Err(invalid(
                path,
                root_offset,
                format!("ID {id} is already unused"),
            ));
        }
        write_bitset_long(value, BITSET_COMMIT, long_index, commit | mask);
        let reuse = read_bitset_long(value, BITSET_REUSE, long_index);
        write_bitset_long(value, BITSET_REUSE, long_index, reuse | mask);
        let reserved = read_bitset_long(value, BITSET_RESERVED, long_index);
        write_bitset_long(value, BITSET_RESERVED, long_index, reserved & !mask);
    }

    entries.sort_unstable_by_key(|(key, _)| *key as i64);
    if entries.len() > MAX_KEY_COUNT {
        return Err(invalid(
            path,
            root_offset,
            "ID range leaf has no capacity for a new key",
        ));
    }
    root[6..10].copy_from_slice(&(entries.len() as u32).to_be_bytes());
    root[BASE_HEADER_SIZE..VALUE_AREA_OFFSET].fill(0);
    root[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + MAX_KEY_COUNT * VALUE_SIZE].fill(0);
    for (position, (key, value)) in entries.into_iter().enumerate() {
        let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
        root[key_offset..key_offset + KEY_SIZE].copy_from_slice(&key.to_be_bytes());
        let value_offset = VALUE_AREA_OFFSET + position * VALUE_SIZE;
        root[value_offset..value_offset + VALUE_SIZE].copy_from_slice(&value);
    }

    let num_unused_ids = header
        .num_unused_ids
        .checked_add(ids.len() as u64)
        .ok_or_else(|| invalid(path, 0, "unused ID count overflow"))?;
    Ok(IdFreeUpdate {
        root_page_offset: root_offset as u64,
        root_page: root,
        header_patches: [1_u64, 2_u64].map(|page_id| {
            (
                (state_header_offset(PAGE_SIZE, page_id) + 28) as u64,
                num_unused_ids.to_be_bytes().to_vec(),
            )
        }),
    })
}

pub(super) fn prepare_take_free_ids(path: &Path, count: usize) -> Result<Option<IdReuseUpdate>> {
    if count == 0 {
        return Err(invalid(path, 0, "ID reuse count must be greater than zero"));
    }
    let (mut file, _, selected, header) = read_tree_header(path, true)?;
    if header.ids_per_entry != EXPECTED_IDS_PER_ENTRY {
        return Err(invalid(path, 0, "unsupported ID range size"));
    }
    let root_offset = usize::try_from(selected.root_id)
        .ok()
        .and_then(|id| id.checked_mul(PAGE_SIZE))
        .ok_or_else(|| invalid(path, 0, "ID range root page offset overflow"))?;
    let mut root = read_region(
        &mut file,
        path,
        root_offset as u64,
        PAGE_SIZE,
        root_offset,
        "ID range root page is truncated",
    )?;
    let persisted_unused_ids = validate_range_root(
        path,
        root_offset,
        &root,
        selected.root_generation,
        header.high_id,
    )?;
    validate_unused_id_count(path, selected.page_id, header, persisted_unused_ids)?;
    if header.num_unused_ids == 0 {
        return Ok(None);
    }
    let key_count = read_u32_from(&root, 6) as usize;
    let mut ids = Vec::with_capacity(count);

    for position in 0..key_count {
        let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
        let range_index = read_u64_from(&root, key_offset);
        let value_offset = VALUE_AREA_OFFSET + position * VALUE_SIZE;
        let value = &mut root[value_offset..value_offset + VALUE_SIZE];
        if read_u64_from(value, 0) != header.generation {
            for long_index in 0..LONGS_PER_ENTRY {
                let commit = read_bitset_long(value, BITSET_COMMIT, long_index);
                write_bitset_long(value, BITSET_REUSE, long_index, commit);
                write_bitset_long(value, BITSET_RESERVED, long_index, 0);
            }
            value[..8].copy_from_slice(&header.generation.to_be_bytes());
        }
        for long_index in 0..LONGS_PER_ENTRY {
            let mut commit = read_bitset_long(value, BITSET_COMMIT, long_index);
            let mut reuse = read_bitset_long(value, BITSET_REUSE, long_index);
            let mut reserved = read_bitset_long(value, BITSET_RESERVED, long_index);
            let mut available = commit & reuse & !reserved;
            while available != 0 && ids.len() < count {
                let bit_index = available.trailing_zeros() as usize;
                let offset_in_range = long_index * u64::BITS as usize + bit_index;
                let id = range_index
                    .checked_mul(EXPECTED_IDS_PER_ENTRY as u64)
                    .and_then(|base| base.checked_add(offset_in_range as u64))
                    .ok_or_else(|| invalid(path, root_offset, "reused ID overflow"))?;
                let mask = 1_u64 << bit_index;
                available &= !mask;
                if id >= header.high_id {
                    continue;
                }
                ids.push(id);
                commit &= !mask;
                reuse &= !mask;
                reserved &= !mask;
            }
            write_bitset_long(value, BITSET_COMMIT, long_index, commit);
            write_bitset_long(value, BITSET_REUSE, long_index, reuse);
            write_bitset_long(value, BITSET_RESERVED, long_index, reserved);
            if ids.len() == count {
                break;
            }
        }
        if ids.len() == count {
            break;
        }
    }
    if ids.is_empty() {
        return Ok(None);
    }
    let num_unused_ids = header
        .num_unused_ids
        .checked_sub(ids.len() as u64)
        .ok_or_else(|| invalid(path, 0, "unused ID count underflow"))?;
    Ok(Some(IdReuseUpdate {
        ids,
        root_page_offset: root_offset as u64,
        root_page: root,
        header_patches: [1_u64, 2_u64].map(|page_id| {
            (
                (state_header_offset(PAGE_SIZE, page_id) + 28) as u64,
                num_unused_ids.to_be_bytes().to_vec(),
            )
        }),
    }))
}

pub(super) fn prepare_reuse_and_free_ids(
    path: &Path,
    reuse_count: usize,
    freed_ids: &[u64],
    high_id_limit: u64,
) -> Result<IdTransactionUpdate> {
    if reuse_count == 0 && freed_ids.is_empty() {
        return Err(invalid(path, 0, "ID transaction update cannot be empty"));
    }
    let mut freed_ids = freed_ids.to_vec();
    freed_ids.sort_unstable();
    if freed_ids.windows(2).any(|pair| pair[0] == pair[1]) {
        return Err(invalid(path, 0, "ID free list contains a duplicate ID"));
    }

    let (mut file, _, selected, header) = read_tree_header(path, true)?;
    if header.ids_per_entry != EXPECTED_IDS_PER_ENTRY {
        return Err(invalid(path, 0, "unsupported ID range size"));
    }
    let high_id_limit = high_id_limit.max(header.high_id);
    if freed_ids.iter().any(|id| *id >= high_id_limit) {
        return Err(invalid(
            path,
            0,
            "cannot free an ID at or above the transaction high ID",
        ));
    }

    let root_offset = usize::try_from(selected.root_id)
        .ok()
        .and_then(|id| id.checked_mul(PAGE_SIZE))
        .ok_or_else(|| invalid(path, 0, "ID range root page offset overflow"))?;
    let mut root = read_region(
        &mut file,
        path,
        root_offset as u64,
        PAGE_SIZE,
        root_offset,
        "ID range root page is truncated",
    )?;
    let persisted_unused_ids = validate_range_root(
        path,
        root_offset,
        &root,
        selected.root_generation,
        header.high_id,
    )?;
    validate_unused_id_count(path, selected.page_id, header, persisted_unused_ids)?;
    let key_count = read_u32_from(&root, 6) as usize;
    let mut entries = (0..key_count)
        .map(|position| {
            let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
            let value_offset = VALUE_AREA_OFFSET + position * VALUE_SIZE;
            let key = read_u64_from(&root, key_offset);
            let value: [u8; VALUE_SIZE] = root[value_offset..value_offset + VALUE_SIZE]
                .try_into()
                .expect("validated fixed-size ID range value");
            (key, value)
        })
        .collect::<Vec<_>>();

    let mut reused_ids = Vec::with_capacity(reuse_count);
    for (range_index, value) in &mut entries {
        if read_u64_from(value, 0) != header.generation {
            for long_index in 0..LONGS_PER_ENTRY {
                let commit = read_bitset_long(value, BITSET_COMMIT, long_index);
                write_bitset_long(value, BITSET_REUSE, long_index, commit);
                write_bitset_long(value, BITSET_RESERVED, long_index, 0);
            }
            value[..8].copy_from_slice(&header.generation.to_be_bytes());
        }
        for long_index in 0..LONGS_PER_ENTRY {
            let mut commit = read_bitset_long(value, BITSET_COMMIT, long_index);
            let mut reuse = read_bitset_long(value, BITSET_REUSE, long_index);
            let mut reserved = read_bitset_long(value, BITSET_RESERVED, long_index);
            let mut available = commit & reuse & !reserved;
            while available != 0 && reused_ids.len() < reuse_count {
                let bit_index = available.trailing_zeros() as usize;
                let offset_in_range = long_index * u64::BITS as usize + bit_index;
                let id = range_index
                    .checked_mul(EXPECTED_IDS_PER_ENTRY as u64)
                    .and_then(|base| base.checked_add(offset_in_range as u64))
                    .ok_or_else(|| invalid(path, root_offset, "reused ID overflow"))?;
                let mask = 1_u64 << bit_index;
                available &= !mask;
                if id >= header.high_id {
                    continue;
                }
                reused_ids.push(id);
                commit &= !mask;
                reuse &= !mask;
                reserved &= !mask;
            }
            write_bitset_long(value, BITSET_COMMIT, long_index, commit);
            write_bitset_long(value, BITSET_REUSE, long_index, reuse);
            write_bitset_long(value, BITSET_RESERVED, long_index, reserved);
            if reused_ids.len() == reuse_count {
                break;
            }
        }
        if reused_ids.len() == reuse_count {
            break;
        }
    }
    if reused_ids.len() != reuse_count {
        return Err(invalid(
            path,
            root_offset,
            "selected reusable ID count changed before commit",
        ));
    }

    for id in &freed_ids {
        let range_index = *id / EXPECTED_IDS_PER_ENTRY as u64;
        let offset_in_range = (*id % EXPECTED_IDS_PER_ENTRY as u64) as usize;
        let position = entries.iter().position(|(key, _)| *key == range_index);
        let position = if let Some(position) = position {
            position
        } else {
            let mut value = [0_u8; VALUE_SIZE];
            value[..8].copy_from_slice(&header.generation.to_be_bytes());
            entries.push((range_index, value));
            entries.len() - 1
        };
        let value = &mut entries[position].1;
        if read_u64_from(value, 0) != header.generation {
            for long_index in 0..LONGS_PER_ENTRY {
                let commit = read_bitset_long(value, BITSET_COMMIT, long_index);
                write_bitset_long(value, BITSET_REUSE, long_index, commit);
                write_bitset_long(value, BITSET_RESERVED, long_index, 0);
            }
            value[..8].copy_from_slice(&header.generation.to_be_bytes());
        }
        let long_index = offset_in_range / u64::BITS as usize;
        let bit_index = offset_in_range % u64::BITS as usize;
        let mask = 1_u64 << bit_index;
        let commit = read_bitset_long(value, BITSET_COMMIT, long_index);
        if commit & mask != 0 {
            return Err(invalid(
                path,
                root_offset,
                format!("ID {id} is already unused"),
            ));
        }
        write_bitset_long(value, BITSET_COMMIT, long_index, commit | mask);
        let reuse = read_bitset_long(value, BITSET_REUSE, long_index);
        write_bitset_long(value, BITSET_REUSE, long_index, reuse | mask);
        let reserved = read_bitset_long(value, BITSET_RESERVED, long_index);
        write_bitset_long(value, BITSET_RESERVED, long_index, reserved & !mask);
    }

    entries.sort_unstable_by_key(|(key, _)| *key as i64);
    if entries.len() > MAX_KEY_COUNT {
        return Err(invalid(
            path,
            root_offset,
            "ID range leaf has no capacity for a new key",
        ));
    }
    root[6..10].copy_from_slice(&(entries.len() as u32).to_be_bytes());
    root[BASE_HEADER_SIZE..VALUE_AREA_OFFSET].fill(0);
    root[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + MAX_KEY_COUNT * VALUE_SIZE].fill(0);
    for (position, (key, value)) in entries.into_iter().enumerate() {
        let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
        root[key_offset..key_offset + KEY_SIZE].copy_from_slice(&key.to_be_bytes());
        let value_offset = VALUE_AREA_OFFSET + position * VALUE_SIZE;
        root[value_offset..value_offset + VALUE_SIZE].copy_from_slice(&value);
    }

    let num_unused_ids = header
        .num_unused_ids
        .checked_sub(reused_ids.len() as u64)
        .and_then(|count| count.checked_add(freed_ids.len() as u64))
        .ok_or_else(|| invalid(path, 0, "unused ID count overflow"))?;
    Ok(IdTransactionUpdate {
        reused_ids,
        root_page_offset: root_offset as u64,
        root_page: root,
        header_patches: [1_u64, 2_u64].map(|page_id| {
            (
                (state_header_offset(PAGE_SIZE, page_id) + 28) as u64,
                num_unused_ids.to_be_bytes().to_vec(),
            )
        }),
    })
}

fn read_tree_header(
    path: &Path,
    require_range_layout: bool,
) -> Result<(File, usize, TreeState, IdGeneratorHeader)> {
    let mut file = File::open(path).map_err(|source| Neo4jStoreError::Read {
        path: path.to_path_buf(),
        source,
    })?;
    let file_len = file
        .metadata()
        .map_err(|source| Neo4jStoreError::Read {
            path: path.to_path_buf(),
            source,
        })?
        .len();
    if file_len < META_SIZE as u64 {
        return Err(invalid(path, 0, "truncated GB+Tree metadata"));
    }
    let metadata = read_region(
        &mut file,
        path,
        0,
        META_SIZE,
        0,
        "truncated GB+Tree metadata",
    )?;
    let payload_size = read_u32(path, &metadata, 4)? as usize;
    if payload_size < STATE_SIZE + HEADER_LENGTH_SIZE + ID_HEADER_SIZE {
        return Err(invalid(path, 4, "invalid GB+Tree payload size"));
    }
    if require_range_layout {
        validate_range_metadata(path, &metadata)?;
    }
    let required_len = (payload_size as u64)
        .checked_mul(3)
        .ok_or_else(|| invalid(path, 4, "GB+Tree file length overflow"))?;
    if file_len < required_len {
        return Err(invalid(
            path,
            usize::try_from(file_len).unwrap_or(usize::MAX),
            "truncated GB+Tree state pages",
        ));
    }

    let page_prefix_len = STATE_SIZE + HEADER_LENGTH_SIZE + ID_HEADER_SIZE;
    let mut pages = Vec::with_capacity(2);
    for page_id in [1_u64, 2_u64] {
        let page_offset = (payload_size as u64)
            .checked_mul(page_id)
            .ok_or_else(|| invalid(path, 0, "state page offset overflow"))?;
        pages.push(read_region(
            &mut file,
            path,
            page_offset,
            page_prefix_len,
            usize::try_from(page_offset).unwrap_or(usize::MAX),
            "truncated tree state",
        )?);
    }
    let state_a = read_state(&pages[0], 1);
    let state_b = read_state(&pages[1], 2);
    let selected = select_newest(path, state_a, state_b)?;
    if selected.root_id > selected.last_id {
        return Err(invalid(
            path,
            payload_size * selected.page_id as usize + 8,
            "ID range tree root page ID is beyond the last allocated page",
        ));
    }
    let selected_index = usize::try_from(selected.page_id - 1).expect("state page is 1 or 2");
    let page_offset = (payload_size as u64)
        .checked_mul(selected.page_id)
        .ok_or_else(|| invalid(path, 0, "state page offset overflow"))?;
    let header = read_embedded_header(path, &pages[selected_index], page_offset, file_len)?;
    Ok((file, payload_size, selected, header))
}

fn read_region(
    file: &mut File,
    path: &Path,
    offset: u64,
    len: usize,
    invalid_offset: usize,
    truncated_message: &str,
) -> Result<Vec<u8>> {
    file.seek(SeekFrom::Start(offset))
        .map_err(|source| Neo4jStoreError::Read {
            path: path.to_path_buf(),
            source,
        })?;
    let mut bytes = vec![0_u8; len];
    let mut actual = 0;
    while actual < len {
        match file.read(&mut bytes[actual..]) {
            Ok(0) => return Err(invalid(path, invalid_offset, truncated_message)),
            Ok(count) => actual += count,
            Err(source) if source.kind() == std::io::ErrorKind::Interrupted => {}
            Err(source) => {
                return Err(Neo4jStoreError::Read {
                    path: path.to_path_buf(),
                    source,
                }
                .into());
            }
        }
    }
    Ok(bytes)
}

fn read_state(page: &[u8], page_id: u64) -> TreeState {
    let state = &page[..STATE_SIZE];
    let first = &state[..STATE_FIELDS_SIZE];
    let second = &state[STATE_FIELDS_SIZE..];
    let stable_generation = u32::from_be_bytes(first[0..4].try_into().expect("fixed field"));
    let unstable_generation = u32::from_be_bytes(first[4..8].try_into().expect("fixed field"));
    let empty = first[..56].iter().all(|byte| *byte == 0);
    TreeState {
        page_id,
        stable_generation,
        unstable_generation,
        root_id: u64::from_be_bytes(first[8..16].try_into().expect("fixed field")),
        root_generation: u64::from_be_bytes(first[16..24].try_into().expect("fixed field")),
        last_id: u64::from_be_bytes(first[24..32].try_into().expect("fixed field")),
        clean: first[56] == 1,
        valid: first == second && !empty,
    }
}

fn validate_range_metadata(path: &Path, bytes: &[u8]) -> Result<()> {
    if read_u32(path, bytes, 4)? as usize != PAGE_SIZE {
        return Err(invalid(
            path,
            4,
            "ID range tree does not use 8192-byte pages",
        ));
    }
    let identifier = bytes
        .get(8..16)
        .ok_or_else(|| invalid(path, 8, "truncated ID range layout identifier"))?;
    if identifier != ID_RANGE_LAYOUT_IDENTIFIER.to_be_bytes() {
        return Err(invalid(
            path,
            8,
            "unexpected ID range tree layout identifier",
        ));
    }
    Ok(())
}

fn validate_range_tree(
    file: &mut File,
    path: &Path,
    selected: TreeState,
    high_id: u64,
) -> Result<u64> {
    let mut visited = BTreeSet::new();
    let mut previous_leaf_key = None;
    validate_range_subtree(
        file,
        path,
        selected.root_id,
        selected.root_generation,
        selected,
        high_id,
        &mut visited,
        &mut previous_leaf_key,
    )
}

#[allow(clippy::too_many_arguments)]
fn validate_range_subtree(
    file: &mut File,
    path: &Path,
    page_id: u64,
    expected_generation: u64,
    selected: TreeState,
    high_id: u64,
    visited: &mut BTreeSet<u64>,
    previous_leaf_key: &mut Option<u64>,
) -> Result<u64> {
    if page_id < 3 || page_id > selected.last_id {
        return Err(invalid(
            path,
            0,
            format!("ID range child page ID {page_id} is outside the allocated tree pages"),
        ));
    }
    if !visited.insert(page_id) {
        return Err(invalid(
            path,
            0,
            format!("ID range tree contains a cycle or duplicate child page {page_id}"),
        ));
    }
    let page_offset = usize::try_from(page_id)
        .ok()
        .and_then(|id| id.checked_mul(PAGE_SIZE))
        .ok_or_else(|| invalid(path, 0, "ID range page offset overflow"))?;
    let page = read_region(
        file,
        path,
        page_offset as u64,
        PAGE_SIZE,
        page_offset,
        "ID range tree page is truncated",
    )?;
    if page[0] != TREE_NODE || (page[1] >> 4) & 0x0f != DATA_LAYER {
        return Err(invalid(
            path,
            page_offset,
            "ID range tree page is not a data-layer tree node",
        ));
    }
    if read_u32_from(&page, 2) as u64 != expected_generation {
        return Err(invalid(
            path,
            page_offset + 2,
            "ID range page generation does not match its parent pointer",
        ));
    }
    match page[1] & 0x0f {
        LEAF_NODE => validate_range_leaf(path, page_offset, &page, high_id, previous_leaf_key),
        INTERNAL_NODE => {
            let key_count = read_u32_from(&page, 6) as usize;
            if key_count > INTERNAL_MAX_KEY_COUNT {
                return Err(invalid(
                    path,
                    page_offset + 6,
                    "ID range internal key count exceeds capacity",
                ));
            }
            let mut previous_key = None;
            for position in 0..key_count {
                let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
                let key = read_u64_from(&page, key_offset);
                i64::try_from(key).map_err(|_| {
                    invalid(
                        path,
                        page_offset + key_offset,
                        "ID range internal contains a negative key",
                    )
                })?;
                if previous_key.is_some_and(|previous| previous >= key) {
                    return Err(invalid(
                        path,
                        page_offset + key_offset,
                        "ID range internal keys are not strictly increasing",
                    ));
                }
                previous_key = Some(key);
            }
            let mut unused_ids = 0_u64;
            for position in 0..=key_count {
                let pointer_offset = INTERNAL_CHILDREN_OFFSET + position * PAGE_REFERENCE_SIZE;
                let (child_id, child_generation) = read_generation_safe_pointer_pair(
                    path,
                    page_offset + pointer_offset,
                    &page[pointer_offset..pointer_offset + PAGE_REFERENCE_SIZE],
                    selected.stable_generation as u64,
                    selected.unstable_generation as u64,
                )?;
                unused_ids = unused_ids
                    .checked_add(validate_range_subtree(
                        file,
                        path,
                        child_id,
                        child_generation,
                        selected,
                        high_id,
                        visited,
                        previous_leaf_key,
                    )?)
                    .ok_or_else(|| invalid(path, page_offset, "unused ID count overflow"))?;
            }
            Ok(unused_ids)
        }
        _ => Err(invalid(
            path,
            page_offset + 1,
            "ID range page has an unsupported tree-node type",
        )),
    }
}

fn read_generation_safe_pointer_pair(
    path: &Path,
    offset: usize,
    bytes: &[u8],
    stable_generation: u64,
    unstable_generation: u64,
) -> Result<(u64, u64)> {
    let candidates = [0, GENERATION_SAFE_POINTER_SIZE].map(|slot| {
        let generation = read_u32_from(bytes, slot) as u64;
        let pointer = read_six_byte_u64(&bytes[slot + 4..slot + 10]);
        let stored_checksum = u16::from_be_bytes(bytes[slot + 10..slot + 12].try_into().unwrap());
        let checksum_valid = stored_checksum == generation_pointer_checksum(generation, pointer);
        let state = if generation == 0 && pointer == 0 {
            PointerState::Empty
        } else if !checksum_valid || generation == 0 {
            PointerState::Broken
        } else if generation <= stable_generation {
            PointerState::Stable
        } else if generation == unstable_generation {
            PointerState::Unstable
        } else {
            PointerState::Crash
        };
        (generation, pointer, state)
    });
    let [a, b] = candidates;
    let selected = match (a.2, b.2) {
        (PointerState::Unstable, PointerState::Stable | PointerState::Empty) => Some(a),
        (PointerState::Stable | PointerState::Empty, PointerState::Unstable) => Some(b),
        (PointerState::Stable, PointerState::Stable) if a.0 > b.0 => Some(a),
        (PointerState::Stable, PointerState::Stable) if b.0 > a.0 => Some(b),
        (PointerState::Stable, _) => Some(a),
        (_, PointerState::Stable) => Some(b),
        _ => None,
    };
    selected
        .map(|(generation, pointer, _)| (pointer, generation))
        .ok_or_else(|| {
            invalid(
                path,
                offset,
                "ID range child pointer pair has no readable slot",
            )
        })
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PointerState {
    Stable,
    Unstable,
    Crash,
    Broken,
    Empty,
}

fn read_six_byte_u64(bytes: &[u8]) -> u64 {
    let mut value = [0_u8; 8];
    value[2..].copy_from_slice(bytes);
    u64::from_be_bytes(value)
}

fn generation_pointer_checksum(generation: u64, pointer: u64) -> u16 {
    generation as u16
        ^ (generation >> 16) as u16
        ^ pointer as u16
        ^ (pointer >> 16) as u16
        ^ (pointer >> 32) as u16
}

fn validate_range_root(
    path: &Path,
    root_offset: usize,
    root: &[u8],
    root_generation: u64,
    high_id: u64,
) -> Result<u64> {
    if root[0] != TREE_NODE || root[1] & 0x0f != LEAF_NODE || (root[1] >> 4) & 0x0f != DATA_LAYER {
        return Err(invalid(
            path,
            root_offset,
            "ID range tree root is not a single data leaf",
        ));
    }
    if read_u32_from(root, 2) as u64 != root_generation {
        return Err(invalid(
            path,
            root_offset + 2,
            "ID range root generation does not match state",
        ));
    }
    let key_count = read_u32_from(root, 6) as usize;
    if key_count > MAX_KEY_COUNT {
        return Err(invalid(
            path,
            root_offset + 6,
            "ID range leaf key count exceeds capacity",
        ));
    }
    let mut previous_key = None;
    validate_range_leaf(path, root_offset, root, high_id, &mut previous_key)
}

fn validate_range_leaf(
    path: &Path,
    page_offset: usize,
    page: &[u8],
    high_id: u64,
    previous_key: &mut Option<u64>,
) -> Result<u64> {
    let key_count = read_u32_from(page, 6) as usize;
    if key_count > MAX_KEY_COUNT {
        return Err(invalid(
            path,
            page_offset + 6,
            "ID range leaf key count exceeds capacity",
        ));
    }
    let mut persisted_unused_ids = 0_u64;
    for position in 0..key_count {
        let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
        let key = read_u64_from(page, key_offset);
        i64::try_from(key).map_err(|_| {
            invalid(
                path,
                page_offset + key_offset,
                "ID range leaf contains a negative range key",
            )
        })?;
        if previous_key.is_some_and(|previous| previous >= key) {
            return Err(invalid(
                path,
                page_offset + key_offset,
                "ID range leaf keys are not strictly increasing globally",
            ));
        }
        let range_start = key
            .checked_mul(EXPECTED_IDS_PER_ENTRY as u64)
            .ok_or_else(|| {
                invalid(
                    path,
                    page_offset + key_offset,
                    "ID range leaf key overflows the ID space",
                )
            })?;
        if range_start >= high_id {
            return Err(invalid(
                path,
                page_offset + key_offset,
                "ID range leaf key starts at or above the high ID",
            ));
        }
        let valid_ids = (high_id - range_start).min(EXPECTED_IDS_PER_ENTRY as u64) as usize;
        let value_offset = VALUE_AREA_OFFSET + position * VALUE_SIZE;
        let value = &page[value_offset..value_offset + VALUE_SIZE];
        for long_index in 0..LONGS_PER_ENTRY {
            let valid_bits = valid_ids
                .saturating_sub(long_index * u64::BITS as usize)
                .min(u64::BITS as usize);
            let valid_mask = match valid_bits {
                0 => 0,
                bits if bits == u64::BITS as usize => u64::MAX,
                bits => (1_u64 << bits) - 1,
            };
            for bitset in 0..BITSET_COUNT {
                if read_bitset_long(value, bitset, long_index) & !valid_mask != 0 {
                    return Err(invalid(
                        path,
                        page_offset + value_offset + bitset_offset(bitset, long_index),
                        "ID range leaf contains bits at or above the high ID",
                    ));
                }
            }
            persisted_unused_ids +=
                read_bitset_long(value, BITSET_COMMIT, long_index).count_ones() as u64;
        }
        *previous_key = Some(key);
    }
    Ok(persisted_unused_ids)
}

fn validate_unused_id_count(
    path: &Path,
    state_page_id: u64,
    header: IdGeneratorHeader,
    persisted_unused_ids: u64,
) -> Result<()> {
    if header.num_unused_ids != persisted_unused_ids {
        return Err(invalid(
            path,
            state_header_offset(PAGE_SIZE, state_page_id) + 28,
            format!(
                "ID header unused count {} does not match {persisted_unused_ids} persisted COMMIT bits",
                header.num_unused_ids
            ),
        ));
    }
    Ok(())
}

fn bitset_offset(bitset: usize, long_index: usize) -> usize {
    8 + (bitset * LONGS_PER_ENTRY + long_index) * 8
}

fn read_bitset_long(value: &[u8], bitset: usize, long_index: usize) -> u64 {
    read_u64_from(value, bitset_offset(bitset, long_index))
}

fn write_bitset_long(value: &mut [u8], bitset: usize, long_index: usize, bits: u64) {
    let offset = bitset_offset(bitset, long_index);
    value[offset..offset + 8].copy_from_slice(&bits.to_be_bytes());
}

fn read_u32_from(bytes: &[u8], offset: usize) -> u32 {
    u32::from_be_bytes(
        bytes[offset..offset + 4]
            .try_into()
            .expect("validated field"),
    )
}

fn read_u64_from(bytes: &[u8], offset: usize) -> u64 {
    u64::from_be_bytes(
        bytes[offset..offset + 8]
            .try_into()
            .expect("validated field"),
    )
}

fn select_newest(path: &Path, a: TreeState, b: TreeState) -> Result<TreeState> {
    match (a.valid, b.valid) {
        (true, false) => Ok(a),
        (false, true) => Ok(b),
        (false, false) => Err(invalid(path, 0, "both GB+Tree state pages are invalid")),
        (true, true)
            if a.stable_generation == b.stable_generation
                && a.unstable_generation == b.unstable_generation
                && a.clean != b.clean =>
        {
            Ok(if a.clean { a } else { b })
        }
        (true, true)
            if a.stable_generation >= b.stable_generation
                && a.unstable_generation > b.unstable_generation =>
        {
            Ok(a)
        }
        (true, true)
            if b.stable_generation >= a.stable_generation
                && b.unstable_generation > a.unstable_generation =>
        {
            Ok(b)
        }
        _ => Err(invalid(
            path,
            0,
            "GB+Tree state generations do not identify a newest page",
        )),
    }
}

fn read_embedded_header(
    path: &Path,
    page: &[u8],
    page_offset: u64,
    file_len: u64,
) -> Result<IdGeneratorHeader> {
    let relative_offset = STATE_SIZE + HEADER_LENGTH_SIZE;
    let absolute_offset = page_offset
        .checked_add(relative_offset as u64)
        .ok_or_else(|| invalid(path, 0, "ID header offset overflow"))?;
    let header_len = read_u32(path, page, STATE_SIZE)? as usize;
    if header_len < ID_HEADER_SIZE {
        return Err(invalid(
            path,
            usize::try_from(page_offset + STATE_SIZE as u64).unwrap_or(usize::MAX),
            "truncated ID header",
        ));
    }
    let header_end = absolute_offset
        .checked_add(header_len as u64)
        .ok_or_else(|| invalid(path, 0, "ID header end offset overflow"))?;
    if header_end > file_len {
        return Err(invalid(
            path,
            usize::try_from(absolute_offset).unwrap_or(usize::MAX),
            "ID header extends beyond file",
        ));
    }
    let header = &page[relative_offset..relative_offset + ID_HEADER_SIZE];
    Ok(IdGeneratorHeader {
        high_id: u64::from_be_bytes(header[0..8].try_into().expect("fixed field")),
        highest_written_id: u64::from_be_bytes(header[8..16].try_into().expect("fixed field")),
        generation: u64::from_be_bytes(header[16..24].try_into().expect("fixed field")),
        ids_per_entry: u32::from_be_bytes(header[24..28].try_into().expect("fixed field")),
        num_unused_ids: u64::from_be_bytes(header[28..36].try_into().expect("fixed field")),
        state_header_offsets: [0; 2],
    })
}

fn state_header_offset(page_size: usize, page_id: u64) -> usize {
    page_size * page_id as usize + STATE_SIZE + HEADER_LENGTH_SIZE
}

fn read_u32(path: &Path, bytes: &[u8], offset: usize) -> Result<u32> {
    let value = bytes
        .get(offset..offset + 4)
        .ok_or_else(|| invalid(path, offset, "truncated 32-bit field"))?;
    Ok(u32::from_be_bytes(value.try_into().expect("fixed field")))
}

fn invalid(path: &Path, offset: usize, message: impl Into<String>) -> crate::Error {
    Neo4jStoreError::InvalidMetadata {
        path: path.to_path_buf(),
        offset: offset as u64,
        message: message.into(),
    }
    .into()
}

fn unsupported(path: &Path, message: impl Into<String>) -> crate::Error {
    Neo4jStoreError::UnsupportedWritableStore {
        path: PathBuf::from(path),
        message: message.into(),
    }
    .into()
}

#[cfg(test)]
mod tests {
    use std::fs;

    use super::*;

    fn write_state(bytes: &mut [u8], page_size: usize, page_id: usize, unstable: u32) {
        let offset = page_size * page_id;
        let mut fields = [0_u8; STATE_FIELDS_SIZE];
        fields[0..4].copy_from_slice(&4_u32.to_be_bytes());
        fields[4..8].copy_from_slice(&unstable.to_be_bytes());
        fields[8..16].copy_from_slice(&3_u64.to_be_bytes());
        fields[24..32].copy_from_slice(&3_u64.to_be_bytes());
        fields[56] = 1;
        bytes[offset..offset + STATE_FIELDS_SIZE].copy_from_slice(&fields);
        bytes[offset + STATE_FIELDS_SIZE..offset + STATE_SIZE].copy_from_slice(&fields);
        let header_offset = offset + STATE_SIZE;
        bytes[header_offset..header_offset + 4].copy_from_slice(&36_u32.to_be_bytes());
        let header = &mut bytes[header_offset + 4..header_offset + 40];
        header[0..8].copy_from_slice(&133_u64.to_be_bytes());
        header[8..16].copy_from_slice(&132_u64.to_be_bytes());
        header[16..24].copy_from_slice(&2_u64.to_be_bytes());
        header[24..28].copy_from_slice(&128_u32.to_be_bytes());
        header[28..36].copy_from_slice(&0_u64.to_be_bytes());
    }

    fn id_range_file(entries: &[(u64, [u8; VALUE_SIZE])]) -> Vec<u8> {
        let mut bytes = vec![0_u8; PAGE_SIZE * 4];
        bytes[4..8].copy_from_slice(&(PAGE_SIZE as u32).to_be_bytes());
        bytes[8..16].copy_from_slice(&ID_RANGE_LAYOUT_IDENTIFIER.to_be_bytes());
        write_state(&mut bytes, PAGE_SIZE, 1, 5);
        write_state(&mut bytes, PAGE_SIZE, 2, 4);
        for page_id in [1, 2] {
            let offset = PAGE_SIZE * page_id;
            for copy in [0, STATE_FIELDS_SIZE] {
                bytes[offset + copy + 16..offset + copy + 24].copy_from_slice(&3_u64.to_be_bytes());
            }
        }
        let root = &mut bytes[3 * PAGE_SIZE..4 * PAGE_SIZE];
        root[0] = TREE_NODE;
        root[1] = LEAF_NODE;
        root[2..6].copy_from_slice(&3_u32.to_be_bytes());
        root[6..10].copy_from_slice(&(entries.len() as u32).to_be_bytes());
        for (position, (key, value)) in entries.iter().enumerate() {
            let key_offset = BASE_HEADER_SIZE + position * KEY_SIZE;
            root[key_offset..key_offset + KEY_SIZE].copy_from_slice(&key.to_be_bytes());
            let value_offset = VALUE_AREA_OFFSET + position * VALUE_SIZE;
            root[value_offset..value_offset + VALUE_SIZE].copy_from_slice(value);
        }
        bytes
    }

    fn write_generation_safe_pointer(bytes: &mut [u8], generation: u32, pointer: u64) {
        bytes[..4].copy_from_slice(&generation.to_be_bytes());
        bytes[4..10].copy_from_slice(&pointer.to_be_bytes()[2..]);
        bytes[10..12].copy_from_slice(
            &generation_pointer_checksum(generation as u64, pointer).to_be_bytes(),
        );
    }

    fn two_leaf_id_range_file() -> Vec<u8> {
        let mut bytes = id_range_file(&[]);
        bytes.resize(PAGE_SIZE * 6, 0);
        for page_id in [1, 2] {
            let state_offset = PAGE_SIZE * page_id;
            for copy in [0, STATE_FIELDS_SIZE] {
                bytes[state_offset + copy + 8..state_offset + copy + 16]
                    .copy_from_slice(&5_u64.to_be_bytes());
                bytes[state_offset + copy + 16..state_offset + copy + 24]
                    .copy_from_slice(&3_u64.to_be_bytes());
                bytes[state_offset + copy + 24..state_offset + copy + 32]
                    .copy_from_slice(&5_u64.to_be_bytes());
            }
        }

        for (page_id, key) in [(3, 0_u64), (4, 1_u64)] {
            let leaf = &mut bytes[page_id * PAGE_SIZE..(page_id + 1) * PAGE_SIZE];
            leaf[0] = TREE_NODE;
            leaf[1] = LEAF_NODE;
            leaf[2..6].copy_from_slice(&3_u32.to_be_bytes());
            leaf[6..10].copy_from_slice(&1_u32.to_be_bytes());
            leaf[BASE_HEADER_SIZE..BASE_HEADER_SIZE + KEY_SIZE].copy_from_slice(&key.to_be_bytes());
            leaf[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + 8].copy_from_slice(&2_u64.to_be_bytes());
        }

        let root = &mut bytes[5 * PAGE_SIZE..6 * PAGE_SIZE];
        root[0] = TREE_NODE;
        root[1] = INTERNAL_NODE;
        root[2..6].copy_from_slice(&3_u32.to_be_bytes());
        root[6..10].copy_from_slice(&1_u32.to_be_bytes());
        root[BASE_HEADER_SIZE..BASE_HEADER_SIZE + KEY_SIZE].copy_from_slice(&1_u64.to_be_bytes());
        for (position, child_id) in [3_u64, 4].into_iter().enumerate() {
            let pointer_offset = INTERNAL_CHILDREN_OFFSET + position * PAGE_REFERENCE_SIZE;
            write_generation_safe_pointer(
                &mut root[pointer_offset..pointer_offset + GENERATION_SAFE_POINTER_SIZE],
                3,
                child_id,
            );
        }
        bytes
    }

    fn set_num_unused_ids(bytes: &mut [u8], count: u64) {
        for page_id in [1, 2] {
            let offset = state_header_offset(PAGE_SIZE, page_id) + 28;
            bytes[offset..offset + 8].copy_from_slice(&count.to_be_bytes());
        }
    }

    #[test]
    fn reads_the_newest_valid_id_generator_header() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let page_size = 512;
        let mut bytes = vec![0_u8; page_size * 3];
        bytes[4..8].copy_from_slice(&(page_size as u32).to_be_bytes());
        write_state(&mut bytes, page_size, 1, 5);
        write_state(&mut bytes, page_size, 2, 4);
        fs::write(&path, bytes).unwrap();

        let header = read(&path).unwrap();
        assert_eq!(header.high_id, 133);
        assert_eq!(header.highest_written_id, 132);
        assert_eq!(header.generation, 2);
        assert_eq!(header.ids_per_entry, 128);
        assert_eq!(header.state_header_offsets, [630, 1142]);
    }

    #[test]
    fn reads_headers_and_range_pages_without_loading_a_sparse_file() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        fs::write(&path, id_range_file(&[])).unwrap();
        let file = fs::OpenOptions::new().write(true).open(&path).unwrap();
        file.set_len(1_u64 << 40).unwrap();

        let header = read(&path).unwrap();
        assert_eq!(header.high_id, 133);
        let update = prepare_mark_deleted_and_free(&path, &[4]).unwrap();
        assert_eq!(update.root_page.len(), PAGE_SIZE);
    }

    #[test]
    fn writable_validation_rejects_invalid_range_metadata_and_root_keys() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut bytes = id_range_file(&[]);
        bytes[8] ^= 1;
        fs::write(&path, bytes).unwrap();

        let error = validate_writable(directory.path(), "records.db", 133).unwrap_err();
        assert!(error.to_string().contains("layout identifier"));

        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&2_u64.to_be_bytes());
        fs::write(&path, id_range_file(&[(0, value), (0, value)])).unwrap();

        let error = validate_writable(directory.path(), "records.db", 133).unwrap_err();
        assert!(
            error.to_string().contains("not strictly increasing"),
            "{error}"
        );
    }

    #[test]
    fn validates_a_two_leaf_range_tree_but_keeps_mutation_leaf_only() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let bytes = two_leaf_id_range_file();
        fs::write(&path, &bytes).unwrap();

        let header = validate_writable(directory.path(), "records.db", 133).unwrap();
        assert_eq!(header.high_id, 133);

        let error = prepare_take_free_ids(&path, 1).unwrap_err();
        assert!(error
            .to_string()
            .contains("ID range tree root is not a single data leaf"));

        let mut duplicate_child = bytes.clone();
        let second_pointer = 5 * PAGE_SIZE + INTERNAL_CHILDREN_OFFSET + PAGE_REFERENCE_SIZE;
        write_generation_safe_pointer(
            &mut duplicate_child[second_pointer..second_pointer + GENERATION_SAFE_POINTER_SIZE],
            3,
            3,
        );
        fs::write(&path, duplicate_child).unwrap();
        let error = validate_writable(directory.path(), "records.db", 133).unwrap_err();
        assert!(error.to_string().contains("cycle or duplicate"), "{error}");

        let mut unordered = bytes.clone();
        let second_leaf_key = 4 * PAGE_SIZE + BASE_HEADER_SIZE;
        unordered[second_leaf_key..second_leaf_key + KEY_SIZE]
            .copy_from_slice(&0_u64.to_be_bytes());
        fs::write(&path, unordered).unwrap();
        let error = validate_writable(directory.path(), "records.db", 133).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("not strictly increasing globally"),
            "{error}"
        );

        let mut corrupt = bytes;
        corrupt[5 * PAGE_SIZE + INTERNAL_CHILDREN_OFFSET + 10] ^= 1;
        fs::write(&path, corrupt).unwrap();
        let error = validate_writable(directory.path(), "records.db", 133).unwrap_err();
        assert!(error.to_string().contains("no readable slot"), "{error}");
    }

    #[test]
    fn rejects_a_root_beyond_the_last_allocated_range_page() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut bytes = id_range_file(&[]);
        let selected_state_offset = PAGE_SIZE;
        for copy in [0, STATE_FIELDS_SIZE] {
            bytes[selected_state_offset + copy + 8..selected_state_offset + copy + 16]
                .copy_from_slice(&4_u64.to_be_bytes());
        }
        fs::write(&path, bytes).unwrap();

        let error = validate_writable(directory.path(), "records.db", 133).unwrap_err();
        assert!(error.to_string().contains("last allocated page"));
    }

    #[test]
    fn rejects_unused_count_that_disagrees_with_commit_bits() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&2_u64.to_be_bytes());
        write_bitset_long(&mut value, BITSET_COMMIT, 0, 1 << 5);

        let mut bytes = id_range_file(&[(0, value)]);
        fs::write(&path, &bytes).unwrap();
        let error = validate_writable(directory.path(), "records.db", 133).unwrap_err();
        assert!(error
            .to_string()
            .contains("unused count 0 does not match 1 persisted COMMIT bits"));

        set_num_unused_ids(&mut bytes, 2);
        fs::write(&path, bytes).unwrap();
        let error = prepare_take_free_ids(&path, 1).unwrap_err();
        assert!(error
            .to_string()
            .contains("unused count 2 does not match 1 persisted COMMIT bits"));
    }

    #[test]
    fn unused_count_includes_current_generation_ids_that_are_not_reusable() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&2_u64.to_be_bytes());
        write_bitset_long(&mut value, BITSET_COMMIT, 0, 1 << 5);
        write_bitset_long(&mut value, BITSET_RESERVED, 0, 1 << 5);
        let mut bytes = id_range_file(&[(0, value)]);
        set_num_unused_ids(&mut bytes, 1);
        fs::write(&path, bytes).unwrap();

        assert!(prepare_take_free_ids(&path, 1).unwrap().is_none());
    }

    #[test]
    fn rejects_invalid_state_copies() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let page_size = 512;
        let mut bytes = vec![0_u8; page_size * 3];
        bytes[4..8].copy_from_slice(&(page_size as u32).to_be_bytes());
        write_state(&mut bytes, page_size, 1, 5);
        write_state(&mut bytes, page_size, 2, 4);
        bytes[page_size + STATE_FIELDS_SIZE] ^= 1;
        bytes[page_size * 2 + STATE_FIELDS_SIZE] ^= 1;
        fs::write(&path, bytes).unwrap();

        assert!(read(&path)
            .unwrap_err()
            .to_string()
            .contains("both GB+Tree state pages are invalid"));
    }

    #[test]
    fn encodes_append_only_high_id_updates_for_both_state_pages() {
        let header = IdGeneratorHeader {
            high_id: 133,
            highest_written_id: 132,
            generation: 2,
            ids_per_entry: 128,
            num_unused_ids: 0,
            state_header_offsets: [8310, 16502],
        };

        let allocation = allocate_high_ids(header, 2).unwrap();
        let patches = allocation.header_patches;
        assert_eq!(allocation.first_id, 133);
        assert_eq!(patches[0].0, 8310);
        assert_eq!(patches[1].0, 16502);
        assert_eq!(
            u64::from_be_bytes(patches[0].1[0..8].try_into().unwrap()),
            135
        );
        assert_eq!(
            u64::from_be_bytes(patches[0].1[8..16].try_into().unwrap()),
            134
        );
        assert_eq!(patches[0].1, patches[1].1);
    }

    #[test]
    fn inserts_deleted_and_free_bits_in_the_correct_range_slot() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        fs::write(&path, id_range_file(&[])).unwrap();

        let update = prepare_mark_deleted_and_free(&path, &[70]).unwrap();
        assert_eq!(update.root_page_offset, (3 * PAGE_SIZE) as u64);
        assert_eq!(read_u32_from(&update.root_page, 6), 1);
        assert_eq!(read_u64_from(&update.root_page, BASE_HEADER_SIZE), 0);
        let value = &update.root_page[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + VALUE_SIZE];
        assert_eq!(read_u64_from(value, 0), 2);
        assert_eq!(read_bitset_long(value, BITSET_COMMIT, 0), 0);
        assert_eq!(read_bitset_long(value, BITSET_COMMIT, 1), 1 << 6);
        assert_eq!(read_bitset_long(value, BITSET_REUSE, 1), 1 << 6);
        assert_eq!(read_bitset_long(value, BITSET_RESERVED, 1), 0);
        assert_eq!(update.header_patches[0].1, 1_u64.to_be_bytes());
        assert_eq!(update.header_patches[1].1, 1_u64.to_be_bytes());
    }

    #[test]
    fn normalizes_an_old_range_and_rejects_freeing_an_unused_id() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&1_u64.to_be_bytes());
        write_bitset_long(&mut value, BITSET_COMMIT, 0, 1 << 5);
        write_bitset_long(&mut value, BITSET_RESERVED, 0, 1 << 5);
        let mut bytes = id_range_file(&[(0, value)]);
        set_num_unused_ids(&mut bytes, 1);
        fs::write(&path, bytes).unwrap();

        let update = prepare_mark_deleted_and_free(&path, &[6]).unwrap();
        let value = &update.root_page[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + VALUE_SIZE];
        assert_eq!(read_u64_from(value, 0), 2);
        assert_eq!(
            read_bitset_long(value, BITSET_COMMIT, 0),
            (1 << 5) | (1 << 6)
        );
        assert_eq!(
            read_bitset_long(value, BITSET_REUSE, 0),
            (1 << 5) | (1 << 6)
        );
        assert_eq!(read_bitset_long(value, BITSET_RESERVED, 0), 0);

        let mut bytes = id_range_file(&[(0, value.try_into().unwrap())]);
        set_num_unused_ids(&mut bytes, 2);
        fs::write(&path, bytes).unwrap();
        assert!(prepare_mark_deleted_and_free(&path, &[6])
            .unwrap_err()
            .to_string()
            .contains("already unused"));
    }

    #[test]
    fn rejects_duplicate_and_out_of_range_ids() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        fs::write(&path, id_range_file(&[])).unwrap();

        assert!(prepare_mark_deleted_and_free(&path, &[4, 4])
            .unwrap_err()
            .to_string()
            .contains("duplicate"));
        assert!(prepare_mark_deleted_and_free(&path, &[133])
            .unwrap_err()
            .to_string()
            .contains("high ID"));
    }

    #[test]
    fn rejects_duplicate_descending_and_negative_range_keys() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&2_u64.to_be_bytes());

        for entries in [
            vec![(0, value), (0, value)],
            vec![(1, value), (0, value)],
            vec![(u64::MAX, value)],
        ] {
            fs::write(&path, id_range_file(&entries)).unwrap();

            let error = prepare_mark_deleted_and_free(&path, &[4]).unwrap_err();
            assert!(error.to_string().contains("ID range leaf"));
        }
    }

    #[test]
    fn rejects_range_keys_and_bits_at_or_above_the_high_id() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&2_u64.to_be_bytes());

        fs::write(&path, id_range_file(&[(2, value)])).unwrap();
        let error = prepare_mark_deleted_and_free(&path, &[4]).unwrap_err();
        assert!(error.to_string().contains("at or above the high ID"));

        write_bitset_long(&mut value, BITSET_COMMIT, 0, 1 << 5);
        fs::write(&path, id_range_file(&[(1, value)])).unwrap();
        let error = prepare_mark_deleted_and_free(&path, &[4]).unwrap_err();
        assert!(error.to_string().contains("bits at or above the high ID"));
    }

    #[test]
    fn takes_reusable_ids_and_clears_their_range_bits() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&2_u64.to_be_bytes());
        write_bitset_long(&mut value, BITSET_COMMIT, 0, (1 << 5) | (1 << 6));
        write_bitset_long(&mut value, BITSET_REUSE, 0, (1 << 5) | (1 << 6));
        write_bitset_long(&mut value, BITSET_RESERVED, 0, 1 << 6);
        write_bitset_long(&mut value, BITSET_COMMIT, 1, 1 << 6);
        write_bitset_long(&mut value, BITSET_REUSE, 1, 1 << 6);
        let mut bytes = id_range_file(&[(0, value)]);
        set_num_unused_ids(&mut bytes, 3);
        fs::write(&path, bytes).unwrap();

        let update = prepare_take_free_ids(&path, 2).unwrap().unwrap();
        assert_eq!(update.ids, [5, 70]);
        let value = &update.root_page[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + VALUE_SIZE];
        assert_eq!(read_bitset_long(value, BITSET_COMMIT, 0), 1 << 6);
        assert_eq!(read_bitset_long(value, BITSET_REUSE, 0), 1 << 6);
        assert_eq!(read_bitset_long(value, BITSET_RESERVED, 0), 1 << 6);
        assert_eq!(read_bitset_long(value, BITSET_COMMIT, 1), 0);
        assert_eq!(read_bitset_long(value, BITSET_REUSE, 1), 0);
        assert_eq!(update.header_patches[0].1, 1_u64.to_be_bytes());
        assert_eq!(update.header_patches[1].1, 1_u64.to_be_bytes());
    }

    #[test]
    fn combines_reuse_free_and_new_high_id_free_in_one_update() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&2_u64.to_be_bytes());
        write_bitset_long(&mut value, BITSET_COMMIT, 0, (1 << 5) | (1 << 6));
        write_bitset_long(&mut value, BITSET_REUSE, 0, (1 << 5) | (1 << 6));
        let mut bytes = id_range_file(&[(0, value)]);
        set_num_unused_ids(&mut bytes, 2);
        fs::write(&path, bytes).unwrap();

        let update = prepare_reuse_and_free_ids(&path, 1, &[5, 70, 133], 134).unwrap();

        assert_eq!(update.reused_ids, [5]);
        assert_eq!(update.header_patches[0].1, 4_u64.to_be_bytes());
        let first = &update.root_page[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + VALUE_SIZE];
        assert_eq!(
            read_bitset_long(first, BITSET_COMMIT, 0),
            (1 << 5) | (1 << 6)
        );
        assert_eq!(read_bitset_long(first, BITSET_COMMIT, 1), 1 << 6);
        let second =
            &update.root_page[VALUE_AREA_OFFSET + VALUE_SIZE..VALUE_AREA_OFFSET + 2 * VALUE_SIZE];
        assert_eq!(read_u64_from(second, 0), 2);
        assert_eq!(read_bitset_long(second, BITSET_COMMIT, 0), 1 << 5);
    }

    #[test]
    fn normalizes_an_old_range_before_reusing_an_id() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("records.db.id");
        let mut value = [0_u8; VALUE_SIZE];
        value[..8].copy_from_slice(&1_u64.to_be_bytes());
        write_bitset_long(&mut value, BITSET_COMMIT, 0, 1 << 9);
        write_bitset_long(&mut value, BITSET_RESERVED, 0, 1 << 9);
        let mut bytes = id_range_file(&[(0, value)]);
        set_num_unused_ids(&mut bytes, 1);
        fs::write(&path, bytes).unwrap();

        let update = prepare_take_free_ids(&path, 1).unwrap().unwrap();
        assert_eq!(update.ids, [9]);
        let value = &update.root_page[VALUE_AREA_OFFSET..VALUE_AREA_OFFSET + VALUE_SIZE];
        assert_eq!(read_u64_from(value, 0), 2);
        assert_eq!(read_bitset_long(value, BITSET_COMMIT, 0), 0);
        assert_eq!(read_bitset_long(value, BITSET_REUSE, 0), 0);
        assert_eq!(read_bitset_long(value, BITSET_RESERVED, 0), 0);
    }

    #[test]
    #[ignore = "requires NEO4J_REAL_FIXTURE generated by tools/neo4j-fixture-generator"]
    fn reads_a_real_property_id_generator() {
        let database = std::env::var("NEO4J_REAL_FIXTURE").unwrap();
        let path = Path::new(&database).join("neostore.propertystore.db.id");
        let header = read(&path).unwrap();
        assert_eq!(header.ids_per_entry, 128);
        assert_eq!(header.highest_written_id + 1, header.high_id);
        assert_eq!(header.high_id, 133);

        let string_path = Path::new(&database).join("neostore.propertystore.db.strings.id");
        let string_header = read(&string_path).unwrap();
        assert_eq!(string_header.ids_per_entry, 128);
        assert!(string_header.high_id > 0);
        assert!(string_header.highest_written_id < string_header.high_id);

        let array_path = Path::new(&database).join("neostore.propertystore.db.arrays.id");
        let array_header = read(&array_path).unwrap();
        assert_eq!(array_header.ids_per_entry, 128);
        assert!(array_header.high_id > 0);
        assert!(array_header.highest_written_id < array_header.high_id);

        for store in [
            "neostore.propertystore.db.index",
            "neostore.propertystore.db.index.keys",
        ] {
            let token_path = Path::new(&database).join(format!("{store}.id"));
            let token_header = read(&token_path).unwrap();
            assert_eq!(token_header.ids_per_entry, 128);
            assert!(token_header.high_id > 0);
            assert!(token_header.highest_written_id < token_header.high_id);
        }
    }
}