Author: j16sdiz
Date: 2008-07-31 00:50:48 +0000 (Thu, 31 Jul 2008)
New Revision: 21515

Modified:
   branches/saltedhashstore/freenet/src/freenet/support/BloomFilter.java
Log:
use Random instead of int[], save a few hash in checkFilter()

Modified: branches/saltedhashstore/freenet/src/freenet/support/BloomFilter.java
===================================================================
--- branches/saltedhashstore/freenet/src/freenet/support/BloomFilter.java       
2008-07-30 22:45:29 UTC (rev 21514)
+++ branches/saltedhashstore/freenet/src/freenet/support/BloomFilter.java       
2008-07-31 00:50:48 UTC (rev 21515)
@@ -2,6 +2,7 @@

 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
+import java.util.Random;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;

@@ -26,11 +27,11 @@

        //-- Core
        public void addKey(byte[] key) {
-               int[] hashes = getHashes(key);
+               Random hashes = getHashes(key);
                lock.writeLock().lock();
                try {
-                       for (int i = 0; i < hashes.length; i++)
-                               setBit(hashes[i]);
+                       for (int i = 0; i < k; i++)
+                               setBit(hashes.nextInt(length));
                } finally {
                        lock.writeLock().unlock();
                }
@@ -40,11 +41,11 @@
        }

        public boolean checkFilter(byte[] key) {
-               int[] hashes = getHashes(key);
+               Random hashes = getHashes(key);
                lock.readLock().lock();
                try {
-                       for (int i = 0; i < hashes.length; i++)
-                               if (!getBit(hashes[i]))
+                       for (int i = 0; i < k; i++)
+                               if (!getBit(hashes.nextInt(length)))
                                        return false;
                } finally {
                        lock.readLock().unlock();
@@ -53,11 +54,11 @@
        }

        public void removeKey(byte[] key) {
-               int[] hashes = getHashes(key);
+               Random hashes = getHashes(key);
                lock.writeLock().lock();
                try {
-                       for (int i = 0; i < hashes.length; i++)
-                               unsetBit(hashes[i]);
+                       for (int i = 0; i < k; i++)
+                               unsetBit(hashes.nextInt(length));
                } finally {
                        lock.writeLock().unlock();
                }
@@ -73,14 +74,8 @@

        protected abstract void unsetBit(int offset);

-       protected int[] getHashes(byte[] key) {
-               int[] hashes = new int[k];
-
-               MersenneTwister mt = new MersenneTwister(key);
-               for (int i = 0; i < k; i++)
-                       hashes[i] = mt.nextInt(length);
-
-               return hashes;
+       protected Random getHashes(byte[] key) {
+               return new MersenneTwister(key);
        }

        //-- Fork & Merge
@@ -166,4 +161,4 @@
                filter = null;
                forkedFilter = null;
        }
-}
\ No newline at end of file
+}


Reply via email to