icydb-core 0.213.36

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
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
//! Module: db::schema::source_binding
//! Responsibility: durable source-key to accepted-identity bindings.
//! Does not own: accepted structural semantics, accepted-ID allocation, proposal lowering, or publication.
//! Boundary: immutable proposal source keys <-> existing store-local accepted identities.

use std::collections::{BTreeMap, BTreeSet};

use icydb_schema::{
    ConstraintSourceKey, EntitySourceKey, FieldSourceKey, IndexSourceKey, RelationSourceKey,
    TypeSourceKey,
};

use crate::{
    db::schema::{
        AcceptedCompositeCatalog, AcceptedConstraintKind, AcceptedEnumCatalog,
        ConstraintActivationKind, ConstraintId, FieldId, PersistedSchemaSnapshot, RelationId,
        SchemaIndexId,
        composite_catalog::{AcceptedCompositeShape, CompositeFieldId, CompositeTypeId},
        wire::{SchemaWireReader, SchemaWireWriter},
    },
    error::InternalError,
    types::EntityTag,
    value::{EnumTypeId, EnumVariantId},
};

#[cfg(feature = "sql")]
use crate::db::schema::{
    AcceptedSchemaRevision, ConstraintOrigin, PersistedFieldOrigin, PersistedIndexOrigin,
    PersistedIndexSnapshot,
};

const ACCEPTED_SOURCE_BINDING_MAGIC: &[u8; 8] = b"ICYDBASB";
const ACCEPTED_SOURCE_BINDING_CODEC_VERSION: u16 = 1;
const ACCEPTED_SOURCE_BINDING_HEADER_BYTES: usize = 42;
const MAX_ACCEPTED_SOURCE_BINDING_BYTES: usize = 2 * 1024 * 1024;
const TYPE_ENUM: u8 = 0;
const TYPE_COMPOSITE: u8 = 1;
type BindingWriter = SchemaWireWriter<MAX_ACCEPTED_SOURCE_BINDING_BYTES>;
type BindingReader<'a> = SchemaWireReader<'a>;

///
/// AcceptedNamedTypeIdentity
///
/// Store-local accepted identity selected by one immutable named-type source
/// key. The variant preserves the structural catalog that owns the definition.
///

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub(in crate::db) enum AcceptedNamedTypeIdentity {
    /// Definition owned by the accepted enum catalog.
    Enum(EnumTypeId),
    /// Definition owned by the accepted composite catalog.
    Composite(CompositeTypeId),
}

///
/// AcceptedSourceBindingCatalog
///
/// Canonical store-local bindings from immutable authorship keys to accepted
/// structural identities. The referenced snapshots and value catalogs remain
/// the sole semantic owners; this catalog carries identity only.
///
/// During the 0.213 hard cut, a structural owner becomes source-addressable
/// only when its admission path supplies an immutable source key. The catalog
/// may therefore be empty, but every binding it does contain must close
/// exactly over the same accepted revision bundle.
///

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(in crate::db) struct AcceptedSourceBindingCatalog {
    entities: BTreeMap<EntitySourceKey, EntityTag>,
    types: BTreeMap<TypeSourceKey, AcceptedNamedTypeIdentity>,
    enum_variants: BTreeMap<(EnumTypeId, TypeSourceKey), EnumVariantId>,
    composite_fields: BTreeMap<(CompositeTypeId, FieldSourceKey), CompositeFieldId>,
    fields: BTreeMap<(EntityTag, FieldSourceKey), FieldId>,
    constraints: BTreeMap<(EntityTag, ConstraintSourceKey), ConstraintId>,
    indexes: BTreeMap<(EntityTag, IndexSourceKey), SchemaIndexId>,
    relations: BTreeMap<(EntityTag, RelationSourceKey), RelationId>,
}

///
/// AcceptedTypedAdapterNames
///
/// Read-only accepted display-name projection for one opaque generated adapter
/// binding. Source identities remain the keys; catalogs remain semantic owners.
///

pub(in crate::db) struct AcceptedTypedAdapterNames {
    pub(in crate::db) composite_fields: Vec<(String, String, String)>,
    pub(in crate::db) enum_variants: Vec<(String, String, String)>,
    pub(in crate::db) named_types: Vec<(String, String)>,
}

impl AcceptedSourceBindingCatalog {
    /// Construct the exact source-addressable identity closure for one initial
    /// catalog-native proposal.
    pub(in crate::db::schema) const fn initial(
        entities: BTreeMap<EntitySourceKey, EntityTag>,
        fields: BTreeMap<(EntityTag, FieldSourceKey), FieldId>,
        constraints: BTreeMap<(EntityTag, ConstraintSourceKey), ConstraintId>,
        indexes: BTreeMap<(EntityTag, IndexSourceKey), SchemaIndexId>,
        relations: BTreeMap<(EntityTag, RelationSourceKey), RelationId>,
    ) -> Self {
        Self {
            entities,
            types: BTreeMap::new(),
            enum_variants: BTreeMap::new(),
            composite_fields: BTreeMap::new(),
            fields,
            constraints,
            indexes,
            relations,
        }
    }

    /// Attach the exact named-type and enum-variant identities allocated for
    /// the same initial accepted candidate.
    #[must_use]
    pub(in crate::db::schema) fn with_initial_named_types(
        mut self,
        types: BTreeMap<TypeSourceKey, AcceptedNamedTypeIdentity>,
        enum_variants: BTreeMap<(EnumTypeId, TypeSourceKey), EnumVariantId>,
        composite_fields: BTreeMap<(CompositeTypeId, FieldSourceKey), CompositeFieldId>,
    ) -> Self {
        self.types = types;
        self.enum_variants = enum_variants;
        self.composite_fields = composite_fields;
        self
    }

    /// Copy the named-type identity closure from the same staged initial
    /// candidate while adding entity-scoped structural bindings.
    #[must_use]
    pub(in crate::db::schema) fn with_initial_named_types_from(
        mut self,
        named_types: &Self,
    ) -> Self {
        self.types.clone_from(&named_types.types);
        self.enum_variants.clone_from(&named_types.enum_variants);
        self.composite_fields
            .clone_from(&named_types.composite_fields);
        self
    }

    /// Resolve one immutable entity source identity.
    #[must_use]
    pub(in crate::db) fn entity(&self, source: &EntitySourceKey) -> Option<EntityTag> {
        self.entities.get(source).copied()
    }

    /// Remove one exact entity binding and every entity-scoped child binding.
    pub(in crate::db::schema) fn remove_entity(
        &mut self,
        source: &EntitySourceKey,
        expected: EntityTag,
    ) -> Result<(), InternalError> {
        match self.entities.get(source) {
            Some(actual) if *actual == expected => {}
            Some(_) | None => return Err(InternalError::store_invariant()),
        }
        let _ = self.entities.remove(source);
        self.fields.retain(|(entity, _), _| *entity != expected);
        self.constraints
            .retain(|(entity, _), _| *entity != expected);
        self.indexes.retain(|(entity, _), _| *entity != expected);
        self.relations.retain(|(entity, _), _| *entity != expected);
        Ok(())
    }

