Skip to main content

relate_engine_api/
export.rs

1//! What a checkpoint 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    /// The ids changed since the previous take — one carrier
19    /// ([`CheckpointDirty`]) for the pair, so an implementor spells the
20    /// node/rel split once.
21    fn checkpoint_dirty(&self) -> CheckpointDirty;
22    fn all_node_ids_at(&self, ts: u64) -> Vec<NodeId>;
23    fn all_rel_ids_at(&self, ts: u64) -> Vec<RelId>;
24    fn node_at(&self, id: NodeId, ts: u64) -> Option<Node>;
25    fn rel_at(&self, id: RelId, ts: u64) -> Option<Relationship>;
26    fn node_checkpoint_bytes(&self, id: NodeId, ts: u64) -> Result<Option<u64>>;
27    fn rel_checkpoint_bytes(&self, id: RelId, ts: u64) -> Result<Option<u64>>;
28    fn named_indexes(&self) -> Vec<IndexDef>;
29    fn named_constraints(&self) -> Vec<ConstraintDef>;
30    fn checkpoint_theater_for_write(&self) -> Result<CheckpointTheater>;
31    fn graph_type_spec(&self) -> Result<Option<String>>;
32    /// SHOW-style catalog DDL the schema mirror writes beside a checkpoint.
33    fn catalog_ddl(&self) -> Vec<String>;
34}
35
36/// The checkpoint-dirty marks a graph with live write state carries: a
37/// checkpoint's cut takes them, a failed dump hands them back. Separate
38/// from [`GraphExport`], so a read-only view over a frozen cut — the
39/// storage crate's checkpoint cut — can neither clear nor restore the
40/// live marks.
41pub trait CheckpointDirtyMarks: GraphExport {
42    /// Hand over the ids changed since the previous take and clear them:
43    /// the checkpoint's cut takes the marks it is about to dump.
44    fn take_checkpoint_dirty(&self) -> CheckpointDirty;
45    /// Hand back a failed dump's marks, so the next checkpoint covers them.
46    fn merge_checkpoint_dirty(&self, dirty: &CheckpointDirty);
47}
48
49/// The ids a graph's checkpoint-dirty tracking has marked since the
50/// previous take (ADR-0022).
51#[derive(Clone, Default)]
52pub struct CheckpointDirty {
53    pub nodes: HashSet<NodeId>,
54    pub rels: HashSet<RelId>,
55}
56
57/// The fill side of a graph: what a codec installs checkpoint rows through.
58/// `Sync` because the restore installs one worker's rows per core — a
59/// slice's install is the same interior-mutable call the single-threaded
60/// path made.
61pub trait GraphImport: GraphExport + Sync {
62    fn set_next_node_id(&self, id: i64);
63    fn set_next_rel_id(&self, id: i64);
64    fn set_commit_ts(&self, ts: u64);
65    fn install_named_index(&self, index: IndexDef) -> Result<()>;
66    fn install_named_constraint(&self, constraint: ConstraintDef) -> Result<()>;
67    fn restore_checkpoint_theater(&self, theater: CheckpointTheater) -> Result<()>;
68    fn mark_legacy_unenforced(&self);
69    fn finish_restore(&self);
70    /// Install a slice of checkpoint nodes — one decoded batch, a
71    /// window-disjoint run of ids. The installer owns the row-id check:
72    /// every id in the slice is validated here, so a caller does not
73    /// repeat it, and an earlier row for an id is replaced by the later one.
74    fn load_checkpoint_nodes(&self, nodes: &[Node]) -> Result<()>;
75    /// Relationship sibling of [`Self::load_checkpoint_nodes`], with the
76    /// same id obligation over a row's id and both endpoints.
77    fn load_checkpoint_rels(&self, rels: &[Relationship]) -> Result<()>;
78    /// Restore one node: the one-element slice of
79    /// [`Self::load_checkpoint_nodes`].
80    fn load_checkpoint_node(&self, node: Node) -> Result<()> {
81        self.load_checkpoint_nodes(std::slice::from_ref(&node))
82    }
83    /// Restore one relationship: the one-element slice of
84    /// [`Self::load_checkpoint_rels`].
85    fn load_checkpoint_rel(&self, rel: Relationship) -> Result<()> {
86        self.load_checkpoint_rels(std::slice::from_ref(&rel))
87    }
88    fn apply_replayed_redo(&self, ops: Vec<RedoOp>, commit_ts: u64) -> Result<()>;
89    fn rebuild_indexes(&self);
90    fn restore_graph_type_spec(&self, spec: Option<&str>) -> Result<()>;
91    fn restore_legacy_enforced_constraints(&self, constraints: Vec<Constraint>) -> Result<()>;
92}
93
94/// A graph a codec can both read and fill. `Default` builds the empty
95/// engine recovery starts from.
96pub trait SnapshotGraph: GraphImport + Default {}
97
98impl<T: GraphImport + Default> SnapshotGraph for T {}