oasis_runtime_sdk/storage/
mod.rs

1//! Storage.
2use oasis_core_runtime::storage::mkvs::Iterator;
3
4pub mod confidential;
5mod hashed;
6pub mod host;
7mod mkvs;
8mod overlay;
9mod prefix;
10mod typed;
11
12/// A key-value store.
13pub trait Store {
14    /// Fetch entry with given key.
15    fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
16
17    /// Update entry with given key to the given value.
18    fn insert(&mut self, key: &[u8], value: &[u8]);
19
20    /// Remove entry with given key.
21    fn remove(&mut self, key: &[u8]);
22
23    /// Returns an iterator over the tree.
24    fn iter(&self) -> Box<dyn Iterator + '_>;
25
26    /// Populate the in-memory tree with nodes for keys starting with given prefixes.
27    fn prefetch_prefixes(&mut self, prefixes: Vec<Prefix>, limit: u16);
28}
29
30/// A key-value store that supports the commit operation.
31pub trait NestedStore: Store {
32    /// Type of the inner store.
33    type Inner;
34
35    /// Commit any changes to the underlying store.
36    ///
37    /// If this method is not called the changes may be discarded by the store.
38    fn commit(self) -> Self::Inner;
39
40    /// Rollback any changes.
41    fn rollback(self) -> Self::Inner;
42
43    /// Whether there are any store updates pending to be committed.
44    fn has_pending_updates(&self) -> bool;
45
46    /// Size (in bytes) of any pending updates.
47    fn pending_update_byte_size(&self) -> usize;
48}
49
50impl<S: Store + ?Sized> Store for &mut S {
51    fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
52        S::get(self, key)
53    }
54
55    fn insert(&mut self, key: &[u8], value: &[u8]) {
56        S::insert(self, key, value)
57    }
58
59    fn remove(&mut self, key: &[u8]) {
60        S::remove(self, key)
61    }
62
63    fn iter(&self) -> Box<dyn Iterator + '_> {
64        S::iter(self)
65    }
66
67    fn prefetch_prefixes(&mut self, prefixes: Vec<Prefix>, limit: u16) {
68        S::prefetch_prefixes(self, prefixes, limit)
69    }
70}
71
72impl<S: Store + ?Sized> Store for Box<S> {
73    fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
74        S::get(self, key)
75    }
76
77    fn insert(&mut self, key: &[u8], value: &[u8]) {
78        S::insert(self, key, value)
79    }
80
81    fn remove(&mut self, key: &[u8]) {
82        S::remove(self, key)
83    }
84
85    fn iter(&self) -> Box<dyn Iterator + '_> {
86        S::iter(self)
87    }
88
89    fn prefetch_prefixes(&mut self, prefixes: Vec<Prefix>, limit: u16) {
90        S::prefetch_prefixes(self, prefixes, limit)
91    }
92}
93
94pub use confidential::{ConfidentialStore, Error as ConfidentialStoreError};
95pub use hashed::HashedStore;
96pub use host::HostStore;
97pub use mkvs::MKVSStore;
98pub use overlay::OverlayStore;
99pub use prefix::PrefixStore;
100pub use typed::TypedStore;
101
102// Re-export the mkvs storage prefix.
103pub use oasis_core_runtime::storage::mkvs::Prefix;