Skip to content
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

add BloomFilter class #6795

Merged
merged 34 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
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 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
110 changes: 110 additions & 0 deletions packages/firestore/src/remote/bloom_filter.ts
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(
dconeybe marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
Loading