Skip to main content

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                    // ADR-0687: schema DDL moves the full-invalidation
94                    // epoch — no token footprint describes an index.
95                    self.note_schema_change();
96                    self.install_named_index(IndexDef {
97                        name,
98                        label,
99                        property,
100                    })?;
101                }
102                RedoOp::DropIndex { name } => {
103                    self.note_schema_change();
104                    self.catalog.lock().unwrap().drop_index(&name);
105                    let removed = {
106                        let mut theater = self.theater.lock().unwrap();
107                        theater.index_options.remove(&name);
108                        theater.index_details.remove(&name)
109                    };
110                    if let Some(detail) = removed {
111                        self.drop_detail(&detail);
112                        self.refresh_index_charge();
113                        self.recompute_used();
114                    }
115                }
116                RedoOp::CreateConstraint {
117                    name,
118                    relationship,
119                    label,
120                    properties,
121                    kind,
122                    property_type,
123                    enforced_label,
124                } => {
125                    self.note_schema_change();
126                    self.apply_create_constraint(ConstraintDef {
127                        name,
128                        relationship,
129                        label,
130                        properties,
131                        kind,
132                        property_type,
133                        enforced_label,
134                    })?
135                }
136                RedoOp::DropConstraint { name } => {
137                    self.note_schema_change();
138                    self.catalog.lock().unwrap().drop_constraint(&name);
139                    self.theater
140                        .lock()
141                        .unwrap()
142                        .derived_constraints
143                        .remove(&name);
144                    self.legacy_unenforced.lock().unwrap().remove(&name);
145                    self.constraint_keys_drop(&name);
146                }
147                RedoOp::CatalogDerivedConstraint { constraint } => {
148                    self.note_schema_change();
149                    if let Some(enforced) = &constraint.enforced_label {
150                        // An enforced label is always a node label (even on a
151                        // relationship constraint, `sourceLabel`/`targetLabel`
152                        // name the endpoint's label).
153                        self.record_schema_names(false, std::iter::once(enforced.clone()));
154                    }
155                    if self
156                        .catalog
157                        .lock()
158                        .unwrap()
159                        .constraints
160                        .get(&constraint.name)
161                        .is_some_and(|def| matching_enforced_label(def, &constraint))
162                    {
163                        self.legacy_unenforced
164                            .lock()
165                            .unwrap()
166                            .remove(&constraint.name);
167                    }
168                    self.theater
169                        .lock()
170                        .unwrap()
171                        .derived_constraints
172                        .insert(constraint.name.clone(), constraint);
173                    // No install here: the load-bearing invariant is that
174                    // no durable image carries a uniqueness kind in this
175                    // record. Witness 1: its only production constructor
176                    // (the CREATE CONSTRAINT push) is gated on
177                    // `enforced_label`. Witness 2: the parser sets that
178                    // field only on `nodeLabelExistence` /
179                    // `sourceLabelExistence` / `targetLabelExistence`.
180                    // Derived uniqueness reachable via `SetCurrentGraphType`
181                    // is the theater half of `uniqueness_specs`;
182                    // `rebuild_constraint_keys` covers it.
183                }
184                RedoOp::CatalogIndexDetail {
185                    name,
186                    detail,
187                    options,
188                } => {
189                    self.note_schema_change();
190                    // Decode before mutating: a corrupted op is an error,
191                    // never a silently dropped index detail/options entry.
192                    let options = redo::props_from_json(&options)?;
193                    // The typed detail is the only op that knows whether its
194                    // labels are node labels or relationship types; the
195                    // catalog's `CreateIndex` row is entity-kind-blind.
196                    self.record_schema_names(detail.relationship, detail.labels.iter().cloned());
197                    {
198                        let mut theater = self.theater.lock().unwrap();
199                        theater.index_details.insert(name.clone(), detail.clone());
200                        theater.index_options.insert(name, options);
201                    }
202                    self.refresh_index_charge();
203                    self.materialize_detail(&detail);
204                    self.recompute_used();
205                }
206                RedoOp::PutDatabase {
207                    name,
208                    language,
209                    composite,
210                } => {
211                    // ADR-0276: the databases catalog is store-level,
212                    // shared by every namespace graph; per the ADR-0116 §8
213                    // invariants only the fixed namespaces are kept,
214                    // replayed entries for other names are ignored.
215                    self.databases().put_database(&name, language, composite);
216                }
217                RedoOp::DropDatabase { .. } => {
218                    // ADR-0116 §1/§8: the fixed namespaces cannot be
219                    // dropped, and the `Namespace`-keyed catalog can only
220                    // ever hold them, so a legacy `DROP DATABASE` redo is
221                    // a no-op.
222                }
223                RedoOp::PutGraphType { name, body } => {
224                    self.note_schema_change();
225                    self.theater.lock().unwrap().graph_types.insert(name, body);
226                }
227                RedoOp::SetCurrentGraphType { body } => {
228                    self.note_schema_change();
229                    self.apply_current_graph_type(&body)?
230                }
231                RedoOp::PullIdentityPut {
232                    source,
233                    kind,
234                    remote_id,
235                    local_id,
236                } => {
237                    self.pull_identity_put(source, kind, remote_id, local_id);
238                }
239                RedoOp::OntologySet { .. } | RedoOp::OntologyDrop { .. } => {
240                    // ADR-0120 §2/§3: store-level ops; they carry no graph
241                    // mutation. The document lives with the store layer —
242                    // no graph is derived from it (ADR-0229 §3) — so replay
243                    // is a deliberate no-op here.
244                }
245            }
246        }
247        self.end_write(commit_ts);
248        self.record_redo.store(true, Ordering::Release);
249        // ADR-0595: reclaim the label postings the removal sites
250        // suspected — here, the single-writer apply's tail, after every
251        // op of this commit is installed, so no concurrent `index_add`
252        // can race the removal. An empty candidate set is one horizon
253        // read.
254        self.drain_label_suspects();
255        if legacy_created {
256            self.mark_legacy_unenforced();
257        }
258        Ok(())
259    }
260}