Giter VIP home page Giter VIP logo

java-bloomfilter's Introduction

java-bloomfilter

java-bloomfilter is a stand-alone Bloom filter implementation written in Java. It is intended to be easy to include in existing projects without the overhead of additional libraries. The first version was inspired by a blog entry by Ian Clarke.

The latest version can be downloaded from GitHub.

Bloom filters

Bloom filters are used for set membership tests. They are fast and space-efficient at the cost of accuracy. Although there is a certain probability of error, Bloom filters never produce false negatives.

If you are new to Bloom filters, you can learn more about them in this tutorial. A more comprehensive overview is available from Wikipedia.

Examples

To create an empty Bloom filter, just call the constructor with the required false positive probability and the number of elements you expect to add to the Bloom filter.

double falsePositiveProbability = 0.1;
int expectedNumberOfElements = 100;

BloomFilter<String> bloomFilter = new BloomFilter<String>(falsePositiveProbability, expectedNumberOfElements);

The constructor chooses a length and number of hash functions which will provide the given false positive probability (approximately). Note that if you insert more elements than the number of expected elements you specify, the actual false positive probability will rapidly increase.

There are several other constructors available which provide different levels of control of how the Bloom filter is initialized. You can also specify the Bloom filter parameters directly (bits per element, number of hash functions and number of elements).

After the Bloom filter has been created, new elements may be added using the add()-method.

bloomFilter.add("foo");

To check whether an element has been stored in the Bloom filter, use the contains()-method.

bloomFilter.contains("foo"); // returns true

Keep in mind that the accuracy of this method depends on the false positive probability. It will always return true for elements which have been added to the Bloom filter, but it may also return true for elements which have not been added. The accuracy can be estimated using the expectedFalsePositiveProbability()-method.

Put together, here is the full example.

double falsePositiveProbability = 0.1;
int expectedSize = 100;

BloomFilter<String> bloomFilter = new BloomFilter<String>(falsePositiveProbability, expectedSize);

bloomFilter.add("foo");

if (bloomFilter.contains("foo")) { // Always returns true
    System.out.println("BloomFilter contains foo!"); 
    System.out.println("Probability of a false positive: " + bloomFilter.expectedFalsePositiveProbability());
}

if (bloomFilter.contains("bar")) { // Should return false, but could return true
    System.out.println("There was a false positive.");
}

Compiling

To compile, run ant from the base directory.

ant

When ant is done, include dist/java-bloomfilter.jar in your project.

Alternatively, java-bloomfilter could be loaded in Netbeans and compiled using the IDE.

If you want to avoid adding another library to your project, all the Bloom filter code is in BloomFilter.java. You may copy this code directly into your project if you leave the LGPL-comment in place and reference the java-bloomfilter web page.

Changes

1.0

  • Improved the speed of the add() and contains()-methods. The speed increase is around 4-5 times on my computer, but the actual increase may vary from system to system.
  • Added benchmark code to test future code optimizations.
  • Fixed a bug in add() where numberOfAddedElements was incorrectly increased twice when adding other elements than byte-arrays. Updated test.
  • Moved project to Github
  • Added javadoc and source jar to build script.

0.9.3

  • New constructor for estimating bitSetSize from a given false positive probability.
  • New constructor for specifying bits per element, elements and hash functions (c, n, k) directly.
  • Added getExpectedBitsPerElement() and getBitsPerElement()

java-bloomfilter's People

Contributors

magnuss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

java-bloomfilter's Issues

Clarify license

There was a change that said "Changed license from GPL to LGPL", but it just added the LGPL without removing the GPL. This makes the licensing a bit ambiguous. I'd love to give this a shot, but in a project which is LGPL, but not GPL, compatible.

Thanks,

83ec16e

the error by bloomfilter

i have import the bloomfiter in my program

but the program can errors a message

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:104)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:84)
at redis.clients.jedis.Connection.sendCommand(Connection.java:127)
at redis.clients.jedis.BinaryClient.setbit(BinaryClient.java:912)
at redis.clients.jedis.Client.setbit(Client.java:671)
at redis.clients.jedis.Jedis.setbit(Jedis.java:2555)
at com.github.wxisme.bloomfilter.bitset.RedisBitSet.set(RedisBitSet.java:70)
at com.github.wxisme.bloomfilter.common.BloomFilter.add(BloomFilter.java:231)

so i can‘t find the problem

Hashing not implemented correctly

Your hash implementation has a problem. What you do is:

  1. Generate a hash, for instance 128bit MD5 hash.
  2. Split the hash in 4 byte chunks and interpret them a pseudorandom integers.
  3. When a value is inserted in the bloomfilter: take each random int and do a modulo w.r.t. the size of the bloom filter

The last part is wrong/dangerous since it violates the assumption that hash values are evenly distributed over [1,m) with m being the size of the bloom filter. Here is why:
Suppose the bloom filter has a size of 3 billion. The hash value are integers so approximately uniformly distributed from 0 to roughly 4 billion. By taking the modulo the hash values between 0 and 1 billion get generated twice as often as the range 1 billion to 3 billion, as the range 3 billion to 4 billion is mapped to 0 - 1 billion. You should easily be able to verify that by taking a histogram of a large number of hashes.

This corruption of uniformity will lead to a decreased false positive rate and hence should be fixed (for instance by discarding the values which are two high instead of taking the modulo).

ArithmeticException in case expectedNumberOElements is 0

I'm getting:
java.lang.ArithmeticException: / by zero
at com.skjegstad.utils.BloomFilter.contains(BloomFilter.java:345)

after creating filter with expectedNumberOfElements = 0 with constructor:
public BloomFilter(double falsePositiveProbability, int expectedNumberOfElements)
and check if it contains some element
What I'm expecting is either:

  • false when calling contains
  • exception on creation of filter which will tell me that 0 is bad value and I'm awful person that I passed it

k should be a double

Looks like the k should be a double.

public BloomFilter(int bitSetSize, int expectedNumberOElements) {
    this(bitSetSize / (double)expectedNumberOElements,
            expectedNumberOElements,
            (int) Math.round((bitSetSize / (double)expectedNumberOElements) * Math.log(2.0)));
}

It never hashes when the bitSetSize is 10, as an example.

Request: "getAndAdd" method

For iterating through huge lists with duplicates and using the bloom filter to make sure each use of an item from the list is only done once, it might speed things up to have a combined containsAndAdd method to avoid hanging to hash the same input twice.

if (myBloom.contains(itemFromList)) {
  return;
}
myBloom.add(itemFromList);
//... do things ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.