-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbloom_tree.cc
2172 lines (1807 loc) · 62.1 KB
/
bloom_tree.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_tree.cc-- classes representing bloom filter trees.
//
// References:
//
// [1] Solomon, Brad, and Carl Kingsford. "Fast search of thousands of
// short-read sequencing experiments." Nature biotechnology 34.3 (2016):
// 300-302.
#include <string>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <iostream>
#include "utilities.h"
#include "bit_utilities.h"
#include "file_manager.h"
#include "bloom_tree.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
//----------
//
// initialize class variables
//
//----------
bool BloomTree::inhibitBvSimplify = false;
bool BloomTree::trackMemory = false;
bool BloomTree::reportUnload = false;
int BloomTree::dbgTraversalCounter = -1;
//----------
//
// BloomTree--
//
//----------
BloomTree::BloomTree
(const string& _name,
const string& _bfFilename)
: isDummy(_bfFilename.empty()),
manager(nullptr),
name(_name),
bfFilename(_bfFilename),
bf(nullptr),
isLeaf(true),
parent(nullptr),
fpRateKnown(false),
fpRate(0.0),
nodesShareFiles(false),
queryStats(nullptr)
{
if (trackMemory)
{
if (bfFilename.empty())
cerr << "@+" << this << " constructor BloomTree(<no file>), variant 1" << endl;
else
cerr << "@+" << this << " constructor BloomTree(" << bfFilename << "), variant 1" << endl;
}
}
BloomTree::BloomTree
(BloomTree* root)
: isDummy(root->isDummy),
manager(nullptr),
name(root->name),
bfFilename(root->bfFilename),
bf(root->bf),
isLeaf(root->isLeaf),
parent(nullptr),
nodesShareFiles(false),
queryStats(nullptr)
{
// nota bene: this doesn't copy the subtree, just the root node; we expect
// the caller will detach everything from the root node
// …… can we do this using copy semantics?
for (const auto& child : root->children)
children.emplace_back (child);
if (trackMemory)
{
if (bfFilename.empty())
cerr << "@+" << this << " constructor BloomTree(<no file>), variant 2" << endl;
else
cerr << "@+" << this << " constructor BloomTree(" << bfFilename << "), variant 2" << endl;
}
}
BloomTree::~BloomTree()
{
if (trackMemory)
{
if (bfFilename.empty())
cerr << "@-" << this << " destructor BloomTree(<no file>)" << endl;
else
cerr << "@-" << this << " destructor BloomTree(" << bfFilename << ")" << endl;
}
if (bf != nullptr) delete bf;
for (const auto& subtree : children)
delete subtree;
if ((trackMemory) && (queryStats != nullptr))
cerr << "@-" << queryStats << " discarding stats for BloomTree(" << bfFilename << ")" << endl;
if (queryStats != nullptr) delete[] queryStats;
}
void BloomTree::preload()
{
if (bf == nullptr) bf = BloomFilter::bloom_filter(bfFilename);
relay_debug_settings();
bf->preload();
}
void BloomTree::load()
{
if (bf == nullptr)
{
if (FileManager::dbgContentLoad)
cerr << "BloomTree::load() creating new BF for \"" << name << "\"" << endl;
bf = BloomFilter::bloom_filter(bfFilename);
}
relay_debug_settings();
bf->reportLoad = reportLoad;
bf->reportSave = reportSave;
if (manager != nullptr) bf->manager = manager;
bf->load(/*bypassManager*/false,/*whichNodeName*/name);
}
void BloomTree::save(bool finished)
{
if (bf == nullptr) bf = BloomFilter::bloom_filter(bfFilename);
for (int bvIx=0 ; bvIx<bf->numBitVectors ; bvIx++)
{
BitVector* bv = bf->get_bit_vector(bvIx);
if (finished)
{
if (not inhibitBvSimplify)
bv = bf->simplify_bit_vector(bvIx);
bv->finished();
}
else
bv->unfinished();
}
relay_debug_settings();
bf->save();
}
void BloomTree::unloadable()
{
// $$$ eventually we will want a more sophisticated caching mechanism
if (reportUnload)
cerr << "marking " << name << " as unloadable" << endl;
if (bf != nullptr)
{
if (bf->manager != nullptr)
bf->discard_bits();
else
{ delete bf; bf = nullptr; }
}
}
void BloomTree::relay_debug_settings()
{
if (bf != nullptr)
{
bf->dbgAdjustPosList = dbgAdjustPosList;
bf->dbgRankSelectLookup = dbgRankSelectLookup;
}
}
void BloomTree::add_child
(BloomTree* offspring)
{
children.emplace_back (offspring);
offspring->parent = this;
isLeaf = false;
}
void BloomTree::disown_children()
{
children.clear(); // it is assumed the caller has saved references to
// .. all children prior to asking us to disown them
}
BloomTree* BloomTree::child
(size_t childNum)
{
if (childNum >= children.size())
fatal ("internal error: request for child"
" #" + std::to_string(childNum)
+ " but " + name
+ " only has " + std::to_string(children.size()) + " children");
return children[childNum];
}
BloomFilter* BloomTree::real_filter()
{
// skip through dummies to find an instance of a representative bloom
// filter; for these purposes, we assume all the bloom filters in the tree
// are similar; note that "real" doesn't mean the filter is loaded or even
// preloaded
if (not is_dummy())
{
if (bf == nullptr) bf = BloomFilter::bloom_filter(bfFilename);
if (manager != nullptr) bf->manager = manager;
return bf;
}
if (not isLeaf)
{
for (const auto& child : children)
{
BloomFilter* realBf = child->real_filter();
if (realBf != nullptr) return realBf;
}
}
return nullptr;
}
void BloomTree::pre_order
(vector<BloomTree*>& order)
{
if (not is_dummy()) // (dummies are left out of the resulting list)
order.emplace_back (this);
for (const auto& child : children)
child->pre_order (order);
}
void BloomTree::post_order
(vector<BloomTree*>& order)
{
for (const auto& child : children)
child->post_order (order);
if (not is_dummy()) // (dummies are left out of the resulting list)
order.emplace_back (this);
}
void BloomTree::leaves
(vector<BloomTree*>& order)
{
if (isLeaf)
order.emplace_back (this);
else
{
for (const auto& child : children)
child->leaves (order);
}
}
void BloomTree::print_topology
(std::ostream& out,
int level,
int format) const
{
int levelInc = 1;
if (isDummy)
levelInc = 0;
else
{
switch (format)
{
case topofmt_nodeNames:
out << string(level,'*') << name << endl;
break;
case topofmt_fileNames:
default:
out << string(level,'*') << bfFilename << endl;
break;
case topofmt_containers:
out << string(level,'*') << name << "[" << bfFilename << "]" << endl;
break;
}
}
for (const auto& child : children)
child->print_topology (out, level+levelInc, format);
}
//~~~~~~~~~~
// build union tree
//~~~~~~~~~~
void BloomTree::construct_union_nodes (u32 compressor)
{
// if we already have a filter, just make sure it is in the pre-loaded
// state (or beyond)
if (bf != nullptr)
{ bf->preload(); return; }
// if we're compressing, compose a filename for the compressed version of
// the node; note that we keep that new name separate from the node's
// simple name until after we're done with the node; we write directly to
// the new name, and by finally installing the new name in the node, the
// calling program can scan the tree for an accurate topology
if (compressor != bvcomp_uncompressed)
{
string bfKindStr = "." + BloomFilter::filter_kind_to_string(bfkind_simple);
string compressionDesc = "." + BitVector::compressor_to_string(compressor);
if (bfKindStr == ".") bfKindStr = "";
if (compressionDesc == ".uncompressed") compressionDesc = "";
futureBfFilename = name + bfKindStr + compressionDesc + ".bf";
}
// if this is a leaf, create and load its filter; if we're NOT compressing
// we're done; but if we ARE compressing we write a compressed copy to the
// new file
if (isLeaf)
{
if (dbgTraversal)
{
cerr << "\n=== loading leaf " << name << " (#" << (++dbgTraversalCounter) << ") ===" << endl;
if (compressor != bvcomp_uncompressed)
cerr << "constructing compressed leaf for " << name << endl;
}
bf = BloomFilter::bloom_filter(bfFilename);
bf->load();
if (compressor != bvcomp_uncompressed)
{
if (bf->numBitVectors!=1)
fatal ("error: " + bfFilename + " contains more than one bit vector");
BitVector* bvInput = bf->get_bit_vector(0);
if (bvInput->is_compressed())
fatal ("error: " + bfFilename + " contains a compressed bit vector");
BloomFilter newBf(bf,futureBfFilename);
newBf.new_bits(bvInput,compressor);
newBf.reportSave = reportSave;
newBf.save();
}
return;
}
// otherwise this is an internal node; first construct its descendants
if (dbgTraversal)
{
if (is_dummy())
cerr << "\n=== constructing children of (dummy node) ===" << endl;
else
cerr << "\n=== constructing children of " << name << " (#" << (++dbgTraversalCounter) << ") ===" << endl;
}
for (const auto& child : children)
child->construct_union_nodes(compressor);
// if this is a dummy node, we don't need to build it, but we do mark its
// children as unloadable
// nota bene: we don't expect a dummy to be a child of some other node
if (is_dummy())
{
for (const auto& child : children)
{
child->unloadable();
if (not child->futureBfFilename.empty())
{
child->bfFilename = child->futureBfFilename;
child->futureBfFilename = "";
}
}
return;
}
// create this filter from the union of the child filters, then mark the
// children as unloadable
if (dbgTraversal)
cerr << "\n=== constructing " << name << " ===" << endl;
if (bf != nullptr)
fatal ("internal error: unexpected non-null filter for " + bfFilename);
bool isFirstChild = true;
for (const auto& child : children)
{
if (dbgTraversal)
cerr << "loading " << child->name << endl;
child->load(); // nota bene: child should have already been loaded
if (child->bf == nullptr)
fatal ("internal error: failed to load " + child->bfFilename);
BitVector* childBv = child->bf->get_bit_vector(0);
if (childBv == nullptr)
fatal ("internal error: failed to load bit vector for " + child->bfFilename);
if (childBv->compressor() != bvcomp_uncompressed)
fatal ("error: " + child->bfFilename + " contains compressed bit vector(s)");
if (dbgTraversal)
cerr << "incorporating " << child->name << " into parent" << endl;
if (isFirstChild) // incorporate first child's filters
{
bf = BloomFilter::bloom_filter(child->bf,bfFilename);
bf->new_bits(childBv);
isFirstChild = false;
}
else // union with later child's filter
{
child->bf->is_consistent_with (bf, /*beFatal*/ true);
bf->union_with(childBv);
}
child->unloadable();
if (not child->futureBfFilename.empty())
{
child->bfFilename = child->futureBfFilename;
child->futureBfFilename = "";
}
}
if (bf == nullptr)
fatal ("internal error:"
" in construct_union_nodes(\"" + name + "\")"
+ ", non-leaf node has no children");
// save the node; if we're compressing we write a compressed copy to the
// new file
if (compressor == bvcomp_uncompressed)
{
bf->reportSave = reportSave;
save(/*finished*/ true);
}
else
{
BitVector* bvInput = bf->get_bit_vector(0);
BloomFilter newBf(bf,futureBfFilename);
newBf.new_bits(bvInput,compressor);
newBf.reportSave = reportSave;
newBf.save();
}
if (parent == nullptr)
{
unloadable();
if (not futureBfFilename.empty())
{
bfFilename = futureBfFilename;
futureBfFilename = "";
}
}
}
//~~~~~~~~~~
// build allsome tree
//~~~~~~~~~~
void BloomTree::construct_allsome_nodes (u32 compressor)
{
// if we already have a filter, just make sure it is in the pre-loaded
// state (or beyond)
if (bf != nullptr)
{ bf->preload(); return; }
string bfKindStr = "." + BloomFilter::filter_kind_to_string(bfkind_allsome);
string compressionDesc = "." + BitVector::compressor_to_string(compressor);
if (compressionDesc == ".uncompressed") compressionDesc = "";
string newBfFilename = name + bfKindStr + compressionDesc + ".bf";
// if this is a leaf, create and load its filter
// bvs[0] = B'all(x) = B(x)
// bvs[1] = B'some(x) = all zeros
// Note that both of these will be modified when the parent is constructed
if (isLeaf)
{
if (dbgTraversal)
cerr << "\n=== constructing leaf (for allsome) " << name << " ===" << endl;
BloomFilter* bfInput = BloomFilter::bloom_filter(bfFilename);
bfInput->load();
if (bfInput->numBitVectors!=1)
fatal ("error: " + bfFilename + " contains more than one bit vector");
BitVector* bvInput = bfInput->get_bit_vector(0);
if (bvInput->is_compressed())
fatal ("error: " + bfFilename + " contains a compressed bit vector");
bf = new AllSomeFilter(newBfFilename);
bf->copy_properties(bfInput);
bf->steal_bits(bfInput,/*src*/0,/*dst*/0,compressor);
delete bfInput;
bf->new_bits(bvcomp_zeros,1);
// if this leaf has no parent (i.e. it's an orphan), we need to finish
// it now, the same way we do (later in this function) for any other
// parentless node
bool finished = false;
if ((parent == nullptr) || (parent->is_dummy()))
finished = true;
// $$$ we don't necessarily want to save it; we want to mark it to be
// .. saved, but keep it around if we have enough room, since we'll
// .. need it to compute its parent, at which time we'll change it
bfFilename = newBfFilename;
bf->reportSave = reportSave;
save(finished);
unloadable();
return;
}
// otherwise this is an internal node; first construct its descendants
if (dbgTraversal)
{
if (is_dummy())
cerr << "\n=== constructing children of (dummy node) ===" << endl;
else
cerr << "\n=== constructing children of " << name << " (#" << (++dbgTraversalCounter) << ") ===" << endl;
}
for (const auto& child : children)
child->construct_allsome_nodes(compressor);
// if this is a dummy node, we don't need to build it, but we do mark its
// children as unloadable
// nota bene: we don't expect a dummy to be a child of some other node
if (is_dummy())
{
for (const auto& child : children)
child->unloadable();
return;
}
// create this filter from its child filters
if (dbgTraversal)
cerr << "\n=== constructing " << name << " ===" << endl;
if (bf != nullptr)
fatal ("internal error: unexpected non-null filter for " + bfFilename);
bool isFirstChild = true;
for (const auto& child : children)
{
if (dbgTraversal)
cerr << "loading " << child->name << " to compute parent" << endl;
child->load();
if (child->bf == nullptr)
fatal ("internal error: failed to load " + child->bfFilename);
BitVector* childBvAll = child->bf->get_bit_vector(0);
BitVector* childBvSome = child->bf->get_bit_vector(1);
if ((childBvAll == nullptr) or (childBvSome == nullptr))
fatal ("internal error: failed to load bit vector(s) for " + child->bfFilename);
if (childBvAll->is_compressed())
fatal ("error: " + child->bfFilename + " contains compressed bit vector(s)");
if ((childBvSome->is_compressed())
&& (childBvSome->compressor() != bvcomp_zeros)
&& (childBvSome->compressor() != bvcomp_ones))
fatal ("error: " + child->bfFilename + " contains compressed bit vector(s)");
if (dbgTraversal)
cerr << "incorporating " << child->name << " into parent" << endl;
if (isFirstChild) // incorporate first child's filters
{
// bvs[0] = Bcap(x) = B'all(child)
// bvs[1] = Bcup(x) = B'all(child) union B'some(child)
bf = new AllSomeFilter(child->bf,newBfFilename);
bf->new_bits(childBvAll,compressor,0);
bf->new_bits(childBvAll,compressor,1);
bf->union_with(childBvSome,1);
isFirstChild = false;
}
else // incorporate later child's filters
{
// bvs[0] = Bcap(x) = Bcap(x) intersect B'all(child)
// bvs[1] = Bcup(x) = Bcup(x) union B'all(child) union B'some(child)
bf->intersect_with(childBvAll,0);
bf->union_with(childBvAll,1);
bf->union_with(childBvSome,1);
}
}
if (bf == nullptr)
fatal ("internal error:"
" in construct_allsome_nodes(\"" + name + "\")"
+ ", non-leaf node has no children");
// convert this node from Bcap,Bcup to B'all,B'some
// bvs[0] = B'all(x) = Bcap(x), no modification needed
// bvs[1] = B'some(x) = Bcup(x) \ Bcap(x)
BitVector* bvCap = bf->get_bit_vector(0);
bf->mask_with(bvCap,1);
// finish the child nodes
// bvs[0] = Bsome(c) = B'some(c), no modification needed
// bvs[1] = Ball(c) = B'all(c) \ B'all(x)
if (not dbgInhibitChildUpdate)
{
BitVector* bvAll = bf->get_bit_vector(0);
for (const auto& child : children)
{
if (dbgTraversal)
cerr << "loading " << child->name << " for update" << endl;
child->load();
child->bf->mask_with(bvAll,0);
child->bf->reportSave = reportSave;
child->save(/*finished*/ true);
child->unloadable();
}
}
// if this node has no parent, we need to finish it now
bool finished = false;
if ((parent == nullptr) || (parent->is_dummy()))
finished = true;
// $$$ we don't necessarily want to save it; we want to mark it to be
// .. saved, but keep it around if we have enough room, since we'll
// .. need it to compute its parent, at which time we'll change it
bfFilename = newBfFilename;
bf->reportSave = reportSave;
save(finished);
unloadable();
}
//~~~~~~~~~~
// build determined tree
//~~~~~~~~~~
void BloomTree::construct_determined_nodes (u32 compressor)
{
// if we already have a filter, just make sure it is in the pre-loaded
// state (or beyond)
if (bf != nullptr)
{ bf->preload(); return; }
string bfKindStr = "." + BloomFilter::filter_kind_to_string(bfkind_determined);
string compressionDesc = "." + BitVector::compressor_to_string(compressor);
if (compressionDesc == ".uncompressed") compressionDesc = "";
string newBfFilename = name + bfKindStr + compressionDesc + ".bf";
// if this is a leaf, create and load its filter
// bvs[0] = Bdet(x) = all ones
// bvs[1] = Bhow(x) = B(x)
// Note that both of these will be modified when the parent is constructed
if (isLeaf)
{
if (dbgTraversal)
cerr << "\n=== constructing leaf (for determined) " << name << " ===" << endl;
BloomFilter* bfInput = BloomFilter::bloom_filter(bfFilename);
bfInput->load();
if (bfInput->numBitVectors!=1)
fatal ("error: " + bfFilename + " contains more than one bit vector");
BitVector* bvInput = bfInput->get_bit_vector(0);
if (bvInput->is_compressed())
fatal ("error: " + bfFilename + " contains a compressed bit vector");
bf = new DeterminedFilter(newBfFilename);
bf->copy_properties(bfInput);
bf->steal_bits(bfInput,/*src*/0,/*dst*/1,compressor);
delete bfInput;
bf->new_bits(compressor,0);
BitVector* bvDet = bf->get_bit_vector(0);
bvDet->fill(1);
// if this leaf has no parent (i.e. it's an orphan), we need to finish
// it now, the same way we do (later in this function) for any other
// parentless node
bool finished = false;
if ((parent == nullptr) || (parent->is_dummy()))
{
BitVector* bvDet = bf->get_bit_vector(0);
bf->intersect_with(bvDet,1);
finished = true;
}
// $$$ we don't necessarily want to save it; we want to mark it to be
// .. saved, but keep it around if we have enough room, since we'll
// .. need it to compute its parent, at which time we'll change it
bfFilename = newBfFilename;
bf->reportSave = reportSave;
save(finished);
unloadable();
return;
}
// otherwise this is an internal node; first construct its descendants
if (dbgTraversal)
{
if (is_dummy())
cerr << "\n=== constructing children of (dummy node) ===" << endl;
else
cerr << "\n=== constructing children of " << name << " (#" << (++dbgTraversalCounter) << ") ===" << endl;
}
for (const auto& child : children)
child->construct_determined_nodes(compressor);
// if this is a dummy node, we don't need to build it, but we do mark its
// children as unloadable
// nota bene: we don't expect a dummy to be a child of some other node
if (is_dummy())
{
for (const auto& child : children)
child->unloadable();
return;
}
// create this filter from its child filters
// bvs[0] = Bdet(x) = Bcap(x) union complement of Bcup(x)
// = Bhow(x) union z
// bvs[1] = Bhow(x) = Bcap(x)
// = intersection, over children c, of Bhow(c)
// z = intersection, over children c, of (Bdet(c) intersect complement of Bhow(c))
if (dbgTraversal)
cerr << "\n=== constructing " << name << " ===" << endl;
if (bf != nullptr)
fatal ("internal error: unexpected non-null filter for " + bfFilename);
bool isFirstChild = true;
for (const auto& child : children)
{
if (dbgTraversal)
cerr << "loading " << child->name << " to compute parent" << endl;
child->load();
if (child->bf == nullptr)
fatal ("internal error: failed to load " + child->bfFilename);
BitVector* childBvDet = child->bf->get_bit_vector(0);
BitVector* childBvHow = child->bf->get_bit_vector(1);
if ((childBvHow == nullptr) or (childBvDet == nullptr))
fatal ("internal error: failed to load bit vector(s) for " + child->bfFilename);
if ((childBvHow->is_compressed()) || (childBvDet->is_compressed()))
fatal ("error: " + child->bfFilename + " contains compressed bit vector(s)");
if (dbgTraversal)
cerr << "incorporating " << child->name << " into parent" << endl;
if (isFirstChild) // incorporate first child's filters
{
// bvs[0] = z = Bdet(c) intersect complement of Bhow(c)
// bvs[1] = Bhow(x) = Bhow(c)
bf = new DeterminedFilter(child->bf,newBfFilename);
bf->new_bits(childBvDet,compressor,0);
bf->intersect_with_complement(childBvHow,0);
bf->new_bits(childBvHow,compressor,1);
isFirstChild = false;
}
else // incorporate later child's filters
{
// bvs[0] = z = z intersect Bdet(c) intersect complement of Bhow(c)
// bvs[1] = Bhow(x) = Bhow(x) intersect Bhow(c)
bf->intersect_with(childBvDet,0);
bf->intersect_with_complement(childBvHow,0);
bf->intersect_with(childBvHow,1);
}
}
if (bf == nullptr)
fatal ("internal error:"
" in construct_determined_nodes(\"" + name + "\")"
+ ", non-leaf node has no children");
// convert this node from the temporary vectors computed in the loop
// bvs[0] = Bdet(x) = Bhow(x) union z
// bvs[1] = Bhow(x), no modification needed
BitVector* bvHow = bf->get_bit_vector(1);
bf->union_with(bvHow,0);
// incorporate bits from this filter, to finish the child nodes
// Idet(c) = active bits of Bdet(c) = complement of Bdet(x)
// Ihow(c) = active bits of Bhow(c) = Bdet(c) intersect Idet(c)
// bvs[0] = Bdet(c) with inactive bits zeroed = Bdet(c) intersect Idet(c)
// = Bdet(c) intersect complement of Bdet(x)
// bvs[1] = Bhow(c) with inactive bits zeroed = Bhow(c) intersect Ihow(c)
// = Bhow(c) intersect Bdet(c) intersect Idet(c)
// = Bhow(c) intersect bvs[0]
if (not dbgInhibitChildUpdate)
{
BitVector* bvDet = bf->get_bit_vector(0);
for (const auto& child : children)
{
if (dbgTraversal)
cerr << "loading " << child->name << " for update" << endl;
child->load();
child->bf->intersect_with_complement(bvDet,0);
BitVector* bvs0 = child->bf->get_bit_vector(0);
child->bf->intersect_with(bvs0,1);
child->bf->reportSave = reportSave;
child->save(/*finished*/ true);
child->unloadable();
}
}
// if this node has no parent, we need to finish it now
// Idet(x) = active bits of Bdet(x) = all 1s
// Ihow(x) = active bits of Bhow(x) = Bdet(x) intersect Idet(x)
// = Bdet(x)
// bvs[0] = Bdet(x) with inactive bits zeroed = Bdet(x) intersect Idet(x)
// = Bdet(x), no modification needed
// bvs[1] = Bhow(x) with inactive bits zeroed = Bhow(x) intersect Ihow(x)
// = Bhow(x) intersect Bdet(x)
bool finished = false;
if ((parent == nullptr) || (parent->is_dummy()))
{
BitVector* bvDet = bf->get_bit_vector(0);
bf->intersect_with(bvDet,1);
finished = true;
}
// $$$ we don't necessarily want to save it; we want to mark it to be
// .. saved, but keep it around if we have enough room, since we'll
// .. need it to compute its parent, at which time we'll change it
bfFilename = newBfFilename;
bf->reportSave = reportSave;
save(finished);
unloadable();
}
//~~~~~~~~~~
// build determined,brief tree
//~~~~~~~~~~
void BloomTree::construct_determined_brief_nodes (u32 compressor)
{
// if we already have a filter, just make sure it is in the pre-loaded
// state (or beyond)
if (bf != nullptr)
{ bf->preload(); return; }
string bfKindStr = "." + BloomFilter::filter_kind_to_string(bfkind_determined_brief);
string compressionDesc = "." + BitVector::compressor_to_string(compressor);
if (compressionDesc == ".uncompressed") compressionDesc = "";
string newBfFilename = name + bfKindStr + compressionDesc + ".bf";
// if this is a leaf, create and load its filter
// bvs[0] = Bdet(x) = all ones
// bvs[1] = Bhow(x) = B(x)
// Note that both of these will be modified when the parent is constructed
if (isLeaf)
{
if (dbgTraversal)
cerr << "\n=== constructing leaf (for determined,brief) " << name << " ===" << endl;
BloomFilter* bfInput = BloomFilter::bloom_filter(bfFilename);
bfInput->load();
if (bfInput->numBitVectors!=1)
fatal ("error: " + bfFilename + " contains more than one bit vector");
BitVector* bvInput = bfInput->get_bit_vector(0);
if (bvInput->is_compressed())
fatal ("error: " + bfFilename + " contains a compressed bit vector");
bf = new DeterminedBriefFilter(newBfFilename);
bf->copy_properties(bfInput);
bf->steal_bits(bfInput,/*src*/0,/*dst*/1,compressor);
delete bfInput;
bf->new_bits(compressor,0);
BitVector* bvDet = bf->get_bit_vector(0);
bvDet->fill(1);
bf->get_bit_vector(0)->filterInfo = DeterminedBriefFilter::notSqueezed;
bf->get_bit_vector(1)->filterInfo = DeterminedBriefFilter::notSqueezed;
// if this leaf has no parent (i.e. it's an orphan), we need to finish
// it now, the same way we do (later in this function) for any other
// parentless node
bool finished = false;
if ((parent == nullptr) || (parent->is_dummy()))
{
bf->squeeze_by(bvDet,1);
bf->get_bit_vector(0)->filterInfo = DeterminedBriefFilter::squeezed;
bf->get_bit_vector(1)->filterInfo = DeterminedBriefFilter::squeezed;
finished = true;
}
// $$$ we don't necessarily want to save it; we want to mark it to be
// .. saved, but keep it around if we have enough room, since we'll
// .. need it to compute its parent, at which time we'll change it
bfFilename = newBfFilename;
bf->reportSave = reportSave;
save(finished);
unloadable();
return;
}
// otherwise this is an internal node; first construct its descendants
if (dbgTraversal)
{
if (is_dummy())
cerr << "\n=== constructing children of (dummy node) ===" << endl;
else
cerr << "\n=== constructing children of " << name << " (#" << (++dbgTraversalCounter) << ") ===" << endl;
}
for (const auto& child : children)
child->construct_determined_brief_nodes(compressor);
// if this is a dummy node, we don't need to build it, but we do mark its
// children as unloadable
// nota bene: we don't expect a dummy to be a child of some other node
if (is_dummy())
{
for (const auto& child : children)
child->unloadable();
return;
}
// create this filter from its child filters
// bvs[0] = Bdet(x) = Bcap(x) union complement of Bcup(x)
// = Bhow(x) union z
// bvs[1] = Bhow(x) = Bcap(x)
// = intersection, over children c, of Bhow(c)
// z = intersection, over children c, of (Bdet(c) intersect complement of Bhow(c))
if (dbgTraversal)
cerr << "\n=== constructing " << name << " ===" << endl;
if (bf != nullptr)
fatal ("internal error: unexpected non-null filter for " + bfFilename);
bool isFirstChild = true;
for (const auto& child : children)
{
if (dbgTraversal)
cerr << "loading " << child->name << " to compute parent" << endl;
child->load();
if (child->bf == nullptr)
fatal ("internal error: failed to load " + child->bfFilename);
BitVector* childBvDet = child->bf->get_bit_vector(0);
BitVector* childBvHow = child->bf->get_bit_vector(1);
if ((childBvHow == nullptr) or (childBvDet == nullptr))
fatal ("internal error: failed to load bit vector(s) for " + child->bfFilename);
if ((childBvHow->is_compressed()) || (childBvDet->is_compressed()))
fatal ("error: " + child->bfFilename + " contains compressed bit vector(s)");
if (dbgTraversal)
cerr << "incorporating " << child->name << " into parent" << endl;
if (isFirstChild) // incorporate first child's filters
{
// bvs[0] = z = Bdet(c) intersect complement of Bhow(c)
// bvs[1] = Bhow(x) = Bhow(c)
bf = new DeterminedBriefFilter(child->bf,newBfFilename);
bf->new_bits(childBvDet,compressor,0);
bf->intersect_with_complement(childBvHow,0);
bf->new_bits(childBvHow,compressor,1);
bf->get_bit_vector(0)->filterInfo = DeterminedBriefFilter::notSqueezed;