Skip to main content

icydb_core/db/store/
data_key.rs

1#![expect(clippy::cast_possible_truncation)]
2use crate::{
3    db::{
4        identity::{EntityName, IdentityDecodeError},
5        store::storage_key::{StorageKey, StorageKeyEncodeError},
6    },
7    error::{ErrorClass, ErrorOrigin, InternalError},
8    traits::{EntityKind, FieldValue, Storable},
9};
10use canic_cdk::structures::storable::Bound;
11use std::{
12    borrow::Cow,
13    fmt::{self, Display},
14};
15use thiserror::Error as ThisError;
16
17///
18/// DataKeyEncodeError
19/// (serialize boundary)
20///
21
22#[derive(Debug, ThisError)]
23pub enum DataKeyEncodeError {
24    #[error("data key encoding failed for {key}: {source}")]
25    KeyEncoding {
26        key: DataKey,
27        source: StorageKeyEncodeError,
28    },
29}
30
31impl From<DataKeyEncodeError> for InternalError {
32    fn from(err: DataKeyEncodeError) -> Self {
33        Self::new(
34            ErrorClass::Unsupported,
35            ErrorOrigin::Serialize,
36            err.to_string(),
37        )
38    }
39}
40
41///
42/// KeyDecodeError
43/// (decode / corruption boundary)
44///
45
46#[derive(Debug, ThisError)]
47pub enum KeyDecodeError {
48    #[error("invalid primary key encoding")]
49    InvalidEncoding,
50}
51
52impl From<&'static str> for KeyDecodeError {
53    fn from(_: &'static str) -> Self {
54        Self::InvalidEncoding
55    }
56}
57
58///
59/// DataKeyDecodeError
60/// (decode / corruption boundary)
61///
62
63#[derive(Debug, ThisError)]
64pub enum DataKeyDecodeError {
65    #[error("invalid entity name")]
66    Entity(#[from] IdentityDecodeError),
67
68    #[error("invalid primary key")]
69    Key(#[from] KeyDecodeError),
70}
71
72///
73/// DataKey
74///
75
76#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
77pub struct DataKey {
78    entity: EntityName,
79    key: StorageKey,
80}
81
82impl DataKey {
83    /// Fixed on-disk size in bytes (stable, protocol-level)
84    pub const STORED_SIZE_BYTES: u64 =
85        EntityName::STORED_SIZE_BYTES + StorageKey::STORED_SIZE_BYTES;
86
87    /// Fixed in-memory size (for buffers and arrays only)
88    pub const STORED_SIZE_USIZE: usize = Self::STORED_SIZE_BYTES as usize;
89
90    // ------------------------------------------------------------------
91    // Constructors
92    // ------------------------------------------------------------------
93
94    /// Construct using compile-time entity metadata.
95    ///
96    /// This requires that the entity key is persistable.
97    pub fn try_new<E>(key: E::Key) -> Result<Self, InternalError>
98    where
99        E: EntityKind,
100    {
101        let value = key.to_value();
102        let key = StorageKey::try_from_value(&value)?;
103
104        Ok(Self {
105            entity: Self::entity_for::<E>(),
106            key,
107        })
108    }
109
110    /// Decode a raw entity key from this data key.
111    ///
112    /// This is a fallible boundary that validates entity identity and
113    /// key compatibility against the target entity type.
114    pub fn try_key<E>(&self) -> Result<E::Key, InternalError>
115    where
116        E: EntityKind,
117    {
118        let expected = Self::entity_for::<E>();
119        if self.entity != expected {
120            return Err(InternalError::new(
121                ErrorClass::Corruption,
122                ErrorOrigin::Store,
123                format!(
124                    "data key entity mismatch: expected {}, found {}",
125                    expected, self.entity
126                ),
127            ));
128        }
129
130        let value = self.key.as_value();
131        <E::Key as FieldValue>::from_value(&value).ok_or_else(|| {
132            InternalError::new(
133                ErrorClass::Corruption,
134                ErrorOrigin::Store,
135                format!("data key primary key decode failed: {value:?}"),
136            )
137        })
138    }
139
140    /// Construct a DataKey from a raw StorageKey using entity metadata.
141    #[must_use]
142    pub fn from_key<E: EntityKind>(key: StorageKey) -> Self {
143        Self {
144            entity: Self::entity_for::<E>(),
145            key,
146        }
147    }
148
149    #[must_use]
150    pub fn lower_bound<E>() -> Self
151    where
152        E: EntityKind,
153    {
154        Self {
155            entity: Self::entity_for::<E>(),
156            key: StorageKey::MIN,
157        }
158    }
159
160    #[must_use]
161    pub fn upper_bound<E>() -> Self
162    where
163        E: EntityKind,
164    {
165        Self {
166            entity: Self::entity_for::<E>(),
167            key: StorageKey::upper_bound(),
168        }
169    }
170
171    #[inline]
172    fn entity_for<E: EntityKind>() -> EntityName {
173        // INVARIANT:
174        // `E::ENTITY_NAME` is compile-time schema/codegen metadata. Runtime
175        // user input cannot influence this value.
176        // A failure here is an internal model/codegen contract break.
177        EntityName::try_from_str(E::ENTITY_NAME)
178            .expect("invariant violation: invalid E::ENTITY_NAME (schema/codegen contract broken)")
179    }
180
181    // ------------------------------------------------------------------
182    // Accessors
183    // ------------------------------------------------------------------
184
185    #[must_use]
186    pub const fn entity_name(&self) -> &EntityName {
187        &self.entity
188    }
189
190    #[must_use]
191    pub(crate) const fn storage_key(&self) -> StorageKey {
192        self.key
193    }
194
195    /// Compute on-disk entry size from value length.
196    #[must_use]
197    pub const fn entry_size_bytes(value_len: u64) -> u64 {
198        Self::STORED_SIZE_BYTES + value_len
199    }
200
201    #[must_use]
202    pub fn max_storable() -> Self {
203        Self {
204            entity: EntityName::max_storable(),
205            key: StorageKey::max_storable(),
206        }
207    }
208
209    // ------------------------------------------------------------------
210    // Encoding / decoding
211    // ------------------------------------------------------------------
212
213    /// Encode into fixed-size on-disk representation.
214    pub fn to_raw(&self) -> Result<RawDataKey, InternalError> {
215        let mut buf = [0u8; Self::STORED_SIZE_USIZE];
216
217        let entity_bytes = self.entity.to_bytes();
218        buf[..EntityName::STORED_SIZE_USIZE].copy_from_slice(&entity_bytes);
219
220        let key_bytes = self
221            .key
222            .to_bytes()
223            .map_err(|err| DataKeyEncodeError::KeyEncoding {
224                key: self.clone(),
225                source: err,
226            })?;
227
228        let key_offset = EntityName::STORED_SIZE_USIZE;
229        buf[key_offset..key_offset + StorageKey::STORED_SIZE_USIZE].copy_from_slice(&key_bytes);
230
231        Ok(RawDataKey(buf))
232    }
233
234    pub fn try_from_raw(raw: &RawDataKey) -> Result<Self, DataKeyDecodeError> {
235        let bytes = &raw.0;
236
237        let entity = EntityName::from_bytes(&bytes[..EntityName::STORED_SIZE_USIZE])?;
238
239        let key = StorageKey::try_from_bytes(&bytes[EntityName::STORED_SIZE_USIZE..])
240            .map_err(KeyDecodeError::from)?;
241
242        Ok(Self { entity, key })
243    }
244}
245
246impl Display for DataKey {
247    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248        write!(f, "#{} ({})", self.entity, self.key)
249    }
250}
251
252///
253/// RawDataKey
254///
255
256#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
257pub struct RawDataKey([u8; DataKey::STORED_SIZE_USIZE]);
258
259impl RawDataKey {
260    #[must_use]
261    pub const fn as_bytes(&self) -> &[u8; DataKey::STORED_SIZE_USIZE] {
262        &self.0
263    }
264}
265
266impl Storable for RawDataKey {
267    fn to_bytes(&self) -> Cow<'_, [u8]> {
268        Cow::Borrowed(&self.0)
269    }
270
271    fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
272        let mut out = [0u8; DataKey::STORED_SIZE_USIZE];
273        if bytes.len() == out.len() {
274            out.copy_from_slice(bytes.as_ref());
275        }
276        Self(out)
277    }
278
279    fn into_bytes(self) -> Vec<u8> {
280        self.0.to_vec()
281    }
282
283    const BOUND: Bound = Bound::Bounded {
284        max_size: DataKey::STORED_SIZE_BYTES as u32,
285        is_fixed_size: true,
286    };
287}
288
289///
290/// TESTS
291///
292
293#[cfg(test)]
294mod tests {
295    use super::*;
296    use std::borrow::Cow;
297
298    #[test]
299    fn data_key_is_exactly_fixed_size() {
300        let data_key = DataKey::max_storable();
301        let size = data_key.to_raw().unwrap().as_bytes().len();
302        assert_eq!(size, DataKey::STORED_SIZE_USIZE);
303    }
304
305    #[test]
306    fn data_key_ordering_matches_bytes() {
307        let keys = vec![
308            DataKey {
309                entity: EntityName::try_from_str("a").unwrap(),
310                key: StorageKey::Int(0),
311            },
312            DataKey {
313                entity: EntityName::try_from_str("aa").unwrap(),
314                key: StorageKey::Int(0),
315            },
316            DataKey {
317                entity: EntityName::try_from_str("b").unwrap(),
318                key: StorageKey::Int(0),
319            },
320            DataKey {
321                entity: EntityName::try_from_str("a").unwrap(),
322                key: StorageKey::Uint(1),
323            },
324        ];
325
326        let mut by_ord = keys.clone();
327        by_ord.sort();
328
329        let mut by_bytes = keys;
330        by_bytes.sort_by(|a, b| {
331            a.to_raw()
332                .unwrap()
333                .as_bytes()
334                .cmp(b.to_raw().unwrap().as_bytes())
335        });
336
337        assert_eq!(by_ord, by_bytes);
338    }
339
340    #[test]
341    fn data_key_rejects_corrupt_entity() {
342        let mut raw = DataKey::max_storable().to_raw().unwrap();
343        raw.0[0] = 0;
344        assert!(DataKey::try_from_raw(&raw).is_err());
345    }
346
347    #[test]
348    fn data_key_rejects_corrupt_key() {
349        let mut raw = DataKey::max_storable().to_raw().unwrap();
350        let off = EntityName::STORED_SIZE_USIZE;
351        raw.0[off] = 0xFF;
352        assert!(DataKey::try_from_raw(&raw).is_err());
353    }
354
355    #[test]
356    #[allow(clippy::cast_possible_truncation)]
357    fn data_key_fuzz_roundtrip_is_canonical() {
358        let mut seed = 0xDEAD_BEEF_u64;
359
360        for _ in 0..1_000 {
361            let mut bytes = [0u8; DataKey::STORED_SIZE_USIZE];
362            for b in &mut bytes {
363                seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
364                *b = (seed >> 24) as u8;
365            }
366
367            let raw = RawDataKey(bytes);
368            if let Ok(decoded) = DataKey::try_from_raw(&raw) {
369                let re = decoded.to_raw().unwrap();
370                assert_eq!(raw.as_bytes(), re.as_bytes());
371            }
372        }
373    }
374
375    #[test]
376    fn raw_data_key_storable_roundtrip() {
377        let key = DataKey::max_storable().to_raw().unwrap();
378        let bytes = key.to_bytes();
379        let decoded = RawDataKey::from_bytes(Cow::Borrowed(&bytes));
380        assert_eq!(key, decoded);
381    }
382}