relate_executor/graph/services.rs
1//! The store-level service bundle and its `Graph` accessors; the bundle's
2//! contract is documented on [`StoreServices`].
3
4use std::sync::Arc;
5
6use dashmap::DashMap;
7use relate_core::PreviewFeatures;
8
9use crate::csv::CsvPolicy;
10use crate::pattern_engine::PatternEngine;
11
12use super::{DatabasesCatalog, Graph, Procedure};
13
14/// Store-level services shared by every namespace graph of a store
15/// (ADR-0116 §10): the procedure registry, the LOAD CSV capability, the
16/// store's resolved preview set (ADR-0371, ADR-0378) and the databases
17/// catalog (ADR-0276). One bundle lives behind one `Arc`; every namespace
18/// graph of the store holds a clone of it (installed via
19/// [`Graph::share_services_from`]), so a procedure registered on the store
20/// is callable from any namespace session, the CSV policy and the preview
21/// set are identical in every namespace, and `ALTER DATABASE` /
22/// `SHOW DATABASES` read and write one shared databases table (ADR-0276;
23/// ADR-0116 §8 invariants) regardless of the session's namespace. Open
24/// installs the preview set once with [`Graph::configure_preview`]; both
25/// the catalog and the `CALL` gate read it back through [`Graph::preview`].
26///
27/// The members split into two kinds. The procedure registry, CSV policy,
28/// preview set and pattern-engine switch (ADR-0519) are configuration the
29/// store owns and a follower keeps across
30/// a reload. The databases catalog is durable state: a checkpoint restores
31/// it into the loaded graph's fresh bundle, so the follow reload installs
32/// it with [`Graph::install_databases_from`] before adopting the surviving
33/// bundle instead of letting that bundle's stale copy win.
34#[derive(Debug)]
35pub(crate) struct StoreServices {
36 /// Registered procedures (TCK / user). Store-level state (ADR-0116 §10).
37 pub(crate) procedures: DashMap<String, Procedure>,
38 /// LOAD CSV capability (ADR-0081). The policy is store-level
39 /// (ADR-0116 §10), so it lives in the shared bundle.
40 pub(crate) csv_policy: CsvPolicy,
41 /// The store's resolved preview set (ADR-0371, ADR-0378), installed at
42 /// open and on follow reload; read by the catalog and the `CALL` gate.
43 pub(crate) preview: PreviewFeatures,
44 /// The store-level databases catalog (ADR-0276; ADR-0116 §8
45 /// invariants): the three fixed
46 /// namespaces' settings. One table for the whole store — per-graph
47 /// copies diverged when `ALTER DATABASE` wrote the `neo4j` graph's
48 /// theater while a `system` session read its own statically seeded one.
49 pub(crate) databases: DatabasesCatalog,
50 /// The per-allocation expression cap (ADR-0463) every namespace graph
51 /// of the store enforces. `None` is the explicit opt-out; the default is
52 /// [`crate::memory::EXPRESSION_BUDGET_BYTES`].
53 pub(crate) expression_memory_limit: Option<u64>,
54 /// The store's diagnostic pattern-engine switch (ADR-0519), resolved
55 /// once at open from `RELATE_PATTERN_ENGINE` and read by the
56 /// kernel rungs' routing. Configuration the store owns, like the
57 /// preview set; the default [`PatternEngine::Auto`] is today's
58 /// routing exactly.
59 pub(crate) pattern_engine: PatternEngine,
60}
61
62impl Default for StoreServices {
63 fn default() -> Self {
64 Self {
65 procedures: DashMap::default(),
66 csv_policy: CsvPolicy::default(),
67 preview: PreviewFeatures::default(),
68 databases: DatabasesCatalog::default(),
69 expression_memory_limit: Some(crate::memory::EXPRESSION_BUDGET_BYTES),
70 pattern_engine: PatternEngine::Auto,
71 }
72 }
73}
74
75impl Graph {
76 /// The store's one `StoreServices` bundle, mutable only before it is
77 /// shared with another graph. Both open-time installers
78 /// ([`Graph::configure_load_csv`], [`Graph::configure_preview`]) run
79 /// through this one accessor, so the pre-share contract and its panic
80 /// are stated once: after [`Graph::share_services_from`] the bundle's
81 /// `Arc` identity is the store's one-bundle invariant (ADR-0116 §10),
82 /// and rebuilding it would silently detach this graph from every other
83 /// namespace.
84 fn services_mut(&mut self) -> &mut StoreServices {
85 Arc::get_mut(&mut self.services).expect(
86 "the StoreServices bundle must be configured before \
87 share_services_from: it is already shared (ADR-0116 §10)",
88 )
89 }
90
91 /// Install the per-allocation expression cap (ADR-0463) before the
92 /// services bundle is shared. `None` disables the cap; the default is
93 /// [`crate::memory::EXPRESSION_BUDGET_BYTES`].
94 #[doc(hidden)]
95 pub fn configure_expression_memory_limit(&mut self, limit: Option<u64>) {
96 self.services_mut().expression_memory_limit = limit;
97 }
98
99 /// The configured per-allocation expression cap (`None` = no cap).
100 pub(crate) fn expression_memory_limit(&self) -> Option<u64> {
101 self.services.expression_memory_limit
102 }
103
104 /// Install a validated local CSV capability before sharing the graph with connections.
105 ///
106 /// Mutates the store's single `StoreServices` bundle in place; panics
107 /// if the bundle is already shared via [`Graph::share_services_from`]
108 /// (see `Graph::services_mut`).
109 #[doc(hidden)]
110 pub fn configure_load_csv(&mut self, policy: CsvPolicy) {
111 self.services_mut().csv_policy = policy;
112 }
113
114 /// Install the databases catalog a checkpoint loaded on `loaded` into
115 /// this graph's store-level bundle (ADR-0276), so a replacement graph
116 /// can then adopt that bundle without losing durable state.
117 ///
118 /// The catalog is the one durable member of [`StoreServices`]: the
119 /// store's configuration (procedure registry, CSV policy, preview set)
120 /// belongs to the surviving bundle, while `open_store_readonly` restores
121 /// this table into the fresh graph's own bundle through
122 /// [`Graph::restore_checkpoint_theater`]. The follow reload
123 /// (`follow::reload`) calls this before [`Graph::share_services_from`];
124 /// the catalog's interior mutability lets it write through `&Graph`
125 /// without rebuilding the bundle's `Arc`. Not public API.
126 #[doc(hidden)]
127 pub fn install_databases_from(&self, loaded: &Graph) {
128 self.databases().install(loaded.databases().snapshot());
129 }
130
131 /// Point this graph at the store-level service bundle of `other`
132 /// (ADR-0116 §10), so every namespace of a store shares one procedure
133 /// registry, one LOAD CSV policy, one resolved preview set and one
134 /// databases catalog. Called when the store creates or swaps a
135 /// namespace slot, and by the follow reload (`follow::reload`). The
136 /// bundle mixes configuration the store owns (procedure registry, CSV
137 /// policy, preview set) with durable state restored from a checkpoint
138 /// (the databases catalog, ADR-0276), so a caller that loaded this graph
139 /// from disk must first install its catalog into `other` with
140 /// [`Graph::install_databases_from`]; a plain share replaces the loaded
141 /// bundle wholesale and would discard the checkpoint's catalog. Not
142 /// public API.
143 #[doc(hidden)]
144 pub fn share_services_from(&mut self, other: &Graph) {
145 self.services = Arc::clone(&other.services);
146 }
147
148 /// Look up a registered procedure in the store's shared registry
149 /// (ADR-0116 §10). Read-only: the registry itself is interior-mutable,
150 /// so lookups never touch the bundle's `Arc` identity.
151 #[doc(hidden)]
152 pub fn procedure(&self, name: &str) -> Option<Procedure> {
153 self.services.procedures.get(name).map(|p| p.clone())
154 }
155
156 /// Whether `name` is registered in the store's shared procedure
157 /// registry (ADR-0116 §10).
158 #[doc(hidden)]
159 pub fn has_procedure(&self, name: &str) -> bool {
160 self.services.procedures.contains_key(name)
161 }
162
163 /// Register a procedure into the store's shared registry (ADR-0116
164 /// §10), visible to every namespace graph of the store. Interior
165 /// mutation only; the bundle's `Arc` identity is untouched.
166 #[doc(hidden)]
167 pub fn register_procedure(&self, proc: Procedure) {
168 self.services.procedures.insert(proc.name.clone(), proc);
169 }
170
171 /// The store-level LOAD CSV policy (ADR-0081, ADR-0116 §10), read-only.
172 pub(crate) fn csv_policy(&self) -> &CsvPolicy {
173 &self.services.csv_policy
174 }
175
176 /// Install the store's resolved preview set (ADR-0371, ADR-0378) on the
177 /// one bundle; same pre-share contract as [`Graph::configure_load_csv`].
178 #[doc(hidden)]
179 pub fn configure_preview(&mut self, preview: PreviewFeatures) {
180 self.services_mut().preview = preview;
181 }
182
183 /// The store-level preview set (ADR-0371, ADR-0378) the `SHOW
184 /// PROCEDURES` catalog and the `CALL` gate read.
185 #[doc(hidden)]
186 pub fn preview(&self) -> PreviewFeatures {
187 self.services.preview
188 }
189
190 /// Install the store's resolved pattern-engine switch (ADR-0519) on
191 /// the one bundle; same pre-share contract as
192 /// [`Graph::configure_preview`]. The open path resolves
193 /// `RELATE_PATTERN_ENGINE` once (the `RELATE_PARSER`
194 /// precedent's resolution site) and installs it here; a follower
195 /// keeps it across a reload with the rest of the store's
196 /// configuration.
197 #[doc(hidden)]
198 pub fn configure_pattern_engine(&mut self, engine: PatternEngine) {
199 self.services_mut().pattern_engine = engine;
200 }
201
202 /// The store-level pattern-engine switch (ADR-0519) the kernel
203 /// rungs' routing reads: [`PatternEngine::Auto`] on every store
204 /// opened without the diagnostic variable. Public since LABS-1117:
205 /// the EXPLAIN tree stamps it on every `MATCH` operator, so the one
206 /// static rung fact travels with the plan.
207 pub fn pattern_engine(&self) -> PatternEngine {
208 self.services.pattern_engine
209 }
210
211 /// The store-level databases catalog (ADR-0276; ADR-0116 §8
212 /// invariants), shared by every namespace graph of the store.
213 pub(crate) fn databases(&self) -> &DatabasesCatalog {
214 &self.services.databases
215 }
216}