Skip to main content

relate_executor/graph/
schema_api.rs

1//! The graph's named-schema surface: named indexes and constraints, and the
2//! schema snapshot. Split out of `graph/mod.rs` (LABS-677).
3
4use relate_catalog::{ConstraintDef, IndexDef};
5
6use super::*;
7
8impl Graph {
9    pub fn named_indexes(&self) -> Vec<IndexDef> {
10        self.catalog
11            .lock()
12            .unwrap()
13            .indexes
14            .values()
15            .cloned()
16            .collect()
17    }
18
19    /// Capture named indexes, constraints and their SHOW metadata at one
20    /// admission boundary. Embedded callers hold the publication read guard
21    /// across this call and `pin_now`; direct callers must serialize with
22    /// their own graph writers in the same way.
23    pub fn schema_snapshot(&self) -> SchemaSnapshot {
24        self.schema_snapshot_with_hook(|| {})
25    }
26
27    pub(super) fn schema_snapshot_with_hook(&self, after_catalog: impl FnOnce()) -> SchemaSnapshot {
28        let (indexes, constraints) = {
29            let catalog = self.catalog.lock().unwrap();
30            (
31                catalog.indexes.values().cloned().collect(),
32                catalog.constraints.values().cloned().collect(),
33            )
34        };
35        after_catalog();
36        // Publication serialization makes these non-overlapping captures
37        // (catalog above, then databases + theater in capture_show_schema)
38        // atomic to embedded readers, without ever nesting catalog/theater
39        // locks.
40        let show = self.capture_show_schema();
41        SchemaSnapshot {
42            indexes,
43            constraints,
44            show,
45        }
46    }
47
48    /// The committed SHOW metadata at this instant: the store-level
49    /// databases table plus this graph's theater, under the theater lock.
50    pub(crate) fn capture_show_schema(&self) -> ShowSchema {
51        ShowSchema::capture(self.databases().snapshot(), &self.theater.lock().unwrap())
52    }
53
54    pub fn named_constraints(&self) -> Vec<ConstraintDef> {
55        self.catalog
56            .lock()
57            .unwrap()
58            .constraints
59            .values()
60            .cloned()
61            .collect()
62    }
63
64    /// Install one catalog constraint row. Restore paths only: this mutates
65    /// the catalog WITHOUT the maintained constraint-key map (ADR-0439), so
66    /// the caller must finish with `rebuild_indexes` / `finish_restore`,
67    /// which builds the map from the catalog ∪ the theater's derived set.
68    /// Calling it on a live graph would leave a uniqueness constraint the
69    /// admission probe cannot see.
70    pub fn install_named_constraint(&self, def: ConstraintDef) -> Result<()> {
71        self.catalog.lock().unwrap().insert_constraint(def)
72    }
73
74    pub fn named_index(&self, name: &str) -> Option<IndexDef> {
75        self.catalog.lock().unwrap().indexes.get(name).cloned()
76    }
77
78    /// By-name constraint probe, the constraint side of [`Self::named_index`].
79    pub fn has_constraint(&self, name: &str) -> bool {
80        self.catalog.lock().unwrap().constraints.contains_key(name)
81    }
82
83    pub fn named_index_by_schema(&self, label: &str, property: &str) -> Option<IndexDef> {
84        self.catalog
85            .lock()
86            .unwrap()
87            .index_by_schema(label, property)
88            .cloned()
89    }
90
91    pub fn install_named_index(&self, def: IndexDef) -> Result<()> {
92        self.catalog.lock().unwrap().insert_index(def)
93    }
94}