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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#![cfg_attr(all(feature = "nightly", test), feature(test))]
#[cfg(all(feature = "nightly", test))]
extern crate test;
#[cfg(all(feature = "nightly", test))]
extern crate histogram;
#[cfg(test)]
extern crate rand;
#[cfg(test)]
extern crate rollsum;
extern crate fmt_extra;
use std::borrow::Borrow;
pub mod bup;
pub mod zpaq;
pub mod rsyncable;
pub mod buzhash;
pub use bup::Bup;
pub use zpaq::Zpaq;
pub use rsyncable::Rsyncable;
#[cfg(all(feature = "nightly", test))]
mod bench;
pub trait Splitter
{
fn find_chunk_edge(&self, data: &[u8]) -> usize {
self.split(data).0.len()
}
fn split<'b>(&self, data: &'b [u8]) -> (&'b[u8], &'b[u8]) {
let l = self.find_chunk_edge(data);
data.split_at(l)
}
fn next_iter<T: Iterator<Item=u8>>(&self, iter: T) -> Option<Vec<u8>>;
fn into_slices<'a>(self, data: &'a [u8]) -> SplitterSlices<'a, Self>
where Self: Sized
{
SplitterSlices::from(self, data)
}
fn as_slices<'a>(&'a self, data: &'a [u8]) -> SplitterSlices<'a, &Self>
where Self: Sized
{
SplitterSlices::from(self, data)
}
fn into_vecs<'a, T: Iterator<Item=u8>>(self, data: T) -> SplitterVecs<T, Self>
where Self: Sized
{
SplitterVecs::from(self, data)
}
fn as_vecs<'a, T: Iterator<Item=u8>>(&'a self, data: T) -> SplitterVecs<T, &Self>
where Self: Sized
{
SplitterVecs::from(self, data)
}
}
impl<'a, S: Splitter + ?Sized> Splitter for &'a S {
fn split<'b>(&self, data: &'b [u8]) -> (&'b[u8], &'b[u8])
{
(*self).split(data)
}
fn next_iter<T: Iterator<Item=u8>>(&self, iter: T) -> Option<Vec<u8>>
{
(*self).next_iter(iter)
}
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum Bound<T> {
Included(T),
Excluded(T),
Unbounded,
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub struct Range<T> {
pub lower: Bound<T>,
pub upper: Bound<T>
}
impl<T> Range<T> {
#[allow(dead_code)]
fn new() -> Self
{
Range { lower: Bound::Unbounded, upper: Bound::Unbounded }
}
#[allow(dead_code)]
fn from_range(r: std::ops::Range<T>) -> Self
{
Range { lower: Bound::Included(r.start), upper: Bound::Excluded(r.end) }
}
fn from_inclusive(r: std::ops::Range<T>) -> Self
{
Range { lower: Bound::Included(r.start), upper: Bound::Included(r.end) }
}
fn exceeds_max(&self, item: &T) -> bool
where T: PartialOrd<T>
{
match self.upper {
Bound::Included(ref i) => if item > i { return true; },
Bound::Excluded(ref i) => if item >= i { return true; },
Bound::Unbounded => {}
}
false
}
fn under_min(&self, item: &T) -> bool
where T: PartialOrd<T>
{
match self.lower {
Bound::Included(ref i) => if item < i { return true; },
Bound::Excluded(ref i) => if item <= i { return true; },
Bound::Unbounded => {}
}
false
}
#[allow(dead_code)]
fn contains(&self, item: &T) -> bool
where T: PartialOrd<T>
{
if self.under_min(item) {
return false;
}
if self.exceeds_max(item) {
return false;
}
true
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SplitterSlices<'a, T: Splitter + 'a> {
parent: T,
d: &'a [u8],
}
impl<'a, T: Splitter> SplitterSlices<'a, T> {
pub fn from(i: T, d : &'a [u8]) -> Self
{
SplitterSlices {
parent: i,
d: d,
}
}
}
impl<'a, T: Splitter> Iterator for SplitterSlices<'a, T> {
type Item = &'a [u8];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.d.is_empty() {
return None;
}
let (a, b) = self.parent.borrow().split(self.d);
if a.is_empty() {
self.d = a;
Some(b)
} else {
self.d = b;
Some(a)
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>)
{
if self.d.is_empty() {
(0, Some(0))
} else {
(1, Some(self.d.len() + 1))
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SplitterVecs<T, P: Splitter> {
parent: P,
d: T,
}
impl<T, P: Splitter> SplitterVecs<T, P> {
pub fn from(i: P, d: T) -> Self
{
SplitterVecs {
parent: i,
d: d,
}
}
}
impl<T: Iterator<Item=u8>, P: Splitter> Iterator for SplitterVecs<T, P> {
type Item = Vec<u8>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.parent.borrow().next_iter(&mut self.d)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>)
{
let (a, b) = self.d.size_hint();
(a, if let Some(c) = b { Some(c + 1) } else { None })
}
}
#[test]
fn test_rsyncable() {
use std::collections::HashSet;
let d1 = b"hello, this is some bytes";
let mut d2 = d1.clone();
d2[4] = ':' as u8;
let b1 = Rsyncable::with_window_and_modulus(4, 8).into_vecs(d1.iter().cloned());
let b2 = Rsyncable::with_window_and_modulus(4, 8).into_vecs(d2.iter().cloned());
let c1 = b1.clone().count();
let c2 = b2.clone().count();
assert_eq!(c1, 4);
assert!((c1 as i64 - c2 as i64).abs() < 1);
let mut blocks = HashSet::with_capacity(c1);
let mut common_in_b1 = 0u64;
for b in b1 {
if !blocks.insert(b) {
common_in_b1 += 1;
}
}
println!("common in b1: {}", common_in_b1);
let mut shared_blocks = 0u64;
for b in b2 {
if blocks.contains(&b) {
shared_blocks += 1;
}
}
println!("shared blocks: {}", shared_blocks);
assert!(shared_blocks > (c1 as u64) / 2);
}