Skip to main content

relate_engine_api/
query.rs

1//! Facade-facing query engine. One call per statement or per commit.
2//! Row-at-a-time execution stays inside the engine.
3
4use std::collections::BTreeMap;
5
6use relate_core::{CancelToken, Result, Value};
7use relate_planner::{LogicalPlan, PatternStats};
8
9use crate::catalog_image::SchemaSnapshot;
10use crate::export::{GraphExport, GraphImport};
11use crate::redo::RedoOp;
12
13/// A read the facade has already planned and admitted.
14pub struct ReadRequest<'a> {
15    pub snapshot_ts: u64,
16    pub schema: &'a SchemaSnapshot,
17    pub plan: &'a LogicalPlan,
18    pub params: &'a BTreeMap<String, Value>,
19    pub query_text: &'a str,
20    pub principal: Option<&'a str>,
21    pub cancel: Option<&'a CancelToken>,
22    pub ontology_document: Option<&'a str>,
23}
24
25/// A write the facade has already planned. `scratch` is the engine's
26/// private transaction state.
27pub struct WriteRequest<'a, S> {
28    pub scratch: &'a mut S,
29    pub plan: &'a LogicalPlan,
30    pub params: &'a BTreeMap<String, Value>,
31    pub query_text: &'a str,
32    pub principal: Option<&'a str>,
33    pub cancel: Option<&'a CancelToken>,
34    pub ontology_document: Option<&'a str>,
35}
36
37pub trait QueryEngine: GraphExport + GraphImport {
38    /// Private transaction scratch. Not shared across engines.
39    type Scratch;
40
41    /// Rows, counters, and effect. The concrete engine names the result
42    /// type so the facade does not depend on a per-row trait.
43    type Output;
44
45    fn gc_versions(&self);
46    fn snapshot_pinned(&self) -> bool;
47    fn apply_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()>;
48    fn validate_redo_group(&self, records: &[(&[RedoOp], u64)]) -> Result<()>;
49    /// Planner cost input for one statement. Estimates that read `params`
50    /// must not outlive this borrow.
51    fn pattern_stats<'a>(
52        &'a self,
53        params: &'a BTreeMap<String, Value>,
54    ) -> Box<dyn PatternStats + 'a>;
55    fn execute_read(&self, request: ReadRequest<'_>) -> Result<Self::Output>;
56    fn execute_write(&self, request: WriteRequest<'_, Self::Scratch>) -> Result<Self::Output>;
57    fn take_redo(&self, scratch: &mut Self::Scratch) -> Vec<RedoOp>;
58}