LateX

Thursday, May 9, 2013

Fast Bloom Filters

I have looked for Bloom Filter in the internet, but all implementations I have found were too slow. So I implemented my own.

It is not fully general, but I am sure if somebody is desperate for performance like I am they will make it more general by themselves :)

I used Murmur hash implementation that you can download here: http://d3s.mff.cuni.cz/~holub/sw/javamurmurhash/


The code:
package uk.ac.cam.cl.ss958.SpringBoardSimulation;

package uk.ac.cam.cl.ss958;

import ie.ucd.murmur.MurmurHash;

import java.util.BitSet;
import java.util.Random;

public class FastBloomFilter {

 private final BitSet bs;
 
 final int [] hashSeeds;

 final int capacity;
 
 public FastBloomFilter(int slots, int hashFunctions) {
  bs = new BitSet(slots);
  Random r = new Random(System.currentTimeMillis());
  hashSeeds = new int[hashFunctions];
  for (int i=0; i < hashFunctions; ++i) {
   hashSeeds[i] = r.nextInt();
  }
  capacity = slots;
 }
 
 public void add(int value) {
  byte [] b = new byte[] {
             (byte)(value >>> 24),
             (byte)(value >>> 16),
             (byte)(value >>> 8),
             (byte)value};
  for (int i=0; i < hashSeeds.length; ++i) {
   int h = MurmurHash.hash32(b, 4, hashSeeds[i]);
   bs.set(Math.abs(h)%capacity, true);
  }
 }
 
 public void clear() {
  bs.clear();
 }
 
 public boolean mightContain(int value) {
  byte [] b = new byte[] {
             (byte)(value >>> 24),
             (byte)(value >>> 16),
             (byte)(value >>> 8),
             (byte)value};
  for (int i=0; i < hashSeeds.length; ++i) {
   int h = MurmurHash.hash32(b, 4, hashSeeds[i]);
   if(!bs.get(Math.abs(h)%capacity)) {
    return false;
   }
  
  }
  
  return true;
 }
 
 
 public static void main(String [] args) {
  FastBloomFilter bf = new FastBloomFilter(1000, 10);
  System.out.println("Query for 2000: " + bf.mightContain(2000));
  System.out.println("Adding 2000");
  bf.add(2000);
  System.out.println("Query for 2000: " + bf.mightContain(2000));

  
 }
}


35 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. English code = true
    Cuz True is an english word

    ReplyDelete
  3. Your blog on computers is fantastic! Your writing is clear, concise, and informative. You have a great way of breaking down complex topics into easily understandable language. Your passion for technology is evident in your writing. Keep up the excellent work!

    Do My Homework is a leading academic company that offers homework help for students of all levels, from high school to college and beyond. With a team of well-qualified and experienced experts, the company provides students with high-quality homework solutions that are tailored to their specific needs.

    At Do My Homework, students can find online experts right now who specialize in a wide range of subjects, from math and science to humanities and social sciences. These experts are carefully selected based on their academic credentials, professional experience, and proven track record of success in helping students achieve their academic goals.

    ReplyDelete
  4. I must say you have written a great article. A must read post that need to bookmarked

    ReplyDelete
  5. This was super interesting article to read. Thanks for sharing it here!

    ReplyDelete
  6. This content data gives truly quality and unique information. Thanks

    ReplyDelete
  7. I’m excited to uncover this page. Thank you for this fantastic read!

    ReplyDelete

  8. I got really good information from this content, thanks for sharing.

    ReplyDelete
  9. Fabulous, what a weblog it is! This blog provides valuable information to us, keep it up.

    ReplyDelete
  10. Either way keep up the excellent quality writing.

    ReplyDelete
  11. You have a good point here! I totally agree with what you have said!!

    ReplyDelete
  12. Thanks for sharing your views. hope more people will read this article!!

    ReplyDelete
  13. I must tell you this is an excellent post.

    ReplyDelete
  14. Greate article. Keep writing such kind of info on your page. Im really impressed by it.

    ReplyDelete
  15. Thanks and Best of luck to your next Blog in future.

    ReplyDelete
  16. Wonderful items from you, man. You’re making it entertaining blog. Thanks

    ReplyDelete
  17. Hello there, You have performed an excellent article.. Goodjob!

    ReplyDelete
  18. We truly dearly loved examining this web site. Thanks for this blog!

    ReplyDelete
  19. Hi everyone, This web site is piece of writing is actually good, keep up posting.

    ReplyDelete
  20. What’s up, everything is going nicely in this blog here, keep up writing.

    ReplyDelete
  21. Pretty! This was an extremely wonderful post. Thank you for providing this info.

    ReplyDelete
  22. This is the right site for everyone who would like to find out about this topic.

    ReplyDelete
  23. Bookmarked this website, This site contains valuable information. Thanks..

    ReplyDelete
  24. I am really happy with the articles quality. Keep on blogging, its great!

    ReplyDelete
  25. Thanks a lot for this great stuff here. I am very much thankful for this site.

    ReplyDelete
  26. I will bookmark this blog and check here again. Keep doing your blog post

    ReplyDelete
  27. Very informative the stuff you provide I really enjoyed reading, Thanks for this

    ReplyDelete
  28. Nice knowledge gaining article. This post is really the best on this valuable topic.

    ReplyDelete
  29. Very useful post. I found so many Interesting stuff in your blog. Its great article.

    ReplyDelete
  30. It shows your effort and dedication. Thanks for sharing such a nice post.

    ReplyDelete