Skip to content

Commit 547b7ee

Browse files
author
Matt Weiden
committed
BloomFilter should warn/raise on unrealistic input.
Given an unrealistic combination of number of elements and false positive probabilitiy, the optimalWidth message should not fail silently. It should warn the user or throw an exception to ensure that the BloomFilter delivers the expected performance.
1 parent e12d97f commit 547b7ee

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

algebird-core/src/main/scala/com/twitter/algebird/BloomFilter.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ object BloomFilter {
5454
def optimalNumHashes(numEntries: Int, width: Int): Int = math.ceil(width / numEntries * math.log(2)).toInt
5555

5656
// Compute optimal width: m = - n ln(p) / (ln(2))^2
57-
def optimalWidth(numEntries: Int, fpProb: Double): Int = math.ceil(-1 * numEntries * math.log(fpProb) / math.log(2) / math.log(2)).toInt
57+
def optimalWidth(numEntries: Int, fpProb: Double): Int = {
58+
val widthEstimate = math.ceil(-1 * numEntries * math.log(fpProb) / math.log(2) / math.log(2)).toInt
59+
60+
if (widthEstimate == Int.MaxValue) {
61+
throw new java.lang.IllegalArgumentException(
62+
"BloomFilter cannot guarantee the specified false positive probability for the number of entries!")
63+
}
64+
65+
return widthEstimate
66+
}
5867
}
5968

6069
/**

0 commit comments

Comments
 (0)