布隆过滤器-Bloom Filter

1.布隆过滤器算法简介

Bloom Filter(BF)是一种空间效率很高的随机数据结构,它利用位数组很简洁的表示一个集合,并能判断一个元素是否属于这个集合。它是一个判断元素是否存在集合的快速的概率算法。Bloom Filter有可能会出现错误判断,但不会漏掉判断。也就是Bloom Filter判断元素不在集合,那么元素肯定不在集合,如果判断元素存在集合中,有一定的概率判断错误。你可以把布隆过滤器看做Java中的集合(collection),你可以往它里面添加元素,查询某个元素是否存在(就像一个HashSet)。

它的优点是空间效率和查询时间都远远超过一般的算法,缺点是有一定的误识别率和删除困难。

2. Bloom-Filter的基本思想

Bloom-Filter算法的核心思想就是利用多个不同的Hash函数来解决“冲突”。

判断某元素x是否在一个集合中,首相能想到的方法就是将所有的已知元素保存起来构成一个集合R,然后用元素x跟这些R中的元素比较来判断是否存在于集合R中;我们可以采用链表等数据结构来实现。但是,随着R中元素的增加,其占用的内存将越来越大。试想,如果有几千万个不同网页需要下载,所需的内存将足以占用整个进程的内存地址空间。即使用MD5,UUID这些方法将URL转换成固定的短小的字符串,内存占用也是相当大的。

我们会想到利用Hash Table的数据结构,运用一个足够好的Hash函数将一个URL映射到二进制位数组(位图数组)中的某一位。如果该位已经被置为1,那么表示该URL已经存在。Hash存在一个冲突(碰撞)问题,用同一个Hash得到的两个URL的值可能相同,为了减少冲突,我们可以引入多个Hash,如果通过其中的一个Hash值我们得出某元素不在集合中,那么该元素肯定不在集合中,只有在所有的Hash函数都表示该元素在集合中时,才能确定该元素存在于集合中。


原理要点:位数组    k个独立的hash函数

1)位数组

将设Bloom Filter使用一个m比特的数组来保存信息,初始状态时,Bloom Filter是一个包含m位的位数组,每一位都置为0,即BF整个数组的元素都设置为0


2)添加元素,k个独立hash函数

       为了表达S={x1, x2,…,xn}这样一个n个元素的集合,Bloom Filter使用k个相互独立的哈希函数(Hash Function),它们分别将集合中的每个元素映射到{1,…,m}的范围中。

         当我们往Bloom Filter中增加任意一个元素x时候,我们使用k个哈希函数得到k个哈希值,然后将数组中对应的比特位设置为1。即第i个哈希函数映射的位置hashi(x)就会被置为1(1≤i≤k)。

注意,如果一个位置多次被置为1,那么只有第一次会起作用,后面几次将没有任何效果。在下图中,k=3,且有两个哈希函数选中同一个位置(从左边数第五位,即第二个“1“处)。   

每个函数都能返回一个值,这个值必须能够作为位数组的索引(可以通过对数组长度进行取模得到)。然后,我们把位数组在这个索引处的值设为1。例如,第一个哈希函数作用于元素I上,返回x。类似的,第二个第三个哈希函数返回y与z,那么:

A[x]=A[y]=A[z] = 1

3)判断元素是否存在集合

    在判断y是否属于这个集合时,我们只需要对y使用k个哈希函数得到k个哈希值,如果所有hashi(y)的位置都是1(1≤i≤k),即k个位置都被设置为1了,那么我们就认为y是集合中的元素,否则就认为y不是集合中的元素。下图中y1就不是集合中的元素(因为y1有一处指向了“0”位)。y2或者属于这个集合,或者刚好是一个false positive。



      显然这 个判断并不保证查找的结果是100%正确的。

Bloom Filter的缺点:

       1)Bloom Filter无法从Bloom Filter集合中删除一个元素。因为该元素对应的位会牵动到其他的元素。所以一个简单的改进就是 counting Bloom filter,用一个counter数组代替位数组,就可以支持删除了。 此外,Bloom Filter的hash函数选择会影响算法的效果。

       2)还有一个比较重要的问题,如何根据输入元素个数n,确定位数组m的大小及hash函数个数,即hash函数选择会影响算法的效果。当hash函数个数k=(ln2)*(m/n)时错误率最小。在错误率不大于E的情况 下,m至少要等于n*lg(1/E) 才能表示任意n个元素的集合。但m还应该更大些,因为还要保证bit数组里至少一半为0,则m应 该>=nlg(1/E)*lge ,大概就是nlg(1/E)1.44(lg表示以2为底的对数)。 

