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
use super::{Merge, MergeError, Record};
use std::collections::BTreeMap;
use std::iter;
pub type Lines = BTreeMap<Key, Value>;
#[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Key {
pub line: u32,
}
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct Value {
pub count: u64,
pub checksum: Option<String>,
}
impl Merge for Value {
fn merge(&mut self, other: Self) -> Result<(), MergeError> {
if let Some(checksum) = other.checksum.as_ref() {
if let Some(my_checksum) = self.checksum.as_ref() {
if checksum != my_checksum {
return Err(MergeError::UnmatchedChecksum);
}
}
}
self.merge_lossy(other);
Ok(())
}
fn merge_lossy(&mut self, other: Self) {
if other.checksum.is_some() {
self.checksum = other.checksum;
}
self.count += other.count;
}
}
pub(crate) fn into_records(lines: Lines) -> Box<dyn Iterator<Item = Record>> {
if lines.is_empty() {
return Box::new(iter::empty());
}
let found = lines.len() as u32;
enum Line {
Data((Key, Value)),
Found,
Hit(u32),
}
let iter = lines
.into_iter()
.map(Line::Data)
.chain(iter::once(Line::Found))
.chain(iter::once(Line::Hit(0)))
.scan(0, |hit_count, mut rec| {
match rec {
Line::Data((_, ref data)) => {
if data.count > 0 {
*hit_count += 1
}
}
Line::Found => {}
Line::Hit(ref mut hit) => *hit = *hit_count,
};
Some(rec)
})
.map(move |rec| match rec {
Line::Data((key, data)) => Record::LineData {
line: key.line,
count: data.count,
checksum: data.checksum,
},
Line::Found => Record::LinesFound { found },
Line::Hit(hit) => Record::LinesHit { hit },
});
Box::new(iter)
}