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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use oasis_core_runtime::storage::mkvs;

use super::{Prefix, Store};

/// A key-value store that prefixes all keys with the given prefix.
pub struct PrefixStore<S: Store, P: AsRef<[u8]>> {
    parent: S,
    prefix: P,
}

impl<S: Store, P: AsRef<[u8]>> PrefixStore<S, P> {
    /// Create a new prefix store with the given prefix.
    pub fn new(parent: S, prefix: P) -> Self {
        Self { parent, prefix }
    }
}

impl<S: Store, P: AsRef<[u8]>> Store for PrefixStore<S, P> {
    fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
        self.parent.get(&[self.prefix.as_ref(), key].concat())
    }

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

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

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

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

/// An iterator over the `PrefixStore`.
pub(crate) struct PrefixStoreIterator<'store> {
    inner: Box<dyn mkvs::Iterator + 'store>,
    prefix: &'store [u8],
}

impl<'store> PrefixStoreIterator<'store> {
    fn new(mut inner: Box<dyn mkvs::Iterator + 'store>, prefix: &'store [u8]) -> Self {
        inner.seek(prefix);
        Self { inner, prefix }
    }
}

impl<'store> Iterator for PrefixStoreIterator<'store> {
    type Item = (Vec<u8>, Vec<u8>);

    fn next(&mut self) -> Option<Self::Item> {
        Iterator::next(&mut self.inner).and_then(|(mut k, v)| {
            if k.starts_with(self.prefix) {
                Some((k.split_off(self.prefix.len()), v))
            } else {
                None
            }
        })
    }
}

impl<'store> mkvs::Iterator for PrefixStoreIterator<'store> {
    fn set_prefetch(&mut self, prefetch: usize) {
        self.inner.set_prefetch(prefetch)
    }

    fn is_valid(&self) -> bool {
        if !self
            .inner
            .get_key()
            .as_ref()
            .unwrap_or(&vec![])
            .starts_with(self.prefix)
        {
            return false;
        }
        self.inner.is_valid()
    }

    fn error(&self) -> &Option<anyhow::Error> {
        self.inner.error()
    }

    fn rewind(&mut self) {
        self.inner.seek(self.prefix);
    }

    fn seek(&mut self, key: &[u8]) {
        self.inner.seek(&[self.prefix, key].concat());
    }

    fn get_key(&self) -> &Option<mkvs::Key> {
        self.inner.get_key()
    }

    fn get_value(&self) -> &Option<Vec<u8>> {
        self.inner.get_value()
    }

    fn next(&mut self) {
        if !self.is_valid() {
            // Could be invalid due to prefix mismatch.
            return;
        }
        mkvs::Iterator::next(&mut *self.inner)
    }
}