-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_bf_operate.cc
592 lines (473 loc) · 17 KB
/
cmd_bf_operate.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// cmd_bf_operate.cc-- perform some user-specified operation on bloom filters
#include <string>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <vector>
#include "utilities.h"
#include "bit_utilities.h"
#include "bit_vector.h"
#include "file_manager.h"
#include "support.h"
#include "commands.h"
#include "cmd_bf_operate.h"
using std::string;
using std::vector;
using std::cout;
using std::cerr;
using std::endl;
#define u8 std::uint8_t
#define u32 std::uint32_t
#define u64 std::uint64_t
static u64 num_one_bits (BloomFilter* dstBf);
void BFOperateCommand::short_description
(std::ostream& s)
{
s << commandName << "-- perform some user-specified operation on bloom filters" << endl;
}
void BFOperateCommand::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, extension .bf; only" << endl;
s << " simple uncompressed bloom filters are supported (except for" << endl;
s << " --unrrr); with these and --list, there should be as many" << endl;
s << " bloom filters as the operation needs." << endl;
s << " --list=<filename> file containing a list of bloom filter files; only" << endl;
s << " filters with uncompressed bit vectors are allowed." << endl;
s << " --out=<filename> name for the resulting bloom filter file" << endl;
s << " --noout don't write the resulting bloom filter to a file" << endl;
s << " --and output = a AND b [AND c ..]" << endl;
s << " --or output = a OR b [OR c ..]" << endl;
s << " --xor output = a XOR b [XOR c ..]" << endl;
s << " --eq output = a EQ b" << endl;
s << " --not output = NOT a (i.e. 1s complement)" << endl;
s << " --rrr output = RRR a" << endl;
s << " --unrrr output = UNRRR a" << endl;
s << " --report:counts report the number of active bits in the bloom filters" << endl;
s << " (inputs and result); only applicable for --and, --or," << endl;
s << " --xor, --eq, or --not" << endl;
// $$$ consider adding ROAR and UNROAR
}
void BFOperateCommand::debug_help
(std::ostream& s)
{
s << "--debug= options" << endl;
s << " (none, yet)" << endl;
}
void BFOperateCommand::parse
(int _argc,
char** _argv)
{
int argc;
char** argv;
// defaults
saveToFile = true;
reportCounts = 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=<filename>
if ((is_prefix_of (arg, "--out="))
|| (is_prefix_of (arg, "--output=")))
{ outputFilename = argVal; continue; }
// --noout
if ((arg == "--noout") || (arg == "--nooutput"))
{ saveToFile = false; continue; }
// --list=<filename>
// <filename> is supposed to contain a list of input bloom filter files
if (is_prefix_of (arg, "--list="))
{
std::string bfFilelist = strip_blank_ends(argVal);
std::ifstream in (bfFilelist);
string bfFilename;
int lineNum = 0;
while (std::getline (in, bfFilename))
{
lineNum++;
if (is_suffix_of (strip_blank_ends(bfFilename), ".bf"))
bfFilenames.emplace_back(strip_blank_ends(bfFilename));
else
chastise ("(at line " + std::to_string(lineNum) + " of \"" + argVal + "\")"
+ " \"" + strip_blank_ends(bfFilename) + "\" isn't a \".bf\" file");
}
continue;
}
// operations (some unadvertised)
if ((arg == "--and") || (arg == "--AND") || (arg == "AND"))
{ operation = "and"; continue; }
if ((arg == "--or") || (arg == "--OR") || (arg == "OR"))
{ operation = "or"; continue; }
if ((arg == "--xor") || (arg == "--XOR") || (arg == "XOR"))
{ operation = "xor"; continue; }
if ((arg == "--eq") || (arg == "--EQ") || (arg == "EQ") || (arg == "=="))
{ operation = "eq"; continue; }
if ((arg == "--not") || (arg == "--NOT") || (arg == "NOT") || (arg == "--complement"))
{ operation = "complement"; continue; }
if ((arg == "--rrr") || (arg == "--RRR") || (arg == "RRR"))
{ operation = "rrr compress"; continue; }
if ((arg == "--unrrr") || (arg == "--UNRRR") || (arg == "UNRRR"))
{ operation = "rrr decompress"; continue; }
// $$$ consider adding ROAR and UNROAR
// --report:counts
if ((arg == "--report:counts") || (arg == "--report=counts")
|| (arg == "--report:count") || (arg == "--report=count")
|| (arg == "--counts") || (arg == "--count"))
{ reportCounts = 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>
if (is_suffix_of (arg, ".bf"))
{ bfFilenames.emplace_back(strip_blank_ends(arg)); continue; }
// unrecognized argument
chastise ("unrecognized argument: \"" + arg + "\"");
}
// sanity checks; note that the operator functions (op_and, etc.) will
// perform additional validation of the filters
if ((saveToFile) && (outputFilename.empty()))
chastise ("an output bloom filter filename is required (--out)");
if ((not saveToFile) && (not outputFilename.empty()))
chastise ("an output bloom filter filename was given, inconsistent with --noout");
if (operation.empty())
chastise ("an operation is required (e.g. --AND)");
if (operation == "and")
{
if (bfFilenames.size() < 2)
chastise ("AND requires at least two input bloom filters");
}
else if (operation == "or")
{
if (bfFilenames.size() < 2)
chastise ("OR requires at least two input bloom filters");
}
else if (operation == "xor")
{
if (bfFilenames.size() < 2)
chastise ("XOR requires at least two input bloom filters");
}
else if (operation == "eq")
{
if (bfFilenames.size() != 2)
chastise ("EQ requires exactly two input bloom filters");
}
else if (operation == "complement")
{
if (bfFilenames.size() != 1)
chastise ("NOT requires exactly one input bloom filter");
}
else if (operation == "rrr compress")
{
if (bfFilenames.size() != 1)
chastise ("RRR requires exactly one input bloom filter");
if (reportCounts)
chastise ("--report:count is not implemented for --rrr");
}
else if (operation == "rrr decompress")
{
if (bfFilenames.size() != 1)
chastise ("UNRRR requires exactly one input bloom filter, rrr-compressed");
if (reportCounts)
chastise ("--report:count is not implemented for --unrrr");
}
return;
}
int BFOperateCommand::execute()
{
if (operation == "and") op_and();
else if (operation == "or") op_or();
else if (operation == "xor") op_xor();
else if (operation == "eq") op_eq();
else if (operation == "complement") op_complement();
else if (operation == "rrr compress") op_rrr();
else if (operation == "rrr decompress") op_unrrr();
FileManager::close_file(); // make sure the last bloom filter file we
// .. opened for read gets closed
return EXIT_SUCCESS;
}
void BFOperateCommand::op_and()
{
BloomFilter* dstBf = nullptr;
std::vector<u64> bfCounts;
// process the input bloom filter files in the order they appear in the array
//
// $$$ it'd be nicer to validate all the bf filenames and whether they are
// uncompressed, before performing any of the bitwise operations, but
// for now, this is adequate
int bfNum = 0;
for (const string &bfFilename : bfFilenames)
{
BloomFilter* bf = BloomFilter::bloom_filter(bfFilename);
bf->load();
BitVector* bv = bf->bvs[0];
if (bf->numBitVectors > 1)
fatal ("error: \"" + bfFilename + "\" contains more than one bit vector");
if (bv->compressor() != bvcomp_uncompressed)
fatal ("error: \"" + bfFilename + "\" doesn't contain an uncompressed bit vector");
if (bfNum == 0)
{
// simply make a copy of the first bloom filter into the dstBf
dstBf = BloomFilter::bloom_filter(bf,outputFilename);
dstBf->new_bits(bv,bvcomp_uncompressed,0);
}
else
{
u64 numBits = dstBf->num_bits();
if (bf->num_bits() != numBits)
fatal ("error: \"" + bfFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bfFilename + "\" has " + std::to_string(bf->num_bits()));
dstBf->intersect_with(bv);
}
if (reportCounts)
bfCounts.emplace_back(num_one_bits(bf));
delete bf;
bfNum++;
}
assert (dstBf != nullptr);
if (reportCounts)
{
for (int bfIx=0 ; bfIx<bfFilenames.size() ; bfIx++)
cout << bfFilenames[bfIx] << " has " << bfCounts[bfIx] << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << num_one_bits(dstBf) << " 'active' bits" << endl;
}
if (saveToFile) dstBf->save();
delete dstBf;
}
void BFOperateCommand::op_or()
{
BloomFilter* dstBf = nullptr;
std::vector<u64> bfCounts;
// process the input bloom filter files in the order they appear in the array
//
// $$$ it'd be nicer to validate all the bf filenames and whether they are
// uncompressed, before performing any of the bitwise operations, but
// for now, this is adequate
int bfNum = 0;
for (const string &bfFilename : bfFilenames)
{
BloomFilter* bf = BloomFilter::bloom_filter(bfFilename);
bf->load();
BitVector* bv = bf->bvs[0];
if (bf->numBitVectors > 1)
fatal ("error: \"" + bfFilename + "\" contains more than one bit vector");
if (bv->compressor() != bvcomp_uncompressed)
fatal ("error: \"" + bfFilename + "\" doesn't contain an uncompressed bit vector");
if (bfNum == 0)
{
// simply make a copy of the first bloom filter into the dstBf
dstBf = BloomFilter::bloom_filter(bf,outputFilename);
dstBf->new_bits(bv,bvcomp_uncompressed,0);
}
else
{
u64 numBits = dstBf->num_bits();
if (bf->num_bits() != numBits)
fatal ("error: \"" + bfFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bfFilename + "\" has " + std::to_string(bf->num_bits()));
dstBf->union_with(bv);
}
if (reportCounts)
bfCounts.emplace_back(num_one_bits(bf));
delete bf;
bfNum++;
}
assert (dstBf != nullptr);
if (reportCounts)
{
for (int bfIx=0 ; bfIx<bfFilenames.size() ; bfIx++)
cout << bfFilenames[bfIx] << " has " << bfCounts[bfIx] << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << num_one_bits(dstBf) << " 'active' bits" << endl;
}
if (saveToFile) dstBf->save();
delete dstBf;
}
void BFOperateCommand::op_xor()
{
BloomFilter* dstBf = nullptr;
std::vector<u64> bfCounts;
// process the input bloom filter files in the order they appear in the array
//
// $$$ it'd be nicer to validate all the bf filenames and whether they are
// uncompressed, before performing any of the bitwise operations, but
// for now, this is adequate
int bfNum = 0;
for (const string &bfFilename : bfFilenames)
{
BloomFilter* bf = BloomFilter::bloom_filter(bfFilename);
bf->load();
BitVector* bv = bf->bvs[0];
if (bf->numBitVectors > 1)
fatal ("error: \"" + bfFilename + "\" contains more than one bit vector");
if (bv->compressor() != bvcomp_uncompressed)
fatal ("error: \"" + bfFilename + "\" doesn't contain an uncompressed bit vector");
if (bfNum == 0)
{
// simply make a copy of the first bloom filter into the dstBf
dstBf = BloomFilter::bloom_filter(bf,outputFilename);
dstBf->new_bits(bv,bvcomp_uncompressed,0);
}
else
{
u64 numBits = dstBf->num_bits();
if (bf->num_bits() != numBits)
fatal ("error: \"" + bfFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bfFilename + "\" has " + std::to_string(bf->num_bits()));
dstBf->xor_with(bv);
}
if (reportCounts)
bfCounts.emplace_back(num_one_bits(bf));
delete bf;
bfNum++;
}
assert (dstBf != nullptr);
if (reportCounts)
{
for (int bfIx=0 ; bfIx<bfFilenames.size() ; bfIx++)
cout << bfFilenames[bfIx] << " has " << bfCounts[bfIx] << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << num_one_bits(dstBf) << " 'active' bits" << endl;
}
if (saveToFile) dstBf->save();
delete dstBf;
}
void BFOperateCommand::op_eq()
{
BloomFilter* bfA = BloomFilter::bloom_filter(bfFilenames[0]);
BloomFilter* bfB = BloomFilter::bloom_filter(bfFilenames[1]);
bfA->load();
bfB->load();
BitVector* bvA = bfA->bvs[0];
BitVector* bvB = bfB->bvs[0];
if (bfA->numBitVectors > 1)
fatal ("error: \"" + bfFilenames[0] + "\" contains more than one bit vector");
if (bfB->numBitVectors > 1)
fatal ("error: \"" + bfFilenames[1] + "\" contains more than one bit vector");
if (bvA->compressor() != bvcomp_uncompressed)
fatal ("error: \"" + bfFilenames[0] + "\" doesn't contain an uncompressed bit vector");
if (bvB->compressor() != bvcomp_uncompressed)
fatal ("error: \"" + bfFilenames[1] + "\" doesn't contain an uncompressed bit vector");
u64 numBits = bfA->num_bits();
if (bfB->num_bits() != numBits)
fatal ("error: \"" + bfFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bfFilenames[1] + "\" has " + std::to_string(bfB->num_bits()));
BloomFilter* dstBf = BloomFilter::bloom_filter(bfA,outputFilename);
dstBf->new_bits(bvA,bvcomp_uncompressed,0);
dstBf->xor_with(bvB);
dstBf->complement();
if (reportCounts)
{
cout << bfFilenames[0] << " has " << num_one_bits(bfA) << " 'active' bits" << endl;
cout << bfFilenames[1] << " has " << num_one_bits(bfB) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << num_one_bits(dstBf) << " 'active' bits" << endl;
}
if (saveToFile) dstBf->save();
delete bfA;
delete bfB;
delete dstBf;
}
void BFOperateCommand::op_complement()
{
BloomFilter* bf = BloomFilter::bloom_filter(bfFilenames[0]);
bf->load();
BitVector* bv = bf->bvs[0];
if (bf->numBitVectors > 1)
fatal ("error: \"" + bfFilenames[0] + "\" contains more than one bit vector");
if (bv->compressor() != bvcomp_uncompressed)
fatal ("error: \"" + bfFilenames[0] + "\" doesn't contain an uncompressed bit vector");
BloomFilter* dstBf = BloomFilter::bloom_filter(bf,outputFilename);
dstBf->new_bits(bv,bvcomp_uncompressed,0);
dstBf->complement();
if (reportCounts)
{
cout << bfFilenames[0] << " has " << num_one_bits(bf) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << num_one_bits(dstBf) << " 'active' bits" << endl;
}
if (saveToFile) dstBf->save();
delete bf;
delete dstBf;
}
void BFOperateCommand::op_rrr()
{
BloomFilter* bf = BloomFilter::bloom_filter(bfFilenames[0]);
bf->load();
BitVector* bv = bf->bvs[0];
if (bf->numBitVectors > 1)
fatal ("error: \"" + bfFilenames[0] + "\" contains more than one bit vector");
if (bv->compressor() != bvcomp_uncompressed)
fatal ("error: \"" + bfFilenames[0] + "\" doesn't contain an uncompressed bit vector");
BloomFilter* dstBf = BloomFilter::bloom_filter(bf,outputFilename);
dstBf->bvs[0] = new RrrBitVector (bv);
if (saveToFile) dstBf->save();
delete bf;
delete dstBf;
}
void BFOperateCommand::op_unrrr()
{
BloomFilter* bf = BloomFilter::bloom_filter(bfFilenames[0]);
bf->load();
BitVector* bv = bf->bvs[0];
if (bf->numBitVectors > 1)
fatal ("error: \"" + bfFilenames[0] + "\" contains more than one bit vector");
if (bv->compressor() != bvcomp_rrr)
fatal ("error: \"" + bfFilenames[0] + "\" doesn't contain an rrr bit vector");
RrrBitVector* rrrBv = (RrrBitVector*) bv;
u64 numBits = rrrBv->num_bits();
BloomFilter* dstBf = BloomFilter::bloom_filter(bf,outputFilename);
dstBf->bvs[0] = BitVector::bit_vector(bvcomp_uncompressed,numBits);
decompress_rrr(rrrBv->rrrBits, dstBf->bvs[0]->bits->data(), numBits);
if (saveToFile) dstBf->save();
delete bf;
delete dstBf;
}
static u64 num_one_bits (BloomFilter* dstBf)
{
// nota bene: we support filters with more than one bit vector here, even
// though, as of this writing, other parts of this module do not
u64 numOnes = 0;
for (int bvIx=0 ; bvIx<dstBf->numBitVectors ; bvIx++)
{
BitVector* dstBv = dstBf->bvs[bvIx];
numOnes += dstBv->rank1(dstBf->num_bits());
}
return numOnes;
}