relate_executor/graph/apply_redo.rs
1use super::*;
2use crate::redo::PullKind;
3
4impl Graph {
5 pub fn apply_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()> {
6 self.apply_redo_inner(ops, commit_ts, false)
7 }
8
9 /// Replay historical WAL under the old admission policy, preserving unknown
10 /// enforcement as legacy rather than inventing a typed checkpoint image.
11 #[doc(hidden)]
12 pub fn apply_replayed_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()> {
13 self.apply_redo_inner(ops, commit_ts, true)
14 }
15
16 pub(super) fn apply_redo_inner(
17 &self,
18 ops: Vec<RedoOp>,
19 commit_ts: u64,
20 legacy_replay: bool,
21 ) -> Result<()> {
22 self.validate_redo_group(std::iter::once((ops.as_slice(), commit_ts)), !legacy_replay)?;
23 let legacy_created = if legacy_replay {
24 ops.iter()
25 .any(|op| matches!(op, RedoOp::CreateConstraint { .. }))
26 } else {
27 false
28 };
29 self.in_write.store(true, Ordering::Release);
30 self.write_ts.store(commit_ts, Ordering::Release);
31 self.record_redo.store(false, Ordering::Release);
32 for op in ops {
33 match op {
34 RedoOp::CreateNode {
35 id,
36 labels,
37 properties,
38 } => {
39 // Move the typed fields in — the caller consumed the WAL
40 // borrow before apply, so no clone is needed (ADR-0078).
41 self.install_node(
42 id,
43 std::sync::Arc::unwrap_or_clone(labels),
44 std::sync::Arc::unwrap_or_clone(properties).into(),
45 )?;
46 }
47 RedoOp::CreateRel {
48 id,
49 start,
50 end,
51 rel_type,
52 properties,
53 } => {
54 self.install_rel(
55 id,
56 start,
57 end,
58 rel_type,
59 std::sync::Arc::unwrap_or_clone(properties).into(),
60 )?;
61 }
62 RedoOp::DeleteNode { id } => {
63 self.delete_node(id, true)?;
64 self.drop_pull_identity(PullKind::Node, id);
65 }
66 RedoOp::DeleteRel { id } => {
67 self.delete_rel(id);
68 self.drop_pull_identity(PullKind::Rel, id);
69 }
70 RedoOp::SetNodeProp { id, key, value } => {
71 self.set_property_node(id, &key, value);
72 }
73 RedoOp::RemoveNodeProp { id, key } => {
74 self.set_property_node(id, &key, Value::Null);
75 }
76 RedoOp::SetRelProp { id, key, value } => {
77 self.set_property_rel(id, &key, value);
78 }
79 RedoOp::RemoveRelProp { id, key } => {
80 self.set_property_rel(id, &key, Value::Null);
81 }
82 RedoOp::AddLabels { id, labels } => {
83 self.add_labels(id, &labels);
84 }
85 RedoOp::RemoveLabels { id, labels } => {
86 self.remove_labels(id, &labels);
87 }
88 RedoOp::CreateIndex {
89 name,
90 label,
91 property,
92 } => {
93 self.install_named_index(IndexDef {
94 name,
95 label,
96 property,
97 })?;
98 }
99 RedoOp::DropIndex { name } => {
100 self.catalog.lock().unwrap().drop_index(&name);
101 let removed = {
102 let mut theater = self.theater.lock().unwrap();
103 theater.index_options.remove(&name);
104 theater.index_details.remove(&name)
105 };
106 if let Some(detail) = removed {
107 self.drop_detail(&detail);
108 self.refresh_index_charge();
109 self.recompute_used();
110 }
111 }
112 RedoOp::CreateConstraint {
113 name,
114 relationship,
115 label,
116 properties,
117 kind,
118 property_type,
119 enforced_label,
120 } => self.apply_create_constraint(ConstraintDef {
121 name,
122 relationship,
123 label,
124 properties,
125 kind,
126 property_type,
127 enforced_label,
128 })?,
129 RedoOp::DropConstraint { name } => {
130 self.catalog.lock().unwrap().drop_constraint(&name);
131 self.theater
132 .lock()
133 .unwrap()
134 .derived_constraints
135 .remove(&name);
136 self.legacy_unenforced.lock().unwrap().remove(&name);
137 self.constraint_keys_drop(&name);
138 }
139 RedoOp::CatalogDerivedConstraint { constraint } => {
140 if let Some(enforced) = &constraint.enforced_label {
141 // An enforced label is always a node label (even on a
142 // relationship constraint, `sourceLabel`/`targetLabel`
143 // name the endpoint's label).
144 self.record_schema_names(false, std::iter::once(enforced.clone()));
145 }
146 if self
147 .catalog
148 .lock()
149 .unwrap()
150 .constraints
151 .get(&constraint.name)
152 .is_some_and(|def| matching_enforced_label(def, &constraint))
153 {
154 self.legacy_unenforced
155 .lock()
156 .unwrap()
157 .remove(&constraint.name);
158 }
159 self.theater
160 .lock()
161 .unwrap()
162 .derived_constraints
163 .insert(constraint.name.clone(), constraint);
164 // No install here: the load-bearing invariant is that
165 // no durable image carries a uniqueness kind in this
166 // record. Witness 1: its only production constructor
167 // (the CREATE CONSTRAINT push) is gated on
168 // `enforced_label`. Witness 2: the parser sets that
169 // field only on `nodeLabelExistence` /
170 // `sourceLabelExistence` / `targetLabelExistence`.
171 // Derived uniqueness reachable via `SetCurrentGraphType`
172 // is the theater half of `uniqueness_specs`;
173 // `rebuild_constraint_keys` covers it.
174 }
175 RedoOp::CatalogIndexDetail {
176 name,
177 detail,
178 options,
179 } => {
180 // Decode before mutating: a corrupted op is an error,
181 // never a silently dropped index detail/options entry.
182 let options = redo::props_from_json(&options)?;
183 // The typed detail is the only op that knows whether its
184 // labels are node labels or relationship types; the
185 // catalog's `CreateIndex` row is entity-kind-blind.
186 self.record_schema_names(detail.relationship, detail.labels.iter().cloned());
187 {
188 let mut theater = self.theater.lock().unwrap();
189 theater.index_details.insert(name.clone(), detail.clone());
190 theater.index_options.insert(name, options);
191 }
192 self.refresh_index_charge();
193 self.materialize_detail(&detail);
194 self.recompute_used();
195 }
196 RedoOp::PutDatabase {
197 name,
198 language,
199 composite,
200 } => {
201 // ADR-0276: the databases catalog is store-level,
202 // shared by every namespace graph; per the ADR-0116 §8
203 // invariants only the fixed namespaces are kept,
204 // replayed entries for other names are ignored.
205 self.databases().put_database(&name, language, composite);
206 }
207 RedoOp::DropDatabase { .. } => {
208 // ADR-0116 §1/§8: the fixed namespaces cannot be
209 // dropped, and the `Namespace`-keyed catalog can only
210 // ever hold them, so a legacy `DROP DATABASE` redo is
211 // a no-op.
212 }
213 RedoOp::PutGraphType { name, body } => {
214 self.theater.lock().unwrap().graph_types.insert(name, body);
215 }
216 RedoOp::SetCurrentGraphType { body } => self.apply_current_graph_type(&body)?,
217 RedoOp::PullIdentityPut {
218 source,
219 kind,
220 remote_id,
221 local_id,
222 } => {
223 self.pull_identity_put(source, kind, remote_id, local_id);
224 }
225 RedoOp::OntologySet { .. } | RedoOp::OntologyDrop { .. } => {
226 // ADR-0120 §2/§3: store-level ops; they carry no graph
227 // mutation. The document lives with the store layer —
228 // no graph is derived from it (ADR-0229 §3) — so replay
229 // is a deliberate no-op here.
230 }
231 }
232 }
233 self.end_write(commit_ts);
234 self.record_redo.store(true, Ordering::Release);
235 // ADR-0595: reclaim the label postings the removal sites
236 // suspected — here, the single-writer apply's tail, after every
237 // op of this commit is installed, so no concurrent `index_add`
238 // can race the removal. An empty candidate set is one horizon
239 // read.
240 self.drain_label_suspects();
241 if legacy_created {
242 self.mark_legacy_unenforced();
243 }
244 Ok(())
245 }
246}