The Bloom filter principle: Wherever a list or set is used, and space is at a premium, consider using a Bloom filter if the effect of false positives can be mitigated.
第一次听说Bloom filter的时候是讨论班上谢师姐的一个报告,当时觉得这玩意挺扯淡的,hash来hash去的也没有什么意思。后来直到自己也面临这个问题,在一个大数据集合中相当频繁的查找数据,数据集越大,访问代价和内存消耗就越大,大到非但老板,连自己都不能忍受。于是才又想起这个神奇的名字。这些天来细细琢磨,Bloom Filter的那些事儿
Bloom Filter是一种空间利用率很高的数据结构,有点类似位图。它被用来检测一个元素是否存在于一个特定的集合中,它的基本思想是:
- 一个空的Bloom filter是一个长为m的位数组。
- 除了位数组外,还有k个不同的hash函数,每个hash函数都能够将元素映射到位数组中的一个位上。
- 针对每一个元素,将其hash K次得到位置 P1,P2,P3……Pn 。
- 在插入时,将为数组中下标为Pi的位置为1
- 在查询时,测试下标为P1,P2,P3……Pn 的位,如果这些位都为1,则认为该元素存在于集合中。
其实Bloom Filter 是一个很古老的算法,允许在有限的内存里(你想在这块内存里存放关键字的完整列表),执行成员测试,用以避免使用磁盘或数据库惊醒查询的性能瓶颈。但是空间的节省并不是没有代价的,存在着可大可小的假命中风险,并且增加了一定的key和filter之后,就很难删除它。并却需要小心的配置其使用的hash函数,使其分布在具体的已经分配好的向量空间。
参考
· Bloom Filters -- the math. A good place to start for an overview of the math behind Bloom filters.
· Some Motley Bloom Tricks. Handy filter tricks and theory page.
· Bloom Filter Survey. A handy survey article on Bloom filter network applications.
· LOAF. Our own system for incorporating social networks onto email using Bloom filters.
· Compressed Bloom Filters. If you are passing filters around a network, you will want to optimize them for minimum size; this paper gives a good overview of compressed Bloom filters.
· Bloom16. A CPAN module implementing a counting Bloom filter.
· Text::Bloom. CPAN module for using Bloom filters with text collections.
· Privacy-Enhanced Searches Using Encryted Bloom Filters. This paper discusses how to use encryption and Bloom filters to set up a query system that prevents the search engine from knowing the query you are running.
· Bloom Filters as Summaries. Some performance data on actually using Bloom filters as cache summaries.
·Using Bloom Filters for Authenticated Yes/No Answers in the DNS. Internet draft for using Bloom filters to implement Secure DNS
和仙子翻译的使用Bloom Filters