1use std::collections::BTreeMap;
6
7use relate_catalog::{ConstraintDef, IndexDef};
8use relate_core::Result;
9
10use crate::graph::{QueryStats, ShowSchema};
11use crate::redo::{CatalogName, IndexFacts, RedoOp};
12
13use super::{ExecGraph, WriteScratch};
14
15trait NamedDef {
18 fn def_name(&self) -> &str;
19}
20
21impl NamedDef for IndexDef {
22 fn def_name(&self) -> &str {
23 &self.name
24 }
25}
26
27impl NamedDef for ConstraintDef {
28 fn def_name(&self) -> &str {
29 &self.name
30 }
31}
32
33impl ExecGraph<'_> {
39 pub fn has_label(&self, label: &str) -> bool {
44 self.base.has_label(label) || self.scratch_wrote(|scratch| scratch.has_written_label(label))
45 }
46
47 pub fn has_rel_type(&self, rel_type: &str) -> bool {
50 self.base.has_rel_type(rel_type)
51 || self.scratch_wrote(|scratch| scratch.has_written_rel_type(rel_type))
52 }
53
54 pub fn has_property_key(&self, key: &str) -> bool {
58 self.base.has_property_key(key)
59 || self.scratch_wrote(|scratch| scratch.has_written_property_key(key))
60 }
61
62 fn scratch_wrote(&self, question: impl Fn(&WriteScratch) -> bool) -> bool {
65 self.scratch
66 .is_some_and(|scratch| question(&scratch.borrow()))
67 }
68}
69
70impl WriteScratch {
76 fn mentions(&self, wanted: CatalogName<'_>) -> bool {
77 self.redo.mentions(wanted)
78 }
79
80 fn has_written_label(&self, label: &str) -> bool {
81 self.mentions(CatalogName::Label(label))
82 }
83
84 fn has_written_rel_type(&self, rel_type: &str) -> bool {
85 self.mentions(CatalogName::RelType(rel_type))
86 }
87
88 fn has_written_property_key(&self, key: &str) -> bool {
89 self.mentions(CatalogName::PropertyKey(key))
90 }
91}
92
93impl<'a> ExecGraph<'a> {
94 pub fn index_facts(&self) -> Result<Vec<IndexFacts>> {
101 let (indexes, constraints, mut show) = match self.schema {
106 Some(schema) => (
107 schema.indexes.clone(),
108 schema.constraints.clone(),
109 schema.show.clone(),
110 ),
111 None => {
112 let snapshot = self.base.schema_snapshot();
113 (snapshot.indexes, snapshot.constraints, snapshot.show)
114 }
115 };
116 let indexes = self.fold_named_indexes(indexes);
117 let constraints = self.fold_named_constraints(constraints);
118 if let Some(scratch) = self.scratch {
119 crate::graph::apply_show_overlay(&mut show, scratch.borrow().redo.catalog_ops())?;
120 }
121 let details = &show.data.index_details;
122 Ok(crate::redo::resolve_index_facts(
123 &indexes,
124 details,
125 &constraints,
126 ))
127 }
128
129 pub(crate) fn show_schema(&self) -> Result<ShowSchema> {
130 let mut show = self
139 .schema
140 .map(|schema| schema.show.clone())
141 .unwrap_or_else(|| self.base.capture_show_schema());
142 if let Some(scratch) = self.scratch {
143 crate::graph::apply_show_overlay(&mut show, scratch.borrow().redo.catalog_ops())?;
144 }
145 Ok(show)
146 }
147
148 fn named_defs<D: NamedDef>(
153 &self,
154 committed: Vec<D>,
155 apply: impl Fn(&mut BTreeMap<String, D>, &RedoOp),
156 ) -> Vec<D> {
157 let mut map: BTreeMap<String, D> = committed
158 .into_iter()
159 .map(|d| (d.def_name().to_string(), d))
160 .collect();
161 if let Some(cell) = self.scratch {
162 for op in cell.borrow().redo.catalog_ops() {
163 apply(&mut map, op);
164 }
165 }
166 map.into_values().collect()
167 }
168
169 pub fn named_indexes(&self) -> Vec<IndexDef> {
170 let committed = self
171 .schema
172 .map(|s| s.indexes.clone())
173 .unwrap_or_else(|| self.base.named_indexes());
174 self.fold_named_indexes(committed)
175 }
176
177 fn fold_named_indexes(&self, committed: Vec<IndexDef>) -> Vec<IndexDef> {
181 self.named_defs(committed, |map, op| match op {
182 RedoOp::CreateIndex {
183 name,
184 label,
185 property,
186 } => {
187 map.insert(
188 name.clone(),
189 IndexDef {
190 name: name.clone(),
191 label: label.clone(),
192 property: property.clone(),
193 },
194 );
195 }
196 RedoOp::DropIndex { name } => {
197 map.remove(name);
198 }
199 _ => {}
200 })
201 }
202
203 pub fn named_index(&self, name: &str) -> Option<IndexDef> {
204 self.named_indexes().into_iter().find(|d| d.name == name)
205 }
206
207 pub fn named_constraint(&self, name: &str) -> Option<ConstraintDef> {
211 self.named_constraints()
212 .into_iter()
213 .find(|def| def.name == name)
214 }
215
216 pub fn named_index_by_schema(&self, label: &str, property: &str) -> Option<IndexDef> {
217 self.named_indexes()
218 .into_iter()
219 .find(|d| crate::redo::holds_index_pair(d, label, property))
220 }
221
222 pub fn named_constraints(&self) -> Vec<ConstraintDef> {
223 let committed = self
224 .schema
225 .map(|s| s.constraints.clone())
226 .unwrap_or_else(|| self.base.named_constraints());
227 self.fold_named_constraints(committed)
228 }
229
230 fn fold_named_constraints(&self, committed: Vec<ConstraintDef>) -> Vec<ConstraintDef> {
234 self.named_defs(committed, |map, op| {
235 if let Some(def) = op.constraint_def() {
236 map.insert(def.name.clone(), def);
237 } else if let RedoOp::DropConstraint { name } = op {
238 map.remove(name);
239 }
240 })
241 }
242
243 pub(crate) fn effective_constraints(&self) -> Result<Vec<ConstraintDef>> {
249 Ok(self.effective_constraints_in(&self.show_schema()?))
250 }
251
252 pub(crate) fn effective_constraints_in(&self, show: &ShowSchema) -> Vec<ConstraintDef> {
257 resolve_enforced_labels(self.named_constraints(), &show.data.derived_constraints)
258 }
259
260 pub fn create_named_index(&self, name: String, label: String, property: String) -> Result<()> {
263 self.push_schema_redo(
264 RedoOp::CreateIndex {
265 name,
266 label,
267 property,
268 },
269 |stats| stats.indexes_added += 1,
270 )
271 }
272
273 pub(crate) fn create_backing_index(
276 &self,
277 name: String,
278 label: String,
279 property: String,
280 ) -> Result<()> {
281 self.push_schema_redo(
282 RedoOp::CreateIndex {
283 name,
284 label,
285 property,
286 },
287 |_| {},
288 )
289 }
290
291 pub(crate) fn push_catalog_redo(&self, op: RedoOp) {
292 self.scratch_mut().push_redo(op);
293 }
294
295 pub fn drop_named_index(&self, name: String) -> Result<()> {
298 self.push_schema_redo(RedoOp::DropIndex { name }, |stats| {
299 stats.indexes_removed += 1
300 })
301 }
302
303 pub(crate) fn drop_backing_index(&self, name: String) -> Result<()> {
306 self.push_schema_redo(RedoOp::DropIndex { name }, |_| {})
307 }
308
309 pub fn create_named_constraint(&self, def: ConstraintDef) -> Result<()> {
310 self.push_schema_redo(RedoOp::create_constraint(def), |stats| {
311 stats.constraints_added += 1
312 })
313 }
314
315 pub fn drop_named_constraint(&self, name: String) -> Result<()> {
316 self.push_schema_redo(RedoOp::DropConstraint { name }, |stats| {
317 stats.constraints_removed += 1
318 })
319 }
320
321 fn push_schema_redo(&self, op: RedoOp, count: impl FnOnce(&mut QueryStats)) -> Result<()> {
324 let mut scratch = self.scratch_mut();
325 count(&mut scratch.statement.stats);
326 scratch.push_redo(op);
327 Ok(())
328 }
329}
330
331fn resolve_enforced_labels(
336 constraints: Vec<ConstraintDef>,
337 derived: &BTreeMap<String, relate_model::Constraint>,
338) -> Vec<ConstraintDef> {
339 constraints
340 .into_iter()
341 .map(|mut def| {
342 let twin_label = derived
343 .get(&def.name)
344 .and_then(|constraint| constraint.enforced_label.as_deref());
345 crate::graph::resolve_enforced_label(&mut def, twin_label);
346 def
347 })
348 .collect()
349}