    /// Resolve one immutable field source identity inside an accepted entity.
    #[must_use]
    pub(in crate::db) fn field(
        &self,
        entity: EntityTag,
        source: &FieldSourceKey,
    ) -> Option<FieldId> {
        self.fields.get(&(entity, source.clone())).copied()
    }

    /// Derive the accepted editable names needed by an opaque typed adapter.
    pub(in crate::db) fn typed_adapter_names(
        &self,
        enum_catalog: &AcceptedEnumCatalog,
        composite_catalog: &AcceptedCompositeCatalog,
    ) -> Result<AcceptedTypedAdapterNames, InternalError> {
        let mut named_types = Vec::with_capacity(self.types.len());
        let mut enum_variants = Vec::with_capacity(self.enum_variants.len());
        let mut composite_fields = Vec::with_capacity(self.composite_fields.len());

        for (source, identity) in &self.types {
            match identity {
                AcceptedNamedTypeIdentity::Enum(type_id) => {
                    let definition = enum_catalog
                        .enum_type(*type_id)
                        .ok_or_else(InternalError::store_invariant)?;
                    named_types.push((source.as_str().to_string(), definition.path().to_string()));
                    for ((bound_type, variant_source), variant_id) in &self.enum_variants {
                        if bound_type != type_id {
                            continue;
                        }
                        let variant = definition
                            .variant(*variant_id)
                            .ok_or_else(InternalError::store_invariant)?;
                        enum_variants.push((
                            source.as_str().to_string(),
                            variant_source.as_str().to_string(),
                            variant.name().to_string(),
                        ));
                    }
                }
                AcceptedNamedTypeIdentity::Composite(type_id) => {
                    let definition = composite_catalog
                        .composite_type(*type_id)
                        .ok_or_else(InternalError::store_invariant)?;
                    named_types.push((source.as_str().to_string(), definition.path().to_string()));
                    let AcceptedCompositeShape::Record(fields) = definition.shape() else {
                        continue;
                    };
                    for ((bound_type, field_source), field_id) in &self.composite_fields {
                        if bound_type != type_id {
                            continue;
                        }
                        let field = fields
                            .iter()
                            .find(|field| field.id() == *field_id)
                            .ok_or_else(InternalError::store_invariant)?;
                        composite_fields.push((
                            source.as_str().to_string(),
                            field_source.as_str().to_string(),
                            field.name().to_string(),
                        ));
                    }
                }
            }
        }

        Ok(AcceptedTypedAdapterNames {
            composite_fields,
            enum_variants,
            named_types,
        })
    }

    /// Remove one exact field binding and move every retained binding through
    /// the dense accepted field-ID reassignment owned by the schema candidate.
    pub(in crate::db::schema) fn remove_field_and_remap(
        &mut self,
        entity: EntityTag,
        source: &FieldSourceKey,
        expected: FieldId,
        mut retained_field_id: impl FnMut(FieldId) -> Option<FieldId>,
    ) -> Result<(), InternalError> {
        match self.fields.remove(&(entity, source.clone())) {
            Some(actual) if actual == expected => {}
            Some(_) | None => return Err(InternalError::store_invariant()),
        }
        for ((bound_entity, _), field_id) in &mut self.fields {
            if *bound_entity != entity {
                continue;
            }
            *field_id = retained_field_id(*field_id).ok_or_else(InternalError::store_invariant)?;
        }
        Ok(())
    }

    /// Resolve one immutable named-type source identity.
    #[must_use]
    pub(in crate::db) fn named_type(
        &self,
        source: &TypeSourceKey,
    ) -> Option<AcceptedNamedTypeIdentity> {
        self.types.get(source).copied()
    }

    /// Remove one exact named-type binding and its child identity closure.
    pub(in crate::db::schema) fn remove_named_type(
        &mut self,
        source: &TypeSourceKey,
        expected: AcceptedNamedTypeIdentity,
    ) -> Result<(), InternalError> {
        match self.types.remove(source) {
            Some(actual) if actual == expected => {}
            Some(_) | None => return Err(InternalError::store_invariant()),
        }
        match expected {
            AcceptedNamedTypeIdentity::Enum(type_id) => {
                self.enum_variants
                    .retain(|(bound_type, _), _| *bound_type != type_id);
            }
            AcceptedNamedTypeIdentity::Composite(type_id) => {
                self.composite_fields
                    .retain(|(bound_type, _), _| *bound_type != type_id);
            }
        }
        Ok(())
    }

    /// Resolve one immutable unit-variant identity inside an accepted enum.
    #[must_use]
    pub(in crate::db::schema) fn enum_variant(
        &self,
        enum_type: EnumTypeId,
        source: &TypeSourceKey,
    ) -> Option<EnumVariantId> {
        self.enum_variants
            .get(&(enum_type, source.clone()))
            .copied()
    }

    /// Resolve one immutable member source identity inside an accepted record.
    #[must_use]
    pub(in crate::db::schema) fn composite_field(
        &self,
        composite_type: CompositeTypeId,
        source: &FieldSourceKey,
    ) -> Option<CompositeFieldId> {
        self.composite_fields
            .get(&(composite_type, source.clone()))
            .copied()
    }

    /// Resolve one immutable accepted-check source identity.
    #[must_use]
    pub(in crate::db::schema) fn constraint(
        &self,
        entity: EntityTag,
        source: &ConstraintSourceKey,
    ) -> Option<ConstraintId> {
        self.constraints.get(&(entity, source.clone())).copied()
    }

    /// Add one exact source binding for a newly reserved accepted check.
    pub(in crate::db::schema) fn insert_constraint(
        &mut self,
        entity: EntityTag,
        source: ConstraintSourceKey,
        constraint: ConstraintId,
    ) -> Result<(), InternalError> {
        let key = (entity, source);
        if self
            .constraints
            .iter()
            .any(|((bound_entity, _), accepted)| *bound_entity == entity && *accepted == constraint)
            || self.constraints.contains_key(&key)
        {
            return Err(InternalError::store_invariant());
        }
        self.constraints.insert(key, constraint);
        Ok(())
    }

    /// Remove one exact accepted-check source binding with its structural owner.
    pub(in crate::db::schema) fn remove_constraint(
        &mut self,
        entity: EntityTag,
        source: &ConstraintSourceKey,
        expected: ConstraintId,
    ) -> Result<(), InternalError> {
        match self.constraints.remove(&(entity, source.clone())) {
            Some(actual) if actual == expected => Ok(()),
            Some(_) | None => Err(InternalError::store_invariant()),
        }
    }

    /// Remove the unique generated-check binding selected by accepted identity.
    ///
    /// Abort owns only the durable application record's accepted identity, so
    /// it resolves the source key from this catalog rather than consulting the
    /// authored proposal again.
    pub(in crate::db::schema) fn remove_constraint_identity(
        &mut self,
        entity: EntityTag,
        expected: ConstraintId,
    ) -> Result<(), InternalError> {
        let mut matching = self
            .constraints
            .iter()
            .filter(|((bound_entity, _), constraint)| {
                *bound_entity == entity && **constraint == expected
            })
            .map(|((_, source), _)| source.clone());
        let source = matching.next().ok_or_else(InternalError::store_invariant)?;
        if matching.next().is_some() {
            return Err(InternalError::store_invariant());
        }
        self.remove_constraint(entity, &source, expected)
    }

