@author Ka Ming Nip
| 23 | * @author Ka Ming Nip |
| 24 | */ |
| 25 | public class CascadingBloomFilter implements BloomFilterInterface { |
| 26 | protected final BloomFilter[] bfs; |
| 27 | protected final BloomFilter topLevelBf; |
| 28 | protected final int numLevels; |
| 29 | protected final long size; // number of bits |
| 30 | protected final long partitionSize; |
| 31 | protected final int numHash; |
| 32 | protected final HashFunction hashFunction; |
| 33 | |
| 34 | public CascadingBloomFilter(long size, int numHash, HashFunction hashFunction, int numLevels) { |
| 35 | this.numLevels = numLevels; |
| 36 | bfs = new BloomFilter[numLevels]; |
| 37 | this.size = size; |
| 38 | this.partitionSize = size/numLevels; |
| 39 | this.numHash = numHash; |
| 40 | this.hashFunction = hashFunction; |
| 41 | for (int i=0; i<numLevels; ++i) { |
| 42 | bfs[i] = new BloomFilter(partitionSize, numHash, hashFunction); |
| 43 | } |
| 44 | topLevelBf = bfs[numLevels-1]; |
| 45 | } |
| 46 | |
| 47 | public int getNumLevels() { |
| 48 | return numLevels; |
| 49 | } |
| 50 | |
| 51 | public BloomFilter getBloomFilter(int level) { |
| 52 | return bfs[level]; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void add(String key) { |
| 57 | long[] hashVals = new long[numHash]; |
| 58 | this.hashFunction.getHashValues(key, numHash, hashVals); |
| 59 | add(hashVals); |
| 60 | } |
| 61 | |
| 62 | public void add(long hashVal) { |
| 63 | add(this.hashFunction.getHashValues(hashVal, numHash)); |
| 64 | } |
| 65 | |
| 66 | public void add(long[] hashVals) { |
| 67 | for (BloomFilter bf : bfs) { |
| 68 | if (!bf.lookupThenAdd(hashVals)) { |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean lookup(String key) { |
| 76 | long[] hashVals = new long[numHash]; |
| 77 | this.hashFunction.getHashValues(key, numHash, hashVals); |
| 78 | return lookup(hashVals); |
| 79 | } |
| 80 | |
| 81 | public boolean lookup(long hashVal) { |
| 82 | return topLevelBf.lookup(hashVal); |
nothing calls this directly
no outgoing calls
no test coverage detected