-
Notifications
You must be signed in to change notification settings - Fork 3
/
bloom_filter.cc
2014 lines (1738 loc) · 58.5 KB
/
bloom_filter.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// bloom_filter.cc-- classes representing bloom filters.
//
// References:
//
// [1] Solomon, Brad, and Carl Kingsford. "Improved Search of Large
// Transcriptomic Sequencing Databases Using Split Sequence Bloom
// Trees." International Conference on Research in Computational
// Molecular Biology. Springer, Cham, 2017.
// [2] https://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives
#include <string>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <vector>
#include <chrono>
#include "utilities.h"
#include "bit_utilities.h"
#include "hash.h"
#include "file_manager.h"
#include "bloom_filter_file.h"
#include "bloom_filter.h"
using std::string;
using std::vector;
using std::pair;
using std::cerr;
using std::endl;
#define u32 std::uint32_t
#define u64 std::uint64_t
//----------
//
// debugging defines--
//
// In "normal" builds, the triggering defines here are *not* #defined, so that
// no run-time penalty is incurred.
//
//----------
//#define bloom_filter_supportDebug // if this is #defined, extra code is added
// .. to allow debugging prints to be
// .. turned on and off;
#ifndef bloom_filter_supportDebug
#define dbgAdd_dump_pos_with_mer ;
#define dbgAdd_dump_h_pos_with_mer ;
#define dbgAdd_dump_pos ;
#define dbgAdd_dump_h_pos ;
#define dbgContains_dump_pos_with_mer ;
#define dbgContains_dump_h_pos_with_mer ;
#define dbgContains_dump_pos ;
#define dbgContains_dump_h_pos ;
#define dbgLookup_determined_brief1 ;
#define dbgLookup_determined_brief2 ;
#define dbgAdjust_pos_list1 ;
#define dbgAdjust_pos_list2 ;
#define dbgRestore_pos_list1 ;
#define dbgRestore_pos_list2 ;
#endif // not bloom_filter_supportDebug
#ifdef bloom_filter_supportDebug
#define dbgAdd_dump_pos_with_mer \
if (dbgAdd) cerr << mer << " write " << pos << endl;
#define dbgAdd_dump_h_pos_with_mer \
if (dbgAdd) cerr << mer << " write" << h << " " << pos << endl;
#define dbgAdd_dump_pos \
if (dbgAdd) cerr << "write " << pos << endl;
#define dbgAdd_dump_h_pos \
if (dbgAdd) cerr << "write" << h << " " << pos << endl;
#define dbgContains_dump_pos_with_mer \
if (dbgContains) cerr << mer << " read " << pos << endl;
#define dbgContains_dump_h_pos_with_mer \
if (dbgContains) cerr << mer << " read" << h << " " << pos << endl;
#define dbgContains_dump_pos \
if (dbgContains) cerr << "read " << pos << endl;
#define dbgContains_dump_h_pos \
if (dbgContains) cerr << "read" << h << " " << pos << endl;
#define dbgLookup_determined_brief1 \
{ \
if (dbgRankSelectLookup) \
cerr << " DeterminedBriefFilter::lookup(" << pos << ")" << endl; \
if (dbgRankSelectLookup) \
{ \
if ((*bvDet)[pos] == 0) \
cerr << " bvDet[" << pos << "] == 0 --> unresolved" << endl;\
else \
cerr << " bvDet[" << pos << "] == 1" << endl; \
} \
}
#define dbgLookup_determined_brief2 \
if (dbgRankSelectLookup) \
cerr << " bvHow[" << howPos << "] == " << (*bvHow)[howPos] \
<< " --> " << (((*bvHow)[howPos]==1)?"present":"absent") \
<< endl;
#define dbgAdjust_pos_list1 \
if (dbgAdjustPosList) \
cerr << " adjust_positions_in_list(" << numUnresolved << ")" << endl;
#define dbgAdjust_pos_list2 \
if (dbgAdjustPosList) \
cerr << " " << pos << " --> " << (pos-rank) << endl;
#define dbgRestore_pos_list1 \
if (dbgAdjustPosList) \
cerr << " restore_positions_in_list(" << numUnresolved << ")" << endl;
#define dbgRestore_pos_list2 \
if (dbgAdjustPosList) \
cerr << " " << pos << " --> " << kmerPositions[posIx] << endl;
#endif // bloom_filter_supportDebug
//----------
//
// initialize class variables
//
//----------
bool BloomFilter::reportSimplify = false;
bool BloomFilter::reportLoadTime = false;
bool BloomFilter::reportSaveTime = false;
bool BloomFilter::reportTotalLoadTime = false;
bool BloomFilter::reportTotalSaveTime = false;
double BloomFilter::totalLoadTime = 0.0;
double BloomFilter::totalSaveTime = 0.0;
bool BloomFilter::trackMemory = false;
bool BloomFilter::reportCreation = false;
bool BloomFilter::reportManager = false;
bool BloomFilter::reportFileBytes = false;
bool BloomFilter::countFileBytes = false;
u64 BloomFilter::totalFileReads = 0;
u64 BloomFilter::totalFileBytesRead = 0;
//----------
//
// BloomFilter--
//
//----------
BloomFilter::BloomFilter
(const string& _filename)
: ready(false),
manager(nullptr),
filename(_filename),
kmerSize(0),
hasher1(nullptr),
hasher2(nullptr),
numHashes(0),
hashSeed1(0),
hashSeed2(0),
hashModulus(0),
numBits(0),
setSizeKnown(false),
setSize(0),
numBitVectors(1)
{
// nota bene: we clear all maxBitVectors entries (instead of just
// numBitVectors), because a subclass using this constructor
// may increase numBitVectors
for (int bvIx=0 ; bvIx<maxBitVectors ; bvIx++) bvs[bvIx] = nullptr;
if (trackMemory)
cerr << "@+" << this << " constructor BloomFilter(" << identity() << "), variant 1" << endl;
}
BloomFilter::BloomFilter
(const string& _filename,
u32 _kmerSize,
u32 _numHashes,
u64 _hashSeed1,
u64 _hashSeed2,
u64 _numBits,
u64 _hashModulus)
: ready(true),
manager(nullptr),
filename(_filename),
kmerSize(_kmerSize),
hasher1(nullptr),
hasher2(nullptr),
numHashes(_numHashes),
hashSeed1(_hashSeed1),
hashSeed2(_hashSeed2),
numBits(_numBits),
setSizeKnown(false),
setSize(0),
numBitVectors(1)
{
// (see note in first constructor)
for (int bvIx=0 ; bvIx<maxBitVectors ; bvIx++) bvs[bvIx] = nullptr;
setup_hashers();
if (_hashModulus == 0) hashModulus = _numBits;
else hashModulus = _hashModulus;
if (trackMemory)
cerr << "@+" << this << " constructor BloomFilter(" << identity() << "), variant 2" << endl;
}
BloomFilter::BloomFilter
(const BloomFilter* templateBf,
const string& newFilename)
: ready(true),
manager(nullptr),
kmerSize(templateBf->kmerSize),
hasher1(nullptr),
hasher2(nullptr),
numHashes(templateBf->numHashes),
hashSeed1(templateBf->hashSeed1),
hashSeed2(templateBf->hashSeed2),
hashModulus(templateBf->hashModulus),
numBits(templateBf->numBits),
setSizeKnown(false),
setSize(0),
numBitVectors(templateBf->numBitVectors)
{
// (see note in first constructor)
for (int bvIx=0 ; bvIx<maxBitVectors ; bvIx++) bvs[bvIx] = nullptr;
if (newFilename != "") filename = newFilename;
else filename = templateBf->filename;
setup_hashers();
if (trackMemory)
cerr << "@+" << this << " constructor BloomFilter(" << identity() << "), variant 3" << endl;
}
BloomFilter::~BloomFilter()
{
if (trackMemory)
cerr << "@-" << this << " destructor BloomFilter(" << identity() << ")" << endl;
if (hasher1 != nullptr) delete hasher1;
if (hasher2 != nullptr) delete hasher2;
// nota bene: we only consider the first numBitVectors entries; the rest
// are never used
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{ if (bvs[bvIx] != nullptr) delete bvs[bvIx]; }
}
string BloomFilter::identity() const
{
return class_identity() + ":\"" + filename + "\"";
}
void BloomFilter::setup_hashers()
{
// $$$ add trackMemory to hash constructor/destructor
if ((numHashes > 0) && (hasher1 == nullptr))
hasher1 = new HashCanonical(kmerSize,hashSeed1);
if ((numHashes > 1) && (hasher2 == nullptr))
hasher2 = new HashCanonical(kmerSize,hashSeed2);
}
bool BloomFilter::preload(bool bypassManager,bool stopOnMultipleContent)
{
// preload usually returns true; a return of false occurs when the
// file contains more than one BF, and stopOnMultipleContent is true
if (ready) return true;
// $$$ should we also write the bvs back to disk?
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
if (bvs[bvIx] != nullptr)
{ delete bvs[bvIx]; bvs[bvIx] = nullptr; }
}
if ((manager != nullptr) and (not bypassManager))
{
// $$$ this should probably honor stopOnMultipleContent
if (reportManager)
cerr << "asking manager to preload " << identity() << " " << this << endl;
manager->preload_content(filename);
// manager will set ready = true
}
else
{
wall_time_ty startTime;
if (reportLoadTime || reportTotalLoadTime) startTime = get_wall_time();
std::ifstream* in = FileManager::open_file(filename,std::ios::binary|std::ios::in,
/* positionAtStart*/ true);
if (not *in)
fatal ("error: " + class_identity() + "::preload()"
" failed to open \"" + filename + "\"");
if (reportLoadTime || reportTotalLoadTime)
{
double elapsedTime = elapsed_wall_time(startTime);
if (reportLoadTime)
cerr << "[BloomFilter open] " << std::setprecision(6) << std::fixed << elapsedTime << " secs " << filename << endl;
if (reportTotalLoadTime)
totalLoadTime += elapsedTime; // $$$ danger of precision error?
}
vector<pair<string,BloomFilter*>> content
= BloomFilter::identify_content(*in,filename);
if ((stopOnMultipleContent) and (content.size() != 1))
{
FileManager::close_file(in);
return false;
}
if (content.size() != 1)
fatal ("(internal?) error: in " + identity() + ".preload()"
+ " file contains multiple bloom filters"
+ " but we aren't using a file manager");
BloomFilter* templateBf = content[0].second;
u32 bfKind = kind();
u32 templateBfKind = templateBf->kind();
if (templateBfKind != bfKind)
fatal ("(internal?) error: in " + identity() + ".preload()"
+ " file contains incompatible"
+ "\n.. bloom filter, expected kind=" + filter_kind_to_string(bfKind,false)
+ " but file has kind=" + filter_kind_to_string(templateBfKind,false));
copy_properties(templateBf);
setSizeKnown = templateBf->setSizeKnown;
setSize = templateBf->setSize;
steal_bits(templateBf);
delete templateBf;
FileManager::close_file(in);
}
if ((numHashes > 0) && (hasher1 == nullptr))
setup_hashers();
return true;
}
//……… we should have a reload() too
//……… we shouldn't be able to load() a filter that didn't come from a file
void BloomFilter::load
(bool bypassManager,
const string& whichNodeName)
{
//…… enable this test, non-null and resident and dirty
// if (bv != nullptr)
// fatal ("internal error for " + identity()
// + "; attempt to load() onto non-null bit vector");
//……
if ((manager != nullptr) and (not bypassManager))
{
if (reportManager)
cerr << "asking manager to load " << identity() << " " << this << endl;
manager->load_content(filename,whichNodeName);
}
else
{
// $$$ assert that whichNodeName == ""
if (not ready) preload ();
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
BitVector* bv = bvs[bvIx];
bv->reportLoad = reportLoad;
bv->load();
}
}
}
void BloomFilter::save()
{
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
if (bvs[bvIx] == nullptr)
{
if (bvIx == 0)
fatal ("internal error for " + identity()
+ "; attempt to save null bloom filter");
else
fatal ("internal error for " + identity()
+ "; attempt to save partially null bloom filter");
}
}
// $$$ this needs to make sure the file isn't currently opened for read!!!!
// allocate the header, with enough room for a bfvectorinfo record for each
// component
//
// note that we are assuming that the header size for the current file
// format version is at least as large as that for any earlier versions
u64 headerBytesNeeded = bffileheader_size(numBitVectors);
headerBytesNeeded = round_up_16(headerBytesNeeded);
u32 headerSize = (u32) headerBytesNeeded;
if (headerSize != headerBytesNeeded)
fatal ("error: header record for \"" + filename + "\""
" would be too large (" + std::to_string(headerSize) + " bytes)");
bffileheader* header = (bffileheader*) new char[headerSize];
if (header == nullptr)
fatal ("error:"
" failed to allocate " + std::to_string(headerSize) + " bytes"
+ " for header record for \"" + filename + "\"");
if (trackMemory)
cerr << "@+" << header << " allocating bf file header for BloomFilter(" << identity() << ")" << endl;
// write a fake header to the file; after we write the rest of the file
// we'll rewind and write the real header; we do this because we don't know
// the component offsets and sizes until after we've written them
if (reportSave)
cerr << "Saving " << filename << endl;
memset (header, 0, headerSize);
header->magic = bffileheaderMagicUn;
header->headerSize = (u32) sizeof(bffileprefix);
std::ofstream out (filename, std::ios::binary | std::ios::trunc | std::ios::out);
out.write ((char*)header, headerSize);
size_t bytesWritten = headerSize; // (based on assumption of success)
if (not out)
fatal ("error: " + class_identity() + "::save(" + identity() + ")"
+ " failed to open \"" + filename + "\"");
// start the real header
header->magic = bffileheaderMagic;
header->headerSize = headerSize;
header->version = bffileheaderVersion;
header->bfKind = kind();
header->padding1 = 0;
header->kmerSize = kmerSize;
header->numHashes = numHashes;
header->hashSeed1 = hashSeed1;
header->hashSeed2 = hashSeed2;
header->hashModulus = hashModulus;
header->numBits = numBits;
header->numVectors = numBitVectors;
header->setSizeKnown = setSizeKnown;
header->setSize = (setSizeKnown)? setSize : 0;
// write the component(s)
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
BitVector* bv = bvs[bvIx];
header->info[bvIx].compressor = bv->compressor();
header->info[bvIx].name = 0;
header->info[bvIx].offset = bytesWritten;
if ((header->info[bvIx].compressor == bvcomp_rrr)
|| (header->info[bvIx].compressor == bvcomp_unc_rrr))
{
header->info[bvIx].compressor |= (RRR_BLOCK_SIZE << 8);
header->info[bvIx].compressor |= (RRR_RANK_PERIOD << 16);
}
size_t numBytes = bv->serialized_out (out, filename, header->info[bvIx].offset);
bytesWritten += numBytes;
header->info[bvIx].numBytes = numBytes;
header->info[bvIx].filterInfo = bv->filterInfo;
}
// rewind and overwrite header
out.seekp(std::ios::beg);
out.write ((char*)header, headerSize);
out.close();
// clean up
if ((trackMemory) && (header != nullptr))
cerr << "@-" << header << " discarding bf file header for BloomFilter(" << identity() << ")" << endl;
if (header != nullptr) delete[] header;
// now we're in the equivalent of the "ready" state; actually we're beyond
// that state, in the same state as the result of load()
ready = true;
}
void BloomFilter::copy_properties
(const BloomFilter* templateBf)
{
kmerSize = templateBf->kmerSize;
numHashes = templateBf->numHashes;
hashSeed1 = templateBf->hashSeed1;
hashSeed2 = templateBf->hashSeed2;
hashModulus = templateBf->hashModulus;
numBits = templateBf->numBits;
}
void BloomFilter::steal_bits
(BloomFilter* templateBf)
{
if (numBitVectors != templateBf->numBitVectors)
fatal ("internal error for " + identity()
+ "; source filter has "
+ std::to_string(templateBf->numBitVectors) + " bitvectors"
+ " (this filter has " + std::to_string(numBitVectors) + ")");
discard_bits();
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
bvs[bvIx] = templateBf->bvs[bvIx];
templateBf->bvs[bvIx] = nullptr;
}
ready = true;
}
void BloomFilter::steal_bits
(BloomFilter* templateBf,
int whichBv)
{
steal_bits(templateBf,/*src*/whichBv,/*dst*/whichBv);
}
void BloomFilter::steal_bits
(BloomFilter* templateBf,
int whichSrcBv,
int whichDstBv,
u32 compressor)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to set bitvector " + std::to_string(whichDstBv));
if ((whichSrcBv < 0) || (whichSrcBv >= templateBf->numBitVectors))
fatal ("internal error for " + identity()
+ "; request to get source filter's bitvector " + std::to_string(whichSrcBv));
discard_bits(whichDstBv);
BitVector* srcBv = templateBf->bvs[whichSrcBv];
templateBf->bvs[whichSrcBv] = nullptr;
if (compressor == srcBv->compressor())
bvs[whichDstBv] = srcBv;
else
{
bvs[whichDstBv] = BitVector::bit_vector(compressor,srcBv);
delete srcBv;
}
ready = true;
}
bool BloomFilter::is_consistent_with
(const BloomFilter* bf,
bool beFatal) const
{
if (bf->kmerSize != kmerSize)
{
if (not beFatal) return false;
fatal ("error: inconsistent kmer size " + std::to_string(bf->kmerSize)
+ " in \"" + bf->filename + "\""
+ " (expected " + std::to_string(kmerSize)
+ " like in \"" + filename + "\")"
+ "\n(all bloom filters are required to have the same kmer size)");
}
if (bf->numHashes != numHashes)
{
if (not beFatal) return false;
fatal ("error: inconsistent number of hashes " + std::to_string(bf->numHashes)
+ " in \"" + bf->filename + "\""
+ " (expected " + std::to_string(numHashes)
+ " like in \"" + filename + "\")"
+ "\n(all bloom filters are required to have the same number of hashes)");
}
if (bf->hashSeed1 != hashSeed1)
{
if (not beFatal) return false;
fatal ("error: inconsistent hash seed " + std::to_string(bf->hashSeed1)
+ " in \"" + bf->filename + "\""
+ " (expected " + std::to_string(hashSeed1)
+ " like in \"" + filename + "\")"
+ "\n(all bloom filters are required to have the same hash seeds)");
}
if (bf->hashSeed2 != hashSeed2)
{
if (not beFatal) return false;
fatal ("error: inconsistent hash seed 2 " + std::to_string(bf->hashSeed2)
+ " in \"" + bf->filename + "\""
+ " (expected " + std::to_string(hashSeed2)
+ " like in \"" + filename + "\")"
+ "\n(all bloom filters are required to have the same hash seeds)");
}
if (bf->hashModulus != hashModulus)
{
if (not beFatal) return false;
fatal ("error: inconsistent hash modulus " + std::to_string(bf->hashModulus)
+ " in \"" + bf->filename + "\""
+ " (expected " + std::to_string(hashModulus)
+ " like in \"" + filename + "\")"
+ "\n(all bloom filters are required to have the same hash modulus -- the same"
+ "\nnumber of bits)");
}
if (bf->numBits != numBits)
{
if (not beFatal) return false;
fatal ("error: inconsistent number of bits " + std::to_string(bf->numBits)
+ " in \"" + bf->filename + "\""
+ " (expected " + std::to_string(numBits)
+ " like in \"" + filename + "\")"
+ "\n(all bloom filters are required to have the same number of bits)");
}
if (bf->kind() != kind())
{
if (not beFatal) return false;
fatal ("error: inconsistent bloom filter kind " + std::to_string(bf->kind())
+ " in \"" + bf->filename + "\""
+ " (expected " + std::to_string(kind())
+ " like in \"" + filename + "\")"
+ "\n(all bloom filters are required to be of the same kind)");
}
return true;
}
void BloomFilter::discard_bits()
{
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
if (bvs[bvIx] != nullptr)
{ delete bvs[bvIx]; bvs[bvIx] = nullptr; }
}
}
void BloomFilter::discard_bits
(int whichBv)
{
if ((whichBv < 0) || (whichBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to discard bitvector " + std::to_string(whichBv));
if (bvs[whichBv] != nullptr)
{ delete bvs[whichBv]; bvs[whichBv] = nullptr; }
}
void BloomFilter::new_bits
(u32 compressor,
int whichBv)
{
if ((whichBv < -1) || (whichBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to replace bitvector " + std::to_string(whichBv));
if (whichBv >= 0)
{
if (bvs[whichBv] != nullptr) delete bvs[whichBv];
bvs[whichBv] = BitVector::bit_vector(compressor,numBits);
}
else
{
// nothing specified, so do them all
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
if (bvs[bvIx] != nullptr) delete bvs[bvIx];
bvs[bvIx] = BitVector::bit_vector(compressor,numBits);
}
}
}
void BloomFilter::new_bits
(BitVector* srcBv,
u32 compressor,
int whichBv)
{
if ((whichBv < 0) || (whichBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to set bitvector " + std::to_string(whichBv));
if (bvs[whichBv] != nullptr) delete bvs[whichBv];
if (srcBv->bits == nullptr)
{
u32 srcCompressor = srcBv->compressor();
if ((srcCompressor != bvcomp_zeros) && (srcCompressor != bvcomp_ones))
fatal ("internal error for " + identity()
+ "; attempt to copy bits from null or compressed bitvector " + srcBv->identity());
}
bvs[whichBv] = BitVector::bit_vector(compressor,srcBv);
}
void BloomFilter::new_bits
(const string& filename) // note that compressor etc. may be encoded in filename
{
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
{
if (bvs[bvIx] != nullptr) delete bvs[bvIx];
bvs[bvIx] = BitVector::bit_vector (filename);
}
}
BitVector* BloomFilter::get_bit_vector
(int whichBv) const
{
if ((whichBv < 0) || (whichBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to get bitvector " + std::to_string(whichBv));
return bvs[whichBv];
}
BitVector* BloomFilter::surrender_bit_vector
(int whichBv)
{
if ((whichBv < 0) || (whichBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to get bitvector " + std::to_string(whichBv));
BitVector* bv = bvs[whichBv];
bvs[whichBv] = nullptr;
return bv;
}
BitVector* BloomFilter::simplify_bit_vector
(int whichBv)
{
// if possible, replace the bit vector with a simpler version, such as
// all-zeros or all-ones
if ((whichBv < 0) || (whichBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to simplify bitvector " + std::to_string(whichBv));
BitVector* bv = bvs[whichBv];
u32 bvCompressor = bv->compressor();
if ((bvCompressor == bvcomp_zeros) || (bvCompressor == bvcomp_ones))
return bv; // bv is already a simple type
if (bv->is_all_zeros())
{
if (reportSimplify)
cerr << "Simplifying " << filename << "." << whichBv << " to all-zeros" << endl;
bvs[whichBv] = new ZerosBitVector(bv->size());
delete bv;
return bvs[whichBv];
}
if (bv->is_all_ones())
{
if (reportSimplify)
cerr << "Simplifying " << filename << "." << whichBv << " to all-ones" << endl;
bvs[whichBv] = new OnesBitVector(bv->size());
delete bv;
return bvs[whichBv];
}
return bv;
}
void BloomFilter::complement
(int whichDstBv)
{
if ((whichDstBv < -1) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to complement bitvector " + std::to_string(whichDstBv));
if (whichDstBv >= 0)
bvs[whichDstBv]->complement();
else // if (whichDstBv == -1)
{
for (int bvIx=0 ; bvIx<numBitVectors ; bvIx++)
bvs[bvIx]->complement();
}
}
void BloomFilter::union_with
(BitVector* srcBv,
int whichDstBv)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to union into bitvector " + std::to_string(whichDstBv));
switch (srcBv->compressor())
{
case bvcomp_zeros:
break;
case bvcomp_ones:
bvs[whichDstBv]->fill(1);
break;
default:
bvs[whichDstBv]->union_with(srcBv->bits);
break;
}
}
void BloomFilter::union_with_complement
(BitVector* srcBv,
int whichDstBv)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to union into bitvector " + std::to_string(whichDstBv));
switch (srcBv->compressor())
{
case bvcomp_zeros:
bvs[whichDstBv]->fill(1);
break;
case bvcomp_ones:
break;
default:
bvs[whichDstBv]->union_with_complement(srcBv->bits);
break;
}
}
void BloomFilter::intersect_with
(BitVector* srcBv,
int whichDstBv)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to intersection into bitvector " + std::to_string(whichDstBv));
switch (srcBv->compressor())
{
case bvcomp_zeros:
bvs[whichDstBv]->fill(0);
break;
case bvcomp_ones:
break;
default:
bvs[whichDstBv]->intersect_with(srcBv->bits);
break;
}
}
void BloomFilter::mask_with
(BitVector* srcBv,
int whichDstBv)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to mask bitvector " + std::to_string(whichDstBv));
switch (srcBv->compressor())
{
case bvcomp_zeros:
break;
case bvcomp_ones:
bvs[whichDstBv]->fill(0);
break;
default:
bvs[whichDstBv]->mask_with(srcBv->bits);
break;
}
}
void BloomFilter::xor_with
(BitVector* srcBv,
int whichDstBv)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to xor into bitvector " + std::to_string(whichDstBv));
switch (srcBv->compressor())
{
case bvcomp_zeros:
break;
case bvcomp_ones:
bvs[whichDstBv]->complement();
break;
default:
bvs[whichDstBv]->xor_with(srcBv->bits);
break;
}
}
void BloomFilter::squeeze_by
(BitVector* srcBv,
int whichDstBv)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to squeeze bitvector " + std::to_string(whichDstBv));
// …… require that whichDstBv is uncompressed?
u32 compressor = srcBv->compressor();
switch (compressor)
{
default:
bvs[whichDstBv]->squeeze_by(srcBv->bits);
break;
case bvcomp_zeros:
case bvcomp_ones:
int fillValue = (compressor == bvcomp_zeros)? 0 : 1;
u64 resultNumBits = bitwise_count(srcBv->bits->data(),numBits);
sdslbitvector* resultBits = new sdslbitvector(resultNumBits,fillValue);
if (trackMemory)
cerr << "@+" << resultBits << " creating sdslbitvector for BitVector "
<< bvs[whichDstBv]->identity() << endl;
bvs[whichDstBv]->replace_bits(resultBits);
break;
}
}
void BloomFilter::squeeze_by
(const sdslbitvector* srcBits,
int whichDstBv)
{
if ((whichDstBv < 0) || (whichDstBv >= numBitVectors))
fatal ("internal error for " + identity()
+ "; request to squeeze bitvector " + std::to_string(whichDstBv));
bvs[whichDstBv]->squeeze_by(srcBits);
}
// mer_to_position--
// Report the position of a kmer in the filter; we return BloomFilter::npos if
// the kmer's position is not within the filter (this is *not* the same as the
// kmer being present in the set represent by the filter); the kmer can be a
// string or 2-bit encoded data
u64 BloomFilter::mer_to_position
(const string& mer) const
{
// nota bene: we don't enforce numHashes == 1
u64 pos = hasher1->hash(mer) % hashModulus;
if (pos < numBits) return pos;
else return npos; // position is *not* in the filter
}
u64 BloomFilter::mer_to_position
(const u64* merData) const
{
// nota bene: we don't enforce numHashes == 1
u64 pos = hasher1->hash(merData) % hashModulus;
if (pos < numBits) return pos;
else return npos; // position is *not* in the filter
}
// add--
// Add a kmer to the filter; the kmer can be a string or 2-bit encoded data
void BloomFilter::add
(const string& mer)
{
// nota bene: we don't enforce mer.length == kmerSize
// nota bene: we don't canonicalize the string; revchash handles that
BitVector* bv = bvs[0];
u64 h1 = hasher1->hash(mer);
u64 pos = h1 % hashModulus;
if (pos < numBits)
{
(*bv).write_bit(pos);
dbgAdd_dump_pos_with_mer;
}
if (numHashes > 1)
{
u64 hashValues[numHashes];
Hash::fill_hash_values(hashValues,numHashes,h1,hasher2->hash(mer));
for (u32 h=1 ; h<numHashes ; h++)
{
pos = hashValues[h] % hashModulus;
if (pos < numBits)
{
(*bv).write_bit(pos); // $$$ MULTI_VECTOR write each bit to a different vector
dbgAdd_dump_h_pos_with_mer;
}
}
}