Skip to content

Commit

Permalink
Fix calculations in bloom filter implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Feb 26, 2015
1 parent 8a2ec36 commit ace4fb8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
Expand Up @@ -20,6 +20,8 @@
/**
* Named thread factory.
*
* TODO: This can be replaced with OpenHFT's NamedThreadFactory
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class NamedThreadFactory implements ThreadFactory {
Expand Down
Expand Up @@ -13,11 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.kuujo.copycat.util.internal;
package net.kuujo.copycat.util.hash;

import net.kuujo.copycat.util.hash.CityHashFunction;
import net.kuujo.copycat.util.hash.HashFunction;
import net.kuujo.copycat.util.hash.Murmur3HashFunction;
import net.openhft.lang.Maths;
import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.collection.DirectBitSetBuilder;

Expand Down Expand Up @@ -46,7 +44,7 @@ public class BloomFilter<T> {
* with the desired false positive probability to calculate the number of bits and hashes to use.
*/
public BloomFilter(double falsePositiveProbability, int expectedSize) {
numBits = (long) Math.pow(2, (long) Math.log(((-expectedSize * Math.log(falsePositiveProbability == 0 ? Double.MIN_VALUE : falsePositiveProbability) / (Math.log(2) * Math.log(2))) - 1) / Math.log(2)) + 1);
numBits = Maths.nextPower2((long) (-expectedSize * Math.log(falsePositiveProbability == 0 ? Double.MIN_VALUE : falsePositiveProbability) / (Math.log(2) * Math.log(2))), 128);
numHashes = Math.max(1, (int) Math.round((double) numBits / expectedSize * Math.log(2)));
bits = new DirectBitSetBuilder().threadSafe(false).create(numBits);
function1 = new CityHashFunction();
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/
package net.kuujo.copycat.util;

import net.kuujo.copycat.util.internal.BloomFilter;
import net.kuujo.copycat.util.hash.BloomFilter;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand All @@ -35,7 +35,6 @@ public void testAddContains() {
filter.add("Hello world!");
Assert.assertTrue(filter.contains("Hello world!"));
Assert.assertFalse(filter.contains("Hello world again!"));
System.out.println(filter.toString());for (;;);
}

}

0 comments on commit ace4fb8

Please sign in to comment.