Skip to main content

icydb_schema/node/
store.rs

1use crate::prelude::*;
2
3///
4/// Store
5///
6/// Schema node describing a stable IC BTreeMap pair that stores:
7/// - primary entity data
8/// - all index data for that entity
9///
10
11#[derive(Clone, Debug, Serialize)]
12pub struct Store {
13    pub def: Def,
14    pub ident: &'static str,
15    pub canister: &'static str,
16    pub data_memory_id: u8,
17    pub index_memory_id: u8,
18}
19
20impl MacroNode for Store {
21    fn as_any(&self) -> &dyn std::any::Any {
22        self
23    }
24}
25
26impl ValidateNode for Store {
27    fn validate(&self) -> Result<(), ErrorTree> {
28        let mut errs = ErrorTree::new();
29        let schema = schema_read();
30
31        match schema.cast_node::<Canister>(self.canister) {
32            Ok(canister) => {
33                // Validate data memory ID
34                if self.data_memory_id < canister.memory_min
35                    || self.data_memory_id > canister.memory_max
36                {
37                    err!(
38                        errs,
39                        "data_memory_id {} outside of range {}-{}",
40                        self.data_memory_id,
41                        canister.memory_min,
42                        canister.memory_max,
43                    );
44                }
45
46                // Validate index memory ID
47                if self.index_memory_id < canister.memory_min
48                    || self.index_memory_id > canister.memory_max
49                {
50                    err!(
51                        errs,
52                        "index_memory_id {} outside of range {}-{}",
53                        self.index_memory_id,
54                        canister.memory_min,
55                        canister.memory_max,
56                    );
57                }
58
59                // Ensure they are not the same
60                if self.data_memory_id == self.index_memory_id {
61                    err!(
62                        errs,
63                        "data_memory_id and index_memory_id must differ (both are {})",
64                        self.data_memory_id,
65                    );
66                }
67            }
68            Err(e) => errs.add(e),
69        }
70
71        errs.result()
72    }
73}
74
75impl VisitableNode for Store {
76    fn route_key(&self) -> String {
77        self.def.path()
78    }
79
80    fn drive<V: Visitor>(&self, v: &mut V) {
81        self.def.accept(v);
82    }
83}