    /// Resolve one immutable secondary-index source identity.
    #[must_use]
    pub(in crate::db::schema) fn index(
        &self,
        entity: EntityTag,
        source: &IndexSourceKey,
    ) -> Option<SchemaIndexId> {
        self.indexes.get(&(entity, source.clone())).copied()
    }

    /// Remove one exact accepted secondary-index source binding.
    pub(in crate::db::schema) fn remove_index(
        &mut self,
        entity: EntityTag,
        source: &IndexSourceKey,
        expected: SchemaIndexId,
    ) -> Result<(), InternalError> {
        match self.indexes.remove(&(entity, source.clone())) {
            Some(actual) if actual == expected => Ok(()),
            Some(_) | None => Err(InternalError::store_invariant()),
        }
    }

    /// Resolve one immutable relation source identity.
    #[must_use]
    pub(in crate::db::schema) fn relation(
        &self,
        entity: EntityTag,
        source: &RelationSourceKey,
    ) -> Option<RelationId> {
        self.relations.get(&(entity, source.clone())).copied()
    }

    /// Remove one exact accepted relation source binding.
    pub(in crate::db::schema) fn remove_relation(
        &mut self,
        entity: EntityTag,
        source: &RelationSourceKey,
        expected: RelationId,
    ) -> Result<(), InternalError> {
        match self.relations.remove(&(entity, source.clone())) {
            Some(actual) if actual == expected => Ok(()),
            Some(_) | None => Err(InternalError::store_invariant()),
        }
    }

    /// Apply one catalog-native SQL-DDL entity transition.
    ///
    /// Existing immutable keys follow their structural owner through rename
    /// and dense field-ID reassignment. A newly admitted SQL-owned object gets
    /// one key derived from the monotonic accepted publication revision;
    /// removed owners lose their binding. The method deliberately does not
    /// backfill unbound owners from earlier development formats.
    #[cfg(feature = "sql")]
    pub(in crate::db::schema) fn with_sql_ddl_entity_transition(
        mut self,
        entity: EntityTag,
        before: &PersistedSchemaSnapshot,
        after: &PersistedSchemaSnapshot,
        publication_revision: AcceptedSchemaRevision,
    ) -> Result<Self, InternalError> {
        if before.entity_path() != after.entity_path() {
            return Err(InternalError::store_invariant());
        }

        self.apply_sql_ddl_field_transition(entity, before, after, publication_revision)?;
        self.apply_sql_ddl_index_transition(entity, before, after, publication_revision)?;
        self.apply_sql_ddl_check_transition(entity, before, after, publication_revision)?;

        Ok(self)
    }

    #[cfg(feature = "sql")]
    fn apply_sql_ddl_field_transition(
        &mut self,
        entity: EntityTag,
        before: &PersistedSchemaSnapshot,
        after: &PersistedSchemaSnapshot,
        publication_revision: AcceptedSchemaRevision,
    ) -> Result<(), InternalError> {
        let field_lineage = field_lineage(before, after)?;
        let mut removed_keys = Vec::new();
        for ((bound_entity, source_key), field_id) in &mut self.fields {
            if *bound_entity != entity {
                continue;
            }
            if !before.fields().iter().any(|field| field.id() == *field_id) {
                return Err(InternalError::store_invariant());
            }
            match field_lineage.get(field_id) {
                Some(after_id) => *field_id = *after_id,
                None => removed_keys.push((*bound_entity, source_key.clone())),
            }
        }
        for key in removed_keys {
            self.fields.remove(&key);
        }

        let added = after
            .fields()
            .iter()
            .filter(|field| {
                field.origin() == PersistedFieldOrigin::SqlDdl
                    && !field_lineage
                        .values()
                        .any(|after_id| *after_id == field.id())
            })
            .collect::<Vec<_>>();
        if added.len() > 1 {
            return Err(InternalError::store_invariant());
        }
        if let Some(field) = added.first() {
            let source =
                FieldSourceKey::try_new(sql_ddl_source_key("field", entity, publication_revision))
                    .map_err(|_| InternalError::store_invariant())?;
            if self.fields.insert((entity, source), field.id()).is_some() {
                return Err(InternalError::store_invariant());
            }
        }
        Ok(())
    }

    #[cfg(feature = "sql")]
    fn apply_sql_ddl_index_transition(
        &mut self,
        entity: EntityTag,
        before: &PersistedSchemaSnapshot,
        after: &PersistedSchemaSnapshot,
        publication_revision: AcceptedSchemaRevision,
    ) -> Result<(), InternalError> {
        let before_ids = before
            .indexes()
            .iter()
            .chain(before.candidate_indexes())
            .map(PersistedIndexSnapshot::schema_id)
            .collect::<BTreeSet<_>>();
        let after_ids = after
            .indexes()
            .iter()
            .chain(after.candidate_indexes())
            .map(PersistedIndexSnapshot::schema_id)
            .collect::<BTreeSet<_>>();
        self.indexes.retain(|(bound_entity, _), index_id| {
            *bound_entity != entity || after_ids.contains(index_id)
        });

        let added = after
            .indexes()
            .iter()
            .chain(after.candidate_indexes())
            .filter(|index| {
                index.origin() == PersistedIndexOrigin::SqlDdl
                    && !before_ids.contains(&index.schema_id())
            })
            .collect::<Vec<_>>();
        if added.len() > 1 {
            return Err(InternalError::store_invariant());
        }
        if let Some(index) = added.first() {
            let source =
                IndexSourceKey::try_new(sql_ddl_source_key("index", entity, publication_revision))
                    .map_err(|_| InternalError::store_invariant())?;
            if self
                .indexes
                .insert((entity, source), index.schema_id())
                .is_some()
            {
                return Err(InternalError::store_invariant());
            }
        }
        Ok(())
    }

    #[cfg(feature = "sql")]
    fn apply_sql_ddl_check_transition(
        &mut self,
        entity: EntityTag,
        before: &PersistedSchemaSnapshot,
        after: &PersistedSchemaSnapshot,
        publication_revision: AcceptedSchemaRevision,
    ) -> Result<(), InternalError> {
        let before_ids = check_ids(before);
        let after_ids = check_ids(after);
        self.constraints
            .retain(|(bound_entity, _), id| *bound_entity != entity || after_ids.contains(id));

        let added = sql_ddl_check_ids(after)
            .difference(&before_ids)
            .copied()
            .collect::<Vec<_>>();
        if added.len() > 1 {
            return Err(InternalError::store_invariant());
        }
        if let Some(id) = added.first() {
            let source = ConstraintSourceKey::try_new(sql_ddl_source_key(
                "constraint",
                entity,
                publication_revision,
            ))
            .map_err(|_| InternalError::store_invariant())?;
            if self.constraints.insert((entity, source), *id).is_some() {
                return Err(InternalError::store_invariant());
            }
        }
        Ok(())
    }

    #[cfg(test)]
    pub(in crate::db) fn field_binding_count_for_tests(&self, entity: EntityTag) -> usize {
        self.fields
            .keys()
            .filter(|(bound_entity, _)| *bound_entity == entity)
            .count()
    }

