Skip to main content

icydb_core/db/index/store/
mod.rs

1mod lookup;
2
3use crate::db::index::{
4    entry::{MAX_INDEX_ENTRY_BYTES, RawIndexEntry},
5    key::RawIndexKey,
6};
7use crate::traits::Storable;
8
9use canic_cdk::structures::{BTreeMap, DefaultMemoryImpl, memory::VirtualMemory, storable::Bound};
10use canic_utils::hash::Xxh3;
11use std::borrow::Cow;
12
13///
14/// IndexStore
15///
16/// Architectural Notes:
17///
18/// - Thin persistence wrapper over a stable BTreeMap.
19/// - RawIndexKey and RawIndexEntry are fully validated before insertion.
20/// - Fingerprints are non-authoritative diagnostic witnesses.
21/// - Fingerprints are always stored, but only verified in debug builds.
22/// - This layer does NOT enforce commit/transaction discipline.
23///   Higher layers are responsible for write coordination.
24/// - IndexStore intentionally does NOT implement Deref to avoid leaking
25///   internal storage representation (StoredIndexValue).
26///
27
28pub struct IndexStore {
29    map: BTreeMap<RawIndexKey, StoredIndexValue, VirtualMemory<DefaultMemoryImpl>>,
30    generation: u64,
31}
32
33impl IndexStore {
34    #[must_use]
35    pub fn init(memory: VirtualMemory<DefaultMemoryImpl>) -> Self {
36        Self {
37            map: BTreeMap::init(memory),
38            generation: 0,
39        }
40    }
41
42    /// Snapshot all index entry pairs (diagnostics only).
43    pub(crate) fn entries(&self) -> Vec<(RawIndexKey, RawIndexEntry)> {
44        self.map
45            .iter()
46            .map(|entry| (entry.key().clone(), entry.value().entry))
47            .collect()
48    }
49
50    pub(in crate::db) fn get(&self, key: &RawIndexKey) -> Option<RawIndexEntry> {
51        let value = self.map.get(key);
52
53        #[cfg(debug_assertions)]
54        if let Some(ref stored) = value {
55            Self::verify_if_debug(key, stored);
56        }
57
58        value.map(|stored| stored.entry)
59    }
60
61    pub fn len(&self) -> u64 {
62        self.map.len()
63    }
64
65    pub fn is_empty(&self) -> bool {
66        self.map.is_empty()
67    }
68
69    #[must_use]
70    pub(in crate::db) const fn generation(&self) -> u64 {
71        self.generation
72    }
73
74    pub(crate) fn insert(
75        &mut self,
76        key: RawIndexKey,
77        entry: RawIndexEntry,
78    ) -> Option<RawIndexEntry> {
79        let fingerprint = Self::entry_fingerprint(&key, &entry);
80
81        let stored = StoredIndexValue { entry, fingerprint };
82        let previous = self.map.insert(key, stored).map(|prev| prev.entry);
83        self.bump_generation();
84        previous
85    }
86
87    pub(crate) fn remove(&mut self, key: &RawIndexKey) -> Option<RawIndexEntry> {
88        let previous = self.map.remove(key).map(|prev| prev.entry);
89        self.bump_generation();
90        previous
91    }
92
93    pub fn clear(&mut self) {
94        self.map.clear();
95        self.bump_generation();
96    }
97
98    /// Sum of bytes used by all stored index entries.
99    pub fn memory_bytes(&self) -> u64 {
100        self.map
101            .iter()
102            .map(|entry| {
103                entry.key().as_bytes().len() as u64
104                    + entry.value().entry.len() as u64
105                    + u64::from(RawIndexFingerprint::STORED_SIZE)
106            })
107            .sum()
108    }
109
110    const fn bump_generation(&mut self) {
111        self.generation = self.generation.saturating_add(1);
112    }
113
114    fn entry_fingerprint(key: &RawIndexKey, entry: &RawIndexEntry) -> RawIndexFingerprint {
115        const VERSION: u8 = 1;
116
117        let mut hasher = Xxh3::with_seed(0);
118        hasher.update(&[VERSION]);
119        hasher.update(key.as_bytes());
120        hasher.update(entry.as_bytes());
121
122        RawIndexFingerprint(hasher.digest128().to_be_bytes())
123    }
124
125    #[cfg(debug_assertions)]
126    fn verify_if_debug(key: &RawIndexKey, stored: &StoredIndexValue) {
127        let expected = Self::entry_fingerprint(key, &stored.entry);
128
129        debug_assert!(
130            stored.fingerprint == expected,
131            "debug invariant violation: index fingerprint mismatch"
132        );
133    }
134}
135
136///
137/// StoredIndexValue
138///
139/// Raw entry plus non-authoritative diagnostic fingerprint.
140/// Encoded as: [RawIndexEntry bytes | 16-byte fingerprint]
141///
142
143#[derive(Clone, Debug)]
144struct StoredIndexValue {
145    entry: RawIndexEntry,
146    fingerprint: RawIndexFingerprint,
147}
148
149impl StoredIndexValue {
150    const STORED_SIZE: u32 = MAX_INDEX_ENTRY_BYTES + RawIndexFingerprint::STORED_SIZE;
151}
152
153impl Storable for StoredIndexValue {
154    fn to_bytes(&self) -> Cow<'_, [u8]> {
155        Cow::Owned(self.clone().into_bytes())
156    }
157
158    fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
159        let bytes = bytes.as_ref();
160
161        let (entry_bytes, fingerprint_bytes) =
162            if bytes.len() < RawIndexFingerprint::STORED_SIZE as usize {
163                (bytes, &[][..])
164            } else {
165                bytes.split_at(bytes.len() - RawIndexFingerprint::STORED_SIZE as usize)
166            };
167
168        let mut out = [0u8; 16];
169        if fingerprint_bytes.len() == out.len() {
170            out.copy_from_slice(fingerprint_bytes);
171        }
172
173        Self {
174            entry: RawIndexEntry::from_bytes(Cow::Borrowed(entry_bytes)),
175            fingerprint: RawIndexFingerprint(out),
176        }
177    }
178
179    fn into_bytes(self) -> Vec<u8> {
180        let mut bytes = self.entry.into_bytes();
181        bytes.extend_from_slice(&self.fingerprint.0);
182        bytes
183    }
184
185    const BOUND: Bound = Bound::Bounded {
186        max_size: Self::STORED_SIZE,
187        is_fixed_size: false,
188    };
189}
190
191///
192/// RawIndexFingerprint
193///
194
195#[derive(Clone, Copy, Debug, Eq, PartialEq)]
196pub(crate) struct RawIndexFingerprint([u8; 16]);
197
198impl RawIndexFingerprint {
199    pub(crate) const STORED_SIZE: u32 = 16;
200}
201
202impl Storable for RawIndexFingerprint {
203    fn to_bytes(&self) -> Cow<'_, [u8]> {
204        Cow::Borrowed(&self.0)
205    }
206
207    fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
208        let mut out = [0u8; 16];
209        if bytes.len() == out.len() {
210            out.copy_from_slice(bytes.as_ref());
211        }
212        Self(out)
213    }
214
215    fn into_bytes(self) -> Vec<u8> {
216        self.0.to_vec()
217    }
218
219    const BOUND: Bound = Bound::Bounded {
220        max_size: Self::STORED_SIZE,
221        is_fixed_size: true,
222    };
223}