Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,47 @@ public void testBitSize() {
}
}

/**
* Tests that bitSize() can be used to predict the serialization size produced by writeTo().
*
* <p>The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash
* functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes).
*/
public void testBitSizeMatchesSerializationSize() throws Exception {
int[] expectedInsertionValues = {1, 10, 100, 1000, 10000};
double[] fppValues = {0.01, 0.03, 0.1};

for (int expectedInsertions : expectedInsertionValues) {
for (double fpp : fppValues) {
BloomFilter<String> bf =
BloomFilter.create(Funnels.unencodedCharsFunnel(), expectedInsertions, fpp);

// Add some elements
for (int i = 0; i < expectedInsertions / 2; i++) {
bf.put("element" + i);
}

// Calculate expected size based on bitSize()
// Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
// Data: bitSize / 8 bytes
long predictedSize = bf.bitSize() / 8 + 6;

// Serialize and measure actual size
ByteArrayOutputStream out = new ByteArrayOutputStream();
bf.writeTo(out);
int actualSize = out.size();

assertEquals(
"Serialization size mismatch for expectedInsertions="
+ expectedInsertions
+ " fpp="
+ fpp,
predictedSize,
actualSize);
}
}
}

public void testApproximateElementCount() {
int numInsertions = 1000;
BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions);
Expand Down
14 changes: 11 additions & 3 deletions android/guava/src/com/google/common/hash/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,17 @@ public long approximateElementCount() {
-Math.log1p(-fractionOfBitsSet) * bitSize / numHashFunctions, RoundingMode.HALF_UP);
}

/** Returns the number of bits in the underlying bit array. */
@VisibleForTesting
long bitSize() {
/**
* Returns the number of bits in the underlying bit array.
*
* <p>This can be useful when pre-allocating space for serialization. The number of bytes written
* by {@link #writeTo(OutputStream)} is {@code bitSize() / 8 + 6} (6 bytes for the header: 1 byte
* for the strategy, 1 byte for the number of hash functions, and 4 bytes for the array length).
*
* @return the number of bits in this Bloom filter's underlying bit array
* @since 35.0
*/
public long bitSize() {
return bits.bitSize();
}

Expand Down
41 changes: 41 additions & 0 deletions guava-tests/test/com/google/common/hash/BloomFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,47 @@ public void testBitSize() {
}
}

/**
* Tests that bitSize() can be used to predict the serialization size produced by writeTo().
*
* <p>The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash
* functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes).
*/
public void testBitSizeMatchesSerializationSize() throws Exception {
int[] expectedInsertionValues = {1, 10, 100, 1000, 10000};
double[] fppValues = {0.01, 0.03, 0.1};

for (int expectedInsertions : expectedInsertionValues) {
for (double fpp : fppValues) {
BloomFilter<String> bf =
BloomFilter.create(Funnels.unencodedCharsFunnel(), expectedInsertions, fpp);

// Add some elements
for (int i = 0; i < expectedInsertions / 2; i++) {
bf.put("element" + i);
}

// Calculate expected size based on bitSize()
// Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
// Data: bitSize / 8 bytes
long predictedSize = bf.bitSize() / 8 + 6;

// Serialize and measure actual size
ByteArrayOutputStream out = new ByteArrayOutputStream();
bf.writeTo(out);
int actualSize = out.size();

assertEquals(
"Serialization size mismatch for expectedInsertions="
+ expectedInsertions
+ " fpp="
+ fpp,
predictedSize,
actualSize);
}
}
}

public void testApproximateElementCount() {
int numInsertions = 1000;
BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions);
Expand Down
14 changes: 11 additions & 3 deletions guava/src/com/google/common/hash/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,17 @@ public long approximateElementCount() {
-Math.log1p(-fractionOfBitsSet) * bitSize / numHashFunctions, RoundingMode.HALF_UP);
}

/** Returns the number of bits in the underlying bit array. */
@VisibleForTesting
long bitSize() {
/**
* Returns the number of bits in the underlying bit array.
*
* <p>This can be useful when pre-allocating space for serialization. The number of bytes written
* by {@link #writeTo(OutputStream)} is {@code bitSize() / 8 + 6} (6 bytes for the header: 1 byte
* for the strategy, 1 byte for the number of hash functions, and 4 bytes for the array length).
*
* @return the number of bits in this Bloom filter's underlying bit array
* @since 35.0
*/
public long bitSize() {
return bits.bitSize();
}

Expand Down