-
Notifications
You must be signed in to change notification settings - Fork 945
add BloomFilter class #6795
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 34 commits into
mila/BloomFilter
from
mila/BloomFilter-add-BloomFilter-class
Nov 30, 2022
Merged
add BloomFilter class #6795
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
7122bab
add md5.js package
milaGGL 3c38d56
remove noises due to format
milaGGL eeef62f
remove changes to yarn lock
milaGGL 389de2e
use crypto.js instead of md5.js
milaGGL dd2e42f
add mightContain method to BloomFilter
milaGGL 3c273e9
discard string representation of md5 hash value, use byte array instead
milaGGL 854195c
Use DateView instead of Buffer
milaGGL 84ef93e
Added Md5 to webchannel-wrapper
dconeybe 877afce
webchannel_wrapper.test.ts added with 1 test (that currently fails du…
dconeybe 02f74ff
webchannel-wrapper/src/index.js: fix Md5 prototype definition
dconeybe 398c166
webchannel_wrapper.test.ts added
dconeybe 01de4cf
webchannel_wrapper.test.ts: fix imports to make lint happy
dconeybe c3b9b42
Merge remote-tracking branch 'origin/dconeybe/Md5FromClosure' into mi…
milaGGL ba8967a
Add golden test, use MD5 from google library
milaGGL 5abbe86
Use number in bitmap instead of hexadecimal string representation
milaGGL dca8d99
add golden test files
milaGGL 4a3fb11
REVERT ME: require() test for json/txt
dconeybe 3a22a98
Revert "REVERT ME: require() test for json/txt"
dconeybe 9ccde39
import golden tests without fs, use Integer instead of BigInt
milaGGL 0d919c1
add the Integer library
milaGGL c5616d7
Merge branch 'master' into mila/BloomFilter-add-BloomFilter-class
milaGGL 2ba1981
Use platform based 64base decoding function
milaGGL 687a5b6
reformat to pass github check
milaGGL 8529ae7
update constructor validation
milaGGL 810d63b
Revert "Merge branch 'master' into mila/BloomFilter-add-BloomFilter-c…
milaGGL 6490034
resolve comments
milaGGL 01bd93e
Revert "Revert "Merge branch 'master' into mila/BloomFilter-add-Bloom…
milaGGL 10b1b67
resolve comments
milaGGL 06f6400
remove type casting on TEST_DATA
milaGGL b3a7784
resolve comments
milaGGL 4aaecda
run yarn format
milaGGL 9321eec
Merge branch 'master' into mila/BloomFilter-add-BloomFilter-class
milaGGL 9a11614
resolve comments
milaGGL 1440908
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 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. | ||
*/ | ||
import { Md5, Integer } from '@firebase/webchannel-wrapper'; | ||
|
||
import { newTextEncoder } from '../platform/serializer'; | ||
import { debugAssert } from '../util/assert'; | ||
|
||
const MAX_64_BIT_UNSIGNED_INTEGER = new Integer([0xffffffff, 0xffffffff], 0); | ||
|
||
// Hash a string using md5 hashing algorithm. | ||
function getMd5HashValue(value: string): Uint8Array { | ||
const encodedValue = newTextEncoder().encode(value); | ||
const md5 = new Md5(); | ||
md5.update(encodedValue); | ||
return new Uint8Array(md5.digest()); | ||
} | ||
|
||
// Interpret the 16 bytes array as two 64-bit unsigned integers, encoded using | ||
// 2’s complement using little endian. | ||
function get64BitUints(Bytes: Uint8Array): [Integer, Integer] { | ||
const dataView = new DataView(Bytes.buffer); | ||
const chunk1 = dataView.getUint32(0, /* littleEndian= */ true); | ||
const chunk2 = dataView.getUint32(4, /* littleEndian= */ true); | ||
const chunk3 = dataView.getUint32(8, /* littleEndian= */ true); | ||
const chunk4 = dataView.getUint32(12, /* littleEndian= */ true); | ||
const integer1 = new Integer([chunk1, chunk2], 0); | ||
const integer2 = new Integer([chunk3, chunk4], 0); | ||
return [integer1, integer2]; | ||
} | ||
|
||
export class BloomFilter { | ||
readonly size: number; | ||
private readonly sizeInInteger: Integer; | ||
|
||
constructor( | ||
private readonly bitmap: Uint8Array, | ||
padding: number, | ||
private readonly hashCount: number | ||
) { | ||
debugAssert(padding >= 0 && padding < 8, `Invalid padding: ${padding}`); | ||
if (bitmap.length > 0) { | ||
debugAssert(this.hashCount > 0, `Invalid hash count: ${hashCount}`); | ||
} else { | ||
// Only empty bloom filter can have 0 hash count. | ||
debugAssert(this.hashCount >= 0, `Invalid hash count: ${hashCount}`); | ||
|
||
// Empty bloom filter should have 0 padding. | ||
debugAssert( | ||
padding === 0, | ||
`Invalid padding when bitmap length is 0: ${padding}` | ||
); | ||
} | ||
|
||
this.size = bitmap.length * 8 - padding; | ||
// Set the size in Integer to avoid repeated calculation in mightContain(). | ||
this.sizeInInteger = Integer.fromNumber(this.size); | ||
dconeybe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Calculate the ith hash value based on the hashed 64bit integers, | ||
// and calculate its corresponding bit index in the bitmap to be checked. | ||
private getBitIndex(num1: Integer, num2: Integer, index: number): number { | ||
// Calculate hashed value h(i) = h1 + (i * h2). | ||
let hashValue = num1.add(num2.multiply(Integer.fromNumber(index))); | ||
// Wrap if hash value overflow 64bit. | ||
if (hashValue.compare(MAX_64_BIT_UNSIGNED_INTEGER) === 1) { | ||
hashValue = new Integer([hashValue.getBits(0), hashValue.getBits(1)], 0); | ||
} | ||
return hashValue.modulo(this.sizeInInteger).toNumber(); | ||
} | ||
|
||
// Return whether the bit on the given index in the bitmap is set to 1. | ||
private isBitSet(index: number): boolean { | ||
// To retrieve bit n, calculate: (bitmap[n / 8] & (0x01 << (n % 8))). | ||
const byte = this.bitmap[Math.floor(index / 8)]; | ||
const offset = index % 8; | ||
return (byte & (0x01 << offset)) !== 0; | ||
} | ||
|
||
mightContain(value: string): boolean { | ||
// Empty bitmap and empty value should always return false on membership | ||
// check. | ||
if (this.size === 0 || value === '') { | ||
return false; | ||
} | ||
|
||
const md5HashedValue = getMd5HashValue(value); | ||
const [hash1, hash2] = get64BitUints(md5HashedValue); | ||
for (let i = 0; i < this.hashCount; i++) { | ||
const index = this.getBitIndex(hash1, hash2, i); | ||
if (!this.isBitSet(index)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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.