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
use crate::csv_parse_result::CsvLeftRightParseResult;
use crate::csv_parse_result::RecordHash;
use crate::csv_parse_result::RecordHashWithPosition;
use crate::csv_parser_hasher::HashMapValue;
use crate::diff_result::*;
use crate::diff_row::*;
use ahash::AHashMap as HashMap;
use std::io::Read;
use std::io::Seek;
pub(crate) struct CsvHashComparer<R: Read + Seek> {
csv_records_left_map: CsvHashValueMap,
csv_records_right_map: CsvHashValueMap,
intermediate_left_map: CsvHashValueMap,
intermediate_right_map: CsvHashValueMap,
max_capacity_left_map: usize,
max_capacity_right_map: usize,
csv_seek_left_reader: csv::Reader<R>,
csv_seek_right_reader: csv::Reader<R>,
diff_records: Vec<DiffByteRecord>,
}
impl<R: Read + std::io::Seek> CsvHashComparer<R> {
// TODO: maybe we can simplify this to only take one capacity and use it for both?
// But keep in mind, we would loose on flexibility (one csv is very small and one very big?)
pub fn with_capacity_and_reader(
left_capacity: usize,
right_capacity: usize,
left_reader: csv::Reader<R>,
right_reader: csv::Reader<R>,
) -> Self {
Self {
csv_records_left_map: HashMap::with_capacity(left_capacity),
csv_records_right_map: HashMap::with_capacity(right_capacity),
intermediate_left_map: HashMap::new(),
intermediate_right_map: HashMap::new(),
max_capacity_left_map: left_capacity,
max_capacity_right_map: right_capacity,
csv_seek_left_reader: left_reader,
csv_seek_right_reader: right_reader,
diff_records: Vec::new(),
}
}
pub fn compare_csv_left_right_parse_result(
&mut self,
csv_left_right_parse_results: impl IntoIterator<
Item = CsvLeftRightParseResult<RecordHashWithPosition>,
>,
) -> csv::Result<DiffByteRecords> {
for csv_left_right_parse_result in csv_left_right_parse_results.into_iter() {
match csv_left_right_parse_result {
CsvLeftRightParseResult::Left(left_record_res) => {
let pos_left = left_record_res.pos;
let key = left_record_res.key();
let record_hash_left = left_record_res.record_hash_num();
match self.csv_records_right_map.get_mut(&key) {
Some(hash_map_val) => {
if let HashMapValue::Initial(ref record_hash_right, ref pos_right) =
hash_map_val
{
if record_hash_left != *record_hash_right {
*hash_map_val = HashMapValue::Modified(pos_left, *pos_right);
} else {
*hash_map_val = HashMapValue::Equal(
left_record_res.record_hash,
RecordHash::new(key, *record_hash_right),
);
}
}
}
None => {
self.csv_records_left_map
.insert(key, HashMapValue::Initial(record_hash_left, pos_left));
}
}
if self.max_capacity_right_map > 0
&& pos_left.line % self.max_capacity_right_map as u64 == 0
{
for (k, v) in self.csv_records_right_map.drain() {
match v {
HashMapValue::Equal(..) => {
// nothing to do - will be removed
}
HashMapValue::Initial(_hash, _pos) => {
// put it back, because we don't know what to do with this value yet
self.intermediate_right_map.insert(k, v);
}
HashMapValue::Modified(pos_left, pos_right) => {
self.csv_seek_left_reader
.seek(pos_left.into())
.expect("must find the given position");
self.csv_seek_right_reader
.seek(pos_right.into())
.expect("must find the given position");
let mut left_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_left_reader
.read_byte_record(&mut left_byte_record)
.expect("can be read");
let mut right_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_right_reader
.read_byte_record(&mut right_byte_record)
.expect("can be read");
let fields_modified = left_byte_record
.iter()
.enumerate()
.zip(right_byte_record.iter())
.fold(
Vec::new(),
|mut acc, ((idx, field_left), field_right)| {
if field_left != field_right {
acc.push(idx);
}
acc
},
);
self.diff_records.push(DiffByteRecord::Modify {
add: ByteRecordLineInfo::new(
right_byte_record,
pos_right.line,
),
delete: ByteRecordLineInfo::new(
left_byte_record,
pos_left.line,
),
field_indices: fields_modified,
});
}
}
}
std::mem::swap(
&mut self.intermediate_right_map,
&mut self.csv_records_right_map,
);
}
}
CsvLeftRightParseResult::Right(right_record_res) => {
let pos_right = right_record_res.pos;
let key = right_record_res.key();
let record_hash_right = right_record_res.record_hash_num();
match self.csv_records_left_map.get_mut(&key) {
Some(hash_map_val) => {
if let HashMapValue::Initial(ref record_hash_left, ref pos_left) =
hash_map_val
{
if *record_hash_left != record_hash_right {
*hash_map_val = HashMapValue::Modified(*pos_left, pos_right);
} else {
*hash_map_val = HashMapValue::Equal(
RecordHash::new(key, *record_hash_left),
right_record_res.record_hash,
);
}
}
}
None => {
self.csv_records_right_map
.insert(key, HashMapValue::Initial(record_hash_right, pos_right));
}
}
if self.max_capacity_left_map > 0
&& pos_right.line % self.max_capacity_left_map as u64 == 0
{
for (k, v) in self.csv_records_left_map.drain() {
match v {
HashMapValue::Equal(..) => {
// nothing to do - will be removed
}
HashMapValue::Initial(_hash, _pos) => {
// put it back, because we don't know what to do with this value yet
self.intermediate_left_map.insert(k, v);
}
HashMapValue::Modified(pos_left, pos_right) => {
self.csv_seek_left_reader
.seek(pos_left.into())
.expect("must find the given position");
self.csv_seek_right_reader
.seek(pos_right.into())
.expect("must find the given position");
let mut left_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_left_reader
.read_byte_record(&mut left_byte_record)
.expect("can be read");
let mut right_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_right_reader
.read_byte_record(&mut right_byte_record)
.expect("can be read");
let fields_modified = left_byte_record
.iter()
.enumerate()
.zip(right_byte_record.iter())
.fold(
Vec::new(),
|mut acc, ((idx, field_left), field_right)| {
if field_left != field_right {
acc.push(idx);
}
acc
},
);
self.diff_records.push(DiffByteRecord::Modify {
add: ByteRecordLineInfo::new(
right_byte_record,
pos_right.line,
),
delete: ByteRecordLineInfo::new(
left_byte_record,
pos_left.line,
),
field_indices: fields_modified,
});
}
}
}
std::mem::swap(
&mut self.intermediate_left_map,
&mut self.csv_records_left_map,
);
}
}
}
}
let mut diff_records = std::mem::take(&mut self.diff_records);
diff_records.extend(
std::mem::take(&mut self.csv_records_left_map)
.into_iter()
.filter_map(|(_, v)| match v {
HashMapValue::Initial(_hash, pos) => {
self.csv_seek_left_reader
.seek(pos.into())
.expect("must be found");
let mut byte_record = csv::ByteRecord::new();
self.csv_seek_left_reader
.read_byte_record(&mut byte_record)
.expect("can be read");
Some(DiffByteRecord::Delete(ByteRecordLineInfo::new(
byte_record,
pos.line,
)))
}
HashMapValue::Modified(pos_left, pos_right) => {
self.csv_seek_left_reader
.seek(pos_left.into())
.expect("must find the given position");
self.csv_seek_right_reader
.seek(pos_right.into())
.expect("must find the given position");
let mut left_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_left_reader
.read_byte_record(&mut left_byte_record)
.expect("can be read");
let mut right_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_right_reader
.read_byte_record(&mut right_byte_record)
.expect("can be read");
let fields_modified = left_byte_record
.iter()
.enumerate()
.zip(right_byte_record.iter())
.fold(Vec::new(), |mut acc, ((idx, field_left), field_right)| {
if field_left != field_right {
acc.push(idx);
}
acc
});
Some(DiffByteRecord::Modify {
add: ByteRecordLineInfo::new(right_byte_record, pos_right.line),
delete: ByteRecordLineInfo::new(left_byte_record, pos_left.line),
field_indices: fields_modified,
})
}
_ => None,
}),
);
diff_records.extend(
std::mem::take(&mut self.csv_records_right_map)
.into_iter()
.filter_map(|(_, v)| match v {
HashMapValue::Initial(_hash, pos) => {
self.csv_seek_right_reader
.seek(pos.into())
.expect("must be found");
let mut byte_record = csv::ByteRecord::new();
self.csv_seek_right_reader
.read_byte_record(&mut byte_record)
.expect("can be read");
Some(DiffByteRecord::Add(ByteRecordLineInfo::new(
byte_record,
pos.line,
)))
}
HashMapValue::Modified(pos_left, pos_right) => {
self.csv_seek_left_reader
.seek(pos_left.into())
.expect("must find the given position");
self.csv_seek_right_reader
.seek(pos_right.into())
.expect("must find the given position");
let mut left_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_left_reader
.read_byte_record(&mut left_byte_record)
.expect("can be read");
let mut right_byte_record = csv::ByteRecord::new();
// TODO: proper error handling (although we are safe here)
self.csv_seek_right_reader
.read_byte_record(&mut right_byte_record)
.expect("can be read");
let fields_modified = left_byte_record
.iter()
.enumerate()
.zip(right_byte_record.iter())
.fold(Vec::new(), |mut acc, ((idx, field_left), field_right)| {
if field_left != field_right {
acc.push(idx);
}
acc
});
Some(DiffByteRecord::Modify {
add: ByteRecordLineInfo::new(right_byte_record, pos_right.line),
delete: ByteRecordLineInfo::new(left_byte_record, pos_left.line),
field_indices: fields_modified,
})
}
_ => None,
}),
);
Ok(DiffByteRecords(diff_records))
}
}