    #[cfg(test)]
    pub(in crate::db) fn named_type_binding_count_for_tests(&self) -> usize {
        self.types.len()
    }

    #[cfg(test)]
    pub(in crate::db) fn enum_variant_binding_count_for_tests(&self) -> usize {
        self.enum_variants.len()
    }

    #[cfg(test)]
    pub(in crate::db) fn composite_field_binding_count_for_tests(
        &self,
        composite_type: CompositeTypeId,
    ) -> usize {
        self.composite_fields
            .keys()
            .filter(|(bound_type, _)| *bound_type == composite_type)
            .count()
    }

    #[cfg(test)]
    pub(in crate::db) fn constraint_binding_count_for_tests(&self, entity: EntityTag) -> usize {
        self.constraints
            .keys()
            .filter(|(bound_entity, _)| *bound_entity == entity)
            .count()
    }

    #[cfg(test)]
    pub(in crate::db) fn index_binding_count_for_tests(&self, entity: EntityTag) -> usize {
        self.indexes
            .keys()
            .filter(|(bound_entity, _)| *bound_entity == entity)
            .count()
    }

    fn validate(
        &self,
        enum_catalog: &AcceptedEnumCatalog,
        composite_catalog: &AcceptedCompositeCatalog,
        entities: &BTreeMap<EntityTag, PersistedSchemaSnapshot>,
    ) -> bool {
        self.identities_are_one_to_one()
            && self.type_bindings_close(enum_catalog, composite_catalog)
            && self.entity_bindings_close(entities)
    }

    fn identities_are_one_to_one(&self) -> bool {
        unique_values(self.entities.values().copied())
            && unique_values(self.types.values().copied())
            && unique_values(
                self.enum_variants
                    .iter()
                    .map(|((enum_type, _), variant)| (*enum_type, *variant)),
            )
            && unique_values(
                self.composite_fields
                    .iter()
                    .map(|((composite_type, _), field)| (*composite_type, *field)),
            )
            && unique_values(
                self.fields
                    .iter()
                    .map(|((entity, _), field)| (*entity, *field)),
            )
            && unique_values(
                self.constraints
                    .iter()
                    .map(|((entity, _), constraint)| (*entity, *constraint)),
            )
            && unique_values(
                self.indexes
                    .iter()
                    .map(|((entity, _), index)| (*entity, *index)),
            )
            && unique_values(
                self.relations
                    .iter()
                    .map(|((entity, _), relation)| (*entity, *relation)),
            )
    }

    fn type_bindings_close(
        &self,
        enum_catalog: &AcceptedEnumCatalog,
        composite_catalog: &AcceptedCompositeCatalog,
    ) -> bool {
        self.types.values().all(|identity| match identity {
            AcceptedNamedTypeIdentity::Enum(id) => enum_catalog.enum_type(*id).is_some(),
            AcceptedNamedTypeIdentity::Composite(id) => {
                composite_catalog.composite_type(*id).is_some()
            }
        }) && self.enum_variant_bindings_close(enum_catalog)
            && self.composite_field_bindings_close(composite_catalog)
    }

    fn enum_variant_bindings_close(&self, enum_catalog: &AcceptedEnumCatalog) -> bool {
        self.enum_variants.iter().all(|((enum_type, _), variant)| {
            self.types
                .values()
                .any(|identity| *identity == AcceptedNamedTypeIdentity::Enum(*enum_type))
                && enum_catalog
                    .enum_type(*enum_type)
                    .is_some_and(|definition| definition.variant(*variant).is_some())
        }) && self.types.values().all(|identity| match identity {
            AcceptedNamedTypeIdentity::Enum(enum_type) => enum_catalog
                .enum_type(*enum_type)
                .is_some_and(|definition| {
                    self.enum_variants
                        .keys()
                        .filter(|(bound_type, _)| bound_type == enum_type)
                        .count()
                        == definition.variant_count()
                }),
            AcceptedNamedTypeIdentity::Composite(_) => true,
        })
    }

    fn composite_field_bindings_close(&self, composite_catalog: &AcceptedCompositeCatalog) -> bool {
        self.composite_fields
            .iter()
            .all(|((composite_type, _), field_id)| {
                self.types.values().any(|identity| {
                    *identity == AcceptedNamedTypeIdentity::Composite(*composite_type)
                }) && composite_catalog
                    .composite_type(*composite_type)
                    .is_some_and(|definition| {
                        matches!(
                            definition.shape(),
                            AcceptedCompositeShape::Record(fields)
                                if fields.iter().any(|field| field.id() == *field_id)
                        )
                    })
            })
            && self.types.values().all(|identity| match identity {
                AcceptedNamedTypeIdentity::Composite(composite_type) => composite_catalog
                    .composite_type(*composite_type)
                    .is_some_and(|definition| {
                        let binding_count = self
                            .composite_fields
                            .keys()
                            .filter(|(bound_type, _)| bound_type == composite_type)
                            .count();
                        match definition.shape() {
                            AcceptedCompositeShape::Record(fields) => binding_count == fields.len(),
                            AcceptedCompositeShape::Tuple(_)
                            | AcceptedCompositeShape::Newtype(_) => binding_count == 0,
                        }
                    }),
                AcceptedNamedTypeIdentity::Enum(_) => true,
            })
    }

    fn entity_bindings_close(
        &self,
        entities: &BTreeMap<EntityTag, PersistedSchemaSnapshot>,
    ) -> bool {
        self.entities
            .values()
            .all(|entity| entities.contains_key(entity))
            && self.fields.iter().all(|((entity, _), field_id)| {
                entities.get(entity).is_some_and(|snapshot| {
                    snapshot
                        .fields()
                        .iter()
                        .any(|field| field.id() == *field_id)
                })
            })
            && self.constraints.iter().all(|((entity, _), constraint_id)| {
                entities.get(entity).is_some_and(|snapshot| {
                    snapshot
                        .constraint_catalog()
                        .constraints()
                        .iter()
                        .any(|constraint| {
                            constraint.id() == *constraint_id
                                && matches!(constraint.kind(), AcceptedConstraintKind::Check { .. })
                        })
                        || snapshot
                            .constraint_catalog()
                            .activations()
                            .iter()
                            .any(|activation| {
                                activation.id() == *constraint_id
                                    && matches!(
                                        activation.kind(),
                                        ConstraintActivationKind::Check { .. }
                                    )
                            })
                })
            })
            && self.indexes.iter().all(|((entity, _), index_id)| {
                entities.get(entity).is_some_and(|snapshot| {
                    snapshot
                        .indexes()
                        .iter()
                        .chain(snapshot.candidate_indexes())
                        .any(|index| index.schema_id() == *index_id)
                })
            })
            && self.relations.iter().all(|((entity, _), relation_id)| {
                entities.get(entity).is_some_and(|snapshot| {
                    snapshot
                        .relations()
                        .iter()
                        .chain(snapshot.candidate_relations())
                        .any(|relation| relation.id() == *relation_id)
                })
            })
    }
}

