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
//! Module: db::schema::identity
//! Responsibility: durable schema identity primitives.
//! Does not own: generated model validation or persisted schema reconciliation.
//! Boundary: small copyable identifiers shared by schema proposal and live-schema code.
use std::num::NonZeroU32;
///
/// ConstraintId
///
/// Stable non-zero identity for one accepted constraint within an entity's
/// schema history.
///
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(in crate::db) struct ConstraintId(NonZeroU32);
impl ConstraintId {
/// Admit one non-zero persisted constraint identity.
#[must_use]
pub(in crate::db) const fn new(raw: u32) -> Option<Self> {
match NonZeroU32::new(raw) {
Some(raw) => Some(Self(raw)),
None => None,
}
}
/// Return the persisted integer identity.
#[must_use]
pub(in crate::db) const fn get(self) -> u32 {
self.0.get()
}
}
///
/// ConstraintIdAllocator
///
/// Persisted non-reusing high-water state for entity-local constraint IDs.
/// Dropping or aborting a constraint never lowers this value.
///
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(in crate::db) struct ConstraintIdAllocator {
high_water: Option<ConstraintId>,
}
impl ConstraintIdAllocator {
/// Build allocator state from its trusted persisted high-water value.
#[must_use]
pub(in crate::db) const fn new(high_water: u32) -> Self {
Self {
high_water: ConstraintId::new(high_water),
}
}
/// Return the greatest constraint ID ever reserved by this entity.
#[must_use]
pub(in crate::db) const fn high_water(self) -> u32 {
match self.high_water {
Some(high_water) => high_water.get(),
None => 0,
}
}
/// Reserve the next non-reusing constraint identity in candidate state.
///
/// The returned allocator is not authoritative until its containing schema
/// candidate publishes. Exhaustion leaves the accepted allocator unchanged.
#[must_use]
pub(in crate::db) const fn checked_reserve(self) -> Option<(Self, ConstraintId)> {
let next = match self.high_water {
Some(high_water) => match high_water.get().checked_add(1) {
Some(next) => next,
None => return None,
},
None => 1,
};
let Some(id) = ConstraintId::new(next) else {
return None;
};
Some((
Self {
high_water: Some(id),
},
id,
))
}
}
///
/// SchemaIndexId
///
/// Stable non-zero logical identity for one accepted secondary-index
/// definition. Physical index keys continue to use dense runtime ordinals.
///
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(in crate::db) struct SchemaIndexId(NonZeroU32);
impl SchemaIndexId {
/// Admit one non-zero persisted logical index identity.
#[must_use]
pub(in crate::db) const fn new(raw: u32) -> Option<Self> {
match NonZeroU32::new(raw) {
Some(raw) => Some(Self(raw)),
None => None,
}
}
/// Return the persisted integer identity.
#[must_use]
pub(in crate::db) const fn get(self) -> u32 {
self.0.get()
}
}
///
/// RelationId
///
/// Stable non-zero logical identity for one accepted relation definition.
/// Relation key encoding remains owned by the existing relation contract.
///
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(in crate::db) struct RelationId(NonZeroU32);
impl RelationId {
/// Admit one non-zero persisted relation identity.
#[must_use]
pub(in crate::db) const fn new(raw: u32) -> Option<Self> {
match NonZeroU32::new(raw) {
Some(raw) => Some(Self(raw)),
None => None,
}
}
/// Return the persisted integer identity.
#[must_use]
pub(in crate::db) const fn get(self) -> u32 {
self.0.get()
}
}
///
/// FieldId
///
/// Durable identity for one logical schema field.
/// This ID is distinct from generated Rust field order and from executor slot
/// indexes so schema reconciliation can preserve identity across safe reorders
/// and renames.
///
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(in crate::db) struct FieldId(u32);
impl FieldId {
/// Build one field ID from a trusted persisted value.
#[must_use]
pub(in crate::db) const fn new(raw: u32) -> Self {
Self(raw)
}
/// Return the raw persisted field identity.
#[must_use]
pub(in crate::db) const fn get(self) -> u32 {
self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn constraint_allocator_preserves_reserved_high_water_identity() {
let empty = ConstraintIdAllocator::default();
let reserved = ConstraintIdAllocator::new(7);
assert_eq!(empty.high_water(), 0);
assert_eq!(reserved.high_water(), 7);
}
#[test]
fn constraint_allocator_reserves_monotonically_and_fails_closed_at_exhaustion() {
let (first, first_id) = ConstraintIdAllocator::default()
.checked_reserve()
.expect("empty allocator should reserve its first identity");
let (second, second_id) = first
.checked_reserve()
.expect("allocator should reserve its next identity");
assert_eq!(first_id.get(), 1);
assert_eq!(second_id.get(), 2);
assert_eq!(second.high_water(), 2);
assert_eq!(ConstraintIdAllocator::new(u32::MAX).checked_reserve(), None,);
}
#[test]
fn logical_structural_ids_reject_zero() {
assert_eq!(SchemaIndexId::new(0), None);
assert_eq!(RelationId::new(0), None);
}
}