calimero_runtime/
store.rs

1use core::fmt::Debug;
2use std::collections::btree_map::IntoIter;
3use std::collections::BTreeMap;
4
5use calimero_primitives::reflect::Reflect;
6
7pub type Key = Vec<u8>;
8pub type Value = Vec<u8>;
9
10pub trait Storage: Reflect {
11    fn get(&self, key: &Key) -> Option<Value>;
12    fn set(&mut self, key: Key, value: Value) -> Option<Value>;
13    fn remove(&mut self, key: &Key) -> Option<Vec<u8>>;
14    fn has(&self, key: &Key) -> bool;
15}
16
17#[derive(Debug, Default)]
18pub struct InMemoryStorage {
19    inner: BTreeMap<Key, Value>,
20}
21
22impl Storage for InMemoryStorage {
23    fn get(&self, key: &Key) -> Option<Value> {
24        self.inner.get(key).cloned()
25    }
26
27    fn set(&mut self, key: Key, value: Value) -> Option<Value> {
28        self.inner.insert(key, value)
29    }
30
31    // todo! revisit this, should we return the value by default?
32    fn remove(&mut self, key: &Key) -> Option<Vec<u8>> {
33        self.inner.remove(key)
34    }
35
36    fn has(&self, key: &Key) -> bool {
37        self.inner.contains_key(key)
38    }
39}
40
41impl IntoIterator for InMemoryStorage {
42    type Item = (Key, Value);
43
44    type IntoIter = IntoIter<Key, Value>;
45
46    fn into_iter(self) -> Self::IntoIter {
47        self.inner.into_iter()
48    }
49}