Skip to main content

relate_engine_api/
export.rs

1//! What a snapshot codec may ask of a query engine, and what recovery may
2//! put back. Codecs never name a concrete graph.
3
4use std::collections::HashSet;
5
6use relate_catalog::{ConstraintDef, IndexDef};
7use relate_core::{Node, NodeId, RelId, Relationship, Result};
8use relate_model::Constraint;
9
10use crate::catalog_image::CheckpointTheater;
11use crate::redo::RedoOp;
12
13pub trait GraphExport {
14    fn commit_ts(&self) -> u64;
15    fn next_node_id(&self) -> i64;
16    fn next_rel_id(&self) -> i64;
17    fn live_node_count(&self) -> usize;
18    fn dirty_node_ids(&self) -> HashSet<NodeId>;
19    fn dirty_rel_ids(&self) -> HashSet<RelId>;
20    fn all_node_ids_at(&self, ts: u64) -> Vec<NodeId>;
21    fn all_rel_ids_at(&self, ts: u64) -> Vec<RelId>;
22    fn node_at(&self, id: NodeId, ts: u64) -> Option<Node>;
23    fn rel_at(&self, id: RelId, ts: u64) -> Option<Relationship>;
24    fn node_checkpoint_bytes(&self, id: NodeId, ts: u64) -> Result<Option<u64>>;
25    fn rel_checkpoint_bytes(&self, id: RelId, ts: u64) -> Result<Option<u64>>;
26    fn named_indexes(&self) -> Vec<IndexDef>;
27    fn named_constraints(&self) -> Vec<ConstraintDef>;
28    fn checkpoint_theater_for_write(&self) -> Result<CheckpointTheater>;
29    fn graph_type_spec(&self) -> Result<Option<String>>;
30    fn clear_checkpoint_dirty(&self);
31    /// SHOW-style catalog DDL the schema mirror writes beside a checkpoint.
32    fn catalog_ddl(&self) -> Vec<String>;
33}
34
35/// The fill side of a graph: what a codec installs checkpoint rows through.
36/// `Sync` because the restore installs one worker's rows per core — a
37/// slice's install is the same interior-mutable call the single-threaded
38/// path made.
39pub trait GraphImport: GraphExport + Sync {
40    fn set_next_node_id(&self, id: i64);
41    fn set_next_rel_id(&self, id: i64);
42    fn set_commit_ts(&self, ts: u64);
43    fn install_named_index(&self, index: IndexDef) -> Result<()>;
44    fn install_named_constraint(&self, constraint: ConstraintDef) -> Result<()>;
45    fn restore_checkpoint_theater(&self, theater: CheckpointTheater) -> Result<()>;
46    fn mark_legacy_unenforced(&self);
47    fn finish_restore(&self);
48    /// Install a slice of checkpoint nodes — one decoded batch, a
49    /// window-disjoint run of ids. The installer owns the row-id check:
50    /// every id in the slice is validated here, so a caller does not
51    /// repeat it, and an earlier row for an id is replaced by the later one.
52    fn load_checkpoint_nodes(&self, nodes: &[Node]) -> Result<()>;
53    /// Relationship sibling of [`Self::load_checkpoint_nodes`], with the
54    /// same id obligation over a row's id and both endpoints.
55    fn load_checkpoint_rels(&self, rels: &[Relationship]) -> Result<()>;
56    /// Restore one node: the one-element slice of
57    /// [`Self::load_checkpoint_nodes`].
58    fn load_checkpoint_node(&self, node: Node) -> Result<()> {
59        self.load_checkpoint_nodes(std::slice::from_ref(&node))
60    }
61    /// Restore one relationship: the one-element slice of
62    /// [`Self::load_checkpoint_rels`].
63    fn load_checkpoint_rel(&self, rel: Relationship) -> Result<()> {
64        self.load_checkpoint_rels(std::slice::from_ref(&rel))
65    }
66    fn apply_replayed_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()>;
67    fn rebuild_indexes(&self);
68    fn restore_graph_type_spec(&self, spec: Option<&str>) -> Result<()>;
69    fn restore_legacy_enforced_constraints(&self, constraints: Vec<Constraint>) -> Result<()>;
70}
71
72/// A graph a codec can both read and fill. `Default` builds the empty
73/// engine recovery starts from.
74pub trait SnapshotGraph: GraphImport + Default {}
75
76impl<T: GraphImport + Default> SnapshotGraph for T {}