Skip to main content

relate_engine_api/
catalog_image.rs

1//! Catalog image a snapshot carries, shared by every query engine.
2//! Derivation and admission behavior stay with the engine that owns the live graph.
3
4use std::collections::BTreeMap;
5
6use relate_catalog::{ConstraintDef, IndexDef};
7use relate_core::{Namespace, Value};
8use serde::{Deserialize, Serialize};
9
10use crate::redo::IndexDetailSpec;
11
12/// The five committed per-graph SHOW-metadata maps that make up a checkpoint
13/// theater. Composed by the live graph state (a plain `TheaterData`), the
14/// serialized checkpoint image (`CheckpointTheater`'s flattened `data`) and
15/// the read-admission snapshot (`ShowSchema`'s `data`). The databases table
16/// is NOT here: it is the store-level databases table (ADR-0276).
17#[derive(Debug, Clone, Default, Serialize, Deserialize)]
18pub struct TheaterData {
19    pub index_details: BTreeMap<String, IndexDetailSpec>,
20    pub index_options: BTreeMap<String, BTreeMap<String, Value>>,
21    pub graph_types: BTreeMap<String, crate::graph_type::GraphTypeSpec>,
22    pub current_graph_type: crate::graph_type::GraphTypeSpec,
23    pub derived_constraints: BTreeMap<String, relate_model::Constraint>,
24}
25
26/// One namespace's entry in the store-level databases catalog (ADR-0116
27/// §8, ADR-0276): the settings `SHOW DATABASES` reports and `ALTER
28/// DATABASE ... SET DEFAULT LANGUAGE` rewrites. Serde keeps the
29/// historical two-element array shape (`[language, composite]`) so
30/// existing checkpoint images keep loading.
31#[doc(hidden)]
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
33#[serde(from = "(u32, bool)", into = "(u32, bool)")]
34pub struct DatabaseEntry {
35    /// Default Cypher language version for the namespace (`5` or `25`).
36    pub default_language: u32,
37    /// Whether the namespace is a composite database.
38    pub composite: bool,
39}
40
41impl From<(u32, bool)> for DatabaseEntry {
42    fn from((default_language, composite): (u32, bool)) -> Self {
43        Self {
44            default_language,
45            composite,
46        }
47    }
48}
49
50impl From<DatabaseEntry> for (u32, bool) {
51    fn from(entry: DatabaseEntry) -> Self {
52        (entry.default_language, entry.composite)
53    }
54}
55
56/// ADR-0116 §8: record a namespace's default language in `map`. The redo
57/// name is the serde/replay boundary: it matches the fixed namespaces
58/// case-insensitively and is stored under the typed key; entries for any
59/// other name (written by earlier `CREATE DATABASE` calls or an older
60/// binary) are dropped here.
61pub fn map_put_database(
62    map: &mut BTreeMap<Namespace, DatabaseEntry>,
63    name: &str,
64    language: u32,
65    composite: bool,
66) {
67    if let Some(ns) = Namespace::parse(name) {
68        map.insert(
69            ns,
70            DatabaseEntry {
71                default_language: language,
72                composite,
73            },
74        );
75    }
76}
77
78/// Typed committed SHOW metadata stored with a checkpoint's named catalog.
79/// Missing images in older snapshots use the historical empty theater.
80#[doc(hidden)]
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct CheckpointTheater {
83    /// Store-level databases table at the checkpoint cut (ADR-0276; ADR-0116
84    /// §8 invariants).
85    /// Declared first so the serialized key order matches the historical
86    /// flattened layout, where `databases` led `TheaterData`.
87    pub databases: BTreeMap<String, DatabaseEntry>,
88    #[serde(flatten)]
89    pub data: TheaterData,
90    /// Exact catalog definitions imported without known label enforcement.
91    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
92    pub legacy_unknown_constraints: BTreeMap<String, ConstraintDef>,
93}
94
95impl CheckpointTheater {
96    /// Enforced label of a named constraint, if present.
97    #[doc(hidden)]
98    pub fn enforced_label(&self, name: &str) -> Option<&str> {
99        self.data
100            .derived_constraints
101            .get(name)
102            .and_then(|constraint| constraint.enforced_label.as_deref())
103    }
104
105    /// Catalog detail of a named index, if present.
106    #[doc(hidden)]
107    pub fn index_detail(&self, name: &str) -> Option<&IndexDetailSpec> {
108        self.data.index_details.get(name)
109    }
110}
111
112/// Catalog facts captured alongside an entity pin at read admission. The
113/// caller serializes capture with commit publication; execution needs no
114/// publication lock and never consults newer named schema.
115pub struct SchemaSnapshot {
116    pub indexes: Vec<IndexDef>,
117    pub constraints: Vec<ConstraintDef>,
118    pub show: ShowSchema,
119}
120
121/// The read-admission SHOW metadata: the store-level databases table plus
122/// the graph's committed theater maps, captured at one admission point.
123/// Composes [`TheaterData`] the way [`CheckpointTheater`] does, so the five
124/// maps exist once. Speculative catalog redo is applied by the query
125/// engine that owns derivation; committed state changes only when that
126/// engine applies redo.
127#[derive(Debug, Clone)]
128pub struct ShowSchema {
129    pub databases: BTreeMap<Namespace, DatabaseEntry>,
130    pub data: TheaterData,
131}
132
133impl ShowSchema {
134    /// Capture the committed SHOW metadata at one admission point: the
135    /// store-level databases table plus this graph's theater maps.
136    pub fn capture(databases: BTreeMap<Namespace, DatabaseEntry>, theater: &TheaterData) -> Self {
137        Self {
138            databases,
139            data: theater.clone(),
140        }
141    }
142}