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
use super::*;
use crate::redo::PullKind;
impl Graph {
pub fn apply_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()> {
self.apply_redo_inner(ops, commit_ts, false)
}
/// Replay historical WAL under the old admission policy, preserving unknown
/// enforcement as legacy rather than inventing a typed checkpoint image.
#[doc(hidden)]
pub fn apply_replayed_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()> {
self.apply_redo_inner(ops, commit_ts, true)
}
pub(super) fn apply_redo_inner(
&self,
ops: Vec<RedoOp>,
commit_ts: u64,
legacy_replay: bool,
) -> Result<()> {
self.validate_redo_group(std::iter::once((ops.as_slice(), commit_ts)), !legacy_replay)?;
let legacy_created = if legacy_replay {
ops.iter()
.any(|op| matches!(op, RedoOp::CreateConstraint { .. }))
} else {
false
};
self.in_write.store(true, Ordering::Release);
self.write_ts.store(commit_ts, Ordering::Release);
self.record_redo.store(false, Ordering::Release);
for op in ops {
match op {
RedoOp::CreateNode {
id,
labels,
properties,
} => {
// Move the typed fields in — the caller consumed the WAL
// borrow before apply, so no clone is needed (ADR-0078).
self.install_node(
id,
std::sync::Arc::unwrap_or_clone(labels),
std::sync::Arc::unwrap_or_clone(properties).into(),
)?;
}
RedoOp::CreateRel {
id,
start,
end,
rel_type,
properties,
} => {
self.install_rel(
id,
start,
end,
rel_type,
std::sync::Arc::unwrap_or_clone(properties).into(),
)?;
}
RedoOp::DeleteNode { id } => {
self.delete_node(id, true)?;
self.drop_pull_identity(PullKind::Node, id);
}
RedoOp::DeleteRel { id } => {
self.delete_rel(id);
self.drop_pull_identity(PullKind::Rel, id);
}
RedoOp::SetNodeProp { id, key, value } => {
self.set_property_node(id, &key, value);
}
RedoOp::RemoveNodeProp { id, key } => {
self.set_property_node(id, &key, Value::Null);
}
RedoOp::SetRelProp { id, key, value } => {
self.set_property_rel(id, &key, value);
}
RedoOp::RemoveRelProp { id, key } => {
self.set_property_rel(id, &key, Value::Null);
}
RedoOp::AddLabels { id, labels } => {
self.add_labels(id, &labels);
}
RedoOp::RemoveLabels { id, labels } => {
self.remove_labels(id, &labels);
}
RedoOp::CreateIndex {
name,
label,
property,
} => {
self.install_named_index(IndexDef {
name,
label,
property,
})?;
}
RedoOp::DropIndex { name } => {
self.catalog.lock().unwrap().drop_index(&name);
let removed = {
let mut theater = self.theater.lock().unwrap();
theater.index_options.remove(&name);
theater.index_details.remove(&name)
};
if let Some(detail) = removed {
self.drop_detail(&detail);
self.refresh_index_charge();
self.recompute_used();
}
}
RedoOp::CreateConstraint {
name,
relationship,
label,
properties,
kind,
property_type,
enforced_label,
} => self.apply_create_constraint(ConstraintDef {
name,
relationship,
label,
properties,
kind,
property_type,
enforced_label,
})?,
RedoOp::DropConstraint { name } => {
self.catalog.lock().unwrap().drop_constraint(&name);
self.theater
.lock()
.unwrap()
.derived_constraints
.remove(&name);
self.legacy_unenforced.lock().unwrap().remove(&name);
self.constraint_keys_drop(&name);
}
RedoOp::CatalogDerivedConstraint { constraint } => {
if let Some(enforced) = &constraint.enforced_label {
// An enforced label is always a node label (even on a
// relationship constraint, `sourceLabel`/`targetLabel`
// name the endpoint's label).
self.record_schema_names(false, std::iter::once(enforced.clone()));
}
if self
.catalog
.lock()
.unwrap()
.constraints
.get(&constraint.name)
.is_some_and(|def| matching_enforced_label(def, &constraint))
{
self.legacy_unenforced
.lock()
.unwrap()
.remove(&constraint.name);
}
self.theater
.lock()
.unwrap()
.derived_constraints
.insert(constraint.name.clone(), constraint);
// No install here: the load-bearing invariant is that
// no durable image carries a uniqueness kind in this
// record. Witness 1: its only production constructor
// (the CREATE CONSTRAINT push) is gated on
// `enforced_label`. Witness 2: the parser sets that
// field only on `nodeLabelExistence` /
// `sourceLabelExistence` / `targetLabelExistence`.
// Derived uniqueness reachable via `SetCurrentGraphType`
// is the theater half of `uniqueness_specs`;
// `rebuild_constraint_keys` covers it.
}
RedoOp::CatalogIndexDetail {
name,
detail,
options,
} => {
// Decode before mutating: a corrupted op is an error,
// never a silently dropped index detail/options entry.
let options = redo::props_from_json(&options)?;
// The typed detail is the only op that knows whether its
// labels are node labels or relationship types; the
// catalog's `CreateIndex` row is entity-kind-blind.
self.record_schema_names(detail.relationship, detail.labels.iter().cloned());
{
let mut theater = self.theater.lock().unwrap();
theater.index_details.insert(name.clone(), detail.clone());
theater.index_options.insert(name, options);
}
self.refresh_index_charge();
self.materialize_detail(&detail);
self.recompute_used();
}
RedoOp::PutDatabase {
name,
language,
composite,
} => {
// ADR-0276: the databases catalog is store-level,
// shared by every namespace graph; per the ADR-0116 §8
// invariants only the fixed namespaces are kept,
// replayed entries for other names are ignored.
self.databases().put_database(&name, language, composite);
}
RedoOp::DropDatabase { .. } => {
// ADR-0116 §1/§8: the fixed namespaces cannot be
// dropped, and the `Namespace`-keyed catalog can only
// ever hold them, so a legacy `DROP DATABASE` redo is
// a no-op.
}
RedoOp::PutGraphType { name, body } => {
self.theater.lock().unwrap().graph_types.insert(name, body);
}
RedoOp::SetCurrentGraphType { body } => self.apply_current_graph_type(&body)?,
RedoOp::PullIdentityPut {
source,
kind,
remote_id,
local_id,
} => {
self.pull_identity_put(source, kind, remote_id, local_id);
}
RedoOp::OntologySet { .. } | RedoOp::OntologyDrop { .. } => {
// ADR-0120 §2/§3: store-level ops; they carry no graph
// mutation. The document lives with the store layer —
// no graph is derived from it (ADR-0229 §3) — so replay
// is a deliberate no-op here.
}
}
}
self.end_write(commit_ts);
self.record_redo.store(true, Ordering::Release);
// ADR-0595: reclaim the label postings the removal sites
// suspected — here, the single-writer apply's tail, after every
// op of this commit is installed, so no concurrent `index_add`
// can race the removal. An empty candidate set is one horizon
// read.
self.drain_label_suspects();
if legacy_created {
self.mark_legacy_unenforced();
}
Ok(())
}
}