-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMtBloomfilter.cpp
256 lines (197 loc) · 5.78 KB
/
MtBloomfilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "MtBloomfilter.h"
mt::mtBloomfilter::mtBloomfilter()
{
}
mt::mtBloomfilter::~mtBloomfilter()
{
}
bool mt::mtBloomfilter::init(uint32_t dwsend, uint32_t n, double p)
{
return this->InitBloomFilter(dwsend, n, p);
}
bool mt::mtBloomfilter::add(const void * key, int len)
{
return this->BloomFilter_Add(key,len);
}
bool mt::mtBloomfilter::find(const void * key, int len)
{
return this->BloomFilter_Check(key,len);
}
void mt::mtBloomfilter::close()
{
this->FreeBloomFilter();
}
uint64_t mt::mtBloomfilter::MurmurHash2_64_64(const void * key, int len, unsigned int seed)
{
const uint64_t m = 0xc6a4a7935bd1e995;
const int r = 47;
uint64_t h = seed ^ (len * m);
const uint64_t * data = (const uint64_t *)key;
const uint64_t * end = data + (len / 8);
while (data != end)
{
uint64_t k = *data++;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
}
const unsigned char * data2 = (const unsigned char*)data;
switch (len & 7)
{
case 7: h ^= uint64_t(data2[6]) << 48;
case 6: h ^= uint64_t(data2[5]) << 40;
case 5: h ^= uint64_t(data2[4]) << 32;
case 4: h ^= uint64_t(data2[3]) << 24;
case 3: h ^= uint64_t(data2[2]) << 16;
case 2: h ^= uint64_t(data2[1]) << 8;
case 1: h ^= uint64_t(data2[0]);
h *= m;
};
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
uint64_t mt::mtBloomfilter::MurmurHash2_64_32(const void * key, int len, unsigned int seed)
{
const unsigned int m = 0x5bd1e995;
const int r = 24;
unsigned int h1 = seed ^ len;
unsigned int h2 = 0;
const unsigned int * data = (const unsigned int *)key;
while (len >= 8)
{
unsigned int k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
unsigned int k2 = *data++;
k2 *= m; k2 ^= k2 >> r; k2 *= m;
h2 *= m; h2 ^= k2;
len -= 4;
}
if (len >= 4)
{
unsigned int k1 = *data++;
k1 *= m; k1 ^= k1 >> r; k1 *= m;
h1 *= m; h1 ^= k1;
len -= 4;
}
switch (len)
{
case 3: h2 ^= ((unsigned char*)data)[2] << 16;
case 2: h2 ^= ((unsigned char*)data)[1] << 8;
case 1: h2 ^= ((unsigned char*)data)[0];
h2 *= m;
};
h1 ^= h2 >> 18; h1 *= m;
h2 ^= h1 >> 22; h2 *= m;
h1 ^= h2 >> 17; h1 *= m;
h2 ^= h1 >> 19; h2 *= m;
uint64_t h = h1;
h = (h << 32) | h2;
return h;
}
void mt::mtBloomfilter::CalcBloomFilterParam(uint32_t n, double p, uint32_t * pm, uint32_t * pk)
{
/**
* n - Number of items in the filter
* p - Probability of false positives, float between 0 and 1 or a number indicating 1-in-p
* m - Number of bits in the filter
* k - Number of hash functions
*
* f = ln(2) × ln(1/2) × m / n = (0.6185) ^ (m/n)
* m = -1 * ln(p) × n / 0.6185
* k = ln(2) × m / n = 0.6931 * m / n
**/
uint32_t m, k;
// 计算指定假阳概率下需要的比特数
m = (uint32_t)ceil(-1 * log(p) * n / 0.6185);
m = (m - m % 64) + 64; // 8字节对齐
// 计算哈希函数个数
k = (uint32_t)(0.6931 * m / n);
k++;
*pm = m;
*pk = k;
return;
}
inline bool mt::mtBloomfilter::InitBloomFilter(uint32_t dwSeed, uint32_t dwMaxItems, double dProbFalse)
{
if (this->m_pstBloomfilter != nullptr) {
return false;
}
// 初始化内存结构,并计算BloomFilter需要的空间
this->m_pstBloomfilter = new BaseBloomFilter;
this->m_pstBloomfilter->dwMaxItems = dwMaxItems;
this->m_pstBloomfilter->dProbFalse = dProbFalse;
this->m_pstBloomfilter->dwSeed = dwSeed;
// 计算 m, k
this->CalcBloomFilterParam(this->m_pstBloomfilter->dwMaxItems, this->m_pstBloomfilter->dProbFalse,
&(this->m_pstBloomfilter->dwFilterBits), &(this->m_pstBloomfilter->dwHashFuncs));
// 分配BloomFilter的存储空间
this->m_pstBloomfilter->dwFilterSize = this->m_pstBloomfilter->dwFilterBits / BYTE_BITS;
this->m_pstBloomfilter->pstFilter = new uchar_t[this->m_pstBloomfilter->dwFilterSize];
// 初始化BloomFilter的内存
memset(this->m_pstBloomfilter->pstFilter, 0, this->m_pstBloomfilter->dwFilterSize);
//哈希结果数组,每个哈希函数一个
this->m_pstBloomfilter->pdwHashPos = new uint32_t[this->m_pstBloomfilter->dwHashFuncs * sizeof(uint32_t)];
return true;
}
inline void mt::mtBloomfilter::FreeBloomFilter()
{
if (this->m_pstBloomfilter != nullptr) {
this->m_pstBloomfilter->dwCount = 0;
if (this->m_pstBloomfilter->pstFilter != nullptr) {
delete[] this->m_pstBloomfilter->pstFilter;
this->m_pstBloomfilter->pstFilter = nullptr;
}
if ( this->m_pstBloomfilter->pdwHashPos != nullptr) {
delete[] this->m_pstBloomfilter->pdwHashPos;
this->m_pstBloomfilter->pdwHashPos = nullptr;
}
delete this->m_pstBloomfilter;
this->m_pstBloomfilter = nullptr;
}
}
inline void mt::mtBloomfilter::bloom_hash_64(const void * key, int len)
{
uint32_t dwFilterBits = this->m_pstBloomfilter->dwFilterBits;
uint64_t hash1 = MurmurHash2_64_64(key, len, this->m_pstBloomfilter->dwSeed);
uint64_t hash2 = MurmurHash2_64_64(key, len, MIX_UINT64(hash1));
for (uint32_t i = 0; i < this->m_pstBloomfilter->dwHashFuncs; ++i) {
this->m_pstBloomfilter->pdwHashPos[i] = (hash1 + i * hash2) % dwFilterBits;
}
}
inline void mt::mtBloomfilter::bloom_hash_32(const void * key, int len)
{
}
inline bool mt::mtBloomfilter::BloomFilter_Add(const void * key, int len)
{
if (this->m_pstBloomfilter == nullptr || key == nullptr || len <= 0) {
return false;
}
if (this->m_pstBloomfilter->dwCount >= this->m_pstBloomfilter->dwMaxItems) {
return false;
}
this->bloom_hash_64(key, len);
for (uint32_t i = 0; i < this->m_pstBloomfilter->dwHashFuncs; ++i) {
SETBIT(this->m_pstBloomfilter, this->m_pstBloomfilter->pdwHashPos[i]);
}
this->m_pstBloomfilter++;
return true;
}
inline bool mt::mtBloomfilter::BloomFilter_Check(const void * key, int len)
{
if ( this->m_pstBloomfilter == nullptr ) {
return false;
}
this->bloom_hash_64(key, len);
for (uint32_t i = 0; i < this->m_pstBloomfilter->dwHashFuncs; ++i) {
if ( GETBIT(this->m_pstBloomfilter, this->m_pstBloomfilter->pdwHashPos[i]) == 0 ) {
return false;
}
}
return true;
}