举个例子我们假设错误率为0.01,则此时m应大概是n的13倍。这样k大概是8个。 

 注意:

         这里m与n的单位不同,m是bit为单位,而n则是以元素个数为单位(准确的说是不同元素的个数)。通常单个元素的长度都是有很多bit的。所以使用bloom filter内存上通常都是节省的。 

       一般BF可以与一些key-value的数据库一起使用,来加快查询。由于BF所用的空间非常小,所有BF可以常驻内存。这样子的话,对于大部分不存在的元素,我们只需要访问内存中的BF就可以判断出来了,只有一小部分,我们需要访问在硬盘上的key-value数据库。从而大大地提高了效率。


一个Bloom Filter有以下参数:

m bit数组的宽度(bit数)
n 加入其中的key的数量
k 使用的hash函数的个数
f False Positive的比率

Bloom Filter的f满足下列公式:


在给定m和n时,能够使f最小化的k值为:

此时给出的f为:

根据以上公式,对于任意给定的f,我们有:


n = m ln(0.6185) / ln(f)    [1]

同时,我们需要k个hash来达成这个目标:

k = - ln(f) / ln(2)             [2]

由于k必须取整数,我们在Bloom Filter的程序实现中,还应该使用上面的公式来求得实际的f:

f = (1 – e-kn/m)k             [3]

以上3个公式是程序实现Bloom Filter的关键公式。

3.哈希算法

哈希算法是影响布隆过滤器性能的地方。我们需要选择一个效率高但不耗时的哈希函数,在论文《更少的哈希函数,相同的性能指标:构造一个更好的过滤器》中,讨论了如何选用两个哈希函数来模拟k个哈希函数。首先,我们需要计算两个哈希函数h1(x)与h2(x)。然后,我们可以用这两个哈希函数来模仿产生k个哈希函数的效果:gi(x) = h1(x) + ih2(x);
这里i的取值范围是1到k的整数。
Google  guava类使用这个技巧实现了一个布隆过滤器,哈希算法的主要逻辑如下:
long hash64 = …; //calculate a 64 bit hash function
//split it in two halves of 32 bit hash values
int hash1 = (int) hash64;
int hash2 = (int) (hash64 >>> 32);
//Generate k different hash functions with a simple loop
for (int i = 1; i <= numHashFunctions; i++) {
int nextHash = hash1 + i * hash2;
}

4. 扩展Counter Bloom Filter

Bloom Filter有个缺点,就是不支持删除操作,因为它不知道某一个位从属于哪些向量。我们可以给Bloom Filter加上计数器,增加时增加计数器,删除时减少计数器。
但这样的Filter需要考虑附加的计数器大小,加入同个元素多次插入的话,计数器位数较少的情况下,就会出现溢出问题。如果对计数器设置上限值的话,会导致Cache Miss,但对某些应用来说,这并不是什么问题,如Web Sharing。

Compressed  Bloom Filter

为了能在服务器之间更快的通过网络传输Bloom Filter,我们有方法能在已完成Bloom  Filter之后,得到一些实际参数的情况下进行压缩,将元素全部添加入Bloom Filter,对原先的哈希值进行求余处理,在误判率不变的情况下,使得其内存大小更合适。


5. Bloom Filter的应用

Bloom-Filter一般用于大数据量的集合中判定某元素是否存在。例如邮件服务器中的垃圾邮件过滤器。在搜索引擎领域,Bloom-Filter最常用于网络蜘蛛(Spider)的URL过来,网络蜘蛛通常有一个URL列表,保存着将要下载和已经下载的网页的URL,网络蜘蛛下载了一个页面,从页面中提取到新的URL后,需要判断该URL是否已经存在于列表中。此时,Bloom Filter算法是最好的选择。

(1)key-value 加速查询

一般Bloom Filter可以与一些key-value的数据库一起使用,来加速查询。一般key-value存储系统的values存在硬盘,查询就是件费时的事情。将Storage的数据都插入Filter,在Filter中查询都不存在时,就不需要去Storage查询了。当False  Position出现时,只是会导致一次多余的Storage查询。

由于Bloom-Filter所用的空间非常小,所有BF可以常驻内存。对于大部分不存在的元素,我们只需要访问内存中的Bloom Filter就可以判断出来了,只有一小部分,我们需要访问在硬盘上的key-value数据库,从而大大提高了效率。如图:


(2)Google的BigTable

Google的BigTable也使用了Bloom  Filter,以减少不存在的行或列在磁盘上的查询,大大提高了数据库的查询操作的性能。

(3)Proxy-Cache

在Internet Cache Protocal中的Proxy-Cache很多都是使用Bloom  Filter存储URLs,除了高效的查询外,还能很方便的传输交换Cache信息。

(4). 网络应用

1)P2P网络中查找资源操作,可以对每条网络通路保存Bloom Filter,当命中时,则选择该通路访问。

2)广播消息时,可以检测某个IP是否已发包。

3)检测广播消息包的环路,将Bloom  Filter保存在包里,每个节点将自己添加入Bloom  Filter。

4)信息队列管理,使用Counter  Bloom  Filter管理信息流量。

(5)垃圾邮件地址过滤

记录下发垃圾邮件的email地址。由于哪些发送者不停地在注册新的地址,全世界少说也有几十亿个发垃圾邮件的地址,将他们存起来则需要大量的网络服务器。如果用哈希表,每存储一亿个email地址,就需要1.6GB的内存(用哈希表实现的具体办法是将每一个email地址对应成一个八字节的信息指纹,然后将这些信息指纹存入哈希表,由于哈希表的存储效率一般只有50%,因此一个email地址需要占用十六个字节。一亿个地址大约要1.6GB,即十六亿字节的内存)。因此存储几十亿个邮件地址可能需要上百GB的内存。

而Bloom  Filter只需要哈希表1/8到1/4的大小就能解决同样的问题。BF绝不会漏掉任何一个在黑名单中的可疑地址。而至于误判问题,常见的补救办法是在建立一个小的白名单,存储哪些可能被误判的邮件地址。


6. Bloom - Filter的具体实现:

php实现:

<?php  
  
/** 
 * Implements a Bloom Filter 
 */  
class BloomFilter {  
    /** 
     * Size of the bit array 
     * 
     * @var int 
     */  
    protected $m;  
  
    /** 
     * Number of hash functions 
     * 
     * @var int 
     */  
    protected $k;  
  
    /** 
     * Number of elements in the filter 
     * 
     * @var int 
     */  
    protected $n;  
  
    /** 
     * The bitset holding the filter information 
     * 
     * @var array 
     */  
    protected $bitset;  
  
    /** 
     * 计算最优的hash函数个数:当hash函数个数k=(ln2)*(m/n)时错误率最小 
     * 
     * @param int $m bit数组的宽度(bit数) 
     * @param int $n 加入布隆过滤器的key的数量 
     * @return int 
     */  
    public static function getHashCount($m, $n) {  
        return ceil(($m / $n) * log(2));  
    }  
  
    /** 
     * Construct an instance of the Bloom filter 
     * 
     * @param int $m bit数组的宽度(bit数) Size of the bit array 
     * @param int $k hash函数的个数 Number of different hash functions to use 
     */  
    public function __construct($m, $k) {  
        $this->m = $m;  
        $this->k = $k;  
        $this->n = 0;  
  
        /* Initialize the bit set */  
        $this->bitset = array_fill(0, $this->m - 1, false);  
    }  
  
    /** 
     * False Positive的比率:f = (1 – e-kn/m)k    
     * Returns the probability for a false positive to occur, given the current number of items in the filter 
     * 
     * @return double 
     */  
    public function getFalsePositiveProbability() {  
        $exp = (-1 * $this->k * $this->n) / $this->m;  
  
        return pow(1 - exp($exp),  $this->k);  
    }  
  
    /** 
     * Adds a new item to the filter 
     * 
     * @param mixed Either a string holding a single item or an array of  
     *              string holding multiple items.  In the latter case, all 
     *              items are added one by one internally. 
     */  
    public function add($key) {  
        if (is_array($key)) {  
            foreach ($key as $k) {  
                $this->add($k);  
            }  
            return;  
        }  
  
        $this->n++;  
  
        foreach ($this->getSlots($key) as $slot) {  
            $this->bitset[$slot] = true;  
        }  
    }  
  
