icydb_core/db/store/
data.rs

1use crate::db::store::{DataKey, RawDataKey, RawRow, StoreRegistry};
2use canic_cdk::structures::{BTreeMap, DefaultMemoryImpl, memory::VirtualMemory};
3use derive_more::{Deref, DerefMut};
4
5///
6/// DataRow
7///
8
9pub type DataRow = (DataKey, RawRow);
10
11///
12/// DataStoreRegistry
13///
14
15#[derive(Deref, DerefMut)]
16pub struct DataStoreRegistry(StoreRegistry<DataStore>);
17
18impl DataStoreRegistry {
19    #[must_use]
20    #[allow(clippy::new_without_default)]
21    /// Create an empty data store registry.
22    pub fn new() -> Self {
23        Self(StoreRegistry::new())
24    }
25}
26
27///
28/// DataStore
29///
30
31#[derive(Deref, DerefMut)]
32pub struct DataStore(BTreeMap<RawDataKey, RawRow, VirtualMemory<DefaultMemoryImpl>>);
33
34impl DataStore {
35    #[must_use]
36    /// Initialize a data store with the provided backing memory.
37    pub fn init(memory: VirtualMemory<DefaultMemoryImpl>) -> Self {
38        Self(BTreeMap::init(memory))
39    }
40
41    /// Sum of bytes used by all stored rows.
42    pub fn memory_bytes(&self) -> u64 {
43        self.iter()
44            .map(|entry| u64::from(DataKey::STORED_SIZE) + entry.value().len() as u64)
45            .sum()
46    }
47}