Skip to main content

icydb_core/db/store/
data.rs

1use crate::db::store::{DataKey, RawDataKey, RawRow};
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/// DataStore
12///
13
14#[derive(Deref, DerefMut)]
15pub struct DataStore(BTreeMap<RawDataKey, RawRow, VirtualMemory<DefaultMemoryImpl>>);
16
17impl DataStore {
18    #[must_use]
19    /// Initialize a data store with the provided backing memory.
20    pub fn init(memory: VirtualMemory<DefaultMemoryImpl>) -> Self {
21        Self(BTreeMap::init(memory))
22    }
23
24    /// Clear all stored rows from the data store.
25    pub fn clear(&mut self) {
26        self.0.clear();
27    }
28
29    /// Sum of bytes used by all stored rows.
30    pub fn memory_bytes(&self) -> u64 {
31        self.iter()
32            .map(|entry| DataKey::STORED_SIZE_BYTES + entry.value().len() as u64)
33            .sum()
34    }
35}