#[cfg(feature = "sql")]
fn sql_ddl_source_key(
    object_kind: &str,
    entity: EntityTag,
    publication_revision: AcceptedSchemaRevision,
) -> String {
    format!(
        "icydb:sql-ddl:{object_kind}:{}:{}",
        entity.value(),
        publication_revision.get(),
    )
}

#[cfg(feature = "sql")]
fn field_lineage(
    before: &PersistedSchemaSnapshot,
    after: &PersistedSchemaSnapshot,
) -> Result<BTreeMap<FieldId, FieldId>, InternalError> {
    let mut lineage = BTreeMap::new();
    let mut used_after = BTreeSet::new();

    for before_field in before.fields() {
        if let Some(after_field) = after
            .fields()
            .iter()
            .find(|after_field| after_field.name() == before_field.name())
        {
            lineage.insert(before_field.id(), after_field.id());
            if !used_after.insert(after_field.id()) {
                return Err(InternalError::store_invariant());
            }
        }
    }
    for before_field in before.fields() {
        if lineage.contains_key(&before_field.id()) {
            continue;
        }
        if let Some(after_field) = after.fields().iter().find(|after_field| {
            after_field.id() == before_field.id() && !used_after.contains(&after_field.id())
        }) {
            lineage.insert(before_field.id(), after_field.id());
            used_after.insert(after_field.id());
        }
    }

    Ok(lineage)
}

#[cfg(feature = "sql")]
fn check_ids(snapshot: &PersistedSchemaSnapshot) -> BTreeSet<ConstraintId> {
    snapshot
        .constraint_catalog()
        .constraints()
        .iter()
        .filter_map(|constraint| {
            matches!(constraint.kind(), AcceptedConstraintKind::Check { .. })
                .then_some(constraint.id())
        })
        .chain(
            snapshot
                .constraint_catalog()
                .activations()
                .iter()
                .filter_map(|activation| {
                    matches!(activation.kind(), ConstraintActivationKind::Check { .. })
                        .then_some(activation.id())
                }),
        )
        .collect()
}

#[cfg(feature = "sql")]
fn sql_ddl_check_ids(snapshot: &PersistedSchemaSnapshot) -> BTreeSet<ConstraintId> {
    snapshot
        .constraint_catalog()
        .constraints()
        .iter()
        .filter_map(|constraint| {
            (constraint.origin() == ConstraintOrigin::SqlDdl
                && matches!(constraint.kind(), AcceptedConstraintKind::Check { .. }))
            .then_some(constraint.id())
        })
        .chain(
            snapshot
                .constraint_catalog()
                .activations()
                .iter()
                .filter_map(|activation| {
                    (activation.origin() == ConstraintOrigin::SqlDdl
                        && matches!(activation.kind(), ConstraintActivationKind::Check { .. }))
                    .then_some(activation.id())
                }),
        )
        .collect()
}

fn unique_values<T: Ord>(values: impl Iterator<Item = T>) -> bool {
    let mut seen = BTreeSet::new();
    values.into_iter().all(|value| seen.insert(value))
}

/// Encode one source-binding catalog after proving exact structural closure.
///
/// # Errors
///
/// Returns a typed internal error when a binding is not one-to-one, references
/// an absent accepted owner, or exceeds the persisted byte bound.
pub(in crate::db::schema) fn encode_accepted_source_bindings(
    catalog: &AcceptedSourceBindingCatalog,
    enum_catalog: &AcceptedEnumCatalog,
    composite_catalog: &AcceptedCompositeCatalog,
    entities: &BTreeMap<EntityTag, PersistedSchemaSnapshot>,
) -> Result<Vec<u8>, InternalError> {
    if !catalog.validate(enum_catalog, composite_catalog, entities) {
        return Err(InternalError::store_invariant());
    }

    let mut writer = BindingWriter::new();
    writer.push_bytes(ACCEPTED_SOURCE_BINDING_MAGIC);
    writer.push_u16(ACCEPTED_SOURCE_BINDING_CODEC_VERSION);
    writer.push_len(catalog.entities.len())?;
    for (source_key, entity) in &catalog.entities {
        writer.push_string(source_key.as_str())?;
        writer.push_u64(entity.value());
    }
    writer.push_len(catalog.types.len())?;
    for (source_key, identity) in &catalog.types {
        writer.push_string(source_key.as_str())?;
        match identity {
            AcceptedNamedTypeIdentity::Enum(id) => {
                writer.push_u8(TYPE_ENUM);
                writer.push_u32(id.get());
            }
            AcceptedNamedTypeIdentity::Composite(id) => {
                writer.push_u8(TYPE_COMPOSITE);
                writer.push_u32(id.get());
            }
        }
    }
    writer.push_len(catalog.enum_variants.len())?;
    for ((enum_type, source_key), variant) in &catalog.enum_variants {
        writer.push_u32(enum_type.get());
        writer.push_string(source_key.as_str())?;
        writer.push_u32(variant.get());
    }
    writer.push_len(catalog.composite_fields.len())?;
    for ((composite_type, source_key), field) in &catalog.composite_fields {
        writer.push_u32(composite_type.get());
        writer.push_string(source_key.as_str())?;
        writer.push_u32(field.get());
    }
    writer.push_len(catalog.fields.len())?;
    for ((entity, source_key), field) in &catalog.fields {
        writer.push_u64(entity.value());
        writer.push_string(source_key.as_str())?;
        writer.push_u32(field.get());
    }
    writer.push_len(catalog.constraints.len())?;
    for ((entity, source_key), constraint) in &catalog.constraints {
        writer.push_u64(entity.value());
        writer.push_string(source_key.as_str())?;
        writer.push_u32(constraint.get());
    }
    writer.push_len(catalog.indexes.len())?;
    for ((entity, source_key), index) in &catalog.indexes {
        writer.push_u64(entity.value());
        writer.push_string(source_key.as_str())?;
        writer.push_u32(index.get());
    }
    writer.push_len(catalog.relations.len())?;
    for ((entity, source_key), relation) in &catalog.relations {
        writer.push_u64(entity.value());
        writer.push_string(source_key.as_str())?;
        writer.push_u32(relation.get());
    }
    writer.finish()
}

