Skip to content

Commit df9bcc2

Browse files
klueverGoogle Java Core Libraries
authored andcommitted
Add BloomFilter.serializedSize().
RELNOTES=`hash`: Added `BloomFilter.serializedSize()`. PiperOrigin-RevId: 881649496
1 parent 576e8b2 commit df9bcc2

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎android/guava-tests/test/com/google/common/hash/BloomFilterTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,13 @@ public void testBitSizeMatchesSerializationSize() throws Exception {
378378
// Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
379379
// Data: bitSize / 8 bytes
380380
long predictedSize = bf.bitSize() / 8 + 6;
381+
assertThat(bf.serializedSize()).isEqualTo(predictedSize);
381382

382383
// Serialize and measure actual size
383384
ByteArrayOutputStream out = new ByteArrayOutputStream();
384385
bf.writeTo(out);
385386
int actualSize = out.size();
387+
assertThat(bf.serializedSize()).isEqualTo(actualSize);
386388

387389
assertWithMessage(
388390
"Serialization size mismatch for expectedInsertions=%s, fpp=%s",

‎android/guava/src/com/google/common/hash/BloomFilter.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ long bitSize() {
226226
return bits.bitSize();
227227
}
228228

229+
/**
230+
* Returns the number of bytes required to serialize this bloom filter using the {@link
231+
* #writeTo(OutputStream)} method. This method can be useful if you need to know the serialization
232+
* size before writing to pre-allocated space (e.g., for memory-mapped files or fixed-size
233+
* records).
234+
*
235+
* @since NEXT
236+
*/
237+
public long serializedSize() {
238+
// We return a long (and not an int) because the max serialized size is approximately
239+
// 17 billion bytes (2^31 * 8 bytes).
240+
241+
// The serialization header is 6 bytes:
242+
// 1 byte for the strategy
243+
// 1 byte for the number of hash functions
244+
// 4 bytes for the array length
245+
return (bits.bitSize() / 8) + 6;
246+
}
247+
229248
/**
230249
* Determines whether a given Bloom filter is compatible with this Bloom filter. For two Bloom
231250
* filters to be compatible, they must:

‎guava-tests/test/com/google/common/hash/BloomFilterTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,13 @@ public void testBitSizeMatchesSerializationSize() throws Exception {
380380
// Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
381381
// Data: bitSize / 8 bytes
382382
long predictedSize = bf.bitSize() / 8 + 6;
383+
assertThat(bf.serializedSize()).isEqualTo(predictedSize);
383384

384385
// Serialize and measure actual size
385386
ByteArrayOutputStream out = new ByteArrayOutputStream();
386387
bf.writeTo(out);
387388
int actualSize = out.size();
389+
assertThat(bf.serializedSize()).isEqualTo(actualSize);
388390

389391
assertWithMessage(
390392
"Serialization size mismatch for expectedInsertions=%s, fpp=%s",

‎guava/src/com/google/common/hash/BloomFilter.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ long bitSize() {
238238
return bits.bitSize();
239239
}
240240

241+
/**
242+
* Returns the number of bytes required to serialize this bloom filter using the {@link
243+
* #writeTo(OutputStream)} method. This method can be useful if you need to know the serialization
244+
* size before writing to pre-allocated space (e.g., for memory-mapped files or fixed-size
245+
* records).
246+
*
247+
* @since NEXT
248+
*/
249+
public long serializedSize() {
250+
// We return a long (and not an int) because the max serialized size is approximately
251+
// 17 billion bytes (2^31 * 8 bytes).
252+
253+
// The serialization header is 6 bytes:
254+
// 1 byte for the strategy
255+
// 1 byte for the number of hash functions
256+
// 4 bytes for the array length
257+
return (bits.bitSize() / 8) + 6;
258+
}
259+
241260
/**
242261
* Determines whether a given Bloom filter is compatible with this Bloom filter. For two Bloom
243262
* filters to be compatible, they must:

0 commit comments

Comments
 (0)