一个用于白名单服务的布隆过滤器(bloom filter)(三)
ash; String valString = element.toString(); for (int x = 0; x < k; x++) { hash = createHash(valString + Integer.toString(x)); hash = hash % (long)bitSetSize; bitset.set(Math.abs((int)hash), true); } numberOfAddedElements ++; } /** * Remove all elements from a Collection to the Bloom filter. * @param c Collection of elements. */ public void removeAll(Collection
c) { for (E element : c) remove(element); } public void remove(E element) { deleteMap.put(element, Boolean.TRUE); } public int getDeleteMapSize(){ return deleteMap.size(); } /** * Adds all elements from a Collection to the Bloom filter. * @param c Collection of elements. */ public void addAll(Collection
c) { for (E element : c) { if (element != null) add(element); } } /** * Returns true if the element could have been inserted into the Bloom filter. * Use getFalsePositiveProbability() to calculate the probability of this * being correct. * * @param element element to check. * @return true if the element could have been inserted into the Bloom filter. */ public boolean contains(E element) { Boolean contains = deleteMap.get(element); if (contains != null && contains) return false; long hash; String valString = element.toString(); for (int x = 0; x < k; x++) { hash = createHash(valString + Integer.toString(x)); hash = hash % (long) bitSetSize; if (!bitset.get(Math.abs((int) hash))) return false; } return true; } /** * Returns true if all the elements of a Collection could have been inserted * into the Bloom filter. Use getFalsePositiveProbability() to calculate the * probability of this being correct. * @param c elements to check. * @return true if all the elements in c could have been inserted into the Bloom filter. */ public boolean containsAll(Collection
c) { for (E element : c) if (!contains(element)) return false; return true; } /** * Read a single bit from the Bloom filter. * @param bit the bit to read. * @return true if the bit is set, false if it is not. */ public boolean getBit(int bit) { return bitset.get(bit); } /** * Set a single bit in the Bloom filter. * @param bit is the bit to set. * @param value If true, the bit is set. If false, the bit is cleared. */ public void setBit(int bit, boolean value) { bitset.set(bit, value); } /** * Return the bit set used to store the Bloom filter. * @return bit set representing the Bloom filter. */ public BitSet getBitSet() { return bitset; } /** * Returns the number of bits in the Bloom filter. Use count() to retrieve * the number of inserted elements. * * @return the size of the bitset used by the Bloom filter. */ public int size() { return this.bitSetSize; } /** * Returns the number of elements added to the Bloom filter after it * was constructed or after clear() was called. * * @return number of elements added to the Bloom filter. */ public int count() { return this.numberOfAddedElements; } /** * Returns the expected number of elements to be inserted into the filter. * This value is the same value as the one passed to the constructor. * * @return expected number of elements. */ public int getExpectedNumberOfElements() { return expectedNumberOfFilterElements; } /** * 返回更新的时间戳机制 * @return */ public long getTimestamp() { return timestamp; } /** * 设置跟新的时间戳 * @param timestamp */ public void setTimestamp(long timestamp) { this.timestamp = timestamp; } @Override public String toString() { return "BloomFilter [timestamp=" + timestamp + ", bitSetSize=" + bitSetSize + ", expectedNumberOfFilterElements=" + expectedNumberOfFilterElements + ", numberOfAddedElements=" + numberOfAddedElements + ", k=" + k +",deleteMapSize=" +getDeleteMapSize()+"]"; } }