icydb_core/db/data/
store.rs1use crate::db::data::{CanonicalRow, DataKey, RawDataKey, RawRow};
7use canic_cdk::structures::{BTreeMap, DefaultMemoryImpl, memory::VirtualMemory};
8
9pub struct DataStore {
19 map: BTreeMap<RawDataKey, RawRow, VirtualMemory<DefaultMemoryImpl>>,
20 secondary_covering_authoritative: bool,
21 secondary_existence_witness_authoritative: bool,
22}
23
24impl DataStore {
25 #[must_use]
27 pub fn init(memory: VirtualMemory<DefaultMemoryImpl>) -> Self {
28 Self {
29 map: BTreeMap::init(memory),
30 secondary_covering_authoritative: false,
31 secondary_existence_witness_authoritative: false,
32 }
33 }
34
35 pub(in crate::db) fn insert(&mut self, key: RawDataKey, row: CanonicalRow) -> Option<RawRow> {
37 let previous = self.map.insert(key, row.into_raw_row());
38 self.invalidate_secondary_covering_authority();
39 self.invalidate_secondary_existence_witness_authority();
40
41 previous
42 }
43
44 #[cfg(test)]
46 pub(crate) fn insert_raw_for_test(&mut self, key: RawDataKey, row: RawRow) -> Option<RawRow> {
47 let previous = self.map.insert(key, row);
48 self.invalidate_secondary_covering_authority();
49 self.invalidate_secondary_existence_witness_authority();
50
51 previous
52 }
53
54 pub fn remove(&mut self, key: &RawDataKey) -> Option<RawRow> {
56 let previous = self.map.remove(key);
57 self.invalidate_secondary_covering_authority();
58 self.invalidate_secondary_existence_witness_authority();
59
60 previous
61 }
62
63 pub fn get(&self, key: &RawDataKey) -> Option<RawRow> {
65 self.map.get(key)
66 }
67
68 #[must_use]
70 pub fn contains(&self, key: &RawDataKey) -> bool {
71 self.map.contains_key(key)
72 }
73
74 pub fn clear(&mut self) {
76 self.map.clear();
77 self.invalidate_secondary_covering_authority();
78 self.invalidate_secondary_existence_witness_authority();
79 }
80
81 #[must_use]
84 pub(in crate::db) const fn secondary_covering_authoritative(&self) -> bool {
85 self.secondary_covering_authoritative
86 }
87
88 pub(in crate::db) const fn mark_secondary_covering_authoritative(&mut self) {
91 self.secondary_covering_authoritative = true;
92 }
93
94 #[must_use]
97 pub(in crate::db) const fn secondary_existence_witness_authoritative(&self) -> bool {
98 self.secondary_existence_witness_authoritative
99 }
100
101 pub(in crate::db) const fn mark_secondary_existence_witness_authoritative(&mut self) {
105 self.secondary_existence_witness_authoritative = true;
106 }
107
108 pub fn memory_bytes(&self) -> u64 {
110 self.iter()
112 .map(|entry| DataKey::STORED_SIZE_BYTES + entry.value().len() as u64)
113 .sum()
114 }
115
116 const fn invalidate_secondary_covering_authority(&mut self) {
119 self.secondary_covering_authoritative = false;
120 }
121
122 const fn invalidate_secondary_existence_witness_authority(&mut self) {
125 self.secondary_existence_witness_authoritative = false;
126 }
127}
128
129impl std::ops::Deref for DataStore {
130 type Target = BTreeMap<RawDataKey, RawRow, VirtualMemory<DefaultMemoryImpl>>;
131
132 fn deref(&self) -> &Self::Target {
133 &self.map
134 }
135}