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. A third member
34/// walks the line: the nested-evaluation budget is plain data in every
35/// build (never production configuration beyond its constant
36/// initializer, never reloaded by a follower), whose only writer is a
37/// `cfg(test)` setter — the budget-bomb seam, absent from every
38/// production build's write surface.
39#[derive(Debug)]
40pub(crate) struct StoreServices {
41 /// Registered procedures (TCK / user). Store-level state (ADR-0116 §10).
42 pub(crate) procedures: DashMap<String, Procedure>,
43 /// LOAD CSV capability (ADR-0081). The policy is store-level
44 /// (ADR-0116 §10), so it lives in the shared bundle.
45 pub(crate) csv_policy: CsvPolicy,
46 /// The store's resolved preview set (ADR-0371, ADR-0378), installed at
47 /// open and on follow reload; read by the catalog and the `CALL` gate.
48 pub(crate) preview: PreviewFeatures,
49 /// The store-level databases catalog (ADR-0276; ADR-0116 §8
50 /// invariants): the three fixed
51 /// namespaces' settings. One table for the whole store — per-graph
52 /// copies diverged when `ALTER DATABASE` wrote the `neo4j` graph's
53 /// theater while a `system` session read its own statically seeded one.
54 pub(crate) databases: DatabasesCatalog,
55 /// The per-allocation expression cap (ADR-0463) every namespace graph
56 /// of the store enforces. `None` is the explicit opt-out; the default is
57 /// [`crate::memory::EXPRESSION_BUDGET_BYTES`].
58 pub(crate) expression_memory_limit: Option<u64>,
59 /// The shared memo live-bytes budget (ADR-0657) every statement of the
60 /// store enforces, in bytes (the open-time setting is whole MiB).
61 /// `0` disables the budgeted memo tables.
62 pub(crate) memo_budget_bytes: u64,
63 /// The store's diagnostic pattern-engine switch (ADR-0519), resolved
64 /// once at open from `RELATE_PATTERN_ENGINE` and read by the
65 /// kernel rungs' routing. Configuration the store owns, like the
66 /// preview set; the default [`PatternEngine::Auto`] is today's
67 /// routing exactly.
68 pub(crate) pattern_engine: PatternEngine,
69 /// The per-statement nested-evaluation budget (ADR-0395, 1 000 000
70 /// since ADR-0670) — plain data in every build, whose only
71 /// production initializer is the published constant; the only writer
72 /// is the `cfg(test)` `set_nested_eval_budget` (the budget bombs).
73 /// The no-knob rule holds by write-path: no production code reads
74 /// anything but the default, and no process-global or thread-local
75 /// channel exists.
76 pub(crate) nested_eval_budget: u64,
77}
78
79impl Default for StoreServices {
80 fn default() -> Self {
81 Self {
82 procedures: DashMap::default(),
83 csv_policy: CsvPolicy::default(),
84 preview: PreviewFeatures::default(),
85 databases: DatabasesCatalog::default(),
86 expression_memory_limit: Some(crate::memory::EXPRESSION_BUDGET_BYTES),
87 memo_budget_bytes: crate::memory::memo_budget_bytes(
88 crate::memory::DEFAULT_MEMO_BUDGET_MIB,
89 ),
90 pattern_engine: PatternEngine::Auto,
91 nested_eval_budget: crate::memory::NESTED_EVAL_BUDGET,
92 }
93 }
94}
95
96impl Graph {
97 /// The store's one `StoreServices` bundle, mutable only before it is
98 /// shared with another graph. Both open-time installers
99 /// ([`Graph::configure_load_csv`], [`Graph::configure_preview`]) run
100 /// through this one accessor, so the pre-share contract and its panic
101 /// are stated once: after [`Graph::share_services_from`] the bundle's
102 /// `Arc` identity is the store's one-bundle invariant (ADR-0116 §10),
103 /// and rebuilding it would silently detach this graph from every other
104 /// namespace.
105 fn services_mut(&mut self) -> &mut StoreServices {
106 Arc::get_mut(&mut self.services).expect(
107 "the StoreServices bundle must be configured before \
108 share_services_from: it is already shared (ADR-0116 §10)",
109 )
110 }
111
112 /// Install the per-allocation expression cap (ADR-0463) before the
113 /// services bundle is shared. `None` disables the cap; the default is
114 /// [`crate::memory::EXPRESSION_BUDGET_BYTES`].
115 #[doc(hidden)]
116 pub fn configure_expression_memory_limit(&mut self, limit: Option<u64>) {
117 self.services_mut().expression_memory_limit = limit;
118 }
119
120 /// Install the shared memo budget (ADR-0657), in whole MiB, before the
121 /// services bundle is shared. `0` disables the budgeted memo tables.
122 #[doc(hidden)]
123 pub fn configure_expression_memo_budget(&mut self, mib: u64) {
124 self.services_mut().memo_budget_bytes = crate::memory::memo_budget_bytes(mib);
125 }
126
127 /// The configured shared memo budget in bytes (ADR-0657).
128 pub(crate) fn memo_budget_bytes(&self) -> u64 {
129 self.services.memo_budget_bytes
130 }
131
132 /// The configured per-allocation expression cap (`None` = no cap).
133 pub(crate) fn expression_memory_limit(&self) -> Option<u64> {
134 self.services.expression_memory_limit
135 }
136
137 /// Install a validated local CSV capability before sharing the graph with connections.
138 ///
139 /// Mutates the store's single `StoreServices` bundle in place; panics
140 /// if the bundle is already shared via [`Graph::share_services_from`]
141 /// (see `Graph::services_mut`).
142 #[doc(hidden)]
143 pub fn configure_load_csv(&mut self, policy: CsvPolicy) {
144 self.services_mut().csv_policy = policy;
145 }
146
147 /// Install the databases catalog a checkpoint loaded on `loaded` into
148 /// this graph's store-level bundle (ADR-0276), so a replacement graph
149 /// can then adopt that bundle without losing durable state.
150 ///
151 /// The catalog is the one durable member of [`StoreServices`]: the
152 /// store's configuration (procedure registry, CSV policy, preview set)
153 /// belongs to the surviving bundle, while `open_store_readonly` restores
154 /// this table into the fresh graph's own bundle through
155 /// [`Graph::restore_checkpoint_theater`]. The follow reload
156 /// (`follow::reload`) calls this before [`Graph::share_services_from`];
157 /// the catalog's interior mutability lets it write through `&Graph`
158 /// without rebuilding the bundle's `Arc`. Not public API.
159 #[doc(hidden)]
160 pub fn install_databases_from(&self, loaded: &Graph) {
161 self.databases().install(loaded.databases().snapshot());
162 }
163
164 /// Point this graph at the store-level service bundle of `other`
165 /// (ADR-0116 §10), so every namespace of a store shares one procedure
166 /// registry, one LOAD CSV policy, one resolved preview set and one
167 /// databases catalog. Called when the store creates or swaps a
168 /// namespace slot, and by the follow reload (`follow::reload`). The
169 /// bundle mixes configuration the store owns (procedure registry, CSV
170 /// policy, preview set) with durable state restored from a checkpoint
171 /// (the databases catalog, ADR-0276), so a caller that loaded this graph
172 /// from disk must first install its catalog into `other` with
173 /// [`Graph::install_databases_from`]; a plain share replaces the loaded
174 /// bundle wholesale and would discard the checkpoint's catalog. Not
175 /// public API.
176 #[doc(hidden)]
177 pub fn share_services_from(&mut self, other: &Graph) {
178 self.services = Arc::clone(&other.services);
179 }
180
181 /// Look up a registered procedure in the store's shared registry
182 /// (ADR-0116 §10). Read-only: the registry itself is interior-mutable,
183 /// so lookups never touch the bundle's `Arc` identity.
184 #[doc(hidden)]
185 pub fn procedure(&self, name: &str) -> Option<Procedure> {
186 self.services.procedures.get(name).map(|p| p.clone())
187 }
188
189 /// Whether `name` is registered in the store's shared procedure
190 /// registry (ADR-0116 §10).
191 #[doc(hidden)]
192 pub fn has_procedure(&self, name: &str) -> bool {
193 self.services.procedures.contains_key(name)
194 }
195
196 /// Register a procedure into the store's shared registry (ADR-0116
197 /// §10), visible to every namespace graph of the store. Interior
198 /// mutation only; the bundle's `Arc` identity is untouched.
199 #[doc(hidden)]
200 pub fn register_procedure(&self, proc: Procedure) {
201 self.services.procedures.insert(proc.name.clone(), proc);
202 }
203
204 /// The store-level LOAD CSV policy (ADR-0081, ADR-0116 §10), read-only.
205 pub(crate) fn csv_policy(&self) -> &CsvPolicy {
206 &self.services.csv_policy
207 }
208
209 /// Install the store's resolved preview set (ADR-0371, ADR-0378) on the
210 /// one bundle; same pre-share contract as [`Graph::configure_load_csv`].
211 #[doc(hidden)]
212 pub fn configure_preview(&mut self, preview: PreviewFeatures) {
213 self.services_mut().preview = preview;
214 }
215
216 /// The store-level preview set (ADR-0371, ADR-0378) the `SHOW
217 /// PROCEDURES` catalog and the `CALL` gate read.
218 #[doc(hidden)]
219 pub fn preview(&self) -> PreviewFeatures {
220 self.services.preview
221 }
222
223 /// Install the store's resolved pattern-engine switch (ADR-0519) on
224 /// the one bundle; same pre-share contract as
225 /// [`Graph::configure_preview`]. The open path resolves
226 /// `RELATE_PATTERN_ENGINE` once (the `RELATE_PARSER`
227 /// precedent's resolution site) and installs it here; a follower
228 /// keeps it across a reload with the rest of the store's
229 /// configuration.
230 #[doc(hidden)]
231 pub fn configure_pattern_engine(&mut self, engine: PatternEngine) {
232 self.services_mut().pattern_engine = engine;
233 }
234
235 /// The store's nested-evaluation budget (ADR-0395/ADR-0670) — the
236 /// field's contract is stated once, at the field.
237 pub(crate) fn nested_eval_budget(&self) -> u64 {
238 self.services.nested_eval_budget
239 }
240
241 /// The budget field's only writer (`cfg(test)` only): the budget
242 /// bombs lower their own store's budget. See the field's contract
243 /// for the no-knob rule it preserves.
244 #[cfg(test)]
245 pub(crate) fn set_nested_eval_budget(&mut self, budget: u64) {
246 self.services_mut().nested_eval_budget = budget;
247 }
248
249 /// The store-level pattern-engine switch (ADR-0519) the kernel
250 /// rungs' routing reads: [`PatternEngine::Auto`] on every store
251 /// opened without the diagnostic variable. Public since LABS-1117:
252 /// the EXPLAIN tree stamps it on every `MATCH` operator, so the one
253 /// static rung fact travels with the plan.
254 pub fn pattern_engine(&self) -> PatternEngine {
255 self.services.pattern_engine
256 }
257
258 /// The store-level databases catalog (ADR-0276; ADR-0116 §8
259 /// invariants), shared by every namespace graph of the store.
260 pub(crate) fn databases(&self) -> &DatabasesCatalog {
261 &self.services.databases
262 }
263}