Skip to main content

oxgraph_db/
schema.rs

1//! Declarative catalog schema.
2//!
3//! Declare a store's roles, labels, relation types, typed property keys,
4//! equality indexes, and graph projections by name ONCE as a [`Schema`], then
5//! apply it idempotently with [`Writer::apply_schema`](crate::Writer::apply_schema)
6//! (register-or-get) or resolve an already-bootstrapped store with
7//! [`Db::bind`](crate::Db::bind). Both return a [`Bound`] handle bag whose typed
8//! getters hand back [`Key<T>`](crate::Key)/[`EqualityIndex<T>`](crate::EqualityIndex)
9//! and plain id newtypes — so a consumer never threads name→id maps by hand and a
10//! name typo is a typed [`DbError::UnknownName`].
11
12use std::collections::BTreeMap;
13
14use crate::{
15    DbError, IndexId, LabelId, ProjectionId, PropertyFamily, PropertyKeyId, PropertyType,
16    RelationTypeId, RoleId,
17    typed::{EqualityIndex, Key, ValueType},
18};
19
20/// A binary graph-projection declaration over a set of relation types.
21///
22/// # Performance
23///
24/// Cloning is `O(relation-type count + name lengths)`.
25#[derive(Clone, Debug)]
26pub struct GraphProjectionSpec {
27    /// Projection name.
28    pub(crate) name: String,
29    /// Relation-type names whose edges the projection traverses.
30    pub(crate) relation_types: Vec<String>,
31    /// Source incidence role name.
32    pub(crate) source_role: String,
33    /// Target incidence role name.
34    pub(crate) target_role: String,
35}
36
37/// A declarative catalog schema, applied once to obtain a [`Bound`] handle bag.
38///
39/// Built fluently; declaration order is preserved (and irrelevant to the result).
40///
41/// # Performance
42///
43/// Each builder method is `O(1)` amortized; cloning is `O(declared item count)`.
44#[derive(Clone, Debug, Default)]
45pub struct Schema {
46    /// Declared role names.
47    pub(crate) roles: Vec<String>,
48    /// Declared label names.
49    pub(crate) labels: Vec<String>,
50    /// Declared relation-type names.
51    pub(crate) relation_types: Vec<String>,
52    /// Declared property keys: `(name, family, value type)`.
53    pub(crate) keys: Vec<(String, PropertyFamily, PropertyType)>,
54    /// Declared equality indexes: `(index name, indexed key name)`.
55    pub(crate) equality_indexes: Vec<(String, String)>,
56    /// Declared graph projections.
57    pub(crate) graph_projections: Vec<GraphProjectionSpec>,
58}
59
60impl Schema {
61    /// Starts an empty schema.
62    ///
63    /// # Performance
64    ///
65    /// This function is `O(1)`.
66    #[must_use]
67    pub fn new() -> Self {
68        Self::default()
69    }
70
71    /// Declares an incidence role.
72    ///
73    /// # Performance
74    ///
75    /// This method is `O(name length)`.
76    #[must_use]
77    pub fn role(mut self, name: &str) -> Self {
78        self.roles.push(name.to_owned());
79        self
80    }
81
82    /// Declares an element/relation label.
83    ///
84    /// # Performance
85    ///
86    /// This method is `O(name length)`.
87    #[must_use]
88    pub fn label(mut self, name: &str) -> Self {
89        self.labels.push(name.to_owned());
90        self
91    }
92
93    /// Declares a relation type.
94    ///
95    /// # Performance
96    ///
97    /// This method is `O(name length)`.
98    #[must_use]
99    pub fn relation_type(mut self, name: &str) -> Self {
100        self.relation_types.push(name.to_owned());
101        self
102    }
103
104    /// Declares a typed property key in `family` whose value type is `T`.
105    ///
106    /// # Performance
107    ///
108    /// This method is `O(name length)`.
109    #[must_use]
110    pub fn key<T: ValueType>(mut self, name: &str, family: PropertyFamily) -> Self {
111        self.keys.push((name.to_owned(), family, T::TYPE));
112        self
113    }
114
115    /// Declares an equality index named `name` over the property key `key`.
116    ///
117    /// # Performance
118    ///
119    /// This method is `O(name lengths)`.
120    #[must_use]
121    pub fn equality_index(mut self, name: &str, key: &str) -> Self {
122        self.equality_indexes
123            .push((name.to_owned(), key.to_owned()));
124        self
125    }
126
127    /// Declares a binary graph projection over `relation_types`, traversing from
128    /// `source_role` to `target_role`.
129    ///
130    /// # Performance
131    ///
132    /// This method is `O(relation-type count + name lengths)`.
133    #[must_use]
134    pub fn graph_projection(
135        mut self,
136        name: &str,
137        relation_types: &[&str],
138        source_role: &str,
139        target_role: &str,
140    ) -> Self {
141        self.graph_projections.push(GraphProjectionSpec {
142            name: name.to_owned(),
143            relation_types: relation_types
144                .iter()
145                .map(|name| (*name).to_owned())
146                .collect(),
147            source_role: source_role.to_owned(),
148            target_role: target_role.to_owned(),
149        });
150        self
151    }
152}
153
154/// Resolved name→id handles for an applied [`Schema`].
155///
156/// The single place names resolve to ids; replaces hand-threaded `*Id` maps.
157/// Typed getters return [`Key<T>`]/[`EqualityIndex<T>`] (the value type is
158/// checked against the declaration); a missing name is a [`DbError::UnknownName`].
159///
160/// # Performance
161///
162/// Cloning is `O(handle count)`; every getter is `O(log n + name length)`.
163#[derive(Clone, Debug, Default)]
164pub struct Bound {
165    /// Role ids by name.
166    pub(crate) roles: BTreeMap<String, RoleId>,
167    /// Label ids by name.
168    pub(crate) labels: BTreeMap<String, LabelId>,
169    /// Relation-type ids by name.
170    pub(crate) relation_types: BTreeMap<String, RelationTypeId>,
171    /// Property key ids (with declared value type) by name.
172    pub(crate) keys: BTreeMap<String, (PropertyKeyId, PropertyType)>,
173    /// Equality index ids (with indexed key value type) by name.
174    pub(crate) equality_indexes: BTreeMap<String, (IndexId, PropertyType)>,
175    /// Projection ids by name.
176    pub(crate) projections: BTreeMap<String, ProjectionId>,
177}
178
179impl Bound {
180    /// Resolves a role handle.
181    ///
182    /// # Errors
183    ///
184    /// [`DbError::UnknownName`] when the role was not declared/bound.
185    ///
186    /// # Performance
187    ///
188    /// This method is `O(log n + name length)`.
189    pub fn role(&self, name: &str) -> Result<RoleId, DbError> {
190        self.roles
191            .get(name)
192            .copied()
193            .ok_or_else(|| DbError::UnknownName {
194                kind: "role",
195                name: name.to_owned(),
196            })
197    }
198
199    /// Resolves a label handle.
200    ///
201    /// # Errors
202    ///
203    /// [`DbError::UnknownName`] when the label was not declared/bound.
204    ///
205    /// # Performance
206    ///
207    /// This method is `O(log n + name length)`.
208    pub fn label(&self, name: &str) -> Result<LabelId, DbError> {
209        self.labels
210            .get(name)
211            .copied()
212            .ok_or_else(|| DbError::UnknownName {
213                kind: "label",
214                name: name.to_owned(),
215            })
216    }
217
218    /// Resolves a relation-type handle.
219    ///
220    /// # Errors
221    ///
222    /// [`DbError::UnknownName`] when the relation type was not declared/bound.
223    ///
224    /// # Performance
225    ///
226    /// This method is `O(log n + name length)`.
227    pub fn relation_type(&self, name: &str) -> Result<RelationTypeId, DbError> {
228        self.relation_types
229            .get(name)
230            .copied()
231            .ok_or_else(|| DbError::UnknownName {
232                kind: "relation type",
233                name: name.to_owned(),
234            })
235    }
236
237    /// Resolves a typed property-key handle, checking the value type matches `T`.
238    ///
239    /// # Errors
240    ///
241    /// [`DbError::UnknownName`] when absent, or [`DbError::SchemaConflict`] when
242    /// the declared value type differs from `T`.
243    ///
244    /// # Performance
245    ///
246    /// This method is `O(log n + name length)`.
247    pub fn key<T: ValueType>(&self, name: &str) -> Result<Key<T>, DbError> {
248        let (id, value_type) =
249            self.keys
250                .get(name)
251                .copied()
252                .ok_or_else(|| DbError::UnknownName {
253                    kind: "property key",
254                    name: name.to_owned(),
255                })?;
256        if value_type == T::TYPE {
257            Ok(Key::from_id(id))
258        } else {
259            Err(DbError::SchemaConflict {
260                name: name.to_owned(),
261                reason: "property key value type differs from the requested typed handle",
262            })
263        }
264    }
265
266    /// Resolves a typed equality-index handle, checking the indexed key's value
267    /// type matches `T`.
268    ///
269    /// # Errors
270    ///
271    /// [`DbError::UnknownName`] when absent, or [`DbError::SchemaConflict`] on a
272    /// value-type mismatch.
273    ///
274    /// # Performance
275    ///
276    /// This method is `O(log n + name length)`.
277    pub fn equality_index<T: ValueType>(&self, name: &str) -> Result<EqualityIndex<T>, DbError> {
278        let (id, value_type) =
279            self.equality_indexes
280                .get(name)
281                .copied()
282                .ok_or_else(|| DbError::UnknownName {
283                    kind: "index",
284                    name: name.to_owned(),
285                })?;
286        if value_type == T::TYPE {
287            Ok(EqualityIndex::from_id(id))
288        } else {
289            Err(DbError::SchemaConflict {
290                name: name.to_owned(),
291                reason: "equality index value type differs from the requested typed handle",
292            })
293        }
294    }
295
296    /// Resolves a projection handle.
297    ///
298    /// # Errors
299    ///
300    /// [`DbError::UnknownName`] when the projection was not declared/bound.
301    ///
302    /// # Performance
303    ///
304    /// This method is `O(log n + name length)`.
305    pub fn projection(&self, name: &str) -> Result<ProjectionId, DbError> {
306        self.projections
307            .get(name)
308            .copied()
309            .ok_or_else(|| DbError::UnknownName {
310                kind: "projection",
311                name: name.to_owned(),
312            })
313    }
314}