Skip to content

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
merged 34 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7122bab
add md5.js package
milaGGL Nov 14, 2022
3c38d56
remove noises due to format
milaGGL Nov 14, 2022
eeef62f
remove changes to yarn lock
milaGGL Nov 14, 2022
389de2e
use crypto.js instead of md5.js
milaGGL Nov 15, 2022
dd2e42f
add mightContain method to BloomFilter
milaGGL Nov 16, 2022
3c273e9
discard string representation of md5 hash value, use byte array instead
milaGGL Nov 17, 2022
854195c
Use DateView instead of Buffer
milaGGL Nov 17, 2022
84ef93e
Added Md5 to webchannel-wrapper
dconeybe Nov 17, 2022
877afce
webchannel_wrapper.test.ts added with 1 test (that currently fails du…
dconeybe Nov 17, 2022
02f74ff
webchannel-wrapper/src/index.js: fix Md5 prototype definition
dconeybe Nov 18, 2022
398c166
webchannel_wrapper.test.ts added
dconeybe Nov 18, 2022
01de4cf
webchannel_wrapper.test.ts: fix imports to make lint happy
dconeybe Nov 18, 2022
c3b9b42
Merge remote-tracking branch 'origin/dconeybe/Md5FromClosure' into mi…
milaGGL Nov 18, 2022
ba8967a
Add golden test, use MD5 from google library
milaGGL Nov 18, 2022
5abbe86
Use number in bitmap instead of hexadecimal string representation
milaGGL Nov 18, 2022
dca8d99
add golden test files
milaGGL Nov 22, 2022
4a3fb11
REVERT ME: require() test for json/txt
dconeybe Nov 22, 2022
3a22a98
Revert "REVERT ME: require() test for json/txt"
dconeybe Nov 22, 2022
9ccde39
import golden tests without fs, use Integer instead of BigInt
milaGGL Nov 22, 2022
0d919c1
add the Integer library
milaGGL Nov 22, 2022
c5616d7
Merge branch 'master' into mila/BloomFilter-add-BloomFilter-class
milaGGL Nov 22, 2022
2ba1981
Use platform based 64base decoding function
milaGGL Nov 23, 2022
687a5b6
reformat to pass github check
milaGGL Nov 23, 2022
8529ae7
update constructor validation
milaGGL Nov 24, 2022
810d63b
Revert "Merge branch 'master' into mila/BloomFilter-add-BloomFilter-c…
milaGGL Nov 25, 2022
6490034
resolve comments
milaGGL Nov 25, 2022
01bd93e
Revert "Revert "Merge branch 'master' into mila/BloomFilter-add-Bloom…
milaGGL Nov 25, 2022
10b1b67
resolve comments
milaGGL Nov 26, 2022
06f6400
remove type casting on TEST_DATA
milaGGL Nov 27, 2022
b3a7784
resolve comments
milaGGL Nov 29, 2022
4aaecda
run yarn format
milaGGL Nov 29, 2022
9321eec
Merge branch 'master' into mila/BloomFilter-add-BloomFilter-class
milaGGL Nov 29, 2022
9a11614
resolve comments
milaGGL Nov 29, 2022
1440908
resolve comments
milaGGL Nov 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/firestore/src/remote/bloom_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @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';

export class BloomFilter {
readonly size: number;

constructor(
private readonly bitmap: Uint8Array,
padding: number,
private readonly hashCount: number
) {
if (this.bitmap.length === 0) {
debugAssert(
padding === 0,
'A valid empty bloom filter should have 0 padding.'
);
}
this.size = this.bitmap.length * 8 - padding;
debugAssert(padding >= 0, 'Padding is negative.');
debugAssert(this.size >= 0, 'Bitmap size is negative.');
// Only empty bloom filter can have 0 hash count
debugAssert(
this.bitmap.length === 0 || this.hashCount > 0,
'Hash count is 0 or negative'
);
}

mightContain(value: string): boolean {
// Empty bitmap should always return false on membership check
if (this.size === 0) {
return false;
}

// Hash the string using md5
const md5 = new Md5();
const encodedValue = newTextEncoder().encode(value);
md5.update(encodedValue);
const encodedBytes = new Uint8Array(md5.digest());

// Interpret the hashed value as two 64-bit chunks as unsigned integers, encoded using 2’s
// complement using little endian.
const dataView = new DataView(encodedBytes.buffer);
const firstUint32 = dataView.getUint32(0, /* littleEndian= */ true);
const secondUint32 = dataView.getUint32(4, /* littleEndian= */ true);
const thirdUint32 = dataView.getUint32(8, /* littleEndian= */ true);
const fourthUint32 = dataView.getUint32(12, /* littleEndian= */ true);
const hash1 = new Integer([firstUint32, secondUint32], 0);
const hash2 = new Integer([thirdUint32, fourthUint32], 0);

const sizeInInteger = Integer.fromNumber(this.size);
const max64BitInteger = Integer.fromNumber(Math.pow(2, 64));

for (let i = 0; i < this.hashCount; i++) {
// Calculate hashed value h(i) = h1 + (i * h2), wrap if hash value overflow
let combinedHash = hash1.add(hash2.multiply(Integer.fromNumber(i)));
if (combinedHash.compare(max64BitInteger) === 1) {
combinedHash = new Integer(
[combinedHash.getBits(0), combinedHash.getBits(1)],
0
);
}
// To retrieve bit n, calculate: (bitmap[n / 8] & (0x01 << (n % 8))).
const modulo = Number(combinedHash.modulo(sizeInInteger));
const byte = this.bitmap[Math.floor(modulo / 8)];

if (!(byte & (0x01 << modulo % 8))) {
return false;
}
}

return true;
}
}
Loading