Skip to main content

relate_executor/
query_engine.rs

1//! The in-memory query engine: today's graph and executor behind
2//! [`relate_engine_api::QueryEngine`]. The facade holds this type. A later
3//! engine implements the same trait in its own crate.
4
5use std::cell::RefCell;
6use std::ops::{Deref, DerefMut};
7
8use relate_core::Result;
9use relate_engine_api::{
10    CheckpointDirty, CheckpointDirtyMarks, GraphExport, GraphImport, QueryEngine, ReadRequest,
11    WriteRequest,
12};
13use relate_planner::PatternStats;
14
15use crate::exec::{execute, execute_read_at, ExecRequest, QueryResult, ReadAdmission};
16use crate::graph::Graph;
17use crate::overlay::{ExecGraph, WriteScratch};
18use crate::redo::RedoOp;
19
20/// Execution plus the in-memory graph and index layout.
21pub struct InMemoryQueryEngine {
22    graph: Graph,
23}
24
25impl InMemoryQueryEngine {
26    pub fn new(graph: Graph) -> Self {
27        Self { graph }
28    }
29
30    /// The graph a snapshot codec checkpoints. Reload still opens a [`Graph`]
31    /// and wraps it here.
32    pub fn graph(&self) -> &Graph {
33        &self.graph
34    }
35
36    /// Run one planned statement on a view the caller already built.
37    /// Profile metrics and batch commits ride on `request`.
38    pub fn execute(&self, view: &ExecGraph<'_>, request: &ExecRequest<'_>) -> Result<QueryResult> {
39        execute(view, request)
40    }
41}
42
43impl Default for InMemoryQueryEngine {
44    fn default() -> Self {
45        Self::new(Graph::default())
46    }
47}
48
49impl Deref for InMemoryQueryEngine {
50    type Target = Graph;
51
52    fn deref(&self) -> &Graph {
53        &self.graph
54    }
55}
56
57impl DerefMut for InMemoryQueryEngine {
58    fn deref_mut(&mut self) -> &mut Graph {
59        &mut self.graph
60    }
61}
62
63impl QueryEngine for InMemoryQueryEngine {
64    type Scratch = RefCell<WriteScratch>;
65    type Output = QueryResult;
66
67    fn gc_versions(&self) {
68        self.graph.gc_versions();
69    }
70
71    fn snapshot_pinned(&self) -> bool {
72        self.graph.snapshot_pinned()
73    }
74
75    fn apply_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()> {
76        self.graph.apply_redo(ops, commit_ts)
77    }
78
79    fn validate_redo_group(&self, records: &[(&[RedoOp], u64)]) -> Result<()> {
80        self.graph
81            .validate_redo_group_applicability(records.iter().copied())
82    }
83
84    fn pattern_stats<'a>(
85        &'a self,
86        params: &'a std::collections::BTreeMap<String, relate_core::Value>,
87    ) -> Box<dyn PatternStats + 'a> {
88        Box::new(crate::exec::graph_stats::GraphStats::new(
89            &self.graph,
90            params,
91        ))
92    }
93
94    fn execute_read(&self, request: ReadRequest<'_>) -> Result<QueryResult> {
95        execute_read_at(&ReadAdmission {
96            graph: &self.graph,
97            snapshot_ts: request.snapshot_ts,
98            schema: request.schema,
99            request: ExecRequest {
100                plan: request.plan,
101                params: request.params,
102                query_text: request.query_text,
103                principal: request.principal,
104                batch_commit: None,
105                plan_metrics: None,
106            },
107            cancel: request.cancel,
108            ontology_document: request.ontology_document,
109        })
110    }
111
112    fn execute_write(&self, request: WriteRequest<'_, Self::Scratch>) -> Result<QueryResult> {
113        let view = ExecGraph::write(&self.graph, request.scratch);
114        self.execute(
115            &view,
116            &ExecRequest {
117                plan: request.plan,
118                params: request.params,
119                query_text: request.query_text,
120                principal: request.principal,
121                batch_commit: None,
122                plan_metrics: None,
123            },
124        )
125    }
126
127    fn take_redo(&self, scratch: &mut Self::Scratch) -> Vec<RedoOp> {
128        scratch.get_mut().take_redo()
129    }
130}
131
132impl GraphExport for InMemoryQueryEngine {
133    fn commit_ts(&self) -> u64 {
134        Graph::commit_ts(&self.graph)
135    }
136    fn next_node_id(&self) -> i64 {
137        Graph::next_node_id(&self.graph)
138    }
139    fn next_rel_id(&self) -> i64 {
140        Graph::next_rel_id(&self.graph)
141    }
142    fn live_node_count(&self) -> usize {
143        Graph::live_node_count(&self.graph)
144    }
145    fn checkpoint_dirty(&self) -> CheckpointDirty {
146        CheckpointDirty {
147            nodes: Graph::dirty_node_ids(&self.graph),
148            rels: Graph::dirty_rel_ids(&self.graph),
149        }
150    }
151    fn all_node_ids_at(&self, ts: u64) -> Vec<relate_core::NodeId> {
152        Graph::all_node_ids_at(&self.graph, ts)
153    }
154    fn all_rel_ids_at(&self, ts: u64) -> Vec<relate_core::RelId> {
155        Graph::all_rel_ids_at(&self.graph, ts)
156    }
157    fn node_at(&self, id: relate_core::NodeId, ts: u64) -> Option<relate_core::Node> {
158        Graph::node_at(&self.graph, id, ts)
159    }
160    fn rel_at(&self, id: relate_core::RelId, ts: u64) -> Option<relate_core::Relationship> {
161        Graph::rel_at(&self.graph, id, ts)
162    }
163    fn node_checkpoint_bytes(&self, id: relate_core::NodeId, ts: u64) -> Result<Option<u64>> {
164        Graph::node_checkpoint_bytes(&self.graph, id, ts)
165    }
166    fn rel_checkpoint_bytes(&self, id: relate_core::RelId, ts: u64) -> Result<Option<u64>> {
167        Graph::rel_checkpoint_bytes(&self.graph, id, ts)
168    }
169    fn named_indexes(&self) -> Vec<relate_catalog::IndexDef> {
170        Graph::named_indexes(&self.graph)
171    }
172    fn named_constraints(&self) -> Vec<relate_catalog::ConstraintDef> {
173        Graph::named_constraints(&self.graph)
174    }
175    fn checkpoint_theater_for_write(&self) -> Result<relate_engine_api::CheckpointTheater> {
176        Graph::checkpoint_theater_for_write(&self.graph)
177    }
178    fn graph_type_spec(&self) -> Result<Option<String>> {
179        Graph::graph_type_spec(&self.graph)
180    }
181    fn catalog_ddl(&self) -> Vec<String> {
182        crate::catalog_create_statements(&self.graph)
183    }
184}
185
186impl CheckpointDirtyMarks for InMemoryQueryEngine {
187    fn take_checkpoint_dirty(&self) -> CheckpointDirty {
188        Graph::take_checkpoint_dirty(&self.graph)
189    }
190    fn merge_checkpoint_dirty(&self, dirty: &CheckpointDirty) {
191        Graph::merge_checkpoint_dirty(&self.graph, dirty)
192    }
193}
194
195impl GraphImport for InMemoryQueryEngine {
196    fn set_next_node_id(&self, id: i64) {
197        Graph::set_next_node_id(&self.graph, id)
198    }
199    fn set_next_rel_id(&self, id: i64) {
200        Graph::set_next_rel_id(&self.graph, id)
201    }
202    fn set_commit_ts(&self, ts: u64) {
203        Graph::set_commit_ts(&self.graph, ts)
204    }
205    fn install_named_index(&self, index: relate_catalog::IndexDef) -> Result<()> {
206        Graph::install_named_index(&self.graph, index)
207    }
208    fn install_named_constraint(&self, constraint: relate_catalog::ConstraintDef) -> Result<()> {
209        Graph::install_named_constraint(&self.graph, constraint)
210    }
211    fn restore_checkpoint_theater(
212        &self,
213        theater: relate_engine_api::CheckpointTheater,
214    ) -> Result<()> {
215        Graph::restore_checkpoint_theater(&self.graph, theater)
216    }
217    fn mark_legacy_unenforced(&self) {
218        Graph::mark_legacy_unenforced(&self.graph)
219    }
220    fn finish_restore(&self) {
221        Graph::finish_restore(&self.graph)
222    }
223    fn load_checkpoint_nodes(&self, nodes: &[relate_core::Node]) -> Result<()> {
224        Graph::load_checkpoint_nodes(&self.graph, nodes)
225    }
226    fn load_checkpoint_rels(&self, rels: &[relate_core::Relationship]) -> Result<()> {
227        Graph::load_checkpoint_rels(&self.graph, rels)
228    }
229    fn apply_replayed_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()> {
230        Graph::apply_replayed_redo(&self.graph, ops, commit_ts)
231    }
232    fn rebuild_indexes(&self) {
233        Graph::rebuild_indexes(&self.graph)
234    }
235    fn restore_graph_type_spec(&self, spec: Option<&str>) -> Result<()> {
236        Graph::restore_graph_type_spec(&self.graph, spec)
237    }
238    fn restore_legacy_enforced_constraints(
239        &self,
240        constraints: Vec<relate_model::Constraint>,
241    ) -> Result<()> {
242        Graph::restore_legacy_enforced_constraints(&self.graph, constraints)
243    }
244}