    /** 
     * Queries the Bloom filter for an element 
     * 
     * If this method return FALSE, it is 100% certain that the element has 
     * not been added to the filter before.  In contrast, if TRUE is returned, 
     * the element *may* have been added to the filter previously.  However with 
     * a probability indicated by getFalsePositiveProbability() the element has 
     * not been added to the filter with contains() still returning TRUE. 
     * 
     * @param mixed Either a string holding a single item or an array of  
     *              strings holding multiple items.  In the latter case the 
     *              method returns TRUE if the filter contains all items. 
     * @return boolean 
     */  
    public function contains($key) {  
        if (is_array($key)) {  
            foreach ($key as $k) {  
                if ($this->contains($k) == false) {  
                    return false;  
                }  
            }  
  
            return true;  
        }  
  
        foreach ($this->getSlots($key) as $slot) {  
            if ($this->bitset[$slot] == false) {  
                return false;  
            }  
        }  
  
        return true;  
    }  
  
    /** 
     * Hashes the argument to a number of positions in the bit set and returns the positions 
     * 
     * @param string Item 
     * @return array Positions 
     */  
    protected function getSlots($key) {  
        $slots = array();  
        $hash = self::getHashCode($key);  
        mt_srand($hash);  
  
        for ($i = 0; $i < $this->k; $i++) {  
            $slots[] = mt_rand(0, $this->m - 1);  
        }  
  
        return $slots;  
    }  
  
    /** 
     * 使用CRC32产生一个32bit(位)的校验值。 
     * 由于CRC32产生校验值时源数据块的每一bit(位)都会被计算,所以数据块中即使只有一位发生了变化,也会得到不同的CRC32值。 
     * Generates a numeric hash for the given string 
     * 
     * Right now the CRC-32 algorithm is used.  Alternatively one could e.g. 
     * use Adler digests or mimick the behaviour of Java's hashCode() method. 
     * 
     * @param string Input for which the hash should be created 
     * @return int Numeric hash 
     */  
    protected static function getHashCode($string) {  
        return crc32($string);  
    }  
      
}  
  
  
  
$items = array("first item", "second item", "third item");  
          
/* Add all items with one call to add() and make sure contains() finds 
 * them all. 
 */  
$filter = new BloomFilter(100, BloomFilter::getHashCount(100, 3));  
$filter->add($items);  
  
//var_dump($filter); exit;  
$items = array("firsttem", "seconditem", "thirditem");  
foreach ($items as $item) {  
 var_dump(($filter->contains($item)));  
}  
  
  
/* Add all items with multiple calls to add() and make sure contains() 
* finds them all. 
*/  
$filter = new BloomFilter(100, BloomFilter::getHashCount(100, 3));  
foreach ($items as $item) {  
    $filter->add($item);  
}  
$items = array("fir sttem", "secondit em", "thir ditem");  
foreach ($items as $item) {  
 var_dump(($filter->contains($item)));  
}  
  

Java实现
/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.skjegstad.utils;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.BitSet;
import java.util.Collection;

/**
 * Implementation of a Bloom-filter, as described here:
 * http://en.wikipedia.org/wiki/Bloom_filter
 *
 * For updates and bugfixes, see http://github.com/magnuss/java-bloomfilter
 *
 * Inspired by the SimpleBloomFilter-class written by Ian Clarke. This
 * implementation provides a more evenly distributed Hash-function by
 * using a proper digest instead of the Java RNG. Many of the changes
 * were proposed in comments in his blog:
 * http://blog.locut.us/2008/01/12/a-decent-stand-alone-java-bloom-filter-implementation/
 *
 * @param <E> Object type that is to be inserted into the Bloom filter, e.g. String or Integer.
 * @author Magnus Skjegstad <magnus@skjegstad.com>
 */
public class BloomFilter<E> implements Serializable {
    private BitSet bitset;
    private int bitSetSize;
    private double bitsPerElement;
    private int expectedNumberOfFilterElements; // expected (maximum) number of elements to be added
    private int numberOfAddedElements; // number of elements actually added to the Bloom filter
    private int k; // number of hash functions

    static final Charset charset = Charset.forName("UTF-8"); // encoding used for storing hash values as strings

    static final String hashName = "MD5"; // MD5 gives good enough accuracy in most circumstances. Change to SHA1 if it's needed
    static final MessageDigest digestFunction;
    static { // The digest method is reused between instances
        MessageDigest tmp;
        try {
            tmp = java.security.MessageDigest.getInstance(hashName);
        } catch (NoSuchAlgorithmException e) {
            tmp = null;
        }
        digestFunction = tmp;
    }

    /**
      * Constructs an empty Bloom filter. The total length of the Bloom filter will be
      * c*n.
      *
      * @param c is the number of bits used per element.
      * @param n is the expected number of elements the filter will contain.
      * @param k is the number of hash functions used.
      */
    public BloomFilter(double c, int n, int k) {
      this.expectedNumberOfFilterElements = n;
      this.k = k;
      this.bitsPerElement = c;
      this.bitSetSize = (int)Math.ceil(c * n);
      numberOfAddedElements = 0;
      this.bitset = new BitSet(bitSetSize);
    }

