Skip to content

Commit

Permalink
BloomFilter should warn/raise on unrealistic input.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Matt Weiden committed Oct 20, 2014
1 parent e12d97f commit 547b7ee
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ object BloomFilter {
def optimalNumHashes(numEntries: Int, width: Int): Int = math.ceil(width / numEntries * math.log(2)).toInt

// Compute optimal width: m = - n ln(p) / (ln(2))^2
def optimalWidth(numEntries: Int, fpProb: Double): Int = math.ceil(-1 * numEntries * math.log(fpProb) / math.log(2) / math.log(2)).toInt
def optimalWidth(numEntries: Int, fpProb: Double): Int = {
val widthEstimate = math.ceil(-1 * numEntries * math.log(fpProb) / math.log(2) / math.log(2)).toInt

if (widthEstimate == Int.MaxValue) {
throw new java.lang.IllegalArgumentException(
"BloomFilter cannot guarantee the specified false positive probability for the number of entries!")
}

return widthEstimate
}
}

/**
Expand Down

0 comments on commit 547b7ee

Please sign in to comment.