-
Notifications
You must be signed in to change notification settings - Fork 4
/
ribbon.hpp
869 lines (764 loc) · 31.2 KB
/
ribbon.hpp
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
// Copyright (c) Lorenz Hübschle-Schneider
// All Rights Reserved. This source code is licensed under the Apache 2.0
// License (found in the LICENSE file in the root directory).
#pragma once
#include "backsubst.hpp"
#include "config.hpp"
#include "construction.hpp"
#include "hasher.hpp"
#include "permute.hpp"
#include "query.hpp"
#include "rocksdb/stop_watch.h"
#include "storage.hpp"
#include "thresh_compress.hpp"
#include "serialization.hpp"
#include <tlx/logger.hpp>
#include <tlx/define/likely.hpp>
#include <fstream>
#include <iostream>
#include <type_traits>
namespace ribbon {
namespace {
template <typename Config>
// Base level filter is always *at least* 64-bit
struct BaseConfig : Config {
using CoeffRow = std::conditional_t<sizeof(typename Config::CoeffRow) < 8,
uint64_t, typename Config::CoeffRow>;
static constexpr typename Config::Index kBucketSize =
recommended_bucket_size<8u * sizeof(CoeffRow), Config::kThreshMode>;
};
// Ribbon base class
template <typename Config>
class ribbon_base {
public:
IMPORT_RIBBON_CONFIG(Config);
using Hasher = ChooseThreshold<Config>;
using mhc_or_key_t = typename HashTraits<Hasher>::mhc_or_key_t;
ribbon_base() {}
ribbon_base(size_t num_slots, double slots_per_item, uint64_t seed) {
if constexpr (!Config::kUseMHC)
Init(num_slots, slots_per_item, seed);
else
assert(false);
}
// TODO make this invalid if !kUseMHC
ribbon_base(size_t num_slots, double slots_per_item, uint64_t seed,
uint32_t idx) {
if constexpr (Config::kUseMHC)
Init(num_slots, slots_per_item, seed, idx);
else
assert(false);
}
// Specialisation for Master Hash Code Hasher
template <typename C = Config>
std::enable_if_t<C::kUseMHC, void>
Init(size_t num_slots, double slots_per_item, uint64_t seed, uint32_t idx) {
sLOGC(Config::log) << "ribbon_base (MHC) called with" << num_slots
<< "slots and" << slots_per_item << "slots per item";
slots_per_item_ = slots_per_item;
hasher_.Seed(seed, idx);
if (num_slots > 0)
Prepare(num_slots);
}
template <typename C = Config>
std::enable_if_t<!C::kUseMHC, void>
Init(size_t num_slots, double slots_per_item, uint64_t seed) {
sLOGC(Config::log) << "ribbon_base called with" << num_slots
<< "slots and" << slots_per_item << "slots per item";
static_assert(!Config::kUseMHC, "wat");
slots_per_item_ = slots_per_item;
hasher_.Seed(seed);
if (num_slots > 0)
Prepare(num_slots);
}
void Realloc(size_t num_slots, double slots_per_item) {
sLOGC(Config::log) << "Reallocating ribbon with a bigger size:" << num_slots
<< "slots," << slots_per_item << "slots per item";
slots_per_item_ = slots_per_item;
Prepare(num_slots);
}
inline std::pair<bool, bool> QueryFilter(const Key &key) const {
if constexpr (!kIsFilter) {
assert(false);
return std::pair(true, false);
}
if constexpr (kUseInterleavedSol) {
// it can happen that a filter is empty but still gets queries
// because the prior filter would have bumped that key if it had
// been inserted
if (TLX_UNLIKELY(sol_.GetNumSlots() == 0))
return std::make_pair(false, false);
return InterleavedFilterQuery(key, hasher_, sol_);
} else if constexpr (kUseCacheLineStorage) {
if (TLX_UNLIKELY(sol_.GetNumSlots() == 0))
return std::make_pair(false, false);
return SimpleFilterQuery(key, hasher_, sol_);
} else {
if (TLX_UNLIKELY(storage_.GetNumSlots() == 0))
return std::make_pair(false, false);
return SimpleFilterQuery(key, hasher_, storage_);
}
}
template <typename C = Config>
inline std::enable_if_t<C::kUseMHC, std::pair<bool, bool>>
QueryFilterMHC(const mhc_or_key_t mhc) const {
if constexpr (!kIsFilter) {
assert(false);
return std::pair(true, false);
}
if constexpr (kUseInterleavedSol) {
// it can happen that a filter is empty but still gets queries
// because the prior filter would have bumped that key if it had
// been inserted
if (TLX_UNLIKELY(sol_.GetNumSlots() == 0))
return std::make_pair(false, false);
return InterleavedFilterQuery(mhc, hasher_, sol_);
} else if constexpr (kUseCacheLineStorage) {
if (TLX_UNLIKELY(sol_.GetNumSlots() == 0))
return std::make_pair(false, false);
return SimpleFilterQuery(mhc, hasher_, sol_);
} else {
if (TLX_UNLIKELY(storage_.GetNumSlots() == 0))
return std::make_pair(false, false);
return SimpleFilterQuery(mhc, hasher_, storage_);
}
}
template <typename Iterator>
bool AddRange(
Iterator begin, Iterator end,
std::vector<std::conditional_t<
kUseMHC, std::conditional_t<kIsFilter, mhc_or_key_t, std::pair<mhc_or_key_t, ResultRow>>,
typename std::iterator_traits<Iterator>::value_type>> *bump_vec) {
const auto input_size = end - begin;
LOGC(Config::log) << "Constructing for " << storage_.GetNumSlots()
<< " slots, " << storage_.GetNumStarts() << " starts, "
<< storage_.GetNumBuckets() << " buckets";
rocksdb::StopWatchNano timer(true);
bool success;
if constexpr (kUseMHC) {
success = BandingAddRangeMHC(&storage_, hasher_, begin, end, bump_vec);
} else {
success = BandingAddRange(&storage_, hasher_, begin, end, bump_vec);
}
LOGC(Config::log) << "Insertion of " << input_size << " items took "
<< timer.ElapsedNanos(true) / 1e6 << "ms";
if (bump_vec == nullptr)
num_bumped = success ? 0 : input_size;
else
num_bumped = bump_vec->size();
empty_slots = static_cast<ssize_t>(storage_.GetNumSlots()) -
input_size + num_bumped;
sLOGC(Config::log) << "Bumped" << num_bumped << "out of" << input_size
<< "=" << (num_bumped * 100.0 / input_size)
<< "% with" << slots_per_item_ << "slots per item =>"
<< empty_slots << "empty slots ="
<< empty_slots * 100.0 / storage_.GetNumSlots() << "%";
return success;
}
void BackSubst() {
if (storage_.GetNumSlots() == 0)
return;
rocksdb::StopWatchNano timer(true);
if constexpr (kUseInterleavedSol) {
InterleavedBackSubst(storage_, &sol_);
// move metadata by swapping pointers
sol_.MoveMetadata(&storage_);
storage_.Reset();
} else if constexpr (kUseCacheLineStorage) {
sol_.Prepare(storage_.GetNumSlots());
SimpleBackSubst(storage_, &sol_);
// copy metadata one-by-one
for (Index bucket = 0; bucket < storage_.GetNumBuckets(); ++bucket) {
sol_.SetMeta(bucket, storage_.GetMeta(bucket));
}
storage_.Reset();
} else {
SimpleBackSubst(storage_, &storage_);
}
LOGC(Config::log) << "Backsubstitution for " << storage_.GetNumSlots()
<< " slots took " << timer.ElapsedNanos(true) / 1e6
<< "ms";
}
inline std::pair<bool, ResultRow> QueryRetrieval(const Key &key) const {
if constexpr (kIsFilter || kUseMHC) {
assert(false);
return std::make_pair(true, 0);
}
if constexpr (kUseInterleavedSol) {
return InterleavedRetrievalQuery(key, hasher_, sol_);
} else if constexpr (kUseCacheLineStorage) {
return SimpleRetrievalQuery(key, hasher_, sol_);
} else {
return SimpleRetrievalQuery(key, hasher_, storage_);
}
}
inline std::pair<bool, ResultRow> QueryRetrievalMHC(const mhc_or_key_t mhc) const {
if constexpr (kIsFilter || !kUseMHC) {
assert(false);
return std::make_pair(true, 0);
}
if constexpr (kUseInterleavedSol) {
return InterleavedRetrievalQuery(mhc, hasher_, sol_);
} else if constexpr (kUseCacheLineStorage) {
return SimpleRetrievalQuery(mhc, hasher_, sol_);
} else {
return SimpleRetrievalQuery(mhc, hasher_, storage_);
}
}
std::tuple<ssize_t, ssize_t, double, ssize_t> GetStats() const {
double frac_empty =
static_cast<double>(empty_slots) / storage_.GetNumSlots();
ssize_t thresh_bytes = 0;
if constexpr (kThreshMode == ThreshMode::onebit) {
thresh_bytes = hasher_.Size();
}
return std::make_tuple(num_bumped, empty_slots, frac_empty, thresh_bytes);
}
size_t Size() const {
size_t sol_size, num_slots;
if constexpr (kUseInterleavedSol || kUseCacheLineStorage) {
sol_size = sol_.Size();
num_slots = sol_.GetNumSlots();
} else {
sol_size = storage_.Size();
num_slots = storage_.GetNumSlots();
}
size_t extra_bytes = 0, extra_count = 0;
if constexpr (kThreshMode == ThreshMode::onebit) {
extra_bytes = hasher_.Size();
extra_count = hasher_.NumEntries();
}
sLOGC(Config::log) << "Size:" << sol_size << "Bytes for" << num_slots
<< "slots +" << extra_bytes << "Bytes for"
<< extra_count << "thresholds";
return sol_size + extra_bytes;
}
/*
void PrintStats() const {
if constexpr (Config::kThreshMode == ThreshMode::onebit) {
sLOG1 << hasher_.hits << "of" << hasher_.count
<< "filter hits =" << hasher_.hits * 100.0 / hasher_.count
<< "%, size" << hasher_.filter_.size() << "with"
<< hasher_.buffer_.size() << "potential 1s ="
<< hasher_.buffer_.size() * 100.0 / hasher_.filter_.size()
<< "%";
hasher_.hits = 0;
hasher_.count = 0;
}
}
*/
protected:
void Prepare(size_t num_slots) {
if (num_slots == 0)
return;
// round up to next multiple of kCoeffBits for interleaved storage
if constexpr (kUseInterleavedSol)
num_slots = ((num_slots + kCoeffBits - 1) / kCoeffBits) * kCoeffBits;
else
num_slots = std::max(num_slots, static_cast<size_t>(kCoeffBits));
storage_.Prepare(num_slots);
hasher_.Prepare(num_slots, slots_per_item_);
}
template <typename Iterator, typename C = Config>
std::enable_if_t<C::kUseMHC,
std::unique_ptr<std::conditional_t<
kIsFilter, mhc_or_key_t, std::pair<mhc_or_key_t, ResultRow>>[]>>
PrepareAddRangeMHC(Iterator begin, Iterator end) {
// this is the top-level filter, transform from vector<Key> to vector<mhc_t>
rocksdb::StopWatchNano timer(true);
const auto input_size = end - begin;
using value_type =
std::conditional_t<kIsFilter, mhc_or_key_t, std::pair<mhc_or_key_t, ResultRow>>;
auto input = std::make_unique<value_type[]>(input_size);
auto it = begin;
for (ssize_t i = 0; i < input_size; i++, ++it) {
if constexpr (kIsFilter)
input[i] = hasher_.GetMHC(*it);
else
input[i] = std::make_pair(hasher_.GetMHC(it->first), it->second);
}
LOGC(Config::log) << "MHC input transformation took "
<< timer.ElapsedNanos() / 1e6 << "ms";
return input;
}
// these (De)SerializeIntern methods are called on each level, while the
// (De)Serialize methods below are only called on the top level
virtual void SerializeIntern(std::ostream &os) {
if constexpr (kUseCacheLineStorage) {
throw config_error("kUseCacheLineStorage not supported");
} else if constexpr (kUseInterleavedSol) {
sol_.SerializeIntern(os);
} else {
storage_.SerializeIntern(os);
}
hasher_.SerializeIntern(os);
}
virtual void DeserializeIntern(std::istream &is, bool switchendian, uint64_t seed, uint32_t idx) {
Index num_buckets = 0;
if constexpr (kUseCacheLineStorage) {
throw config_error("kUseCacheLineStorage not supported");
} else if constexpr (kUseInterleavedSol) {
sol_.DeserializeIntern(is, switchendian);
num_buckets = sol_.GetNumBuckets();
} else {
storage_.DeserializeIntern(is, switchendian);
num_buckets = storage_.GetNumBuckets();
}
hasher_.DeserializeIntern(is, switchendian, num_buckets);
if constexpr (Config::kUseMHC) {
hasher_.Seed(seed, idx);
} else {
hasher_.Seed(seed);
}
}
// these methods are in ribbon_base so they can still be called by
// the base case ribbon
void Serialize(std::ostream &os, uint8_t depth) {
auto mask = os.exceptions();
os.exceptions(~std::ios::goodbit);
if constexpr (kUseCacheLineStorage)
throw config_error("kUseCacheLineStorage not supported");
os.write("BuRR", 4);
uint16_t bom = 0xFEFF;
os.write(reinterpret_cast<const char *>(&bom), sizeof(uint16_t));
uint16_t version = 0;
os.write(reinterpret_cast<const char *>(&version), sizeof(uint16_t));
char tmp = sizeof(CoeffRow);
os.write(&tmp, 1);
tmp = sizeof(ResultRow);
os.write(&tmp, 1);
tmp = kResultBits;
os.write(&tmp, 1);
tmp = sizeof(Index);
os.write(&tmp, 1);
os.write(reinterpret_cast<const char *>(&kBucketSize), sizeof(Index));
tmp = sizeof(Hash);
os.write(&tmp, 1);
tmp = static_cast<int>(kThreshMode);
os.write(&tmp, 1);
tmp = kUseMultiplyShiftHash | (kIsFilter << 1) |
(kFirstCoeffAlwaysOne << 2) | (kSparseCoeffs << 3) |
(kUseInterleavedSol << 4) | (kUseMHC << 5);
os.write(&tmp, 1);
uint8_t d = depth;
os.write(reinterpret_cast<const char *>(&d), sizeof(uint8_t));
uint64_t seed = this->hasher_.GetSeed();
os.write(reinterpret_cast<const char *>(&seed), sizeof(uint64_t));
// this has to call the overridden method in the child class so
// all levels of the data structure are serialized
SerializeIntern(os);
// set exception mask to original mask again
os.exceptions(mask);
}
// FIXME: should all internal structures be reset on deserialize?
void Deserialize(std::istream &is, uint8_t depth) {
auto mask = is.exceptions();
is.exceptions(~std::ios::goodbit);
if constexpr (kUseCacheLineStorage)
throw config_error("kUseCacheLineStorage not supported");
char magic[4];
is.read(magic, 4);
if (strncmp(magic, "BuRR", 4))
throw parse_error("wrong magic number");
uint16_t bom;
is.read(reinterpret_cast<char *>(&bom), sizeof(uint16_t));
bool switchendian = false;
if (bom == 0xFFFE)
switchendian = true;
else if (bom != 0xFEFF)
throw parse_error("invalid endianness specification");
uint16_t version = 0;
is.read(reinterpret_cast<char *>(&version), sizeof(uint16_t));
if (version != 0)
throw parse_error("invalid version number");
char tmp;
is.read(&tmp, 1);
if (tmp != sizeof(CoeffRow))
throw config_error("sizeof(CoeffRow) mismatch");
is.read(&tmp, 1);
if (tmp != sizeof(ResultRow))
throw config_error("sizeof(ResultRow) mismatch");
is.read(&tmp, 1);
if (tmp != kResultBits)
throw config_error("kResultBits mismatch");
is.read(&tmp, 1);
if (tmp != sizeof(Index))
throw config_error("sizeof(Index) mismatch");
Index bucketsz;
is.read(reinterpret_cast<char *>(&bucketsz), sizeof(Index));
if (switchendian && !bswap_generic(bucketsz))
throw parse_error("error converting endianness");
else if (bucketsz != kBucketSize)
throw config_error("kBucketSize mismatch");
is.read(&tmp, 1);
if (tmp != sizeof(Hash))
throw config_error("sizeof(Hash) mismatch");
is.read(&tmp, 1);
if (tmp != static_cast<int>(kThreshMode))
throw config_error("kThreshMode mismatch");
is.read(&tmp, 1);
if ((tmp & 0x1) != kUseMultiplyShiftHash)
throw config_error("kUseMultiplyShiftHash mismatch");
else if (((tmp >> 1) & 0x1) != kIsFilter)
throw config_error("kIsFilter mismatch");
else if (((tmp >> 2) & 0x1) != kFirstCoeffAlwaysOne)
throw config_error("kFirstCoeffAlwaysOne mismatch");
else if (((tmp >> 3) & 0x1) != kSparseCoeffs)
throw config_error("kSparseCoeffs mismatch");
else if (((tmp >> 4) & 0x1) != kUseInterleavedSol)
throw config_error("kUseInterleavedSol mismatch");
else if (((tmp >> 5) & 0x1) != kUseMHC)
throw config_error("kUseMHC mismatch");
uint8_t d;
is.read(reinterpret_cast<char *>(&d), sizeof(uint8_t));
if (d != depth)
throw config_error("depth mismatch");
uint64_t seed;
is.read(reinterpret_cast<char *>(&seed), sizeof(uint64_t));
if (switchendian && !bswap_generic(seed))
throw parse_error("error converting endianness");
// this has to call the overridden method in the child class so
// all levels of the data structure are deserialized
DeserializeIntern(is, switchendian, seed, 0);
// reset exception mask
is.exceptions(mask);
}
// convenience methods
void Serialize(const std::string &filename, uint8_t depth) {
std::ofstream os(filename, std::ios::binary|std::ios::out|std::ios::trunc);
if (!os.is_open())
throw file_open_error("unable to open file " + filename + " for writing");
Serialize(os, depth);
}
void Deserialize(const std::string &filename, uint8_t depth) {
std::ifstream is(filename, std::ios::binary|std::ios::in);
if (!is.is_open())
throw file_open_error("unable to open file " + filename + " for reading");
Deserialize(is, depth);
}
// statistics
ssize_t num_bumped;
ssize_t empty_slots;
// actual data
double slots_per_item_;
BasicStorage<Config> storage_;
template <bool /* cls */, bool /* int */, typename C>
struct sol_t {
// dummy
using type = sol_t<false, false, C>;
};
template <typename C>
struct sol_t<true, false, C> {
using type = CacheLineStorage<C>;
};
template <typename C>
struct sol_t<false, true, C> {
using type = InterleavedSolutionStorage<C>;
};
typename sol_t<kUseCacheLineStorage, kUseInterleavedSol, Config>::type sol_;
Hasher hasher_;
};
} // namespace
template <uint8_t depth, typename Config>
class ribbon_filter : public ribbon_base<Config> {
public:
IMPORT_RIBBON_CONFIG(Config);
using Super = ribbon_base<Config>;
using Super::slots_per_item_;
using mhc_or_key_t = typename Super::mhc_or_key_t;
ribbon_filter() = default;
ribbon_filter(std::istream &is) {
Deserialize(is);
}
ribbon_filter(const std::string &filename) {
Deserialize(filename);
}
// MHC top-level constructor
template <typename C = Config>
ribbon_filter(size_t num_slots, double slots_per_item, uint64_t seed,
typename std::enable_if<C::kUseMHC>::type * = 0)
: Super(num_slots, slots_per_item, seed, 0),
child_ribbon_(0, slots_per_item, seed, 1) {}
protected:
// MHC child constructor
template <typename C = Config>
ribbon_filter(size_t num_slots, double slots_per_item, uint64_t seed,
uint32_t idx, typename std::enable_if<C::kUseMHC>::type * = 0)
: Super(num_slots, slots_per_item, seed, idx),
child_ribbon_(0, slots_per_item, seed, idx + 1) {}
public:
// non-MHC top-level constructor
template <typename C = Config>
ribbon_filter(size_t num_slots, double slots_per_item, uint64_t seed,
typename std::enable_if<!C::kUseMHC>::type * = 0)
: Super(num_slots, slots_per_item, seed),
child_ribbon_(0, slots_per_item, seed + 1) {}
// non-MHC init
template <typename C = Config>
std::enable_if_t<!C::kUseMHC, void>
Init(size_t num_slots, double slots_per_item, uint64_t seed) {
Super::Init(num_slots, slots_per_item, seed);
child_ribbon_.Init(0, slots_per_item, seed + 1);
}
// MHC init
template <typename C = Config>
std::enable_if_t<C::kUseMHC, void>
Init(size_t num_slots, double slots_per_item, uint64_t seed, uint32_t idx) {
Super::Init(num_slots, slots_per_item, seed, idx);
child_ribbon_.Init(0, slots_per_item, seed, idx + 1);
}
template <typename Iterator, typename C = Config>
std::enable_if_t<!C::kUseMHC, bool> AddRange(Iterator begin, Iterator end) {
const auto input_size = end - begin;
std::vector<std::conditional_t<kIsFilter, Key, std::pair<Key, ResultRow>>> bumped_items;
bumped_items.reserve(std::max(
0l, static_cast<ssize_t>((1 - slots_per_item_) * input_size)));
if (!Super::AddRange(begin, end, &bumped_items))
return false;
if (bumped_items.size() == 0)
return true;
// child ribbon will round this up as needed
const size_t child_slots =
std::max(size_t{1},
static_cast<size_t>(slots_per_item_ * bumped_items.size()));
child_ribbon_.Prepare(child_slots);
return child_ribbon_.AddRange(bumped_items.data(),
bumped_items.data() + bumped_items.size());
}
// MHC version
template <typename Iterator, typename C = Config>
std::enable_if_t<C::kUseMHC, bool> AddRange(Iterator begin, Iterator end) {
auto input = Super::PrepareAddRangeMHC(begin, end);
return AddRangeMHCInternal(input.get(), input.get() + (end - begin));
}
void BackSubst() {
Super::BackSubst();
child_ribbon_.BackSubst();
}
inline bool QueryFilter(const Key &key) const {
if constexpr (kUseMHC) {
const auto mhc = this->hasher_.GetMHC(key);
return QueryFilterMHC(mhc);
} else {
auto [was_bumped, found] = Super::QueryFilter(key);
if (was_bumped) {
return child_ribbon_.QueryFilter(key);
} else {
return found;
}
}
}
template <typename C = Config>
inline std::enable_if_t<C::kUseMHC, bool>
QueryFilterMHC(const mhc_or_key_t mhc) const {
auto [was_bumped, found] = Super::QueryFilterMHC(mhc);
if (was_bumped)
return child_ribbon_.QueryFilterMHC(mhc);
else
return found;
}
inline ResultRow QueryRetrieval(const Key &key) const {
if constexpr (kUseMHC) {
const auto mhc = this->hasher_.GetMHC(key);
return QueryRetrievalMHC(mhc);
} else {
auto [was_bumped, result] = Super::QueryRetrieval(key);
if (was_bumped) {
return child_ribbon_.QueryRetrieval(key);
} else {
return result;
}
}
}
template <typename C = Config>
inline std::enable_if_t<C::kUseMHC, ResultRow>
QueryRetrievalMHC(const mhc_or_key_t mhc) const {
auto [was_bumped, result] = Super::QueryRetrievalMHC(mhc);
if (was_bumped)
return child_ribbon_.QueryRetrievalMHC(mhc);
else
return result;
}
size_t Size() const {
return Super::Size() + child_ribbon_.Size();
}
void Serialize(const std::string &filename) {
Super::Serialize(filename, depth);
}
void Deserialize(const std::string &filename) {
Super::Deserialize(filename, depth);
}
void Serialize(std::ostream &os) {
Super::Serialize(os, depth);
}
void Deserialize(std::istream &is) {
Super::Deserialize(is, depth);
}
/*
void PrintStats() const {
Super::PrintStats();
child_ribbon_.PrintStats();
}
*/
protected:
template <typename C = Config, typename Iterator>
std::enable_if_t<C::kUseMHC, bool> AddRangeMHCInternal(Iterator begin,
Iterator end) {
const auto input_size = end - begin;
using value_type =
std::conditional_t<kIsFilter, mhc_or_key_t, std::pair<mhc_or_key_t, ResultRow>>;
std::vector<value_type> bumped_items;
bumped_items.reserve(std::max(
0l, static_cast<ssize_t>((1 - slots_per_item_) * input_size)));
if (!Super::AddRange(begin, end, &bumped_items))
return false;
if (bumped_items.size() == 0)
return true;
// child ribbon will round this up as needed
const size_t child_slots =
std::max(size_t{1},
static_cast<size_t>(slots_per_item_ * bumped_items.size()));
child_ribbon_.Prepare(child_slots);
return child_ribbon_.AddRangeMHCInternal(
bumped_items.data(), bumped_items.data() + bumped_items.size());
}
void SerializeIntern(std::ostream &os) override {
Super::SerializeIntern(os);
child_ribbon_.SerializeIntern(os);
}
void DeserializeIntern(std::istream &is, bool switchendian, uint64_t seed, uint32_t idx) override {
Super::DeserializeIntern(is, switchendian, seed, idx);
child_ribbon_.DeserializeIntern(is, switchendian, seed + 1, idx + 1);
}
ribbon_filter<depth - 1, Config> child_ribbon_;
friend ribbon_filter<depth + 1, Config>;
friend Super;
};
// base case ribbon
template <typename Config>
class ribbon_filter<0u, Config> : public ribbon_base<BaseConfig<Config>> {
public:
IMPORT_RIBBON_CONFIG(BaseConfig<Config>);
using Super = ribbon_base<BaseConfig<Config>>;
double base_slots_per_item_ = 1.0;
ribbon_filter() = default;
ribbon_filter(std::istream &is) {
Deserialize(is);
}
ribbon_filter(const std::string &filename) {
Deserialize(filename);
}
ribbon_filter(size_t num_slots, double parent_slots_per_item, uint64_t seed) {
if constexpr (!Config::kUseMHC) {
Init(num_slots, parent_slots_per_item, seed);
} else {
// assume top level -> hasher index 0
Init(num_slots, parent_slots_per_item, seed, 0);
}
}
ribbon_filter(size_t num_slots, double parent_slots_per_item, uint64_t seed,
uint32_t idx) {
if constexpr (Config::kUseMHC) {
Init(num_slots, parent_slots_per_item, seed, idx);
} else {
// you called the wrong constructor but we can just ignore it
LOG1 << "Warning: called MHC base ribbon constructor in non-MHC "
"configuration, check the number of arguments! Should be "
"3, not 4. Ignoring last argument which is only relevant "
"in MHC configurations";
Init(num_slots, parent_slots_per_item, seed);
}
}
template <typename C = Config>
std::enable_if_t<!C::kUseMHC, void>
Init(size_t num_slots, double parent_slots_per_item, uint64_t seed) {
parent_slots_per_item_ = parent_slots_per_item;
orig_num_slots_ = num_slots / parent_slots_per_item * base_slots_per_item_;
Super::Init(orig_num_slots_, base_slots_per_item_, seed);
}
template <typename C = Config>
std::enable_if_t<C::kUseMHC, void> Init(size_t num_slots,
double parent_slots_per_item,
uint64_t seed, uint32_t idx) {
parent_slots_per_item_ = parent_slots_per_item;
orig_num_slots_ = num_slots / parent_slots_per_item * base_slots_per_item_;
Super::Init(orig_num_slots_, base_slots_per_item_, seed, idx);
}
template <typename Iterator, typename C = Config>
std::enable_if_t<!C::kUseMHC, bool> AddRange(Iterator begin, Iterator end) {
// there's really no distinction in the base case
return AddRangeMHCInternal(begin, end);
}
// MHC version
template <typename Iterator, typename C = Config>
std::enable_if_t<C::kUseMHC, bool> AddRange(Iterator begin, Iterator end) {
auto input = Super::PrepareAddRangeMHC(begin, end);
return AddRangeMHCInternal(input.get(), input.get() + (end - begin));
}
inline bool QueryFilter(const Key &key) const {
if constexpr (kUseMHC) {
const auto mhc = this->hasher_.GetMHC(key);
return QueryFilterMHC(mhc);
} else {
auto [was_bumped, found] = Super::QueryFilter(key);
assert(!was_bumped);
return found;
}
}
template <typename C = Config>
inline std::enable_if_t<C::kUseMHC, bool>
QueryFilterMHC(const typename Super::mhc_or_key_t mhc) const {
auto [was_bumped, found] = Super::QueryFilterMHC(mhc);
assert(!was_bumped);
return found;
}
ResultRow QueryRetrieval(const Key &key) const {
auto [was_bumped, result] = Super::QueryRetrieval(key);
assert(!was_bumped);
return result;
}
template <typename C = Config>
inline std::enable_if_t<C::kUseMHC, ResultRow>
QueryRetrievalMHC(const typename Super::mhc_or_key_t mhc) const {
auto [was_bumped, result] = Super::QueryRetrievalMHC(mhc);
assert(!was_bumped);
return result;
}
void Serialize(const std::string &filename) {
Super::Serialize(filename, 0);
}
void Deserialize(const std::string &filename) {
Super::Deserialize(filename, 0);
}
void Serialize(std::ostream &os) {
Super::Serialize(os, 0);
}
void Deserialize(std::istream &is) {
Super::Deserialize(is, 0);
}
protected:
void Prepare(size_t num_slots) {
orig_num_slots_ = num_slots / parent_slots_per_item_ * base_slots_per_item_;
Super::Prepare(orig_num_slots_);
}
// misnomer but needed for recursive construction
template <typename Iterator>
bool AddRangeMHCInternal(Iterator begin, Iterator end) {
bool success;
do {
success = Super::AddRange(begin, end, nullptr);
if (!success) {
// increase epsilon and try again
base_slots_per_item_ += 0.05;
size_t new_num_slots = orig_num_slots_ * base_slots_per_item_;
Super::Realloc(new_num_slots, base_slots_per_item_);
}
} while (!success);
return success;
}
size_t orig_num_slots_;
double parent_slots_per_item_;
friend ribbon_filter<1, Config>;
friend Super;
};
} // namespace ribbon