布隆过滤器
它实际上是一个很长的二进制向量和一系列随机映射函数布隆过滤器可以用于检索一个元素是否在一个集合中。它的优点是空间效率和查询时间都远远超过一般的算法,缺点是有一定的误识别率和删除困难。
优点:
相比于其它的数据结构,布隆过滤器在空间和时间方面都有巨大的优势。布隆过滤器存储空间和插入/查询时间都是常数。另外, Hash函数相互之间没有关系,方便由硬件并行实现。布隆过滤器不需要存储元素本身,在某些对保密要求非常严格的场合有优势。
缺点:
误算率是其中之一。随着存入的元素数量增加,误算率随之增加。但是如果元素数量太少,则使用散列表足矣。
另外,一般情况下不能从布隆过滤器中删除元素。我们很容易想到把位列阵变成整数数组,每插入一个元素相应的计数器加1, 这样删除元素时将计数器减掉就可以了。然而要保证安全的删除元素并非如此简单。首先我们必须保证删除的元素的确在布隆过滤器里面. 这一点单凭这个过滤器是无法保证的。另外计数器回绕也会造成问题。
在降低误算率方面,有不少工作,使得出现了很多布隆过滤器的变种。
BloomFilter.h
#pragma once
#include<iostream>
using namespace std;
#include<vector>
#include<string.h>
class BitMap
{
public:
BitMap(size_t size = 0)//size表示位的个数
:_size(0)
{
_a.resize((size >> 5) + 1);//+1是为防止不能整除有余数
}
void Set(size_t x)//x对应位置1
{
size_t index = x >> 5; //取到x在第index个size_t中
size_t num = x % 32; //取到x在第index个size_t中的第num位
if (!(_a[index] & (1 << num)))
{
++_size;
}
_a[index] |= (1 << num); //对应位置1
//cout << "_a[index] "<<_a[index]<< endl;
}
void Reset(size_t size) //清空size对应的位
{
size_t index = size >> 5;
size_t num = size % 32;
_a[index] &= ~(1 << num);
_size--;
//cout << "_a[index] " << _a[index] << endl;
}
bool Test(size_t size)//检测size是否存在,也就是检测size对应位是否为1
{
size_t index = size >> 5;
size_t num = size % 32;
if (_a[index] & (1 << num))
{
return true;
}
return false;
}
void ReSize(size_t size)
{
_a.resize((size >> 5) + 1);
}
protected:
vector<size_t> _a;
size_t _size;//表示表中元素个数
};
//布隆过滤器
// 若判定不在布隆过滤器内就一定不在,若判定在布隆过滤器内则不一定存在
//可以删除布隆过滤器内的元素,但必须采用引用计数设置布隆过滤器
template <class K>
//使用搜索到的5种Hash函数
//http://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html
struct _HashFunc1
{
size_t DJBHash(const char *str)
{
if (!*str)
return 0;
register size_t hash = 5381;
while (size_t ch = (size_t)*str++)
{
hash += (hash << 5) + ch;
}
return hash;
}
size_t operator()(const K& str)
{
return DJBHash(str.c_str());
}
};
template <class K>
struct _HashFunc2
{
size_t SDBMHash(const char *str)
{
register size_t hash = 0;
while (size_t ch=(size_t)*str++)
{
hash = 65599 * hash + ch;
}
return hash;
}
size_t operator()(const K& str)
{
return SDBMHash(str.c_str());
}
};
template <class K>
struct _HashFunc3
{
size_t RSHash(const char *str)
{
register size_t hash = 0;
size_t magic = 63689;
while (size_t ch = (size_t)*str++)
{
hash = hash * magic + ch;
magic *= 378551;
}
return hash;
}
size_t operator()(const K& str)
{
return RSHash(str.c_str());
}
};
template <class K>
struct _HashFunc4
{
size_t APHash(const char *str)
{
register size_t hash = 0;
size_t ch;
for (long i = 0; ch = (size_t)*str++; i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ ch ^ (hash >> 3));
}
else
{
hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));
}
}
return hash;
}
size_t operator()(const K& str)
{
return APHash(str.c_str());
}
};
template <class K>
struct _HashFunc5
{
size_t JSHash(const char *str)
{
if (!*str)
return 0;
register size_t hash = 1315423911;
while (size_t ch = (size_t)*str++)
{
hash ^= ((hash << 5) + ch + (hash >> 2));
}
return hash;
}
size_t operator()(const K& str)
{
return JSHash(str.c_str());
}
};
size_t GetPrimeSize(size_t size) //求大于等于size的最小素数
{
static const int _prime = 28;
static const unsigned long _PrimeList[_prime] =
{
53ul, 97ul, 193ul, 389ul, 769ul,
1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
1610612741ul, 3221225473ul, 4294967291ul
};
for (size_t i = 0; i < _prime; i++)
{
if (_PrimeList[i] >= size)
{
return _PrimeList[i];
}
}
return _PrimeList[_prime - 1];
}
template<class K = string
, class HashFunc1 = _HashFunc1<K>
, class HashFunc2 = _HashFunc2<K>
, class HashFunc3 = _HashFunc3<K>
, class HashFunc4 = _HashFunc4<K>
, class HashFunc5 = _HashFunc5<K>>
class BloomFilter
{
public:
BloomFilter(size_t size = 0)
{
_capacity = GetPrimeSize(size);
_bm.ReSize(_capacity);
}
void Set(const K &key)
{
size_t index1 = HashFunc1()(key);
size_t index2 = HashFunc2()(key);
size_t index3 = HashFunc3()(key);
size_t index4 = HashFunc4()(key);
size_t index5 = HashFunc5()(key);
_bm.Set(index1%_capacity);
_bm.Set(index2%_capacity);
_bm.Set(index3%_capacity);
_bm.Set(index4%_capacity);
_bm.Set(index5%_capacity);
}
bool Test(const K &key)
{
size_t index1 = HashFunc1()(key);
size_t index2 = HashFunc2()(key);
size_t index3 = HashFunc3()(key);
size_t index4 = HashFunc4()(key);
size_t index5 = HashFunc5()(key);
if (!(_bm.Test(index1%_capacity)))
return false;
if (!(_bm.Test(index2%_capacity)))
return false;
if (!(_bm.Test(index3%_capacity)))
return false;
if (!(_bm.Test(index4%_capacity)))
return false;
if (!(_bm.Test(index5%_capacity)))
return false;
return true;
}
protected:
BitMap _bm;
size_t _capacity;
};
void testFloomFiler()
{
BloomFilter<> bf(50);
bf.Set("FootMart");
bf.Set("翔");
bf.Set("Happy");
bf.Set("http://10740329.blog.51cto.com/10730329/1772637#0-sqq-1-5364-9737f6f9e09dfaf5d3fd14d775bfee85");
bf.Set("http://baike.baidu.com/link?url=r_iijDxeb04ijVjBOONjFQDh6Cb0ceXRVHnt9xp1_0---j42HrYSl8ZjfZRnQ4r6eynyHj6RqVvMFUvmoupRla");
cout << " is emist? " << bf.Test("FootMart") << endl;
cout << " is emist? " << bf.Test("翔") << endl;
cout << " is emist? " << bf.Test("Happy1") << endl;
cout << " is emist? " << bf.Test("http://10740329.blog.51cto.com/10730329/1772637#0-sqq-1-5364-9737f6f9e09dfaf5d3fd14d775bfee85") << endl;
cout << " is emist? " << bf.Test("http://baike.baidu.com/link?url=r_iijDxeb04ijVjBOONjFQDh6Cb0ceXRVHnt9xp1_0---j42HrYSl8ZjfZRnQ4r6eynyHj6RqVvMFUvmoupRla2") << endl;
}
布隆过滤器应用场景举例:
(1)拼写检查、数据库系统、文件系统
(2)假设要你写一个网络蜘蛛(web crawler)。由于网络间的链接错综复杂,蜘蛛在网络间爬行很可能会形成“环”。为了避免形成“环”,就需要知道蜘蛛已经访问过那些URL。给一个URL,怎样知道蜘蛛是否已经访问过呢?
(3)网络应用
P2P网络中查找资源操作,可以对每条网络通路保存Bloom Filter,当命中时,则选择该通路访问。
广播消息时,可以检测某个IP是否已发包。
检测广播消息包的环路,将Bloom Filter保存在包里,每个节点将自己添加入Bloom Filter。
信息队列管理,使用Counter Bloom Filter管理信息流量。
(4)垃圾邮件地址过滤
像网易,QQ这样的公众电子邮件(email)提供商,总是需要过滤来自发送垃圾邮件的人(spamer)的垃圾邮件。一个办法就是记录下那些发垃圾邮件的email 地址。由于那些发送者不停地在注册新的地址,全世界少说也有几十亿个发垃圾邮件的地址,将他们都存起来则需要大量的网络服务器。如果用哈希表,每存储一亿个 email 地址,就需要1.6GB 的内存(用哈希表实现的具体办法是将每一个email 地址对应成一个八字节的信息指纹,然后将这些信息指纹存入哈希表,由于哈希表的存储效率一般只有50%,因此一个email 地址需要占用十六个字节。一亿个地址大约要1.6GB, 即十六亿字节的内存)。因此存贮几十亿个邮件地址可能需要上百GB 的内存。而Bloom Filter只需要哈希表1/8 到1/4 的大小就能解决同样的问题。Bloom Filter决不会漏掉任何一个在黑名单中的可疑地址。而至于误判问题,常见的补救办法是在建立一个小的白名单,存储那些可能别误判的邮件地址。
(5)Bloomfilter在HBase中的作用
HBase利用Bloomfilter来提高随机读(Get)的性能,对于顺序读(Scan)而言,设置Bloomfilter是没有作用的(0.92以后,如果设置了bloomfilter为ROWCOL,对于指定了qualifier的Scan有一定的优化,但不是那种直接过滤文件,排除在查找范围的形式)
转载于:https://blog.51cto.com/10739786/1772877