Skip to content

Commit 687a5b6

Browse files
committed
reformat to pass github check
1 parent 2ba1981 commit 687a5b6

File tree

3 files changed

+78
-38
lines changed

3 files changed

+78
-38
lines changed

packages/firestore/src/remote/bloom_filter.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ export class BloomFilter {
2626
padding: number,
2727
private readonly hashCount: number
2828
) {
29-
debugAssert(padding >= 0, 'Padding is negative.');
3029
this.bitSize = this.bitmap.length * 8 - padding;
31-
debugAssert(this.bitSize >= 0, 'Bitmap size is negative.');
32-
debugAssert(this.hashCount >= 0, 'Hash count is negative.');
30+
if (this.bitSize === 0) {
31+
debugAssert(
32+
this.hashCount === 0,
33+
'An empty bitmap should correspond to 0 hashCount.'
34+
);
35+
} else {
36+
debugAssert(padding >= 0, 'Padding is negative.');
37+
debugAssert(this.bitSize >= 0, 'Bitmap size is negative.');
38+
debugAssert(this.hashCount >= 0, 'Hash count is negative.');
39+
}
3340
}
3441

3542
getBitSize(): number {
@@ -41,10 +48,6 @@ export class BloomFilter {
4148
if (this.bitSize === 0) {
4249
return false;
4350
}
44-
// If document name is an empty string, return false
45-
if (!document) {
46-
return false;
47-
}
4851

4952
// Hash the string using md5
5053
const md5 = new Md5();

packages/firestore/test/unit/remote/bloom_filter.test.ts

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,45 @@ import {
4848

4949
describe('BloomFilter', () => {
5050
it('can initiate an empty BloomFilter', () => {
51-
const bloomFilter = new BloomFilter(
52-
/* bitmap */ new Uint8Array(0),
53-
/* padding */ 0,
54-
/* hashCount */ 0
55-
);
51+
const bloomFilter = new BloomFilter(new Uint8Array(0), 0, 0);
5652
expect(bloomFilter.getBitSize()).to.equal(0);
5753
});
5854

5955
it('can initiate a non empty BloomFilter', () => {
6056
const bloomFilter = new BloomFilter(
61-
/* bitmap */ new Uint8Array([151, 153, 236, 116, 7]),
62-
/* padding */ 3,
63-
/* hashCount */ 13
57+
new Uint8Array([151, 153, 236, 116, 7]),
58+
3,
59+
13
6460
);
6561
expect(bloomFilter.getBitSize()).to.equal(37);
6662
});
6763

64+
it('should throw error if empty BloomFilter has hash count', () => {
65+
try {
66+
new BloomFilter(new Uint8Array(0), 0, 1);
67+
expect.fail();
68+
} catch (error) {
69+
expect(
70+
(error as Error)?.message.includes(
71+
'INTERNAL ASSERTION FAILED: An empty bitmap should correspond to 0 hashCount.'
72+
)
73+
).to.be.true;
74+
}
75+
try {
76+
new BloomFilter(new Uint8Array(1), 8, 1);
77+
expect.fail();
78+
} catch (error) {
79+
expect(
80+
(error as Error)?.message.includes(
81+
'INTERNAL ASSERTION FAILED: An empty bitmap should correspond to 0 hashCount.'
82+
)
83+
).to.be.true;
84+
}
85+
});
86+
6887
it('should throw error if padding is negative', () => {
6988
try {
70-
new BloomFilter(new Uint8Array(0), -1, 0);
89+
new BloomFilter(new Uint8Array(1), -1, 1);
7190
expect.fail();
7291
} catch (error) {
7392
expect(
@@ -80,7 +99,7 @@ describe('BloomFilter', () => {
8099

81100
it('should throw error if bitmap size is negative', () => {
82101
try {
83-
new BloomFilter(new Uint8Array(0), 1, 0);
102+
new BloomFilter(new Uint8Array(0), 1, 1);
84103
expect.fail();
85104
} catch (error) {
86105
expect(
@@ -93,7 +112,7 @@ describe('BloomFilter', () => {
93112

94113
it('should throw error if hash count is negative', () => {
95114
try {
96-
new BloomFilter(new Uint8Array(0), 0, -1);
115+
new BloomFilter(new Uint8Array(1), 1, -1);
97116
expect.fail();
98117
} catch (error) {
99118
expect(
@@ -123,17 +142,18 @@ describe('BloomFilter', () => {
123142

124143
/**
125144
* Golden tests are generated by backend based on inserting n number of
126-
* documents path into Bloom filter.
145+
* documents path into a bloom filter.
127146
*
128-
* Full documents path is generated by concating documentPrefix and n, ie,
129-
* projects/project-1/databases/database-1/documents/coll/doc11
147+
* Full document path is generated by concating documentPrefix and number n,
148+
* ie, projects/project-1/databases/database-1/documents/coll/doc12
130149
*
131150
* The test result is generated by checking the membership of documents from
132151
* documentPrefix+0 to documentPrefix+2n. The membership results from 0 to n
133-
* is expected to be 1, and from n to 2n to be 0 with some false positive results.
152+
* is expected to be true (1), and the membership results from n to 2n is
153+
* expected to be false (0) with some false positive results (1).
134154
*
135-
* The test result of BloomFilter.mightContain() should match the backend result
136-
* exactly.
155+
* The test result of BloomFilter.mightContain() should match the backend
156+
* result exactly.
137157
*/
138158
describe('BloomFilter membership test', () => {
139159
const documentPrefix =
@@ -155,7 +175,7 @@ describe('BloomFilter', () => {
155175
return ByteString.fromBase64String(encoded).toUint8Array();
156176
}
157177

158-
it('mightContain result for 1 document with 1 false positive rate should match backend result', () => {
178+
it('mightContain result for 1 document with 1 false positive rate', () => {
159179
const { bits, hashCount } = testDataCount1Rate1 as TestDataType;
160180
const { membershipTestResults } = testResultCount1Rate1 as TestResultType;
161181

@@ -171,7 +191,7 @@ describe('BloomFilter', () => {
171191
expect(mightContain).to.equal(backendMembershipResult);
172192
}
173193
});
174-
it('mightContain result for 1 document with 0.01 false positive rate should match backend result', () => {
194+
it('mightContain result for 1 document with 0.01 false positive rate', () => {
175195
const { bits, hashCount } = testDataCount1Rate01 as TestDataType;
176196
const { membershipTestResults } =
177197
testResultCount1Rate01 as TestResultType;
@@ -188,7 +208,7 @@ describe('BloomFilter', () => {
188208
expect(mightContain).to.equal(backendMembershipResult);
189209
}
190210
});
191-
it('mightContain result for 1 document with 0.0001 false positive rate should match backend result', () => {
211+
it('mightContain result for 1 document with 0.0001 false positive rate', () => {
192212
const { bits, hashCount } = testDataCount1Rate0001 as TestDataType;
193213
const { membershipTestResults } =
194214
testResultCount1Rate0001 as TestResultType;
@@ -205,7 +225,7 @@ describe('BloomFilter', () => {
205225
expect(mightContain).to.equal(backendMembershipResult);
206226
}
207227
});
208-
it('mightContain result for 500 documents with 1 false positive rate should match backend result', () => {
228+
it('mightContain result for 500 documents with 1 false positive rate', () => {
209229
const { bits, hashCount } = testDataCount500Rate1 as TestDataType;
210230
const { membershipTestResults } =
211231
testResultCount500Rate1 as TestResultType;
@@ -222,7 +242,7 @@ describe('BloomFilter', () => {
222242
expect(mightContain).to.equal(backendMembershipResult);
223243
}
224244
});
225-
it('mightContain result for 500 documents with 0.01 false positive rate should match backend result', () => {
245+
it('mightContain result for 500 documents with 0.01 false positive rate', () => {
226246
const { bits, hashCount } = testDataCount500Rate01 as TestDataType;
227247
const { membershipTestResults } =
228248
testResultCount500Rate01 as TestResultType;
@@ -239,7 +259,7 @@ describe('BloomFilter', () => {
239259
expect(mightContain).to.equal(backendMembershipResult);
240260
}
241261
});
242-
it('mightContain result for 500 document with 0.0001 false positive rate should match backend result', () => {
262+
it('mightContain result for 500 document with 0.0001 false positive rate', () => {
243263
const { bits, hashCount } = testDataCount500Rate0001 as TestDataType;
244264
const { membershipTestResults } =
245265
testResultCount500Rate0001 as TestResultType;
@@ -256,7 +276,7 @@ describe('BloomFilter', () => {
256276
expect(mightContain).to.equal(backendMembershipResult);
257277
}
258278
});
259-
it('mightContain result for 5000 documents with 1 false positive rate should match backend result', () => {
279+
it('mightContain result for 5000 documents with 1 false positive rate', () => {
260280
const { bits, hashCount } = testDataCount5000Rate1 as TestDataType;
261281
const { membershipTestResults } =
262282
testResultCount5000Rate1 as TestResultType;
@@ -273,7 +293,7 @@ describe('BloomFilter', () => {
273293
expect(mightContain).to.equal(backendMembershipResult);
274294
}
275295
});
276-
it('mightContain result for 5000 documenta with 0.01 false positive rate should match backend result', () => {
296+
it('mightContain result for 5000 documenta with 0.01 false positive rate', () => {
277297
const { bits, hashCount } = testDataCount5000Rate01 as TestDataType;
278298
const { membershipTestResults } =
279299
testResultCount5000Rate01 as TestResultType;
@@ -290,7 +310,7 @@ describe('BloomFilter', () => {
290310
expect(mightContain).to.equal(backendMembershipResult);
291311
}
292312
});
293-
it('mightContain result for 5000 documenta with 0.0001 false positive rate should match backend result', () => {
313+
it('mightContain result for 5000 documenta with 0.0001 false positive rate', () => {
294314
const { bits, hashCount } = testDataCount5000Rate0001 as TestDataType;
295315
const { membershipTestResults } =
296316
testResultCount5000Rate0001 as TestResultType;
@@ -307,7 +327,7 @@ describe('BloomFilter', () => {
307327
expect(mightContain).to.equal(backendMembershipResult);
308328
}
309329
});
310-
it('mightContain result for 50000 documents with 1 false positive rate should match backend result', () => {
330+
it('mightContain result for 50000 documents with 1 false positive rate', () => {
311331
const { bits, hashCount } = testDataCount50000Rate1 as TestDataType;
312332
const { membershipTestResults } =
313333
testResultCount50000Rate1 as TestResultType;
@@ -324,7 +344,7 @@ describe('BloomFilter', () => {
324344
expect(mightContain).to.equal(backendMembershipResult);
325345
}
326346
});
327-
it('mightContain result for 50000 documents with 0.01 false positive rate should match backend result', () => {
347+
it('mightContain result for 50000 documents with 0.01 false positive rate', () => {
328348
const { bits, hashCount } = testDataCount50000Rate01 as TestDataType;
329349
const { membershipTestResults } =
330350
testResultCount50000Rate01 as TestResultType;
@@ -341,9 +361,9 @@ describe('BloomFilter', () => {
341361
expect(mightContain).to.equal(backendMembershipResult);
342362
}
343363
//Extend default timeout(2000)
344-
}).timeout(5000);
364+
}).timeout(10_000);
345365

346-
it('mightContain result for 50000 documents with 0.0001 false positive rate should match backend result', () => {
366+
it('mightContain result for 50000 documents with 0.0001 false positive rate', () => {
347367
const { bits, hashCount } = testDataCount50000Rate0001 as TestDataType;
348368
const { membershipTestResults } =
349369
testResultCount50000Rate0001 as TestResultType;
@@ -360,6 +380,6 @@ describe('BloomFilter', () => {
360380
expect(mightContain).to.equal(backendMembershipResult);
361381
}
362382
//Extend default timeout(2000)
363-
}).timeout(5000);
383+
}).timeout(10_000);
364384
});
365385
});

packages/firestore/test/unit/remote/bloom_filter_golden_test_data/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
export { default as testDataCount1Rate1 } from './Validation_BloomFilterTest_MD5_1_1_bloom_filter_proto.json';
219
export { default as testResultCount1Rate1 } from './Validation_BloomFilterTest_MD5_1_1_membership_test_result.json';
320
export { default as testDataCount1Rate01 } from './Validation_BloomFilterTest_MD5_1_01_bloom_filter_proto.json';

0 commit comments

Comments
 (0)