{"id":204812,"date":"2020-11-18T10:17:58","date_gmt":"2020-11-18T02:17:58","guid":{"rendered":"https:\/\/gulass.cn\/?p=204812"},"modified":"2020-11-05T16:18:50","modified_gmt":"2020-11-05T08:18:50","slug":"what-bloom-filter","status":"publish","type":"post","link":"https:\/\/gulass.cn\/what-bloom-filter.html","title":{"rendered":"\u5565\u4e8b\u5e03\u9686\u8fc7\u6ee4\u5668\uff1f\u4e3b\u8981\u5e72\u561b\u7528\u7684\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5e03\u9686\u8fc7\u6ee4\u5668\u4e3b\u8981\u662f\u5728redis\u4e2d\u95ee\u7684\u6bd4\u8f83\u591a\uff0c\u56e0\u6b64\u50cf\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u7c7b\u7684\uff0c\u4e3b\u8981\u662f\u8003\u539f\u7406\u4ee5\u53ca\u4f7f\u7528\u573a\u666f\u3002\u4e0b\u9762\u4e00\u70b9\u4e00\u70b9\u5f00\u59cb\u9010\u6b65\u4ecb\u7ecd\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u8fd9\u4e00\u7bc7\u662f\u6211\u91cd\u5199\u7684\uff0c\u4e4b\u524d\u5199\u8fc7\u4e00\u7bc7\u53d1\u73b0\u9762\u8bd5\u7684\u65f6\u5019\u95ee\u7684\u95ee\u9898\u867d\u7136\u5927\u6982\u80fd\u89e3\u51b3\uff0c\u4f46\u662f\u6709\u51e0\u4e2a\u70b9\u6ca1\u6709\u6574\u7406\u5230\u4f4d\uff0c\u6240\u4ee5\u81ea\u5df1\u7ed9\u81ea\u5df1\u5217\u51fa\u4e86\u5f88\u591a\u9762\u8bd5\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u51c6\u5907\u4e00\u7bc7\u4e00\u7bc7\u53bb\u89e3\u51b3\u3002\u672c\u6587\u6574\u4f53\u601d\u8def\u662f\u5ef6\u7eed\u4e4b\u524d\u7684\u90a3\u7bc7\u6587\u7ae0\uff0c\u5728\u6b64\u57fa\u7840\u4e4b\u4e0a\u6dfb\u52a0\u4e86\u51e0\u4e2a\u70b9\u800c\u5df2\u3002<\/p>\n

\u5e03\u9686\u8fc7\u6ee4\u5668\u4e3b\u8981\u662f\u5728redis\u4e2d\u95ee\u7684\u6bd4\u8f83\u591a\uff0c\u56e0\u6b64\u50cf\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u7c7b\u7684\uff0c\u4e3b\u8981\u662f\u8003\u539f\u7406\u4ee5\u53ca\u4f7f\u7528\u573a\u666f\u3002\u4e0b\u9762\u4e00\u70b9\u4e00\u70b9\u5f00\u59cb\u9010\u6b65\u4ecb\u7ecd\u3002<\/p>\n

\u4e00\u3001\u8ba4\u8bc6\u5e03\u9686\u8fc7\u6ee4\u5668<\/strong><\/div>\n
1\u3001\u6982\u5ff5<\/strong><\/span><\/div>\n

\u5e03\u9686\u8fc7\u6ee4\u5668\u5176\u5b9e\u5c31\u662f\u52a0\u5feb\u5224\u5b9a\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d\u51fa\u73b0\u7684\u65b9\u6cd5\u3002\u6bd4\u5982\u8bf4\u5728\u4e00\u4e2a\u5927\u5b57\u5178\u4e2d\uff0c\u8981\u67e5\u627e\u67d0\u4e2a\u5355\u8bcd\u662f\u5426\u5b58\u5728\uff0c\u4e8e\u662f\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u5feb\u901f\u9ad8\u6548\u7701\u65f6\u7701\u529b\u3002<\/p>\n

\u8fd9\u91cc\u6709\u4e00\u4e2a\u8003\u5bdf\u70b9\uff0c\u90a3\u5c31\u662f\u5e03\u9686\u8fc7\u6ee4\u5668\u53ea\u80fd\u5224\u5b9a\u4e00\u4e2a\u5143\u7d20\u4e0d\u5728\u96c6\u5408\u91cc\u9762\uff0c\u4e0d\u80fd\u5224\u65ad\u5b58\u5728\uff0c\u4ec0\u4e48\u610f\u601d\u5462!\u5c31\u662f\u8bf4\u4e00\u4e2a\u82f9\u679c\u4e0d\u5728\u7bee\u5b50\u91cc\uff0c\u8fd9\u4e2a\u6211\u53ef\u4ee5\u901a\u8fc7\u5e03\u9686\u8fc7\u6ee4\u5668\u77e5\u9053\uff0c\u4f46\u662f\u4e00\u5b9a\u5728\u7bee\u5b50\u91cc\u561b?\u8fd9\u4e2a\u901a\u8fc7\u5e03\u9686\u8fc7\u6ee4\u5668\u6211\u662f\u4e0d\u80fd\u5224\u5b9a\u7684\u3002<\/p>\n

\u4e0b\u9762\u901a\u8fc7\u539f\u7406\u5c31\u80fd\u7406\u89e3\u8fd9\u4e2a\u4e86\u3002<\/p>\n

2\u3001\u539f\u7406<\/strong><\/span><\/div>\n

\u5148\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5728\u6211\u4eec\u8eab\u8fb9\u5145\u65a5\u7740\u5404\u79cd\u5404\u6837\u7684XX\u7f51\u7ad9\uff0c\u4e3a\u4e86\u4e0d\u6bd2\u5bb3\u6211\u4eec\u7956\u56fd\u7684\u82b1\u6735\uff0c\u4e8e\u662f\u56fd\u5bb6\u7f51\u8b66\u5c31\u5f00\u59cb\u5bf9\u8fd9\u4e9b\u7f51\u7ad9\u8fdb\u884c\u5272\u9664\u8fc7\u6ee4\uff0c\u95ee\u9898\u6765\u4e86\uff0c\u8fd9\u4e9b\u7f51\u7ad9\u7684\u5730\u5740\u5176\u5b9e\u662f\u4e0d\u505c\u7684\u66f4\u6362\u7684\uff0c\u8fd9\u4e9b\u5783\u573e\u7f51\u7ad9\u548c\u6b63\u5e38\u7f51\u7ad9\u52a0\u8d77\u6765\u5168\u4e16\u754c\u636e\u7edf\u8ba1\u4e5f\u6709\u51e0\u5341\u4ebf\u4e2a\u3002\u56e0\u6b64\u5c31\u4f1a\u5e26\u6765\u5982\u4e0b\u7684\u95ee\u9898\uff1a<\/p>\n

(1)\u7f51\u7ad9\u6570\u91cf\u592a\u591a\uff0c\u5b58\u50a8\u8d77\u6765\u6bd4\u8f83\u9ebb\u70e6\u3002\u4e00\u4e2a\u5730\u5740\u6700\u8d77\u7801\u670932\u4e2a\u5b57\u8282\uff0c\u4e00\u4ebf\u4e2a\u5730\u5740\u5c31\u9700\u89811.6G\u7684\u5185\u5b58\u3002<\/p>\n

(2)\u4e00\u4e2a\u4e00\u4e2a\u6bd4\u8f83\uff0c\u592a\u8d39\u65f6\u95f4\u4e86\u3002<\/p>\n

\u56e0\u6b64\u5e03\u9686\u8fc7\u6ee4\u5668\u88ab\u8bbe\u8ba1\u51fa\u6765\u4e86\uff0c\u4ed6\u662f\u5982\u4f55\u505a\u5230\u9ad8\u6548\u7684\u5462?\u672c\u8d28\u4e0a\u5176\u5b9e\u5c31\u662f\u4e00\u4e2aHASH\u6620\u5c04\u5668\u3002\u4ed6\u7684\u5e95\u5c42\u5176\u5b9e\u662f\u4e00\u4e2a\u8d85\u5927\u7684\u4e8c\u8fdb\u5236\u5411\u91cf\u548c\u4e00\u7cfb\u5217\u968f\u673a\u6620\u5c04\u51fd\u6570\u3002\u73b0\u5728\u6211\u4eec\u6309\u7167\u4e4b\u524d\u7684\u90a3\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u5b58\u50a81\u4ebf\u4e2a\u5783\u573e\u7f51\u7ad9\u5730\u5740\u3002<\/p>\n

(1)\u7b2c\u4e00\u6b65\uff1a\u5efa\u7acb\u4e00\u4e2a32\u4ebf\u4e8c\u8fdb\u5236(\u6bd4\u7279)\uff0c\u4e5f\u5c31\u662f4\u4ebf\u5b57\u8282\u7684\u5411\u91cf\u3002\u5168\u90e8\u7f6e0\u3002<\/p>\n

\"\"<\/p>\n

(2)\u7b2c\u4e8c\u6b65\uff1a\u7f51\u8b66\u7528\u516b\u4e2a\u4e0d\u540c\u7684\u968f\u673a\u6570\u4ea7\u751f\u5668(F1,F2, \u2026,F8) \u4ea7\u751f\u516b\u4e2a\u4fe1\u606f\u6307\u7eb9(f1, f2, \u2026, f8)\u3002<\/p>\n

(3)\u7b2c\u4e09\u6b65\uff1a\u7528\u4e00\u4e2a\u968f\u673a\u6570\u4ea7\u751f\u5668 G \u628a\u8fd9\u516b\u4e2a\u4fe1\u606f\u6307\u7eb9\u6620\u5c04\u5230 1 \u523032\u4ebf\u4e2d\u7684\u516b\u4e2a\u81ea\u7136\u6570 g1, g2, \u2026,g8\u3002<\/p>\n

(4)\u7b2c\u56db\u6b65\uff1a\u628a\u8fd9\u516b\u4e2a\u4f4d\u7f6e\u7684\u4e8c\u8fdb\u5236\u5168\u90e8\u8bbe\u7f6e\u4e3a\u4e00\u3002<\/p>\n

\"\"<\/p>\n

OK\uff0c\u6709\u4e00\u5929\u7f51\u8b66\u67e5\u5230\u4e86\u4e00\u4e2a\u53ef\u7591\u7684\u7f51\u7ad9\uff0c\u60f3\u5224\u65ad\u4e00\u4e0b\u662f\u5426\u662fXX\u7f51\u7ad9\uff0c\u4e8e\u662f\u5c31\u5f00\u59cb\u68c0\u67e5\u4e86\u3002\u901a\u8fc7\u540c\u6837\u7684\u65b9\u6cd5\u5c06XX\u7f51\u7ad9\u901a\u8fc7\u54c8\u5e0c\u6620\u5c04\u523032\u4ebf\u4e2a\u6bd4\u7279\u4f4d\u6570\u7ec4\u4e0a\u76848\u4e2a\u70b9\u3002\u5982\u679c8\u4e2a\u70b9\u7684\u5176\u4e2d\u6709\u4e00\u4e2a\u70b9\u4e0d\u4e3a1\uff0c\u5219\u53ef\u4ee5\u5224\u65ad\u8be5\u5143\u7d20\u4e00\u5b9a\u4e0d\u5b58\u5728\u96c6\u5408\u4e2d\u3002<\/p>\n

\u6ce8\u610f\uff1a\u73b0\u5728\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u4e24\u4e2aXX\u7f51\u7ad9\u901a\u8fc7\u4e0a\u9762\u7684\u6b65\u9aa4\u6620\u5c04\u5230\u4e86\u76f8\u540c\u76848\u4e2a\u70b9\u4e0a\uff0c\u6216\u8005\u662f\u6709\u4e00\u90e8\u5206\u70b9\u662f\u91cd\u5408\u7684\uff0c\u8fd9\u65f6\u5019\u8be5\u600e\u4e48\u529e?\u4e8e\u662f\u5c31\u51fa\u73b0\u4e86\u8bef\u62a5\uff0c\u4e5f\u5c31\u662f\u8bf4A\u7f51\u7ad9\u572812345678\u4e2a\u70b9\u4e0a\u5168\u90e8\u7f6e1\uff0cB\u7f51\u7ad9\u901a\u8fc7\u540c\u6837\u7684\u65b9\u5f0f\u572823456789\u4e0a\u5168\u90e8\u7f6e1\uff0c\u8fd9\u65f6\u5019B\u7f51\u7ad9\u6765\u4e86\u662f\u4e0d\u80fd\u786e\u5b9a\u662f\u5426\u5305\u542b\u7684\u3002\u8fd9\u4e2a\u903b\u8f91\u76f8\u4fe1\u5404\u4f4d\u90fd\u7406\u89e3\u3002\u8fd9\u4e2a\u662f\u6700\u57fa\u7840\u7684\u9762\u8bd5\u95ee\u9898\u3002<\/p>\n

3\u3001\u8bef\u62a5\u7387<\/strong><\/span><\/div>\n

\u8fd9\u4e00\u5c0f\u8282\u662f\u7a0d\u5fae\u9ad8\u7ea7\u4e00\u70b9\u70b9\uff0c\u67d0\u4e2d\u5382\u95ee\u5230\u4e86\u4e00\u6b21\uff0c\u4e8e\u662f\u8fd9\u4e00\u6b21\u5c31\u6dfb\u52a0\u4e86\u8fdb\u6765\u3002<\/p>\n

\u901a\u8fc7\u4e0a\u9762\u7684\u89e3\u91ca\u76f8\u4fe1\u90fd\u5927\u6982\u4e86\u89e3\u7684\u5dee\u4e0d\u591a\u4e86\uff0c\u5176\u5b9e\u5c31\u662fhash\u51fd\u6570\u6620\u5c04\uff0c\u7531\u4e8e\u6709hash\u51b2\u7a81\u4ea7\u751f\u4e86\u8bef\u62a5\u7387\uff0c\u8bef\u62a5\u7387\u4e5f\u5c31\u662f\u5224\u65ad\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/p>\n

\u65e2\u7136\u662f\u7531\u4e8ehash\u51b2\u7a81\uff0c\u90a3\u6211\u628a\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u4e8c\u8fdb\u5236\u5411\u91cf\u8c03\u5230\u5f88\u5927\uff0c\u8fd9\u6837\u4e0d\u5c31\u89e3\u51b3\u4e86\u561b\uff0c\u4f46\u662f\u7531\u4e8e\u6570\u636e\u91cf\u6bd4\u8f83\u5927\uff0c\u56e0\u6b64\u73b0\u5728\u5c31\u8981\u8003\u8651\u4e00\u4e0b\u8bef\u62a5\u7387\u548c\u5b58\u50a8\u6548\u7387\u4e4b\u95f4\u9009\u62e9\u4e00\u4e2a\u6298\u4e2d\u503c\u4e86\u3002\u6709\u4e00\u4e2a\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b\uff1a\u516c\u5f0f\u6765\u6e90\u4e8egithub<\/p>\n

\u5047\u8bbe\u4f4d\u6570\u7ec4\u7684\u957f\u5ea6\u4e3am\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4e2a\u6570\u4e3ak\u3002\u68c0\u6d4b\u67d0\u4e00\u5143\u7d20\u662f\u5426\u5728\u8be5\u96c6\u5408\u4e2d\u7684\u8bef\u62a5\u7387\u662f\uff1a<\/p>\n

\"\"<\/p>\n

\u5982\u4f55\u4f7f\u5f97\u8bef\u62a5\u7387\u6700\u5c0f\uff0c\u6570\u5b66\u95ee\u9898\uff0c\u6c42\u5bfc\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n

4\u3001\u4f7f\u7528\u573a\u666f<\/strong><\/span><\/div>\n

(1)google\u7684guava\u5305\u4e2d\u6709\u5bf9Bloom Filter\u7684\u5b9e\u73b0<\/p>\n

(2)\u901a\u5e38\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\u53bb\u89e3\u51b3redis\u4e2d\u7684\u7f13\u5b58\u7a7f\u900f\uff0c\u89e3\u51b3\u65b9\u6848\u662fredis\u4e2dbitmap\u7684\u5b9e\u73b0\uff0c<\/p>\n

(3)\u9493\u9c7c\u7f51\u7ad9\u3001\u5783\u573e\u90ae\u4ef6\u68c0\u6d4b<\/p>\n

\u5927\u4f53\u5c31\u8fd9\u4e9b\uff0c\u53ef\u80fd\u8fd8\u6709\u5f88\u591a!!!<\/p>\n

\u4e8c\u3001\u4ee3\u7801\u5b9e\u73b0\u5e03\u9686\u8fc7\u6ee4\u5668<\/strong><\/div>\n

\u4e0a\u9762\u53ea\u662f\u7ed9\u51fa\u4e86\u5176\u539f\u7406\uff0c\u4e0b\u9762\u6211\u4eec\u4ee3\u7801\u5b9e\u73b0\u4e00\u4e0b\u3002<\/p>\n

\r\npublic   class  MyBloomFilter { \r\n    \/\/ 2 << 25\u8868\u793a32\u4ebf\u4e2a\u6bd4\u7279\u4f4d \r\n     private static final int DEFAULT_SIZE =  2 << 25 ; \r\n     private static final int[] seeds = new int [] {3,5,7,11,13,19,23,37 }; \r\n     \/\/\u8fd9\u4e48\u5927\u5b58\u50a8\u5728BitSet \r\n     private  BitSet  bits = new BitSet(DEFAULT_SIZE); \r\n     private  SimpleHash[] func  = new  SimpleHash[seeds.length]; \r\n \r\n     public   static   void  main(String[] args) { \r\n        \/\/\u53ef\u7591\u7f51\u7ad9 \r\n        String value = \"www.\u611a\u516c\u8981\u79fb\u5c71.com\" ; \r\n        MyBloomFilter filter = new MyBloomFilter(); \r\n        \/\/\u52a0\u5165\u4e4b\u524d\u5224\u65ad\u4e00\u4e0b \r\n        System.out.println(filter.contains(value)); \r\n        filter.add(value); \r\n        \/\/\u52a0\u5165\u4e4b\u540e\u5224\u65ad\u4e00\u4e0b \r\n        System.out.println(filter.contains(value)); \r\n    } \r\n    \/\/\u6784\u9020\u51fd\u6570 \r\n     public  MyBloomFilter() { \r\n         for  ( int  i  =   0 ; i  <  seeds.length; i ++ ) { \r\n            func[i]  =   new  SimpleHash(DEFAULT_SIZE, seeds[i]); \r\n        } \r\n    } \r\n     \/\/\u6dfb\u52a0\u7f51\u7ad9 \r\n     public   void  add(String value) { \r\n         for  (SimpleHash f : func) { \r\n            bits.set(f.hash(value),  true ); \r\n        } \r\n    } \r\n     \/\/\u5224\u65ad\u53ef\u7591\u7f51\u7ad9\u662f\u5426\u5b58\u5728 \r\n     public   boolean  contains(String value) { \r\n         if  (value  ==   null ) { \r\n             return   false ; \r\n        } \r\n         boolean  ret  =   true ; \r\n         for  (SimpleHash f : func) { \r\n            \/\/\u6838\u5fc3\u5c31\u662f\u901a\u8fc7\u201c\u4e0e\u201d\u7684\u64cd\u4f5c \r\n            ret  =  ret  &&  bits.get(f.hash(value)); \r\n        } \r\n         return  ret; \r\n    } \r\n} \r\n<\/pre>\n

\u8fd8\u6709\u4e00\u4e2aSimpleHash\uff0c\u6211\u4eec\u770b\u4e00\u4e0b<\/p>\n

\r\npublic   static   class  SimpleHash { \r\n        private  int  cap; \r\n        private  int  seed; \r\n \r\n        public  SimpleHash( int  cap,  int  seed) { \r\n            this .cap  =  cap; \r\n            this .seed  =  seed; \r\n       } \r\n        public   int  hash(String value) { \r\n            int  result  =   0 ; \r\n            int  len  =  value.length(); \r\n            for  ( int  i  =   0 ; i  <  len; i ++ ) { \r\n               result  =  seed  *  result  +  value.charAt(i); \r\n           } \r\n            return  (cap  -   1 )  &  result; \r\n       } \r\n   } \r\n<\/pre>\n

\u8fd9\u5c31\u662f\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8fd9\u4e00\u7bc7\u662f\u6211\u91cd\u5199\u7684\uff0c\u4e4b\u524d\u5199\u8fc7\u4e00\u7bc7\u53d1\u73b0\u9762\u8bd5\u7684\u65f6\u5019\u95ee\u7684\u95ee\u9898\u867d\u7136\u5927\u6982\u80fd\u89e3\u51b3\uff0c\u4f46\u662f\u6709\u51e0\u4e2a\u70b9\u6ca1\u6709\u6574\u7406\u5230\u4f4d\uff0c\u6240\u4ee5\u81ea\u5df1\u7ed9\u81ea\u5df1\u5217 […]<\/p>\n","protected":false},"author":317,"featured_media":158082,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-204812","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/204812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=204812"}],"version-history":[{"count":1,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/204812\/revisions"}],"predecessor-version":[{"id":204817,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/204812\/revisions\/204817"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/158082"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=204812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=204812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=204812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}