/// Decode one bounded current-form source-binding catalog and prove closure.
///
/// # Errors
///
/// Returns a typed internal error for malformed, obsolete, oversized,
/// non-canonical, or structurally unclosed bytes.
pub(in crate::db::schema) fn decode_accepted_source_bindings(
    bytes: &[u8],
    enum_catalog: &AcceptedEnumCatalog,
    composite_catalog: &AcceptedCompositeCatalog,
    entities: &BTreeMap<EntityTag, PersistedSchemaSnapshot>,
) -> Result<AcceptedSourceBindingCatalog, InternalError> {
    if bytes.len() < ACCEPTED_SOURCE_BINDING_HEADER_BYTES
        || bytes.len() > MAX_ACCEPTED_SOURCE_BINDING_BYTES
    {
        return Err(InternalError::store_corruption());
    }

    let mut reader = BindingReader::new(bytes);
    if reader.read_array::<8>()? != *ACCEPTED_SOURCE_BINDING_MAGIC {
        return Err(InternalError::store_corruption());
    }
    if reader.read_u16()? != ACCEPTED_SOURCE_BINDING_CODEC_VERSION {
        return Err(InternalError::serialize_incompatible_persisted_format());
    }

    let mut entity_bindings = BTreeMap::new();
    for _ in 0..reader.read_count()? {
        let source = EntitySourceKey::try_new(reader.read_string()?)
            .map_err(|_| InternalError::store_corruption())?;
        if entity_bindings
            .insert(source, EntityTag::new(reader.read_u64()?))
            .is_some()
        {
            return Err(InternalError::store_corruption());
        }
    }

    let type_bindings = read_named_type_bindings(&mut reader)?;
    let enum_variant_bindings = read_enum_variant_bindings(&mut reader)?;
    let composite_field_bindings = read_composite_field_bindings(&mut reader)?;

    let field_bindings =
        read_entity_local_bindings(&mut reader, FieldSourceKey::try_new, FieldId::new)?;
    let constraint_bindings = read_nonzero_entity_local_bindings(
        &mut reader,
        ConstraintSourceKey::try_new,
        ConstraintId::new,
    )?;
    let index_bindings = read_nonzero_entity_local_bindings(
        &mut reader,
        IndexSourceKey::try_new,
        SchemaIndexId::new,
    )?;
    let relation_bindings = read_nonzero_entity_local_bindings(
        &mut reader,
        RelationSourceKey::try_new,
        RelationId::new,
    )?;
    reader.finish()?;

    let catalog = AcceptedSourceBindingCatalog {
        entities: entity_bindings,
        types: type_bindings,
        enum_variants: enum_variant_bindings,
        composite_fields: composite_field_bindings,
        fields: field_bindings,
        constraints: constraint_bindings,
        indexes: index_bindings,
        relations: relation_bindings,
    };
    if !catalog.validate(enum_catalog, composite_catalog, entities) {
        return Err(InternalError::store_corruption());
    }
    let canonical =
        encode_accepted_source_bindings(&catalog, enum_catalog, composite_catalog, entities)
            .map_err(|_| InternalError::store_corruption())?;
    if canonical != bytes {
        return Err(InternalError::store_corruption());
    }
    Ok(catalog)
}

fn read_named_type_bindings(
    reader: &mut BindingReader<'_>,
) -> Result<BTreeMap<TypeSourceKey, AcceptedNamedTypeIdentity>, InternalError> {
    let mut bindings = BTreeMap::new();
    for _ in 0..reader.read_count()? {
        let source = TypeSourceKey::try_new(reader.read_string()?)
            .map_err(|_| InternalError::store_corruption())?;
        let tag = reader.read_u8()?;
        let raw = reader.read_u32()?;
        let identity = match tag {
            TYPE_ENUM => AcceptedNamedTypeIdentity::Enum(
                EnumTypeId::new(raw).ok_or_else(InternalError::store_corruption)?,
            ),
            TYPE_COMPOSITE => AcceptedNamedTypeIdentity::Composite(
                CompositeTypeId::new(raw).ok_or_else(InternalError::store_corruption)?,
            ),
            _ => return Err(InternalError::store_corruption()),
        };
        if bindings.insert(source, identity).is_some() {
            return Err(InternalError::store_corruption());
        }
    }
    Ok(bindings)
}

fn read_enum_variant_bindings(
    reader: &mut BindingReader<'_>,
) -> Result<BTreeMap<(EnumTypeId, TypeSourceKey), EnumVariantId>, InternalError> {
    let mut bindings = BTreeMap::new();
    for _ in 0..reader.read_count()? {
        let enum_type =
            EnumTypeId::new(reader.read_u32()?).ok_or_else(InternalError::store_corruption)?;
        let source = TypeSourceKey::try_new(reader.read_string()?)
            .map_err(|_| InternalError::store_corruption())?;
        let variant =
            EnumVariantId::new(reader.read_u32()?).ok_or_else(InternalError::store_corruption)?;
        if bindings.insert((enum_type, source), variant).is_some() {
            return Err(InternalError::store_corruption());
        }
    }
    Ok(bindings)
}

fn read_composite_field_bindings(
    reader: &mut BindingReader<'_>,
) -> Result<BTreeMap<(CompositeTypeId, FieldSourceKey), CompositeFieldId>, InternalError> {
    let mut bindings = BTreeMap::new();
    for _ in 0..reader.read_count()? {
        let composite_type =
            CompositeTypeId::new(reader.read_u32()?).ok_or_else(InternalError::store_corruption)?;
        let source = FieldSourceKey::try_new(reader.read_string()?)
            .map_err(|_| InternalError::store_corruption())?;
        let field = CompositeFieldId::new(reader.read_u32()?)
            .ok_or_else(InternalError::store_corruption)?;
        if bindings.insert((composite_type, source), field).is_some() {
            return Err(InternalError::store_corruption());
        }
    }
    Ok(bindings)
}

fn read_entity_local_bindings<K: Ord>(
    reader: &mut BindingReader<'_>,
    source_key: impl Fn(String) -> Result<K, icydb_schema::SchemaContractError>,
    identity: impl Fn(u32) -> FieldId,
) -> Result<BTreeMap<(EntityTag, K), FieldId>, InternalError> {
    let mut bindings = BTreeMap::new();
    for _ in 0..reader.read_count()? {
        let entity = EntityTag::new(reader.read_u64()?);
        let source =
            source_key(reader.read_string()?).map_err(|_| InternalError::store_corruption())?;
        let accepted = identity(reader.read_u32()?);
        if bindings.insert((entity, source), accepted).is_some() {
            return Err(InternalError::store_corruption());
        }
    }
    Ok(bindings)
}

