• C++数据结构与算法:布隆过滤器(Bloom Filter)原理与实现


    文本代码下载地址:Github:
    https://github.com/dongyusheng/csdn-code/tree/master/BloomFilter

    一、什么是布隆过滤器

    布隆过滤器(Bloom Filter)是1970年由布隆提出的

    它实际上是一个很长的二进制向量和一系列随机映射函数。布隆过滤器可以用于检索一个元素是否在一个集合中

    优点:

    • 可以高效地进行查询,可以用来告诉你“某样东西一定不存在或者可能存在”
    • 可以高效的进行插入
    • 相比于传统的List、Set、Map等数据结构,它占用空间更少,因为其本身并不存储任何数据(重点)

    缺点:

    • 其返回的结果是概率性(存在误差)的
    • 一般不提供删除操作

    布隆过滤器一般使用在数据量特别大的场景下,一般不会使用

    用的使用场景:

    • 使用word文档时,判断某个单词是否拼写正确。例如我们在编写word时,某个单词错误那么就会在单词下面显示红色波浪线
    • 网络爬虫程序,不去爬相同的url页面
    • 垃圾邮件的过滤算法
    • 缓存崩溃后造成的缓存击穿
    • 集合重复元素的判别
    • 查询加速(比如基于key-value的存储系统,如redis等)

    二、什么时候选择布隆过滤器,而不使用其他数据结构

    如果想要判断一个元素是不是在一个集合里,一般想到的是将所有元素保存起来,然后通过比较确定。链表,树、哈希表等数据结构都是这种思路(如下图所示)

     上面这些数据结构面对数据量特别大的时候显现的缺点:

    • 存储容量占比高,考虑到负载因子的存在,通常空间是不能被用满的
    • 当数据量特别大时,会占用大量的内存空间。如果存储了类似于URL这样的key,那么内存消费太严重
    • 如果使用hashmap,如果已有元素超过了总容量的一半之后,一般就需要考虑扩容了,因为元素多了之后哈希冲突就会增加,退化为链表存储的效率了

    下面是两个测试程序,分别测试hashmap和红黑树,当元素特别多时,其查询和占用的内存会非常大

    测试map(内部使用红黑树)

    1. #include <iostream>
    2. #include <map>
    3. #include <string>
    4. #include <sys/time.h>
    5. #include <utility>
    6. #include <iomanip>
    7. #define MAP_ITEMS 100000
    8. using namespace std;
    9. int main()
    10. {
    11. std::map<std::string, bool> mp;
    12. timeval startTime, endTime;
    13. //1.插入MAP_ITEMS个元素到map中
    14. gettimeofday(&startTime, NULL);
    15. std::string key = "https://blog.csdn.net/qq_41453285";
    16. for(int i = 0; i < MAP_ITEMS; ++i){
    17. string sub_key = to_string(i);
    18. mp.insert(std::make_pair(key + sub_key, 1));
    19. }
    20. gettimeofday(&endTime, NULL);
    21. long insert_time = (endTime.tv_sec - startTime.tv_sec)*1000 + (endTime.tv_usec-startTime.tv_usec)/1000;
    22. //2.在map中查找一个元素
    23. gettimeofday(&startTime, NULL);
    24. if( mp.find(key + "10000") == mp.end())
    25. std::cout << "not found!" << std::endl;
    26. gettimeofday(&endTime, NULL);
    27. long find_time = endTime.tv_usec - startTime.tv_usec;
    28. //3.估算当前key的平均大小
    29. double key_size = key.size() + to_string(MAP_ITEMS).size()/2;
    30. //4.打印相关信息
    31. std::cout << "Number of members " << "key size " << "insert time(ms) " << "find time(us) " << std::endl;
    32. std::cout << left << setw(19) << MAP_ITEMS;
    33. std::cout << left << setw(10) << key_size;
    34. std::cout << left << setw(17) << insert_time;
    35. std::cout << left << setw(15) << find_time << std::endl;
    36. }

    代码中的MAP_ITEMS常量代表当前map中存储的元素的个数

    当MAP_ITEMS为100000时,结果如下:

     当MAP_ITEMS为1000000时,结果如下:

     当MAP_ITEMS为10000000时,结果如下:

     相关视频推荐

    redis、布隆过滤器、分布式一致性hash中hash的妙用

    5种红黑树的用途,从应用到内核场景的优缺点

    学习地址:c/c++ linux服务器开发/后台架构师

    需要C/C++ Linux服务器架构师学习资料加qun812855908获取(资料包括C/C++,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等),免费分享

     测试unordered_map(内部使用hashmap)

    1. #include <iostream>
    2. #include <unordered_map>
    3. #include <string>
    4. #include <sys/time.h>
    5. #include <utility>
    6. #include <iomanip>
    7. #define MAP_ITEMS 100000
    8. using namespace std;
    9. int main()
    10. {
    11. unordered_map<string, bool> unordermp;
    12. timeval startTime, endTime;
    13. //1.插入MAP_ITEMS个元素到map中
    14. gettimeofday(&startTime, NULL);
    15. std::string key = "https://blog.csdn.net/qq_41453285";
    16. for(int i = 0; i < MAP_ITEMS; ++i){
    17. string sub_key = to_string(i);
    18. unordermp.insert(std::make_pair(key + sub_key, 1));
    19. }
    20. gettimeofday(&endTime, NULL);
    21. long insert_time = (endTime.tv_sec - startTime.tv_sec)*1000 + (endTime.tv_usec-startTime.tv_usec)/1000;
    22. //2.在map中查找一个元素
    23. gettimeofday(&startTime, NULL);
    24. if( unordermp.find(key + "10000") == unordermp.end())
    25. std::cout << "not found!" << std::endl;
    26. gettimeofday(&endTime, NULL);
    27. long find_time = endTime.tv_usec - startTime.tv_usec;
    28. //3.估算当前key的平均大小
    29. double key_size = key.size() + to_string(MAP_ITEMS).size()/2;
    30. //4.打印相关信息
    31. std::cout << "Number of members " << "key size " << "insert time(ms) " << "find time(us) " << std::endl;
    32. std::cout << left << setw(19) << MAP_ITEMS;
    33. std::cout << left << setw(10) << key_size;
    34. std::cout << left << setw(17) << insert_time;
    35. std::cout << left << setw(15) << find_time << std::endl;
    36. }

    代码中的MAP_ITEMS常量代表当前unordered_map中存储的元素的个数

    当MAP_ITEMS为100000时,结果如下:

     当MAP_ITEMS为1000000时,结果如下:

     当MAP_ITEMS为10000000时,结果如下:

     

    三、布隆过滤器的数据结构与实现原理

    数据结构

    布隆过滤器是一个bit向量或者说是一个bit数组(下面的数字为索引)。如下所示:

     其最小单位为bit,初始化时全部置为0

    添加、查询原理

    布隆过滤器添加原理:利用K个Hash函数,将元素传入到这K个Hash函数中,并且映射到bit向量的K个点中,并且将映射到的K个点置为1

    布隆过滤器查询原理:

    • 利用K个Hash函数,将元素传入到这K个Hash函数中,并且映射到bit向量的K个点中
    • 如果这些点中有任何一个为0,则被检测的元素一定不存在
    • 如果这些点都返回1,则被检测的元素很可能(因为布隆过滤器存在误差)存在,但是不一定百分百存在

    上面添加、查询使用的Hash函数一般都是相同的,实现设计好的

    为什么布隆过滤器要使用多个Hash函数?

    • Hash面临的问题就是冲突。假设Hash函数是良好的,如果我们的位阵列长度为m个点,那么如果我们想将冲突率降低到例如 1%,这个散列表就只能容纳 m/100个元素
    • 解决方法较简单,使用K>1的布隆过滤器,即K个函数将每个元素改为对应于K个bits,因为误判度会降低很多,并且如果参数k和m选取得好,一半的m可被置为1

    一个重要的概念:针对于一个特定的哈希函数和一个特定的值,那么该哈希函数返回的值每次都是固定的,不可能出现多次调用之间出现哈希函数返回值不同的情况

    演示说明

    假设我们的布隆过滤器有三个哈希函数,分别名为hash1、hash2、hash3

    ①添加元素:针对于“baidu”这个元素,我们调用三个哈希函数,将其映射到bit向量的三个位置(分别为1、4、7),并且将对应的位置置为1

     ②添加元素:现在针对于“tencent”这个元素,我们也调用三个哈希函数,将其映射到bit向量的三个位置(分别为3、4、8),并且将对应的位置置为1

     ③此时,整个bit向量的1、3、4、7、8这几个位置被置为1了。其中4这个索引被覆盖了,因为“baidu”和“tencent”都将其置为1,覆盖的索引与误判率有关,详情见下面的介绍

    ④去查询一个不存在的元素,并且确定其肯定不存在:例如现在我们去查询“dongshao”这个元素,假设调用上面的三个哈希函数返回的索引是1、5、8,通过上图我们知道5这个索引处为0,因此“dongshao”这个元素一定不存在,因为如果存在的话,那么5这个位置应该被置为1才对(见上面的“一个重要概念”)

    ⑤去查询“baidu”这个元素,不能判断其百分百存在:我们将“baidu”传入上面的三个哈希函数中,哈希返回的对应索引值为1、4、7,发现1、4、7这几个索引处都为1,因此我们判断“baidu”这个元素可能存在。为什么不是百分百确定呢?见下面的误判率介绍

    误判率

    布隆过滤器允许存在一定的误判断,误判率也称为“假阳”

    误判率一般是出现在查询的时候

    例如上面我们去查询“baidu”的时候,由于“baidu”之前被我们插入过,为什么还不能百分百确定它一定存在呢?

    • 因为“tencent”这个元素在插入的时候,将4这个索引置为1了
    • 假设我们查询“baidu”的时候实际返回的是1、7索引为1,4索引为0。而4索引又被tencent覆盖为1,所以最终“baidu”最终看到的是1、4、7索引都为1,我们不能百分百确定“baidu”这个元素存在

    因此,当随着增加的值越来越多时,bit向量被置为1的数量也就会越来越多,因此误判率会越来越大。例如,当查询“taobao”时,万一所有的哈希函数返回的对应bit都为1,那么布隆过滤器可能也认为“taobao”这个元素存在

    布隆过滤器一般不拥有删除的功能

    我们一般不能从布隆过滤器中删除元素。考虑下面几种情况:

    • 因为要删除该元素,我们必须百分百确保该元素存在于布隆过滤器中,而布隆过滤器由于存在误判率,无法确定该元素百分百存在于布隆过滤器内
    • 另外计数器回绕也会造成问题
    • 如果我们因为某一个元素而将其对应的bit位删除变为0,那么如果这些bit位也是其他元素正在使用的,那么其他元素在查询时就会返回0,从而认为元素不存在而造成误判

    四、误判概率的相关证明和计算

    证明①(哈希函数越多、插入元素越少,误判率越低)

    假设布隆过滤器中的hash函数满足simple uniform hashing(简单一致散列)假设:每个元素都等概率地hash到m个slot中的任何一个,与其它元素被hash到哪个slot无关

    若m为bit数(向量表的长度), 则对某一特定bit位在一个元素由某特定hash函数插入时没有被置位为1的概率为:

     则k个hash函数中没有一个对其置位的概率为,随着k的增加,概率会变小:

     如果插入了n个元素,但都没有将其置位的概率为:

     现在考虑查询阶段,若对应某个待查询元素的k bits全部置位为1,则可判定其在集合中。 因此将某元素误判的概率p为:

     现在考虑查询阶段,若对应某个待query元素的k bits全部置位为1,则可判定其在集合中。 因此将某元素误判的概率p为:

     由于

     当x→0时,并且

     当m很大时趋近于0,所以:

     从上式中可以看出,当m增大或n减小时,都会使得误判率减小

    证明②(何时误判率最低?)

    现在计算对于给定的m和n,k为何值时可以使得误判率最低。设误判率为k的函数为:

     下面求最值,即是误差趋近于0

     因此,即当

     时误判率最低,此时误判率为:

     可以看出若要使得误判率≤1/2,则:

     这说明了若想保持某固定误判率不变,布隆过滤器的bit数m与被增加的元素数n应该是线性同步增加的

    五、Hash函数的选择

    常见的应用比较广的hash函数有MD5, SHA1, SHA256,一般用于信息安全方面,比如签名认证和加密等。比如我们传输文件时习惯用对原文件内容计算它的MD5值,生成128 bit的整数,通 常我们说的32位MD5值,是转换为HEX格式后的32个字符

    MurmurHash:

    • MurmurHash是2008年发明的,相比较MD5, MurMurhash不太安全(当然MD5也被破译了, sha也可以被破译),但是性能是MD5的几十倍
    • MurmurHash有很多个版本, MurmurHash3修复了MurmurHash2的一些缺陷同时速度还要快一些,因此很多开源项目有用,比如nginx、 redis、 memcashed、 Hadoop等,比如用于计算一致性hash等
    • MurmurHash被比较好的测试过了,测试方法见https://github.com/aappleby/smhasher
    • MurMurhash的实现也可以参考smhasher,或者参考https://sites.google.com/site/murmurhash
    • 我们演示的布隆过滤器中的hash函数选择MurmurHash2算法

    补充:双重散列

    双重散列是线性开型寻址散列(开放寻址法)中的冲突解决技术。双重散列使用在发生冲突时将第二个散列函数应用于键的想法

    此算法使用下面的公式来进行双哈希处理。hash1() 和 hash2() 是哈希函数,而 TABLE_SIZE 是哈希表的大小。 当发生碰撞时,我们通过重复增加步长i 来寻找键

     

    六、布隆过滤器的实现

    布隆过滤器在实现时一般设计考虑下面几样东西:

    • n:布隆过滤器最大处理的元素的个数
    • P:希望的误差率
    • m:布隆过滤器的bit位数目
    • k:哈希函数的个数

    应用时首先要先由用户决定要增加的最多元素个数n和希望的误差率P。这也是一个设计完整的布隆过滤器需要用户输入的仅有的两个参数(加入hash种子则为3个),之后的所有参数将由系统计算,并由此建立布隆过滤器

    ①首先根据传入的n和p计算需要的内存大小m bits:

     ②再由m,n得到hash function的个数:

     至此系统所需的参数已经备齐,后面就可以添加n个元素到布隆过滤器中,进行查询

    布隆过滤器空间利用率问题

    根据公式,当k最优时:

     因此可验证当P=1%时,存储每个元素需要9.6 bits:

     而每当想将误判率降低为原来的1/10,则存储每个元素需要增加4.8 bits:

    布隆过滤器误判率对比表

    如果方便知道需要使用多少位才能降低错误概率,可以从下表所示的存储项目和位数 比率估计布隆过滤器的误判率

     为每个URL分配两个字节就可以达到千分之几的冲突。比较保守的实现是,为每个URL 分配4个字节,项目和位数比是1∶32,误判率是0.00000021167340。对于5000万数量级的URL,布隆过滤器只占用200MB的空间

    七、在线验证公式

    测试网址:
    https://hur.st/bloomfilter/

    下面是一个测试网址,可以根据你输入的数值返回对应的数据:

    • n:布隆过滤器最大处理的元素的个数
    • P:希望的误差率
    • m:布隆过滤器的bit位数目
    • k:哈希函数的个数

     

    八、编码实现

    bloomfilter.h

    这个代码是布隆过滤器的实现代码

    1. #ifndef __MICRO_BLOOMFILTER_H__
    2. #define __MICRO_BLOOMFILTER_H__
    3. /**
    4. *
    5. * 仿照Cassandra中的BloomFilter实现,Hash选用MurmurHash2,通过双重散列公式生成散列函数,参考:http://hur.st/bloomfilter
    6. * Hash(key, i) = (H1(key) + i * H2(key)) % m
    7. *
    8. **/
    9. #include <stdio.h>
    10. #include <stdlib.h>
    11. #include <stdint.h>
    12. #include <string.h>
    13. #include <math.h>
    14. #define __BLOOMFILTER_VERSION__ "1.1"
    15. #define __MGAIC_CODE__ (0x01464C42)
    16. /**
    17. * BloomFilter使用例子:
    18. * static BaseBloomFilter stBloomFilter = {0};
    19. *
    20. * 初始化BloomFilter(最大100000元素,不超过0.00001的错误率):
    21. * InitBloomFilter(&stBloomFilter, 0, 100000, 0.00001);
    22. * 重置BloomFilter:
    23. * ResetBloomFilter(&stBloomFilter);
    24. * 释放BloomFilter:
    25. * FreeBloomFilter(&stBloomFilter);
    26. *
    27. * 向BloomFilter中新增一个数值(0-正常,1-加入数值过多):
    28. * uint32_t dwValue;
    29. * iRet = BloomFilter_Add(&stBloomFilter, &dwValue, sizeof(uint32_t));
    30. * 检查数值是否在BloomFilter内(0-存在,1-不存在):
    31. * iRet = BloomFilter_Check(&stBloomFilter, &dwValue, sizeof(uint32_t));
    32. *
    33. * (1.1新增) 将生成好的BloomFilter写入文件:
    34. * iRet = SaveBloomFilterToFile(&stBloomFilter, "dump.bin")
    35. * (1.1新增) 从文件读取生成好的BloomFilter:
    36. * iRet = LoadBloomFilterFromFile(&stBloomFilter, "dump.bin")
    37. **/
    38. // 注意,要让Add/Check函数内联,必须使用 -O2 或以上的优化等级
    39. #define FORCE_INLINE __attribute__((always_inline))
    40. #define BYTE_BITS (8)
    41. #define MIX_UINT64(v) ((uint32_t)((v>>32)^(v)))
    42. #define SETBIT(filter, n) (filter->pstFilter[n/BYTE_BITS] |= (1 << (n%BYTE_BITS)))
    43. #define GETBIT(filter, n) (filter->pstFilter[n/BYTE_BITS] & (1 << (n%BYTE_BITS)))
    44. #pragma pack(1)
    45. // BloomFilter结构定义
    46. typedef struct
    47. {
    48. uint8_t cInitFlag; // 初始化标志,为0时的第一次Add()会对stFilter[]做初始化
    49. uint8_t cResv[3];
    50. uint32_t dwMaxItems; // n - BloomFilter中最大元素个数 (输入量)
    51. double dProbFalse; // p - 假阳概率(误判率) (输入量,比如万分之一:0.00001)
    52. uint32_t dwFilterBits; // m = ; - BloomFilter的比特数
    53. uint32_t dwHashFuncs; // k = round(log(2.0) * m / n); - 哈希函数个数
    54. uint32_t dwSeed; // MurmurHash的种子偏移量
    55. uint32_t dwCount; // Add()的计数,超过MAX_BLOOMFILTER_N则返回失败
    56. uint32_t dwFilterSize; // dwFilterBits / BYTE_BITS
    57. unsigned char *pstFilter; // BloomFilter存储指针,使用malloc分配
    58. uint32_t *pdwHashPos; // 存储上次hash得到的K个bit位置数组(由bloom_hash填充)
    59. } BaseBloomFilter;
    60. // BloomFilter文件头部定义
    61. typedef struct
    62. {
    63. uint32_t dwMagicCode; // 文件头部标识,填充 __MGAIC_CODE__
    64. uint32_t dwSeed;
    65. uint32_t dwCount;
    66. uint32_t dwMaxItems; // n - BloomFilter中最大元素个数 (输入量)
    67. double dProbFalse; // p - 假阳概率 (输入量,比如万分之一:0.00001)
    68. uint32_t dwFilterBits; // m = ceil((n * log(p)) / log(1.0 / (pow(2.0, log(2.0))))); - BloomFilter的比特数
    69. uint32_t dwHashFuncs; // k = round(log(2.0) * m / n); - 哈希函数个数
    70. uint32_t dwResv[6];
    71. uint32_t dwFileCrc; // (未使用)整个文件的校验和
    72. uint32_t dwFilterSize; // 后面Filter的Buffer长度
    73. } BloomFileHead;
    74. #pragma pack()
    75. // 计算BloomFilter的参数m,k
    76. static inline void _CalcBloomFilterParam(uint32_t n, double p, uint32_t *pm, uint32_t *pk)
    77. {
    78. /**
    79. * n - Number of items in the filter
    80. * p - Probability of false positives, float between 0 and 1 or a number indicating 1-in-p
    81. * m - Number of bits in the filter
    82. * k - Number of hash functions
    83. *
    84. * f = ln(2) × ln(1/2) × m / n = (0.6185) ^ (m/n)
    85. * m = -1 * ln(p) × n / 0.6185 , 这里有错误
    86. * k = ln(2) × m / n = 0.6931 * m / n
    87. * darren修正:
    88. * m = -1*n*ln(p)/((ln(2))^2) = -1*n*ln(p)/(ln(2)*ln(2)) = -1*n*ln(p)/(0.69314718055995*0.69314718055995))
    89. * = -1*n*ln(p)/0.4804530139182079271955440025
    90. * k = ln(2)*m/n
    91. **/
    92. uint32_t m, k, m2;
    93. // printf("ln(2):%lf, ln(p):%lf\n", log(2), log(p)); // 用来验证函数正确性
    94. // 计算指定假阳(误差)概率下需要的比特数
    95. m =(uint32_t) ceil(-1.0 * n * log(p) / 0.480453); //darren 修正
    96. //m2 =(uint32_t) ceil(-1 * n * log(p) / 0.480453); //错误写法
    97. m = (m - m % 64) + 64; // 8字节对齐
    98. // 计算哈希函数个数
    99. double double_k = (0.69314 * m / n); // ln(2)*m/n // 这里只是为了debug出来看看具体的浮点数值
    100. k = round(double_k); // 返回x的四舍五入整数值。
    101. printf("orig_k:%lf, k:%u\n", double_k, k);
    102. *pm = m;
    103. *pk = k;
    104. return;
    105. }
    106. // 根据目标精度和数据个数,初始化BloomFilter结构
    107. /**
    108. * @brief 初始化布隆过滤器
    109. * @param pstBloomfilter 布隆过滤器实例
    110. * @param dwSeed hash种子
    111. * @param dwMaxItems 存储容量
    112. * @param dProbFalse 允许的误判率
    113. * @return 返回值
    114. * -1 传入的布隆过滤器为空
    115. * -2 hash种子错误或误差>=1
    116. */
    117. inline int InitBloomFilter(BaseBloomFilter *pstBloomfilter,
    118. uint32_t dwSeed,
    119. uint32_t dwMaxItems, double dProbFalse)
    120. {
    121. if (pstBloomfilter == NULL)
    122. return -1;
    123. if ((dProbFalse <= 0) || (dProbFalse >= 1))
    124. return -2;
    125. // 先检查是否重复Init,释放内存
    126. if (pstBloomfilter->pstFilter != NULL)
    127. free(pstBloomfilter->pstFilter);
    128. if (pstBloomfilter->pdwHashPos != NULL)
    129. free(pstBloomfilter->pdwHashPos);
    130. memset(pstBloomfilter, 0, sizeof(BaseBloomFilter));
    131. // 初始化内存结构,并计算BloomFilter需要的空间
    132. pstBloomfilter->dwMaxItems = dwMaxItems; // 最大存储
    133. pstBloomfilter->dProbFalse = dProbFalse; // 误差
    134. pstBloomfilter->dwSeed = dwSeed; // hash种子
    135. // 计算 m, k
    136. _CalcBloomFilterParam(pstBloomfilter->dwMaxItems, pstBloomfilter->dProbFalse,
    137. &pstBloomfilter->dwFilterBits, &pstBloomfilter->dwHashFuncs);
    138. // 分配BloomFilter的存储空间
    139. pstBloomfilter->dwFilterSize = pstBloomfilter->dwFilterBits / BYTE_BITS;
    140. pstBloomfilter->pstFilter = (unsigned char *) malloc(pstBloomfilter->dwFilterSize);
    141. if (NULL == pstBloomfilter->pstFilter)
    142. return -100;
    143. // 哈希结果数组,每个哈希函数一个
    144. pstBloomfilter->pdwHashPos = (uint32_t*) malloc(pstBloomfilter->dwHashFuncs * sizeof(uint32_t));
    145. if (NULL == pstBloomfilter->pdwHashPos)
    146. return -200;
    147. printf(">>> Init BloomFilter(n=%u, p=%e, m=%u, k=%d), malloc() size=%.6fMB, items:bits=1:%0.1lf\n",
    148. pstBloomfilter->dwMaxItems, pstBloomfilter->dProbFalse, pstBloomfilter->dwFilterBits,
    149. pstBloomfilter->dwHashFuncs, (double)pstBloomfilter->dwFilterSize/1024/1024,
    150. pstBloomfilter->dwFilterBits*1.0/pstBloomfilter->dwMaxItems);
    151. // 初始化BloomFilter的内存
    152. memset(pstBloomfilter->pstFilter, 0, pstBloomfilter->dwFilterSize);
    153. pstBloomfilter->cInitFlag = 1;
    154. return 0;
    155. }
    156. // 释放BloomFilter
    157. inline int FreeBloomFilter(BaseBloomFilter *pstBloomfilter)
    158. {
    159. if (pstBloomfilter == NULL)
    160. return -1;
    161. pstBloomfilter->cInitFlag = 0;
    162. pstBloomfilter->dwCount = 0;
    163. free(pstBloomfilter->pstFilter);
    164. pstBloomfilter->pstFilter = NULL;
    165. free(pstBloomfilter->pdwHashPos);
    166. pstBloomfilter->pdwHashPos = NULL;
    167. return 0;
    168. }
    169. // 重置BloomFilter
    170. // 注意: Reset()函数不会立即初始化stFilter,而是当一次Add()时去memset
    171. inline int ResetBloomFilter(BaseBloomFilter *pstBloomfilter)
    172. {
    173. if (pstBloomfilter == NULL)
    174. return -1;
    175. pstBloomfilter->cInitFlag = 0;
    176. pstBloomfilter->dwCount = 0;
    177. return 0;
    178. }
    179. // 和ResetBloomFilter不同,调用后立即memset内存
    180. inline int RealResetBloomFilter(BaseBloomFilter *pstBloomfilter)
    181. {
    182. if (pstBloomfilter == NULL)
    183. return -1;
    184. memset(pstBloomfilter->pstFilter, 0, pstBloomfilter->dwFilterSize);
    185. pstBloomfilter->cInitFlag = 1;
    186. pstBloomfilter->dwCount = 0;
    187. return 0;
    188. }
    189. ///
    190. /// 函数FORCE_INLINE,加速执行
    191. ///
    192. // MurmurHash2, 64-bit versions, by Austin Appleby
    193. // https://sites.google.com/site/murmurhash/
    194. FORCE_INLINE uint64_t MurmurHash2_x64 ( const void * key, int len, uint32_t seed )
    195. {
    196. const uint64_t m = 0xc6a4a7935bd1e995;
    197. const int r = 47;
    198. uint64_t h = seed ^ (len * m);
    199. const uint64_t * data = (const uint64_t *)key;
    200. const uint64_t * end = data + (len/8);
    201. while(data != end)
    202. {
    203. uint64_t k = *data++;
    204. k *= m;
    205. k ^= k >> r;
    206. k *= m;
    207. h ^= k;
    208. h *= m;
    209. }
    210. const uint8_t * data2 = (const uint8_t*)data;
    211. switch(len & 7)
    212. {
    213. case 7: h ^= ((uint64_t)data2[6]) << 48;
    214. case 6: h ^= ((uint64_t)data2[5]) << 40;
    215. case 5: h ^= ((uint64_t)data2[4]) << 32;
    216. case 4: h ^= ((uint64_t)data2[3]) << 24;
    217. case 3: h ^= ((uint64_t)data2[2]) << 16;
    218. case 2: h ^= ((uint64_t)data2[1]) << 8;
    219. case 1: h ^= ((uint64_t)data2[0]);
    220. h *= m;
    221. };
    222. h ^= h >> r;
    223. h *= m;
    224. h ^= h >> r;
    225. return h;
    226. }
    227. // 双重散列封装,k个函数函数, 比如要20
    228. FORCE_INLINE void bloom_hash(BaseBloomFilter *pstBloomfilter, const void * key, int len)
    229. {
    230. //if (pstBloomfilter == NULL) return;
    231. int i;
    232. uint32_t dwFilterBits = pstBloomfilter->dwFilterBits;
    233. uint64_t hash1 = MurmurHash2_x64(key, len, pstBloomfilter->dwSeed);
    234. uint64_t hash2 = MurmurHash2_x64(key, len, MIX_UINT64(hash1));
    235. for (i = 0; i < (int)pstBloomfilter->dwHashFuncs; i++)
    236. {
    237. // k0 = (hash1 + 0*hash2) % dwFilterBits; // dwFilterBits bit向量的长度
    238. // k1 = (hash1 + 1*hash2) % dwFilterBits;
    239. pstBloomfilter->pdwHashPos[i] = (hash1 + i*hash2) % dwFilterBits;
    240. }
    241. return;
    242. }
    243. // 向BloomFilter中新增一个元素
    244. // 成功返回0,当添加数据超过限制值时返回1提示用户
    245. FORCE_INLINE int BloomFilter_Add(BaseBloomFilter *pstBloomfilter, const void * key, int len)
    246. {
    247. if ((pstBloomfilter == NULL) || (key == NULL) || (len <= 0))
    248. return -1;
    249. int i;
    250. if (pstBloomfilter->cInitFlag != 1)
    251. {
    252. // Reset后没有初始化,使用前需要memset
    253. memset(pstBloomfilter->pstFilter, 0, pstBloomfilter->dwFilterSize);
    254. pstBloomfilter->cInitFlag = 1;
    255. }
    256. // hash key到bloomfilter中, 为了计算不同hash命中的位置,保存pdwHashPos数组
    257. bloom_hash(pstBloomfilter, key, len);
    258. for (i = 0; i < (int)pstBloomfilter->dwHashFuncs; i++)
    259. {
    260. // dwHashFuncs[0] = hash0(key)
    261. // dwHashFuncs[1] = hash1(key)
    262. // dwHashFuncs[k-1] = hashk-1(key)
    263. SETBIT(pstBloomfilter, pstBloomfilter->pdwHashPos[i]);
    264. }
    265. // 增加count
    266. pstBloomfilter->dwCount++;
    267. if (pstBloomfilter->dwCount <= pstBloomfilter->dwMaxItems)
    268. return 0;
    269. else
    270. return 1; // 超过N最大值,可能出现准确率下降等情况
    271. }
    272. // 检查一个元素是否在bloomfilter中
    273. // 返回:0-存在,1-不存在,负数表示失败
    274. FORCE_INLINE int BloomFilter_Check(BaseBloomFilter *pstBloomfilter, const void * key, int len)
    275. {
    276. if ((pstBloomfilter == NULL) || (key == NULL) || (len <= 0))
    277. return -1;
    278. int i;
    279. bloom_hash(pstBloomfilter, key, len);
    280. for (i = 0; i < (int)pstBloomfilter->dwHashFuncs; i++)
    281. {
    282. // 如果有任意bit不为1,说明key不在bloomfilter中
    283. // 注意: GETBIT()返回不是0|1,高位可能出现128之类的情况
    284. if (GETBIT(pstBloomfilter, pstBloomfilter->pdwHashPos[i]) == 0)
    285. return 1;
    286. }
    287. return 0;
    288. }
    289. /* 文件相关封装 */
    290. // 将生成好的BloomFilter写入文件
    291. inline int SaveBloomFilterToFile(BaseBloomFilter *pstBloomfilter, char *szFileName)
    292. {
    293. if ((pstBloomfilter == NULL) || (szFileName == NULL))
    294. return -1;
    295. int iRet;
    296. FILE *pFile;
    297. static BloomFileHead stFileHeader = {0};
    298. pFile = fopen(szFileName, "wb");
    299. if (pFile == NULL)
    300. {
    301. perror("fopen");
    302. return -11;
    303. }
    304. // 先写入文件头
    305. stFileHeader.dwMagicCode = __MGAIC_CODE__;
    306. stFileHeader.dwSeed = pstBloomfilter->dwSeed;
    307. stFileHeader.dwCount = pstBloomfilter->dwCount;
    308. stFileHeader.dwMaxItems = pstBloomfilter->dwMaxItems;
    309. stFileHeader.dProbFalse = pstBloomfilter->dProbFalse;
    310. stFileHeader.dwFilterBits = pstBloomfilter->dwFilterBits;
    311. stFileHeader.dwHashFuncs = pstBloomfilter->dwHashFuncs;
    312. stFileHeader.dwFilterSize = pstBloomfilter->dwFilterSize;
    313. iRet = fwrite((const void*)&stFileHeader, sizeof(stFileHeader), 1, pFile);
    314. if (iRet != 1)
    315. {
    316. perror("fwrite(head)");
    317. return -21;
    318. }
    319. // 接着写入BloomFilter的内容
    320. iRet = fwrite(pstBloomfilter->pstFilter, 1, pstBloomfilter->dwFilterSize, pFile);
    321. if ((uint32_t)iRet != pstBloomfilter->dwFilterSize)
    322. {
    323. perror("fwrite(data)");
    324. return -31;
    325. }
    326. fclose(pFile);
    327. return 0;
    328. }
    329. // 从文件读取生成好的BloomFilter
    330. inline int LoadBloomFilterFromFile(BaseBloomFilter *pstBloomfilter, char *szFileName)
    331. {
    332. if ((pstBloomfilter == NULL) || (szFileName == NULL))
    333. return -1;
    334. int iRet;
    335. FILE *pFile;
    336. static BloomFileHead stFileHeader = {0};
    337. if (pstBloomfilter->pstFilter != NULL)
    338. free(pstBloomfilter->pstFilter);
    339. if (pstBloomfilter->pdwHashPos != NULL)
    340. free(pstBloomfilter->pdwHashPos);
    341. //
    342. pFile = fopen(szFileName, "rb");
    343. if (pFile == NULL)
    344. {
    345. perror("fopen");
    346. return -11;
    347. }
    348. // 读取并检查文件头
    349. iRet = fread((void*)&stFileHeader, sizeof(stFileHeader), 1, pFile);
    350. if (iRet != 1)
    351. {
    352. perror("fread(head)");
    353. return -21;
    354. }
    355. if ((stFileHeader.dwMagicCode != __MGAIC_CODE__)
    356. || (stFileHeader.dwFilterBits != stFileHeader.dwFilterSize*BYTE_BITS))
    357. return -50;
    358. // 初始化传入的 BaseBloomFilter 结构
    359. pstBloomfilter->dwMaxItems = stFileHeader.dwMaxItems;
    360. pstBloomfilter->dProbFalse = stFileHeader.dProbFalse;
    361. pstBloomfilter->dwFilterBits = stFileHeader.dwFilterBits;
    362. pstBloomfilter->dwHashFuncs = stFileHeader.dwHashFuncs;
    363. pstBloomfilter->dwSeed = stFileHeader.dwSeed;
    364. pstBloomfilter->dwCount = stFileHeader.dwCount;
    365. pstBloomfilter->dwFilterSize = stFileHeader.dwFilterSize;
    366. pstBloomfilter->pstFilter = (unsigned char *) malloc(pstBloomfilter->dwFilterSize);
    367. if (NULL == pstBloomfilter->pstFilter)
    368. return -100;
    369. pstBloomfilter->pdwHashPos = (uint32_t*) malloc(pstBloomfilter->dwHashFuncs * sizeof(uint32_t));
    370. if (NULL == pstBloomfilter->pdwHashPos)
    371. return -200;
    372. // 将后面的Data部分读入 pstFilter
    373. iRet = fread((void*)(pstBloomfilter->pstFilter), 1, pstBloomfilter->dwFilterSize, pFile);
    374. if ((uint32_t)iRet != pstBloomfilter->dwFilterSize)
    375. {
    376. perror("fread(data)");
    377. return -31;
    378. }
    379. pstBloomfilter->cInitFlag = 1;
    380. printf(">>> Load BloomFilter(n=%u, p=%f, m=%u, k=%d), malloc() size=%.2fMB\n",
    381. pstBloomfilter->dwMaxItems, pstBloomfilter->dProbFalse, pstBloomfilter->dwFilterBits,
    382. pstBloomfilter->dwHashFuncs, (double)pstBloomfilter->dwFilterSize/1024/1024);
    383. fclose(pFile);
    384. return 0;
    385. }
    386. #endif

    bloomfilter.cpp

    这个是布隆过滤器的测试代码

    1. #include "bloomfilter.h"
    2. #include <stdio.h>
    3. #define MAX_ITEMS 6000000 // 设置最大元素个数
    4. #define ADD_ITEMS 1000 // 添加测试元素
    5. #define P_ERROR 0.0001// 设置误差
    6. //
    7. int main(int argc, char** argv)
    8. {
    9. printf(" test bloomfilter\n");
    10. // 1. 定义BaseBloomFilter
    11. static BaseBloomFilter stBloomFilter = {0};
    12. // 2. 初始化stBloomFilter,调用时传入hash种子,存储容量,以及允许的误判率
    13. InitBloomFilter(&stBloomFilter, 0, MAX_ITEMS, P_ERROR);
    14. // 3. 向BloomFilter中新增数值
    15. char url[128] = {0};
    16. for(int i = 0; i < ADD_ITEMS; i++){
    17. sprintf(url, "https://blog.csdn.net/qq_41453285/%d.html", i);
    18. if(0 == BloomFilter_Add(&stBloomFilter, (const void*)url, strlen(url))){
    19. // printf("add %s success", url);
    20. }else{
    21. printf("add %s failed", url);
    22. }
    23. memset(url, 0, sizeof(url));
    24. }
    25. // 4. check url exist or not
    26. char* str = "https://blog.csdn.net/qq_41453285/0.html";
    27. if (0 == BloomFilter_Check(&stBloomFilter, (const void*)str, strlen(str)) ){
    28. printf("https://blog.csdn.net/qq_41453285/0.html exist\n");
    29. }
    30. char* str2 = "https://blog.csdn.net/qq_41453285/10001.html";
    31. if (0 != BloomFilter_Check(&stBloomFilter, (const void*)str2, strlen(str2)) ){
    32. printf("https://blog.csdn.net/qq_41453285/10001.html not exist\n");
    33. }
    34. // 5. free bloomfilter
    35. FreeBloomFilter(&stBloomFilter);
    36. getchar();
    37. return 0;
    38. }

    结果图下图所示:

    • n:布隆过滤器最大处理的元素的个数
    • P:希望的误差率
    • m:布隆过滤器的bit位数目
    • k:哈希函数的个数

     

  • 相关阅读:
    hm商城微服务远程调用及拆分
    代码随想录day38 || 动态规划理论基础 || 斐波那契数 || 爬楼梯 || 最小花费爬楼梯
    【C++练级之路】【Lv.26】类型转换
    HTML5的结构
    人脸识别4G执法记录仪、一体化智能AI布控球在智慧社区、智能网格中的应用
    Spring Cloud的魔法世界
    3年轻量:腾讯云轻量2核2G4M应用服务器366三年!
    UE4引擎支持HTML5
    5G三大应用场景: eMBB、uRLLC和mMTC
    第五章 误差反向传播法——计算图&链式法则&反向传播&简单层的实现&激活函数层的实现&Affine/Softmax层的实现&误差反向传播法的实现
  • 原文地址:https://blog.csdn.net/qq_40989769/article/details/126781815