Skip to main content

corium_core/
schema.rs

1//! Schema model shared by transaction validation and peers.
2
3use std::collections::BTreeMap;
4
5use crate::AttrId;
6
7/// Supported value types in v1.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub enum ValueType {
10    /// Boolean values.
11    Bool,
12    /// Signed 64-bit integers.
13    Long,
14    /// Double precision floating point values.
15    Double,
16    /// Milliseconds since Unix epoch.
17    Instant,
18    /// UUID values.
19    Uuid,
20    /// Interned keywords.
21    Keyword,
22    /// UTF-8 strings.
23    Str,
24    /// Byte arrays.
25    Bytes,
26    /// Entity references.
27    Ref,
28}
29
30/// Attribute cardinality.
31#[derive(Clone, Copy, Debug, Eq, PartialEq)]
32pub enum Cardinality {
33    /// One value per entity.
34    One,
35    /// Many values per entity.
36    Many,
37}
38
39/// Attribute uniqueness mode.
40#[derive(Clone, Copy, Debug, Eq, PartialEq)]
41pub enum Unique {
42    /// Upsert identity.
43    Identity,
44    /// Unique value with conflict errors.
45    Value,
46}
47
48/// Schema attribute metadata.
49#[derive(Clone, Debug, Eq, PartialEq)]
50pub struct Attribute {
51    /// Attribute entity id.
52    pub id: AttrId,
53    /// Value type.
54    pub value_type: ValueType,
55    /// Cardinality.
56    pub cardinality: Cardinality,
57    /// Optional uniqueness.
58    pub unique: Option<Unique>,
59    /// Component reference flag.
60    pub is_component: bool,
61    /// AVET coverage flag.
62    pub indexed: bool,
63    /// Skip history storage.
64    pub no_history: bool,
65}
66
67/// Immutable schema cache.
68#[derive(Clone, Debug, Default, Eq, PartialEq)]
69pub struct Schema {
70    attrs: BTreeMap<AttrId, Attribute>,
71}
72
73impl Schema {
74    /// Adds or replaces an attribute.
75    pub fn insert(&mut self, attr: Attribute) {
76        self.attrs.insert(attr.id, attr);
77    }
78
79    /// Looks up an attribute.
80    #[must_use]
81    pub fn get(&self, id: AttrId) -> Option<&Attribute> {
82        self.attrs.get(&id)
83    }
84
85    /// Iterates over every installed attribute in id order.
86    pub fn iter(&self) -> impl Iterator<Item = (&AttrId, &Attribute)> {
87        self.attrs.iter()
88    }
89}