fn read_nonzero_entity_local_bindings<K: Ord, V>(
    reader: &mut BindingReader<'_>,
    source_key: impl Fn(String) -> Result<K, icydb_schema::SchemaContractError>,
    identity: impl Fn(u32) -> Option<V>,
) -> Result<BTreeMap<(EntityTag, K), V>, InternalError> {
    let mut bindings = BTreeMap::new();
    for _ in 0..reader.read_count()? {
        let entity = EntityTag::new(reader.read_u64()?);
        let source =
            source_key(reader.read_string()?).map_err(|_| InternalError::store_corruption())?;
        let accepted = identity(reader.read_u32()?).ok_or_else(InternalError::store_corruption)?;
        if bindings.insert((entity, source), accepted).is_some() {
            return Err(InternalError::store_corruption());
        }
    }
    Ok(bindings)
}

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

    use super::{
        AcceptedNamedTypeIdentity, AcceptedSourceBindingCatalog, decode_accepted_source_bindings,
        encode_accepted_source_bindings,
    };
    use crate::{
        db::schema::{
            AcceptedCompositeCatalog, AcceptedFieldKind, AcceptedSchemaRevision,
            AcceptedSchemaRevisionBundle, CandidateSchemaRevision, ConstraintId, FieldId,
            PersistedFieldSnapshot, PersistedSchemaSnapshot, SchemaFieldSlot, SchemaInsertDefault,
            SchemaRowLayout, SchemaVersion,
            build_initial_accepted_enum_catalog_from_kinds_for_tests,
            composite_catalog::{
                AcceptedCompositeElement, AcceptedCompositeField, AcceptedCompositeShape,
                CompositeFieldId, CompositeTypeId,
            },
        },
        model::field::{EnumVariantModel, FieldKind, FieldStorageDecode, LeafCodec, ScalarCodec},
        types::EntityTag,
    };
    use icydb_schema::{ConstraintSourceKey, FieldSourceKey};

    static STATUS_VARIANTS: [EnumVariantModel; 2] = [
        EnumVariantModel::new("Active", None, FieldStorageDecode::ByKind),
        EnumVariantModel::new("Disabled", None, FieldStorageDecode::ByKind),
    ];
    const STATUS_KIND: FieldKind = FieldKind::Enum {
        path: "test::Status",
        variants: &STATUS_VARIANTS,
    };

    #[test]
    fn empty_source_binding_catalog_has_one_canonical_current_form() {
        let enums = build_initial_accepted_enum_catalog_from_kinds_for_tests(&[])
            .expect("empty enum catalog should build");
        let composites = AcceptedCompositeCatalog::empty();
        let entities = BTreeMap::new();
        let catalog = AcceptedSourceBindingCatalog::default();

        let encoded = encode_accepted_source_bindings(&catalog, &enums, &composites, &entities)
            .expect("empty source bindings should encode");
        let decoded = decode_accepted_source_bindings(&encoded, &enums, &composites, &entities)
            .expect("empty source bindings should decode");

        assert_eq!(decoded, catalog);
    }

    #[test]
    fn constraint_binding_insertion_enforces_entity_local_identity() {
        let mut catalog = AcceptedSourceBindingCatalog::default();
        let first_entity = EntityTag::new(7);
        let second_entity = EntityTag::new(8);
        let first_source = ConstraintSourceKey::try_new("test:first")
            .expect("first constraint source should build");
        let second_source = ConstraintSourceKey::try_new("test:second")
            .expect("second constraint source should build");
        let third_source = ConstraintSourceKey::try_new("test:third")
            .expect("third constraint source should build");
        let local_id = ConstraintId::new(1).expect("local constraint ID should build");

        catalog
            .insert_constraint(first_entity, first_source.clone(), local_id)
            .expect("first entity should accept its local ID");
        catalog
            .insert_constraint(second_entity, second_source, local_id)
            .expect("another entity may reuse the same local ID");

        assert!(
            catalog
                .insert_constraint(first_entity, third_source, local_id)
                .is_err(),
        );
        assert!(
            catalog
                .insert_constraint(
                    first_entity,
                    first_source,
                    ConstraintId::new(2).expect("second local constraint ID should build"),
                )
                .is_err(),
        );
    }

    #[test]
    fn field_removal_carries_retained_source_identity_through_dense_ids() {
        let entity = EntityTag::new(7);
        let first = FieldSourceKey::try_new("test:first").expect("first field source should build");
        let removed =
            FieldSourceKey::try_new("test:removed").expect("removed field source should build");
        let retained =
            FieldSourceKey::try_new("test:retained").expect("retained field source should build");
        let mut catalog = AcceptedSourceBindingCatalog::default();
        catalog
            .fields
            .insert((entity, first.clone()), FieldId::new(1));
        catalog
            .fields
            .insert((entity, removed.clone()), FieldId::new(2));
        catalog
            .fields
            .insert((entity, retained.clone()), FieldId::new(3));

        catalog
            .remove_field_and_remap(entity, &removed, FieldId::new(2), |field_id| match field_id
                .get()
            {
                1 => Some(FieldId::new(1)),
                3 => Some(FieldId::new(2)),
                _ => None,
            })
            .expect("exact removal lineage should apply");

        assert_eq!(catalog.field(entity, &first), Some(FieldId::new(1)));
        assert_eq!(catalog.field(entity, &retained), Some(FieldId::new(2)));
        assert_eq!(catalog.field(entity, &removed), None);
    }

    #[test]
    fn source_binding_catalog_rejects_missing_structural_owner() {
        let enums = build_initial_accepted_enum_catalog_from_kinds_for_tests(&[])
            .expect("empty enum catalog should build");
        let composites = AcceptedCompositeCatalog::empty();
        let entities = BTreeMap::new();
        let mut catalog = AcceptedSourceBindingCatalog::default();
        catalog.entities.insert(
            icydb_schema::EntitySourceKey::try_new("test:missing")
                .expect("source key should build"),
            EntityTag::new(7),
        );

        assert!(encode_accepted_source_bindings(&catalog, &enums, &composites, &entities).is_err());
    }

    #[test]
    fn source_binding_catalog_round_trips_existing_accepted_identities() {
        let enums = build_initial_accepted_enum_catalog_from_kinds_for_tests(&[])
            .expect("empty enum catalog should build");
        let composites = AcceptedCompositeCatalog::empty();
        let entity = EntityTag::new(7);
        let field = FieldId::new(1);
        let snapshot = PersistedSchemaSnapshot::new(
            SchemaVersion::initial(),
            "test::Bound".to_string(),
            "Bound".to_string(),
            field,
            SchemaRowLayout::initial(vec![(field, SchemaFieldSlot::new(0))]),
            vec![PersistedFieldSnapshot::new_initial(
                field,
                "id".to_string(),
                SchemaFieldSlot::new(0),
                AcceptedFieldKind::Nat64,
                Vec::new(),
                false,
                SchemaInsertDefault::None,
                FieldStorageDecode::ByKind,
                LeafCodec::Scalar(ScalarCodec::Nat64),
            )],
        );
        let entities = BTreeMap::from([(entity, snapshot)]);
        let mut catalog = AcceptedSourceBindingCatalog::default();
        catalog.entities.insert(
            icydb_schema::EntitySourceKey::try_new("test:bound")
                .expect("entity source key should build"),
            entity,
        );
        catalog.fields.insert(
            (
                entity,
                icydb_schema::FieldSourceKey::try_new("test:bound:id")
                    .expect("field source key should build"),
            ),
            field,
        );

        let encoded = encode_accepted_source_bindings(&catalog, &enums, &composites, &entities)
            .expect("closed source bindings should encode");
        let decoded = decode_accepted_source_bindings(&encoded, &enums, &composites, &entities)
            .expect("closed source bindings should decode");

        assert_eq!(decoded, catalog);
    }

    #[test]
    fn source_binding_catalog_closes_enum_variant_source_identities() {
        let enums = build_initial_accepted_enum_catalog_from_kinds_for_tests(&[STATUS_KIND])
            .expect("enum catalog should build");
        let composites = AcceptedCompositeCatalog::empty();
        let entities = BTreeMap::new();
        let enum_type = enums
            .type_id("test::Status")
            .expect("accepted enum type should exist");
        let definition = enums
            .enum_type(enum_type)
            .expect("accepted enum definition should exist");
        let active = definition
            .variant_id("Active")
            .expect("accepted active variant should exist");
        let disabled = definition
            .variant_id("Disabled")
            .expect("accepted disabled variant should exist");
        let mut catalog = AcceptedSourceBindingCatalog::default();
        catalog.types.insert(
            icydb_schema::TypeSourceKey::try_new("test:status")
                .expect("type source key should build"),
            AcceptedNamedTypeIdentity::Enum(enum_type),
        );
        catalog.enum_variants.insert(
            (
                enum_type,
                icydb_schema::TypeSourceKey::try_new("test:status:active")
                    .expect("variant source key should build"),
            ),
            active,
        );
        catalog.enum_variants.insert(
            (
                enum_type,
                icydb_schema::TypeSourceKey::try_new("test:status:disabled")
                    .expect("variant source key should build"),
            ),
            disabled,
        );

        let encoded = encode_accepted_source_bindings(&catalog, &enums, &composites, &entities)
            .expect("closed enum bindings should encode");
        let decoded = decode_accepted_source_bindings(&encoded, &enums, &composites, &entities)
            .expect("closed enum bindings should decode");

        assert_eq!(decoded, catalog);
        let adapter_names = catalog
            .typed_adapter_names(&enums, &composites)
            .expect("accepted adapter names should resolve");
        assert_eq!(
            adapter_names.named_types,
            vec![("test:status".to_string(), "test::Status".to_string())],
        );
        assert_eq!(
            adapter_names.enum_variants,
            vec![
                (
                    "test:status".to_string(),
                    "test:status:active".to_string(),
                    "Active".to_string(),
                ),
                (
                    "test:status".to_string(),
                    "test:status:disabled".to_string(),
                    "Disabled".to_string(),
                ),
            ],
        );

        let mut incomplete = catalog.clone();
        incomplete
            .enum_variants
            .retain(|(_, source), _| source.as_str() != "test:status:disabled");
        assert!(
            encode_accepted_source_bindings(&incomplete, &enums, &composites, &entities).is_err(),
            "a bound enum type must bind every accepted variant",
        );
    }

    #[test]
    fn source_binding_catalog_closes_record_member_source_identities() {
        let enums = build_initial_accepted_enum_catalog_from_kinds_for_tests(&[])
            .expect("empty enum catalog should build");
        let composite_type = CompositeTypeId::new(1).expect("one is non-zero");
        let alpha = CompositeFieldId::new(1).expect("one is non-zero");
        let zeta = CompositeFieldId::new(2).expect("two is non-zero");
        let composites = AcceptedCompositeCatalog::from_initial_definitions(
            BTreeMap::from([(
                composite_type,
                (
                    "test::Record".to_string(),
                    AcceptedCompositeShape::Record(vec![
                        AcceptedCompositeField::new(
                            alpha,
                            "alpha".to_string(),
                            AcceptedCompositeElement::new(AcceptedFieldKind::Nat64, false),
                        ),
                        AcceptedCompositeField::new(
                            zeta,
                            "zeta".to_string(),
                            AcceptedCompositeElement::new(AcceptedFieldKind::Bool, false),
                        ),
                    ]),
                ),
            )]),
            &enums,
        )
        .expect("record catalog should build");
        let entities = BTreeMap::new();
        let mut catalog = AcceptedSourceBindingCatalog::default();
        catalog.types.insert(
            icydb_schema::TypeSourceKey::try_new("test:record")
                .expect("type source key should build"),
            AcceptedNamedTypeIdentity::Composite(composite_type),
        );
        catalog.composite_fields.insert(
            (
                composite_type,
                icydb_schema::FieldSourceKey::try_new("test:record:alpha")
                    .expect("field source key should build"),
            ),
            alpha,
        );
        catalog.composite_fields.insert(
            (
                composite_type,
                icydb_schema::FieldSourceKey::try_new("test:record:zeta")
                    .expect("field source key should build"),
            ),
            zeta,
        );

        let encoded = encode_accepted_source_bindings(&catalog, &enums, &composites, &entities)
            .expect("closed record bindings should encode");
        let decoded = decode_accepted_source_bindings(&encoded, &enums, &composites, &entities)
            .expect("closed record bindings should decode");

        assert_eq!(decoded, catalog);
        let adapter_names = catalog
            .typed_adapter_names(&enums, &composites)
            .expect("accepted adapter names should resolve");
        assert_eq!(
            adapter_names.named_types,
            vec![("test:record".to_string(), "test::Record".to_string())],
        );
        assert_eq!(
            adapter_names.composite_fields,
            vec![
                (
                    "test:record".to_string(),
                    "test:record:alpha".to_string(),
                    "alpha".to_string(),
                ),
                (
                    "test:record".to_string(),
                    "test:record:zeta".to_string(),
                    "zeta".to_string(),
                ),
            ],
        );

        let mut incomplete = catalog;
        incomplete
            .composite_fields
            .retain(|(_, source), _| source.as_str() != "test:record:zeta");
        assert!(
            encode_accepted_source_bindings(&incomplete, &enums, &composites, &entities).is_err(),
            "a bound record type must bind every accepted member",
        );
    }

    #[test]
    fn accepted_schema_fingerprint_covers_source_bindings() {
        let enums = build_initial_accepted_enum_catalog_from_kinds_for_tests(&[])
            .expect("empty enum catalog should build");
        let composites = AcceptedCompositeCatalog::empty();
        let entity = EntityTag::new(7);
        let field = FieldId::new(1);
        let snapshot = PersistedSchemaSnapshot::new(
            SchemaVersion::initial(),
            "test::Bound".to_string(),
            "Bound".to_string(),
            field,
            SchemaRowLayout::initial(vec![(field, SchemaFieldSlot::new(0))]),
            vec![PersistedFieldSnapshot::new_initial(
                field,
                "id".to_string(),
                SchemaFieldSlot::new(0),
                AcceptedFieldKind::Nat64,
                Vec::new(),
                false,
                SchemaInsertDefault::None,
                FieldStorageDecode::ByKind,
                LeafCodec::Scalar(ScalarCodec::Nat64),
            )],
        );
        let entities = BTreeMap::from([(entity, snapshot)]);
        let mut first_bindings = AcceptedSourceBindingCatalog::default();
        first_bindings.entities.insert(
            icydb_schema::EntitySourceKey::try_new("test:first")
                .expect("first source key should build"),
            entity,
        );
        let mut second_bindings = AcceptedSourceBindingCatalog::default();
        second_bindings.entities.insert(
            icydb_schema::EntitySourceKey::try_new("test:second")
                .expect("second source key should build"),
            entity,
        );

        let first = CandidateSchemaRevision::new(
            AcceptedSchemaRevisionBundle::new_with_source_bindings(
                AcceptedSchemaRevision::INITIAL,
                "test::Store",
                enums.clone(),
                composites.clone(),
                first_bindings,
                entities.clone(),
            )
            .expect("first bundle should close"),
        )
        .expect("first candidate should encode");
        let second = CandidateSchemaRevision::new(
            AcceptedSchemaRevisionBundle::new_with_source_bindings(
                AcceptedSchemaRevision::INITIAL,
                "test::Store",
                enums,
                composites,
                second_bindings,
                entities,
            )
            .expect("second bundle should close"),
        )
        .expect("second candidate should encode");

        assert_ne!(
            first.root().fingerprint().as_bytes(),
            second.root().fingerprint().as_bytes(),
        );
    }
}