-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement BloomFilter class #10846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
milaGGL
merged 18 commits into
mila/BloomFilter
from
mila/BloomFilter-add-BloomFilter-cpp-file
Feb 28, 2023
Merged
Implement BloomFilter class #10846
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b2e9b93
add cpp file
milaGGL 2f14bc7
Update bloom_filter.h
milaGGL 33c65fa
implement BloomFilter class
milaGGL 9338fe2
add validation test case
milaGGL 4bb92a1
use CC_MD5
milaGGL 1eb96c4
Merge branch 'mila/BloomFilter' into mila/BloomFilter-add-BloomFilter…
milaGGL ac6557e
update bloom filter unit test
milaGGL be543a8
make Hash struct private
milaGGL 61c7e82
Update bloom_filter.h
milaGGL b33f0d1
remove bloom filter exception
milaGGL d6f7a64
update the Create method and test cases
milaGGL 7ddaf72
fromat
milaGGL d105a3c
resolve comments
milaGGL 60511bc
fix typo
milaGGL b6fcea7
resolve comments
milaGGL c67c68b
resolve comments
milaGGL 73af04a
Update bloom_filter.cc
milaGGL 2a8989e
resolve comments
milaGGL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright 2023 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| #include "Firestore/core/src/remote/bloom_filter.h" | ||
|
|
||
| #include <utility> | ||
|
|
||
| #include "CommonCrypto/CommonDigest.h" | ||
| #include "Firestore/core/src/util/hard_assert.h" | ||
| #include "Firestore/core/src/util/statusor.h" | ||
| #include "Firestore/core/src/util/warnings.h" | ||
|
|
||
| namespace firebase { | ||
| namespace firestore { | ||
| namespace remote { | ||
|
|
||
| using util::Status; | ||
| using util::StatusOr; | ||
|
|
||
| // TODO(Mila): Replace CommonCrypto with platform based MD5 calculation and | ||
| // remove suppress. | ||
| SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN(); | ||
|
|
||
| BloomFilter::Hash BloomFilter::Md5HashDigest(absl::string_view key) const { | ||
| unsigned char md5_digest[CC_MD5_DIGEST_LENGTH]; | ||
|
|
||
| CC_MD5_CTX context; | ||
| CC_MD5_Init(&context); | ||
| CC_MD5_Update(&context, key.data(), key.size()); | ||
| CC_MD5_Final(md5_digest, &context); | ||
|
|
||
| // TODO(Mila): Replace this casting with safer function (b/270568625). | ||
| uint64_t* hash128 = reinterpret_cast<uint64_t*>(md5_digest); | ||
| return Hash{hash128[0], hash128[1]}; | ||
| } | ||
| SUPPRESS_END(); | ||
|
|
||
| int32_t BloomFilter::GetBitIndex(const Hash& hash, int32_t hash_index) const { | ||
| HARD_ASSERT(hash_index >= 0); | ||
| uint64_t hash_index_uint64 = static_cast<uint64_t>(hash_index); | ||
dconeybe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| uint64_t bit_count_uint64 = static_cast<uint64_t>(bit_count_); | ||
|
|
||
| uint64_t combined_hash = hash.h1 + (hash_index_uint64 * hash.h2); | ||
| uint64_t bit_index = combined_hash % bit_count_uint64; | ||
|
|
||
| HARD_ASSERT(bit_index <= INT32_MAX); | ||
| return bit_index; | ||
| } | ||
|
|
||
| bool BloomFilter::IsBitSet(int32_t index) const { | ||
| uint8_t byte_at_index = bitmap_[index / 8]; | ||
| int32_t offset = index % 8; | ||
| return (byte_at_index & (static_cast<uint8_t>(0x01) << offset)) != 0; | ||
| } | ||
|
|
||
| BloomFilter::BloomFilter(std::vector<uint8_t> bitmap, | ||
| int32_t padding, | ||
| int32_t hash_count) | ||
| : bit_count_(static_cast<int32_t>(bitmap.size()) * 8 - padding), | ||
| hash_count_(hash_count), | ||
| bitmap_(std::move(bitmap)) { | ||
| HARD_ASSERT(padding >= 0 && padding < 8); | ||
| HARD_ASSERT(hash_count_ >= 0); | ||
| // Only empty bloom filter can have 0 hash count. | ||
| HARD_ASSERT(bitmap_.size() == 0 || hash_count_ != 0); | ||
| // Empty bloom filter should have 0 padding. | ||
| HARD_ASSERT(bitmap_.size() != 0 || padding == 0); | ||
| HARD_ASSERT(bit_count_ >= 0); | ||
| } | ||
|
|
||
| StatusOr<BloomFilter> BloomFilter::Create(std::vector<uint8_t> bitmap, | ||
| int32_t padding, | ||
| int32_t hash_count) { | ||
| if (padding < 0 || padding >= 8) { | ||
| return Status(firestore::Error::kErrorInvalidArgument, | ||
| "Invalid padding: " + std::to_string(padding)); | ||
milaGGL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if (hash_count < 0) { | ||
| return Status(firestore::Error::kErrorInvalidArgument, | ||
| "Invalid hash count: " + std::to_string(hash_count)); | ||
| } | ||
| if (bitmap.size() > 0 && hash_count == 0) { | ||
| // Only empty bloom filter can have 0 hash count. | ||
| return Status(firestore::Error::kErrorInvalidArgument, | ||
| "Invalid hash count: " + std::to_string(hash_count)); | ||
| } | ||
| if (bitmap.size() == 0 && padding != 0) { | ||
| // Empty bloom filter should have 0 padding. | ||
| return Status(firestore::Error::kErrorInvalidArgument, | ||
| "Expected padding of 0 when bitmap length is 0, but got " + | ||
| std::to_string(padding)); | ||
| } | ||
|
|
||
| return BloomFilter(std::move(bitmap), padding, hash_count); | ||
| } | ||
|
|
||
| bool BloomFilter::MightContain(absl::string_view value) const { | ||
| // Empty bitmap should return false on membership check. | ||
| if (bit_count_ == 0) return false; | ||
| Hash hash = Md5HashDigest(value); | ||
| // The `hash_count_` and `bit_count_` fields are guaranteed to be | ||
| // non-negative when the `BloomFilter` object is constructed. | ||
| for (int32_t i = 0; i < hash_count_; ++i) { | ||
| int32_t index = GetBitIndex(hash, i); | ||
| if (!IsBitSet(index)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace remote | ||
| } // namespace firestore | ||
| } // namespace firebase | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.