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
use std::cmp::Ordering;
pub trait Cmp: Sync + Send {
fn cmp(&self, _: &[u8], _: &[u8]) -> Ordering;
fn find_shortest_sep(&self, _: &[u8], _: &[u8]) -> Vec<u8>;
fn find_short_succ(&self, _: &[u8]) -> Vec<u8>;
fn id(&self) -> &'static str;
}
#[derive(Clone)]
pub struct DefaultCmp;
impl Cmp for DefaultCmp {
fn cmp(&self, a: &[u8], b: &[u8]) -> Ordering {
a.cmp(b)
}
fn id(&self) -> &'static str {
"leveldb.BytewiseComparator"
}
fn find_shortest_sep(&self, a: &[u8], b: &[u8]) -> Vec<u8> {
if a == b {
return a.to_vec();
}
let min = if a.len() < b.len() { a.len() } else { b.len() };
let mut diff_at = 0;
while diff_at < min && a[diff_at] == b[diff_at] {
diff_at += 1;
}
while diff_at < min {
let diff = a[diff_at];
if diff < 0xff && diff + 1 < b[diff_at] {
let mut sep = Vec::from(&a[0..diff_at + 1]);
sep[diff_at] += 1;
assert!(self.cmp(&sep, b) == Ordering::Less);
return sep;
}
diff_at += 1;
}
let mut sep = Vec::with_capacity(a.len() + 1);
sep.extend_from_slice(a);
sep.extend_from_slice(&[0]);
return sep;
}
fn find_short_succ(&self, a: &[u8]) -> Vec<u8> {
let mut result = a.to_vec();
for i in 0..a.len() {
if a[i] != 0xff {
result[i] += 1;
result.resize(i + 1, 0);
return result;
}
}
result.push(255);
return result;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cmp_defaultcmp_shortest_sep() {
assert_eq!(
DefaultCmp.find_shortest_sep("abcd".as_bytes(), "abcf".as_bytes()),
"abce".as_bytes()
);
assert_eq!(
DefaultCmp.find_shortest_sep("abc".as_bytes(), "acd".as_bytes()),
"abc\0".as_bytes()
);
assert_eq!(
DefaultCmp.find_shortest_sep("abcdefghi".as_bytes(), "abcffghi".as_bytes()),
"abce".as_bytes()
);
assert_eq!(
DefaultCmp.find_shortest_sep("a".as_bytes(), "a".as_bytes()),
"a".as_bytes()
);
assert_eq!(
DefaultCmp.find_shortest_sep("a".as_bytes(), "b".as_bytes()),
"a\0".as_bytes()
);
assert_eq!(
DefaultCmp.find_shortest_sep("abc".as_bytes(), "zzz".as_bytes()),
"b".as_bytes()
);
assert_eq!(
DefaultCmp.find_shortest_sep("yyy".as_bytes(), "z".as_bytes()),
"yyy\0".as_bytes()
);
assert_eq!(
DefaultCmp.find_shortest_sep("".as_bytes(), "".as_bytes()),
"".as_bytes()
);
}
#[test]
fn test_cmp_defaultcmp_short_succ() {
assert_eq!(
DefaultCmp.find_short_succ("abcd".as_bytes()),
"b".as_bytes()
);
assert_eq!(
DefaultCmp.find_short_succ("zzzz".as_bytes()),
"{".as_bytes()
);
assert_eq!(DefaultCmp.find_short_succ(&[]), &[0xff]);
assert_eq!(
DefaultCmp.find_short_succ(&[0xff, 0xff, 0xff]),
&[0xff, 0xff, 0xff, 0xff]
);
}
}