This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
per_stripe_bloom.h
181 lines (154 loc) · 6.41 KB
/
per_stripe_bloom.h
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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: per_stripe_bloom.h
// -----------------------------------------------------------------------------
#ifndef CI_PER_STRIPE_BLOOM_H_
#define CI_PER_STRIPE_BLOOM_H_
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
#include "absl/strings/str_cat.h"
#include "data.h"
#include "evaluation_utils.h"
#include "index_structure.h"
#include "leveldb/filter_policy.h"
#include "leveldb/slice.h"
namespace ci {
class PerStripeBloom : public IndexStructure {
public:
PerStripeBloom(const std::vector<int>& data, std::size_t num_rows_per_stripe,
std::size_t num_bits_per_key)
: num_bits_per_key_(num_bits_per_key),
policy_(leveldb::NewBloomFilterPolicy(num_bits_per_key_)) {
num_stripes_ = data.size() / num_rows_per_stripe;
// Pre-allocate `num_stripes_` strings to store filters.
filters_.resize(num_stripes_);
for (size_t stripe_id = 0; stripe_id < num_stripes_; ++stripe_id) {
const std::size_t stripe_begin = num_rows_per_stripe * stripe_id;
const std::size_t stripe_end = stripe_begin + num_rows_per_stripe;
const std::size_t num_values = stripe_end - stripe_begin;
// Collect values of current stripe. (LevelDB's Bloom filter only accepts
// leveldb::Slice.)
absl::flat_hash_set<std::string> string_values;
string_values.reserve(num_values);
for (size_t i = stripe_begin; i < stripe_end; ++i) {
string_values.insert(std::to_string(data[i]));
}
// Create the filter for the current stripe.
const std::vector<leveldb::Slice> slices(string_values.begin(),
string_values.end());
policy_->CreateFilter(slices.data(), static_cast<int>(slices.size()),
&filters_[stripe_id]);
}
}
bool StripeContains(std::size_t stripe_id, int value) const override {
if (stripe_id >= num_stripes_) {
std::cerr << "`stripe_id` is out of bounds." << std::endl;
exit(EXIT_FAILURE);
}
// Probe corresponding filter.
return policy_->KeyMayMatch(leveldb::Slice(std::to_string(value)),
filters_[stripe_id]);
}
std::string name() const override {
return std::string("PerStripeBloom/") + std::to_string(num_bits_per_key_);
}
size_t byte_size() const override {
std::size_t result = 0;
for (const std::string filter : filters_) {
result += filter.size();
}
return result;
}
size_t compressed_byte_size() const override {
std::string data;
for (const std::string& filter : filters_) {
absl::StrAppend(&data, filter);
}
return Compress(data).size();
}
std::size_t num_stripes() { return num_stripes_; }
private:
std::size_t num_stripes_;
std::size_t num_bits_per_key_;
std::unique_ptr<const leveldb::FilterPolicy> policy_;
std::vector<std::string> filters_;
};
class PerStripeBloomFactory : public IndexStructureFactory {
public:
explicit PerStripeBloomFactory(size_t num_bits_per_key)
: num_bits_per_key_(num_bits_per_key) {}
std::unique_ptr<IndexStructure> Create(
const Column& column, size_t num_rows_per_stripe) const override {
return absl::make_unique<PerStripeBloom>(column.data(), num_rows_per_stripe,
num_bits_per_key_);
}
std::string index_name() const override {
return std::string("PerStripeBloom/") + std::to_string(num_bits_per_key_);
}
const size_t num_bits_per_key_;
};
// A version of `PerStripeBloom` factory that allows to pass another filter's
// factory in order to build a Bloom filter of a comparable compressed size.
// This is useful, e.g. when trying to compare scan rates of filters with
// roughly the same size.
class PerStripeBloomComparableSizeFactory : public IndexStructureFactory {
public:
explicit PerStripeBloomComparableSizeFactory(
std::unique_ptr<IndexStructureFactory> other_index_factory)
: other_index_factory_(std::move(other_index_factory)) {}
std::unique_ptr<IndexStructure> Create(
const Column& column, size_t num_rows_per_stripe) const override {
constexpr size_t kMaxBitsPerKey = 30;
IndexStructurePtr other_index =
other_index_factory_->Create(column, num_rows_per_stripe);
const size_t target_size = other_index->compressed_byte_size();
// Find the number of bits per key that minimizes the difference in size
// between the filters.
size_t argmin_num_bits_per_key;
size_t min_size_diff = std::numeric_limits<size_t>::max();
size_t min_bits = 1;
size_t max_bits = kMaxBitsPerKey;
while (min_bits <= max_bits) {
const size_t num_bits_per_key = (min_bits + max_bits) / 2;
auto bloom_index = absl::make_unique<PerStripeBloom>(
column.data(), num_rows_per_stripe, num_bits_per_key);
size_t abs_size_diff = target_size < bloom_index->compressed_byte_size()
? bloom_index->compressed_byte_size() - target_size
: target_size - bloom_index->compressed_byte_size();
if (abs_size_diff < min_size_diff) {
min_size_diff = abs_size_diff;
argmin_num_bits_per_key = num_bits_per_key;
}
if (target_size < bloom_index->compressed_byte_size()) {
max_bits = num_bits_per_key - 1;
} else {
min_bits = num_bits_per_key + 1;
}
}
return absl::make_unique<PerStripeBloom>(column.data(), num_rows_per_stripe,
argmin_num_bits_per_key);
}
std::string index_name() const override {
return std::string("PerStripeBloomComparableSize/" +
other_index_factory_->index_name());
}
std::unique_ptr<IndexStructureFactory> other_index_factory_;
};
} // namespace ci
#endif // CI_PER_STRIPE_BLOOM_H_