|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef FIRESTORE_CORE_SRC_REMOTE_BLOOM_FILTER_H_ |
| 18 | +#define FIRESTORE_CORE_SRC_REMOTE_BLOOM_FILTER_H_ |
| 19 | + |
| 20 | +#include <string> |
| 21 | +#include <vector> |
| 22 | +#include "absl/strings/string_view.h" |
| 23 | + |
| 24 | +namespace firebase { |
| 25 | +namespace firestore { |
| 26 | +namespace remote { |
| 27 | + |
| 28 | +class BloomFilter final { |
| 29 | + public: |
| 30 | + BloomFilter(std::vector<uint8_t> bitmap, int32_t padding, int32_t hash_count); |
| 31 | + |
| 32 | + // Copyable & movable. |
| 33 | + BloomFilter(const BloomFilter&) = default; |
| 34 | + BloomFilter(BloomFilter&&) = default; |
| 35 | + BloomFilter& operator=(const BloomFilter&) = default; |
| 36 | + BloomFilter& operator=(BloomFilter&&) = default; |
| 37 | + |
| 38 | + /** |
| 39 | + * Check whether the given string is a possible member of the bloom filter. It |
| 40 | + * might return false positive result, ie, the given string is not a member of |
| 41 | + * the bloom filter, but the method returned true. |
| 42 | + * |
| 43 | + * @param value the string to be tested for membership. |
| 44 | + * @return true if the given string might be contained in the bloom filter, or |
| 45 | + * false if the given string is definitely not contained in the bloom filter. |
| 46 | + */ |
| 47 | + bool MightContain(absl::string_view value) const; |
| 48 | + |
| 49 | + // Get the `bit_count_` field. Used for testing purpose. |
| 50 | + int32_t bit_count() const { |
| 51 | + return bit_count_; |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + // The number of bits in the bloom filter. Guaranteed to be non-negative, and |
| 56 | + // less than the max number of bits `bitmap_` can represent, i.e., |
| 57 | + // bitmap_.size() * 8. |
| 58 | + int32_t bit_count_ = 0; |
| 59 | + |
| 60 | + // The number of hash functions used to construct the filter. Guaranteed to be |
| 61 | + // non-negative. |
| 62 | + int32_t hash_count_ = 0; |
| 63 | + |
| 64 | + // Bloom filter's bitmap. |
| 65 | + std::vector<uint8_t> bitmap_; |
| 66 | +}; |
| 67 | + |
| 68 | +} // namespace remote |
| 69 | +} // namespace firestore |
| 70 | +} // namespace firebase |
| 71 | + |
| 72 | +#endif // FIRESTORE_CORE_SRC_REMOTE_BLOOM_FILTER_H_ |
0 commit comments