-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_compress_bf.cc
486 lines (401 loc) · 14.3 KB
/
cmd_compress_bf.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
// cmd_compress_bf.cc-- copy bloom filters using a different compression format
#include <string>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <vector>
#include "utilities.h"
#include "bloom_filter.h"
#include "file_manager.h"
#include "support.h"
#include "commands.h"
#include "cmd_compress_bf.h"
using std::string;
using std::vector;
using std::cout;
using std::cerr;
using std::endl;
#define u32 std::uint32_t
#define u64 std::uint64_t
void CompressBFCommand::short_description
(std::ostream& s)
{
s << commandName << "-- copy bloom filters using a different compression format" << endl;
}
void CompressBFCommand::usage
(std::ostream& s,
const string& message)
{
if (!message.empty())
{
s << message << endl;
s << endl;
}
short_description(s);
s << "usage: " << commandName << " <filename> [<filename>..] [options]" << endl;
// 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
s << " <filename> (cumulative) a bloom filter file (usually .bf)" << endl;
s << " --out=<template> filename template for resulting bloom filter files;" << endl;
s << " this must contain the substring {in}, which is replaced" << endl;
s << " by the root of the input filename; this option is" << endl;
s << " usually only needed if the output filename would be the;" << endl;
s << " same as the input filename otherwise" << endl;
s << " (by default, we derive a filename from the input file;" << endl;
s << " using simple rules)" << endl;
s << " --list=<filename> file containing a list of bloom filters to compress;" << endl;
s << " this is used in place of the <filename>s on the command" << endl;
s << " line" << endl;
s << " --tree=<filename> name of topology file for tree containing the filters;" << endl;
s << " this is used in place of the <filename>s or --list" << endl;
s << " --outtree=<filename> name of topology file to write tree consisting of the" << endl;
s << " compressed filters" << endl;
s << " (by default, when --tree is given, we derive a name for" << endl;
s << " the resulting topology from the input filename)" << endl;
s << " --noouttree don't write the resulting topology file" << endl;
s << " --rrr copy the filter(s) to rrr-compressed bit vector(s)" << endl;
s << " (this is the default)" << endl;
s << " --roar copy the filter(s) to roar-compressed bit vector(s)" << endl;
s << " --uncompressed copy the filter(s) to uncompressed bit vector(s)" << endl;
s << " (this may be very slow)" << endl;
}
void CompressBFCommand::debug_help
(std::ostream& s)
{
s << "--debug= options" << endl;
s << " trackmemory" << endl;
s << " bfsimplify" << endl;
}
void CompressBFCommand::parse
(int _argc,
char** _argv)
{
int argc;
char** argv;
// defaults
listFilename = "";
dstCompressor = bvcomp_rrr;
inhibitBvSimplify = false;
bool inhibitOutTree = false;
// skip command name
argv = _argv+1; argc = _argc - 1;
if (argc <= 0) chastise ();
//////////
// scan arguments
//////////
for (int argIx=0 ; argIx<argc ; argIx++)
{
string arg = argv[argIx];
string argVal;
if (arg.empty()) continue;
string::size_type argValIx = arg.find('=');
if (argValIx == string::npos) argVal = "";
else argVal = arg.substr(argValIx+1);
// --help, etc.
if ((arg == "--help")
|| (arg == "-help")
|| (arg == "--h")
|| (arg == "-h")
|| (arg == "?")
|| (arg == "-?")
|| (arg == "--?"))
{ usage (cerr); std::exit (EXIT_SUCCESS); }
if ((arg == "--help=debug")
|| (arg == "--help:debug")
|| (arg == "?debug"))
{ debug_help(cerr); std::exit (EXIT_SUCCESS); }
// --out=<template>
if (is_prefix_of (arg, "--out="))
{
dstFilenameTemplate = argVal;
std::size_t fieldIx = dstFilenameTemplate.find ("{in}");
if (fieldIx == string::npos)
chastise ("--out is required to contain the substring \"{in}\", or a variant of it");
dstFilenameTemplate = BloomFilter::strip_filter_suffix(strip_file_path(dstFilenameTemplate),false);
continue;
}
// --list=<filename>
if (is_prefix_of (arg, "--list="))
{ listFilename = argVal; continue; }
// --tree=<filename>, etc.
if ((is_prefix_of (arg, "--tree="))
|| (is_prefix_of (arg, "--intree="))
|| (is_prefix_of (arg, "--topology=")))
{ inTreeFilename = argVal; continue; }
// --outtree=<filename>, etc.
if (is_prefix_of (arg, "--outtree="))
{ outTreeFilename = argVal; inhibitOutTree = false; continue; }
if (arg == "--noouttree")
{ inhibitOutTree = true; continue; }
// compression type
if (arg == "--uncompressed")
{ dstCompressor = bvcomp_uncompressed; continue; }
if ((arg == "--rrr")
|| (arg == "--RRR"))
{ dstCompressor = bvcomp_rrr; continue; }
if ((arg == "--roar")
|| (arg == "--roaring"))
{ dstCompressor = bvcomp_roar; continue; }
// (unadvertised) special compressor types
if (arg == "--uncrrr")
{ dstCompressor = bvcomp_unc_rrr; continue; }
if (arg == "--uncroar")
{ dstCompressor = bvcomp_unc_roar; continue; }
// (unadvertised) --nobvsimplify
if (arg == "--nobvsimplify")
{ inhibitBvSimplify = true; continue; }
// (unadvertised) debug options
if (arg == "--debug")
{ debug.insert ("debug"); continue; }
if (is_prefix_of (arg, "--debug="))
{
for (const auto& field : parse_comma_list(argVal))
debug.insert(to_lower(field));
continue;
}
// unrecognized --option
if (is_prefix_of (arg, "--"))
chastise ("unrecognized option: \"" + arg + "\"");
// <filename>
bfFilenames.emplace_back(strip_blank_ends(arg));
}
// sanity checks
int numSourceTypes = 0;
if (not bfFilenames.empty()) numSourceTypes++;
if (not listFilename.empty()) numSourceTypes++;
if (not inTreeFilename.empty()) numSourceTypes++;
if (numSourceTypes == 0)
chastise ("at least one bloom filter filename is required");
else if (numSourceTypes > 1)
{
if (not bfFilenames.empty())
chastise ("cannot use --list or --tree with bloom filter filename(s)"
" (e.g. " + bfFilenames[0] + ") in the command");
else
chastise ("cannot use both --list and --tree");
}
if ((not outTreeFilename.empty()) and (inTreeFilename.empty()))
chastise ("cannot use --outtree unless you provide the input tree");
if ((not inTreeFilename.empty()) and (outTreeFilename.empty()) and (not inhibitOutTree))
{
string compressorStr = BitVector::compressor_to_string(dstCompressor);
outTreeFilename = strip_file_path(inTreeFilename);
string::size_type dotIx = outTreeFilename.find_last_of(".");
if (dotIx == string::npos)
outTreeFilename = outTreeFilename + "." + compressorStr + ".sbt";
else if (is_suffix_of(outTreeFilename,".sbt"))
outTreeFilename = outTreeFilename.substr(0,dotIx) + "." + compressorStr + ".sbt";
else
outTreeFilename = outTreeFilename + "." + compressorStr + ".sbt";
cout << "topology will be written to \"" << outTreeFilename << "\"" << endl;
}
return;
}
int CompressBFCommand::execute()
{
if (contains(debug,"trackmemory"))
trackMemory = true;
if (contains(debug,"bfsimplify"))
BloomFilter::reportSimplify = true;
if (not bfFilenames.empty())
{
for (const auto& bfFilename : bfFilenames)
process_bloom_filter(bfFilename);
}
else if (not listFilename.empty())
{
std::ifstream in (listFilename);
if (not in)
fatal ("error: failed to open \"" + listFilename + "\"");
string line;
int lineNum = 0;
while (std::getline(in,line))
{
lineNum++;
string bfFilename = strip_blank_ends(line);
process_bloom_filter(bfFilename);
}
in.close();
}
else // if (not inTreeFilename.empty())
{
std::ofstream* treeOut = nullptr;
if (not outTreeFilename.empty())
{
treeOut = new std::ofstream(outTreeFilename);
if (treeOut == nullptr)
fatal ("error: failed to open ofstream \"" + outTreeFilename + "\"");
if (trackMemory)
cerr << "@+" << treeOut << " creating ofstream \"" << outTreeFilename << "\"" << endl;
}
std::string inTreePath;
string::size_type slashIx = inTreeFilename.find_last_of("/");
if (slashIx != string::npos)
inTreePath = inTreeFilename.substr(0,slashIx);
std::ifstream in (inTreeFilename);
if (not in)
fatal ("error: failed to open \"" + inTreeFilename + "\"");
string line;
int lineNum = 0;
while (std::getline(in,line))
{
lineNum++;
std::size_t level = line.find_first_not_of("*");
string bfFilename = strip_blank_ends(line.substr (level));
string::size_type brackIx = bfFilename.find_first_of("[]");
if (brackIx != string::npos)
bfFilename = bfFilename.substr(0,brackIx);
if ((not inTreePath.empty())
&& (bfFilename.find_first_of("/") == string::npos))
bfFilename = inTreePath + "/" + bfFilename;
string dstFilename = process_bloom_filter(bfFilename);
if (treeOut != nullptr)
*treeOut << string(level,'*') << dstFilename << endl;
}
in.close();
if (treeOut != nullptr)
{
treeOut->close();
if (trackMemory)
cerr << "@-" << treeOut << " deleting ofstream \"" << outTreeFilename << "\"" << endl;
delete treeOut;
}
}
FileManager::close_file(); // make sure the last bloom filter file we
// .. opened for read gets closed
return EXIT_SUCCESS;
}
string CompressBFCommand::process_bloom_filter(const string& filename)
{
// determine the name for the compressed filter file; note that we will
// always create this in the current directory, even if the source filter
// is from another directory
string dstFilename;
string rootName = BloomFilter::strip_filter_suffix(strip_file_path(filename),2);
if (dstFilenameTemplate.empty())
dstFilename = rootName;
else
{
string nakedName = BloomFilter::strip_filter_suffix(strip_file_path(filename),3);
dstFilename = dstFilenameTemplate;
string field = "{in}";
std::size_t fieldIx = dstFilename.find(field);
dstFilename.replace(fieldIx,field.length(),nakedName);
dstFilename += rootName.substr(nakedName.length());
}
string compressionDesc = BitVector::compressor_to_string(dstCompressor);
if (compressionDesc == "uncompressed")
dstFilename += ".bf";
else if ((compressionDesc == "zeros")
|| (compressionDesc == "ones"))
dstFilename += "." + compressionDesc + ".bf";
else
{
dstFilename += "." + compressionDesc + ".bf";
compressionDesc += "-compressed";
}
if (dstFilename == filename)
{
cerr << "warning: not converting \"" << filename << "\""
<< " (the new filename would be the same; use --out)" << endl;
return dstFilename;
}
// load the source filter
BloomFilter* srcBf = BloomFilter::bloom_filter(filename);
srcBf->load();
// make sure all vectors in the source filter have the same compression
// type excluding any super-compressed vectors (all-zeros or all-ones) from
// this test; if any of them is rrr or roar in uncompressed form, modify
// the compression type to reflect that fact
if (srcBf->numBitVectors == 0)
fatal ("error: \"" + filename + "\" contains no bit vectors");
u32 srcCompressor = bvcomp_unknown;
for (int whichBv=0 ; whichBv<srcBf->numBitVectors ; whichBv++)
{
u32 bvCompressor = srcBf->bvs[whichBv]->compressor();
if ((bvCompressor == bvcomp_zeros) || (bvCompressor == bvcomp_ones))
continue;
if (srcCompressor == bvcomp_unknown)
srcCompressor = bvCompressor;
else if (bvCompressor != srcCompressor)
fatal ("error: not converting \"" + filename + "\""
+ " (its bit vectors are inconsistently compressed)");
}
if (srcCompressor == bvcomp_rrr)
{
for (int whichBv=0 ; whichBv<srcBf->numBitVectors ; whichBv++)
{
u32 bvCompressor = srcBf->bvs[whichBv]->compressor();
if ((bvCompressor == bvcomp_zeros) || (bvCompressor == bvcomp_ones))
continue;
if (srcBf->bvs[whichBv]->bits != nullptr)
{ srcCompressor = bvcomp_unc_rrr; break; }
}
}
else if (srcCompressor == bvcomp_roar)
{
for (int whichBv=1 ; whichBv<srcBf->numBitVectors ; whichBv++)
{
u32 bvCompressor = srcBf->bvs[whichBv]->compressor();
if ((bvCompressor == bvcomp_zeros) || (bvCompressor == bvcomp_ones))
continue;
if (srcBf->bvs[whichBv]->bits != nullptr)
{ srcCompressor = bvcomp_unc_roar; break; }
}
}
// create the destination filter, either copying the bits en masse (if the
// source is uncompressed) or copying bits one-by-one (if the source is
// compressed)
BloomFilter* dstBf = BloomFilter::bloom_filter(srcBf,dstFilename);
if (srcCompressor == bvcomp_uncompressed)
{
for (int whichBv=0 ; whichBv<srcBf->numBitVectors ; whichBv++)
{
u32 bvCompressor = srcBf->bvs[whichBv]->compressor();
if ((bvCompressor == bvcomp_zeros) || (bvCompressor == bvcomp_ones))
dstBf->new_bits(bvCompressor,whichBv);
else
dstBf->new_bits(srcBf->bvs[whichBv],dstCompressor,whichBv);
}
}
else
{
for (int whichBv=0 ; whichBv<srcBf->numBitVectors ; whichBv++)
{
BitVector* srcBv = srcBf->bvs[whichBv];
u32 bvCompressor = srcBv->compressor();
if ((bvCompressor == bvcomp_zeros) || (bvCompressor == bvcomp_ones))
dstBf->new_bits(bvCompressor,whichBv);
else if (bvCompressor == dstCompressor)
dstBf->bvs[whichBv] = BitVector::bit_vector(dstCompressor,srcBv);
else
{
// $$$ improve this for RRR by decoding chunk by chunk
dstBf->new_bits(dstCompressor,whichBv);
BitVector* dstBv = dstBf->bvs[whichBv];
for (u64 pos=0 ; pos<srcBf->numBits ; pos++)
{ if ((*srcBv)[pos] == 1) dstBv->write_bit(pos); }
}
}
}
// save the destination filter; note that the bit vector will automatically
// be compressed (if necessary) as part of the save process
if ((dstCompressor == bvcomp_unc_rrr) // non-yet-compressed rrr
|| (dstCompressor == bvcomp_unc_roar)) // non-yet-compressed roar
{
for (int whichBv=0 ; whichBv<dstBf->numBitVectors ; whichBv++)
{
BitVector* dstBv = dstBf->bvs[whichBv];
dstBv->unfinished();
}
}
else if (not inhibitBvSimplify)
{
for (int whichBv=0 ; whichBv<dstBf->numBitVectors ; whichBv++)
dstBf->simplify_bit_vector(whichBv);
}
dstBf->reportSave = true;
dstBf->save();
delete srcBf;
delete dstBf;
return dstFilename;
}