-
Notifications
You must be signed in to change notification settings - Fork 5
/
summary.cc
498 lines (425 loc) · 12.5 KB
/
summary.cc
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "summary.h"
#include "summary/table.h"
#include "summary/codec.h"
#include "summary/stats.h"
#include "stream.h"
#include "core/file.h"
#include "exceptions.h"
#include "matcher.h"
#include "formatter.h"
#include "core/time.h"
#include "types/bundle.h"
#include "types/area.h"
#include "utils/geos.h"
#include "utils/compress.h"
#include "structured/emitter.h"
#include "structured/reader.h"
#include "structured/keys.h"
#include "iotrace.h"
#include "utils/string.h"
#include "utils/sys.h"
#include <fcntl.h>
using namespace std;
using namespace arki::core;
using namespace arki::utils;
using namespace arki::types;
using namespace arki::summary;
namespace arki {
Summary::Summary()
: root(new summary::Table)
{
}
Summary::~Summary()
{
delete root;
}
bool Summary::operator==(const Summary& m) const
{
return root->equals(*m.root);
}
void Summary::clear()
{
delete root;
root = new summary::Table;
}
namespace summary {
#ifdef HAVE_GEOS
struct StatsHull : public ItemVisitor
{
arki::utils::geos::GeometryVector geoms;
std::set<std::vector<uint8_t>> seen;
bool operator()(const Type& type) override
{
const Area& a = *dynamic_cast<const Area*>(&type);
vector<uint8_t> encoded;
core::BinaryEncoder enc(encoded);
a.encodeBinary(enc);
pair<set<vector<uint8_t>>::iterator, bool> i = seen.insert(encoded);
if (i.second)
{
const arki::utils::geos::Geometry& g = a.bbox();
if (!g) return true;
geoms.emplace_back(g.clone());
}
return true;
}
arki::utils::geos::Geometry makeBBox()
{
if (geoms.empty())
return arki::utils::geos::Geometry();
auto collection = arki::utils::geos::Geometry::create_collection(std::move(geoms));
return collection.convex_hull();
}
};
#endif
}
bool Summary::empty() const
{
return root->empty();
}
size_t Summary::count() const
{
return root->stats.count;
}
unsigned long long Summary::size() const
{
return root->stats.size;
}
void Summary::dump(std::ostream& out) const
{
root->dump(out);
}
core::Interval Summary::get_reference_time() const
{
if (root->empty())
throw_consistency_error("get summary reference time", "summary is empty");
else
return root->stats.make_interval();
}
void Summary::expand_date_range(core::Interval& interval) const
{
if (root->empty())
return;
interval.extend(Interval(root->stats.begin, root->stats.end));
}
arki::utils::geos::Geometry Summary::getConvexHull() const
{
#ifdef HAVE_GEOS
summary::StatsHull merger;
root->visitItem(summary::Visitor::posForCode(TYPE_AREA), merger);
return merger.makeBBox();
#else
return arki::utils::geos::Geometry();
#endif
}
bool Summary::read(core::NamedFileDescriptor& in)
{
iotrace::trace_file(in, 0, 0, "read summary");
types::Bundle bundle;
if (!bundle.read_header(in))
return false;
// Ensure first 2 bytes are SU
if (bundle.signature != "SU")
throw_consistency_error("parsing file " + in.name(), "summary entry does not start with 'SU'");
if (!bundle.read_data(in))
return false;
core::BinaryDecoder dec(bundle.data);
read_inner(dec, bundle.version, in.name());
return true;
}
bool Summary::read(core::AbstractInputFile& in)
{
iotrace::trace_file(in, 0, 0, "read summary");
types::Bundle bundle;
if (!bundle.read_header(in))
return false;
// Ensure first 2 bytes are SU
if (bundle.signature != "SU")
throw_consistency_error("parsing file " + in.name(), "summary entry does not start with 'SU'");
if (!bundle.read_data(in))
return false;
core::BinaryDecoder dec(bundle.data);
read_inner(dec, bundle.version, in.name());
return true;
}
bool Summary::read(core::BinaryDecoder& dec, const std::string& filename)
{
string signature;
unsigned version;
core::BinaryDecoder inner = dec.pop_metadata_bundle(signature, version);
// Ensure first 2 bytes are SU
if (signature != "SU")
throw std::runtime_error("cannot parse file " + filename + ": summary entry does not start with 'SU'");
read_inner(inner, version, filename);
return true;
}
void Summary::read_inner(core::BinaryDecoder& dec, unsigned version, const std::string& filename)
{
using namespace summary;
summary::decode(dec, version, filename, *root);
}
std::vector<uint8_t> Summary::encode(bool compressed) const
{
// Encode
vector<uint8_t> inner;
core::BinaryEncoder innerenc(inner);
if (!root->empty())
{
EncodingVisitor visitor(innerenc);
visit(visitor);
}
// Prepend header
vector<uint8_t> res;
core::BinaryEncoder enc(res);
// Signature
enc.add_string("SU");
// Version
enc.add_unsigned(3u, 2);
if (compressed)
{
vector<uint8_t> comp = utils::compress::lzo(inner.data(), inner.size());
if (comp.size() + 4 >= inner.size())
{
// No point in compressing
enc.add_unsigned(inner.size() + 1, 4);
// Add compression type (uncompressed)
enc.add_unsigned(0u, 1);
enc.add_raw(inner);
} else {
// Compression makes sense
// Add total size
enc.add_unsigned(comp.size() + 5, 4);
// Add compression type (LZO)
enc.add_unsigned(1u, 1);
// Add uncompressed size
enc.add_unsigned(inner.size(), 4);
// Add compressed data
enc.add_raw(comp);
}
} else {
enc.add_unsigned(inner.size() + 1, 4);
enc.add_unsigned(0u, 1);
enc.add_raw(inner);
}
return res;
}
void Summary::write(NamedFileDescriptor& out) const
{
// Prepare the encoded data
std::vector<uint8_t> encoded = encode(true);
iotrace::trace_file(out, 0, encoded.size(), "write summary");
// Write out
out.write(encoded.data(), encoded.size());
}
stream::SendResult Summary::write(StreamOutput& out) const
{
// Prepare the encoded data
std::vector<uint8_t> encoded = encode(true);
iotrace::trace_file(out, 0, encoded.size(), "write summary");
// Write out
return out.send_buffer(encoded.data(), encoded.size());
}
void Summary::writeAtomically(const std::string& fname)
{
vector<uint8_t> enc = encode(true);
iotrace::trace_file(fname, 0, enc.size(), "write summary");
sys::write_file_atomically(fname, enc.data(), enc.size(), 0666);
}
namespace summary {
struct YamlPrinter : public Visitor
{
ostream& out;
string indent;
const Formatter* f;
YamlPrinter(ostream& out, size_t indent, const Formatter* f = 0) : out(out), indent(indent, ' '), f(f) {}
virtual bool operator()(const std::vector<const Type*>& md, const Stats& stats)
{
// Write the metadata items
out << "SummaryItem:" << endl;
for (vector<const Type*>::const_iterator i = md.begin(); i != md.end(); ++i)
{
if (!*i) continue;
string ucfirst(str::lower((*i)->tag()));
ucfirst[0] = toupper(ucfirst[0]);
out << indent << ucfirst << ": ";
(*i)->writeToOstream(out);
if (f) out << "\t# " << f->format(**i);
out << endl;
}
// Write the stats
out << "SummaryStats:" << endl;
out << indent << "Count: " << stats.count << endl;
out << indent << "Size: " << stats.size << endl;
out << indent << "Reftime: " << stats.begin << " to " << stats.end << endl;
return true;
}
};
}
bool Summary::visit(summary::Visitor& visitor) const
{
if (root->empty()) return true;
return root->visit(visitor);
}
bool Summary::visitFiltered(const Matcher& matcher, summary::Visitor& visitor) const
{
if (root->empty()) return true;
if (matcher.empty())
return root->visit(visitor);
else
return root->visitFiltered(matcher, visitor);
}
std::string Summary::to_yaml(const Formatter* formatter) const
{
std::stringstream buf;
if (root->empty()) return buf.str();
summary::YamlPrinter printer(buf, 2, formatter);
visit(printer);
return buf.str();
}
void Summary::write_yaml(core::NamedFileDescriptor& out, const Formatter* formatter) const
{
std::string yaml = to_yaml(formatter);
out.write_all_or_retry(yaml.data(), yaml.size());
}
void Summary::serialise(structured::Emitter& e, const structured::Keys& keys, const Formatter* f) const
{
e.start_mapping();
e.add(keys.summary_items);
e.start_list();
if (!root->empty())
{
struct Serialiser : public summary::Visitor
{
structured::Emitter& e;
const structured::Keys& keys;
const Formatter* f;
Serialiser(structured::Emitter& e, const structured::Keys& keys, const Formatter* f) : e(e), keys(keys), f(f) {}
virtual bool operator()(const std::vector<const Type*>& md, const Stats& stats)
{
e.start_mapping();
for (std::vector<const Type*>::const_iterator i = md.begin();
i != md.end(); ++i)
{
if (!*i) continue;
e.add((*i)->tag());
e.start_mapping();
if (f) e.add(keys.summary_desc, f->format(**i));
(*i)->serialise_local(e, keys, f);
e.end_mapping();
}
e.add(keys.summary_stats);
e.start_mapping();
stats.serialiseLocal(e, f);
e.end_mapping();
e.end_mapping();
return true;
}
} visitor(e, keys, f);
visit(visitor);
}
e.end_list();
e.end_mapping();
}
void Summary::read(const structured::Keys& keys, const structured::Reader& val)
{
using namespace structured::memory;
val.sub(keys.summary_items, "summary item list", [&](const structured::Reader& items) {
unsigned size = items.list_size("summary item list");
for (unsigned i = 0; i < size; ++i)
{
items.sub(i, "summary item", [&](const structured::Reader& item) {
root->merge(keys, item);
});
}
});
}
void Summary::read_file(const std::string& fname)
{
// Read all the metadata
sys::File in(fname, O_RDONLY);
read(in);
in.close();
}
bool Summary::readYaml(LineReader& in, const std::string& filename)
{
return root->merge_yaml(in, filename);
}
void Summary::add(const Metadata& md)
{
return root->merge(md);
}
void Summary::add(const Metadata& md, const summary::Stats& s)
{
return root->merge(md, s);
}
namespace summary {
struct SummaryMerger : public Visitor
{
Table& root;
SummaryMerger(Table& root) : root(root) {}
virtual bool operator()(const std::vector<const Type*>& md, const Stats& stats)
{
root.merge(md, stats);
return true;
}
};
struct PruningSummaryMerger : public Visitor
{
const vector<unsigned> positions;
Table& root;
PruningSummaryMerger(const vector<unsigned>& positions, Table& root)
: positions(positions), root(root) {}
virtual bool operator()(const std::vector<const Type*>& md, const Stats& stats)
{
root.merge(md, stats, positions);
return true;
}
};
}
void Summary::add(const Summary& s)
{
if (s.root->empty()) return;
summary::SummaryMerger merger(*root);
s.visit(merger);
}
void Summary::add(const Summary& s, const std::set<types::Code>& keep_only)
{
if (s.root->empty()) return;
vector<unsigned> positions;
for (set<types::Code>::const_iterator i = keep_only.begin();
i != keep_only.end(); ++i)
{
int pos = Visitor::posForCode(*i);
if (pos < 0) continue;
positions.push_back(pos);
}
summary::PruningSummaryMerger merger(positions, *root);
s.visit(merger);
}
namespace summary {
struct MatchVisitor : public Visitor
{
bool res;
MatchVisitor() : res(false) {}
virtual bool operator()(const std::vector<const Type*>& md, const Stats& stats)
{
res = true;
// Stop iteration
return false;
}
};
}
bool Summary::match(const Matcher& matcher) const
{
if (root->empty() && !matcher.empty()) return false;
summary::MatchVisitor visitor;
visitFiltered(matcher, visitor);
return visitor.res;
}
void Summary::filter(const Matcher& matcher, Summary& result) const
{
if (root->empty()) return;
summary::SummaryMerger merger(*result.root);
visitFiltered(matcher, merger);
}
}