1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use oasis_core_runtime::storage::mkvs;

use super::{NestedStore, Store};

/// A key-value store backed by MKVS.
pub struct MKVSStore<M: mkvs::MKVS> {
    parent: M,
}

impl<M: mkvs::MKVS> MKVSStore<M> {
    pub fn new(parent: M) -> Self {
        Self { parent }
    }
}

impl<M: mkvs::MKVS> Store for MKVSStore<M> {
    fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
        self.parent.get(key)
    }

    fn insert(&mut self, key: &[u8], value: &[u8]) {
        self.parent.insert(key, value);
    }

    fn remove(&mut self, key: &[u8]) {
        self.parent.remove(key);
    }

    fn iter(&self) -> Box<dyn mkvs::Iterator + '_> {
        self.parent.iter()
    }

    fn prefetch_prefixes(&mut self, prefixes: Vec<mkvs::Prefix>, limit: u16) {
        self.parent.prefetch_prefixes(&prefixes, limit);
    }
}

impl<M: mkvs::MKVS> NestedStore for MKVSStore<M> {
    type Inner = M;

    fn commit(self) -> Self::Inner {
        // Commit is not needed.
        self.parent
    }

    fn rollback(self) -> Self::Inner {
        panic!("attempted to rollback a non-transactional store");
    }

    fn has_pending_updates(&self) -> bool {
        true
    }

    fn pending_update_byte_size(&self) -> usize {
        0
    }
}