    /**
     * Constructs an empty Bloom filter. The optimal number of hash functions (k) is estimated from the total size of the Bloom
     * and the number of expected elements.
     *
     * @param bitSetSize defines how many bits should be used in total for the filter.
     * @param expectedNumberOElements defines the maximum number of elements the filter is expected to contain.
     */
    public BloomFilter(int bitSetSize, int expectedNumberOElements) {
        this(bitSetSize / (double)expectedNumberOElements,
             expectedNumberOElements,
             (int) Math.round((bitSetSize / (double)expectedNumberOElements) * Math.log(2.0)));
    }

    /**
     * Constructs an empty Bloom filter with a given false positive probability. The number of bits per
     * element and the number of hash functions is estimated
     * to match the false positive probability.
     *
     * @param falsePositiveProbability is the desired false positive probability.
     * @param expectedNumberOfElements is the expected number of elements in the Bloom filter.
     */
    public BloomFilter(double falsePositiveProbability, int expectedNumberOfElements) {
        this(Math.ceil(-(Math.log(falsePositiveProbability) / Math.log(2))) / Math.log(2), // c = k / ln(2)
             expectedNumberOfElements,
             (int)Math.ceil(-(Math.log(falsePositiveProbability) / Math.log(2)))); // k = ceil(-log_2(false prob.))
    }

    /**
     * Construct a new Bloom filter based on existing Bloom filter data.
     *
     * @param bitSetSize defines how many bits should be used for the filter.
     * @param expectedNumberOfFilterElements defines the maximum number of elements the filter is expected to contain.
     * @param actualNumberOfFilterElements specifies how many elements have been inserted into the <code>filterData</code> BitSet.
     * @param filterData a BitSet representing an existing Bloom filter.
     */
    public BloomFilter(int bitSetSize, int expectedNumberOfFilterElements, int actualNumberOfFilterElements, BitSet filterData) {
        this(bitSetSize, expectedNumberOfFilterElements);
        this.bitset = filterData;
        this.numberOfAddedElements = actualNumberOfFilterElements;
    }

    /**
     * Generates a digest based on the contents of a String.
     *
     * @param val specifies the input data.
     * @param charset specifies the encoding of the input data.
     * @return digest as long.
     */
    public static int createHash(String val, Charset charset) {
        return createHash(val.getBytes(charset));
    }

    /**
     * Generates a digest based on the contents of a String.
     *
     * @param val specifies the input data. The encoding is expected to be UTF-8.
     * @return digest as long.
     */
    public static int createHash(String val) {
        return createHash(val, charset);
    }

    /**
     * Generates a digest based on the contents of an array of bytes.
     *
     * @param data specifies input data.
     * @return digest as long.
     */
    public static int createHash(byte[] data) {
        return createHashes(data, 1)[0];
    }

    /**
     * Generates digests based on the contents of an array of bytes and splits the result into 4-byte int's and store them in an array. The
     * digest function is called until the required number of int's are produced. For each call to digest a salt
     * is prepended to the data. The salt is increased by 1 for each call.
     *
     * @param data specifies input data.
     * @param hashes number of hashes/int's to produce.
     * @return array of int-sized hashes
     */
    public static int[] createHashes(byte[] data, int hashes) {
        int[] result = new int[hashes];

        int k = 0;
        byte salt = 0;
        while (k < hashes) {
            byte[] digest;
            synchronized (digestFunction) {
                digestFunction.update(salt);
                salt++;
                digest = digestFunction.digest(data);                
            }
        
            for (int i = 0; i < digest.length/4 && k < hashes; i++) {
                int h = 0;
                for (int j = (i*4); j < (i*4)+4; j++) {
                    h <<= 8;
                    h |= ((int) digest[j]) & 0xFF;
                }
                result[k] = h;
                k++;
            }
        }
        return result;
    }

