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