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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::*;
use crate::{KeySelector, RangeOption, Transaction};
use std::borrow::Cow;
#[derive(Debug, Clone)]
pub struct Subspace {
prefix: Vec<u8>,
}
impl<E: TuplePack> From<E> for Subspace {
fn from(e: E) -> Self {
Self { prefix: pack(&e) }
}
}
impl Subspace {
pub fn all() -> Subspace {
Self { prefix: Vec::new() }
}
pub fn from_bytes(bytes: &[u8]) -> Self {
Self {
prefix: bytes.to_vec(),
}
}
pub fn subspace<T: TuplePack>(&self, t: &T) -> Self {
Self {
prefix: self.pack(t),
}
}
pub fn bytes(&self) -> &[u8] {
self.prefix.as_slice()
}
pub fn pack<T: TuplePack>(&self, t: &T) -> Vec<u8> {
let mut out = self.prefix.clone();
pack_into(t, &mut out);
out
}
pub fn unpack<'de, T: TupleUnpack<'de>>(&self, key: &'de [u8]) -> PackResult<T> {
if !self.is_start_of(key) {
return Err(PackError::BadPrefix);
}
let key = &key[self.prefix.len()..];
unpack(key)
}
pub fn is_start_of(&self, key: &[u8]) -> bool {
key.starts_with(&self.prefix)
}
pub fn range(&self) -> (Vec<u8>, Vec<u8>) {
let mut begin = Vec::with_capacity(self.prefix.len() + 1);
begin.extend_from_slice(&self.prefix);
begin.push(0x00);
let mut end = Vec::with_capacity(self.prefix.len() + 1);
end.extend_from_slice(&self.prefix);
end.push(0xff);
(begin, end)
}
}
impl<'a> From<&'a Subspace> for RangeOption<'static> {
fn from(subspace: &Subspace) -> Self {
let (begin, end) = subspace.range();
Self {
begin: KeySelector::first_greater_or_equal(Cow::Owned(begin)),
end: KeySelector::first_greater_or_equal(Cow::Owned(end)),
..Self::default()
}
}
}
impl Transaction {
pub fn clear_subspace_range(&self, subspace: &Subspace) {
let (begin, end) = subspace.range();
self.clear_range(&begin, &end)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sub() {
let ss0: Subspace = 1.into();
let ss1 = ss0.subspace(&2);
let ss2: Subspace = (1, 2).into();
assert_eq!(ss1.bytes(), ss2.bytes());
}
#[test]
fn pack_unpack() {
let ss0: Subspace = 1.into();
let tup = (2, 3);
let packed = ss0.pack(&tup);
let expected = pack(&(1, 2, 3));
assert_eq!(expected, packed);
let tup_unpack: (i64, i64) = ss0.unpack(&packed).unwrap();
assert_eq!(tup, tup_unpack);
assert!(ss0.unpack::<(i64, i64, i64)>(&packed).is_err());
}
#[test]
fn is_start_of() {
let ss0: Subspace = 1.into();
let ss1: Subspace = 2.into();
let tup = (2, 3);
assert!(ss0.is_start_of(&ss0.pack(&tup)));
assert!(!ss1.is_start_of(&ss0.pack(&tup)));
assert!(Subspace::from("start").is_start_of(&pack(&"start")));
assert!(Subspace::from("start").is_start_of(&pack(&"start".to_string())));
assert!(!Subspace::from("start").is_start_of(&pack(&"starting")));
assert!(Subspace::from("start").is_start_of(&pack(&("start", "end"))));
assert!(Subspace::from(("start", 42)).is_start_of(&pack(&("start", 42, "end"))));
}
#[test]
fn range() {
let ss: Subspace = 1.into();
let tup = (2, 3);
let packed = ss.pack(&tup);
let (begin, end) = ss.range();
assert!(packed >= begin && packed <= end);
}
}