    /**
     * Compares the contents of two instances to see if they are equal.
     *
     * @param obj is the object to compare to.
     * @return True if the contents of the objects are equal.
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final BloomFilter<E> other = (BloomFilter<E>) obj;        
        if (this.expectedNumberOfFilterElements != other.expectedNumberOfFilterElements) {
            return false;
        }
        if (this.k != other.k) {
            return false;
        }
        if (this.bitSetSize != other.bitSetSize) {
            return false;
        }
        if (this.bitset != other.bitset && (this.bitset == null || !this.bitset.equals(other.bitset))) {
            return false;
        }
        return true;
    }

    /**
     * Calculates a hash code for this class.
     * @return hash code representing the contents of an instance of this class.
     */
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 61 * hash + (this.bitset != null ? this.bitset.hashCode() : 0);
        hash = 61 * hash + this.expectedNumberOfFilterElements;
        hash = 61 * hash + this.bitSetSize;
        hash = 61 * hash + this.k;
        return hash;
    }


    /**
     * Calculates the expected probability of false positives based on
     * the number of expected filter elements and the size of the Bloom filter.
     * <br /><br />
     * The value returned by this method is the <i>expected</i> rate of false
     * positives, assuming the number of inserted elements equals the number of
     * expected elements. If the number of elements in the Bloom filter is less
     * than the expected value, the true probability of false positives will be lower.
     *
     * @return expected probability of false positives.
     */
    public double expectedFalsePositiveProbability() {
        return getFalsePositiveProbability(expectedNumberOfFilterElements);
    }

    /**
     * Calculate the probability of a false positive given the specified
     * number of inserted elements.
     *
     * @param numberOfElements number of inserted elements.
     * @return probability of a false positive.
     */
    public double getFalsePositiveProbability(double numberOfElements) {
        // (1 - e^(-k * n / m)) ^ k
        return Math.pow((1 - Math.exp(-k * (double) numberOfElements
                        / (double) bitSetSize)), k);

    }

    /**
     * Get the current probability of a false positive. The probability is calculated from
     * the size of the Bloom filter and the current number of elements added to it.
     *
     * @return probability of false positives.
     */
    public double getFalsePositiveProbability() {
        return getFalsePositiveProbability(numberOfAddedElements);
    }


    /**
     * Returns the value chosen for K.<br />
     * <br />
     * K is the optimal number of hash functions based on the size
     * of the Bloom filter and the expected number of inserted elements.
     *
     * @return optimal k.
     */
    public int getK() {
        return k;
    }

    /**
     * Sets all bits to false in the Bloom filter.
     */
    public void clear() {
        bitset.clear();
        numberOfAddedElements = 0;
    }

    /**
     * Adds an object to the Bloom filter. The output from the object's
     * toString() method is used as input to the hash functions.
     *
     * @param element is an element to register in the Bloom filter.
     */
    public void add(E element) {
       add(element.toString().getBytes(charset));
    }

    /**
     * Adds an array of bytes to the Bloom filter.
     *
     * @param bytes array of bytes to add to the Bloom filter.
     */
    public void add(byte[] bytes) {
       int[] hashes = createHashes(bytes, k);
       for (int hash : hashes)
           bitset.set(Math.abs(hash % bitSetSize), true);
       numberOfAddedElements ++;
    }

    /**
     * Adds all elements from a Collection to the Bloom filter.
     * @param c Collection of elements.
     */
    public void addAll(Collection<? extends E> c) {
        for (E element : c)
            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) {
        return contains(element.toString().getBytes(charset));
    }

    /**
     * Returns true if the array of bytes could have been inserted into the Bloom filter.
     * Use getFalsePositiveProbability() to calculate the probability of this
     * being correct.
     *
     * @param bytes array of bytes to check.
     * @return true if the array could have been inserted into the Bloom filter.
     */
    public boolean contains(byte[] bytes) {
        int[] hashes = createHashes(bytes, k);
        for (int hash : hashes) {
            if (!bitset.get(Math.abs(hash % bitSetSize))) {
                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<? extends E> 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;
    }

    /**
     * Get expected number of bits per element when the Bloom filter is full. This value is set by the constructor
     * when the Bloom filter is created. See also getBitsPerElement().
     *
     * @return expected number of bits per element.
     */
    public double getExpectedBitsPerElement() {
        return this.bitsPerElement;
    }

    /**
     * Get actual number of bits per element based on the number of elements that have currently been inserted and the length
     * of the Bloom filter. See also getExpectedBitsPerElement().
     *
     * @return number of bits per element.
     */
    public double getBitsPerElement() {
        return this.bitSetSize / (double)numberOfAddedElements;
    }
}



参考:
http://blog.csdn.net/hguisu/article/details/7866173
https://github.com/MagnusS/Java-BloomFilter/blob/master/src/com/skjegstad/utils/BloomFilter.java      

http://www.codeceo.com/article/java-bloomfilter-cache-system.html




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值