/
BloomFilter.java
327 lines (287 loc) · 9.01 KB
/
BloomFilter.java
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package org.olylakers.bloomfilter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* BloomFilter based on redis
* @author olylakers
*
* 2013-12-4 上午9:46:37
*/
public class BloomFilter {
private String hosts;
private int timeout;
private int maxKey;
private float errorRate;
private int hashFunctionCount;
private int bitSize;
private ShardedJedisPool pool;
private String defaultKey = "redis:bloomfilter";
private static final String hostConfig = "127.0.0.1:6001";
public BloomFilter(String hosts, int timeout, float errorRate, int maxKey){
this.hosts = hosts;
this.timeout = timeout;
this.maxKey = maxKey;
this.errorRate = errorRate;
String[] hostInfos = hosts.split(";");
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
if(StringUtils.isNotBlank(hosts)){
for (String hostInfo : hostInfos) {
String[] host = hostInfo.split(":");
if(host.length != 2){
throw new IllegalArgumentException("hosts should not be null or empty");
}
int port = 0;
if(NumberUtils.isNumber(host[1])){
port = NumberUtils.toInt(host[1]);
}
shards.add(new JedisShardInfo(host[0],port,timeout));
}
}else{
throw new IllegalArgumentException("redis host.length != 2");
}
pool = initRedisPool(shards);
bitSize = calcOptimalM(maxKey, errorRate);
hashFunctionCount =calcOptimalK(bitSize, maxKey);
}
private ShardedJedisPool initRedisPool(List<JedisShardInfo> shards){
Config config = new Config();
//在借出的时候不测试有效性
config.testOnBorrow = false;
//在还回的时候不测试有效性
config.testOnReturn = false;
//最小空闲数
config.minIdle = 8;
//最大空闲数
config.maxIdle = 50;
//最大连接数
config.maxActive = 50;
//允许最大等待时间,2s,单位:ms
config.maxWait = 2 * 1000;
config.minEvictableIdleTimeMillis = 1000L * 60L * 60L * 5L;
return new ShardedJedisPool(config,shards);
}
/**
* add one object, using default key
* @param bizId
*/
public void add(int bizId){
add(defaultKey, bizId);
}
/**
* add one object using the specified key
* @param key
* @param bizId
*/
public void add(String key, long bizId){
int[] offset = HashUtils.murmurHashOffset(bizId, hashFunctionCount, bitSize);
ShardedJedis jedis = null;
boolean connected = true;
try {
jedis = pool.getResource();
for (int i : offset) {
jedis.setbit(key, i, true);
}
}finally{
if(jedis != null){
if(connected){
pool.returnResource(jedis);
}else{
pool.returnBrokenResource(jedis);
}
}
}
}
/**
* add one object using the specified key
* @param key
* @param bizId
*/
public void addWithPipe(String key, long bizId){
int[] offset = HashUtils.murmurHashOffset(bizId, hashFunctionCount, bitSize);
ShardedJedis jedis = null;
boolean connected = true;
try {
jedis = pool.getResource();
ShardedJedisPipeline pipeline = jedis.pipelined();
for (int i : offset) {
pipeline.setbit(key, i, true);
}
pipeline.sync();
}finally{
if(jedis != null){
if(connected){
pool.returnResource(jedis);
}else{
pool.returnBrokenResource(jedis);
}
}
}
}
/**
* Check if a bizId is part of the set
* @param key
* @param bizId
*/
public boolean include(String key, long bizId){
int[] offset = HashUtils.murmurHashOffset(bizId, hashFunctionCount, bitSize);
ShardedJedis jedis = null;
boolean connected = true;
try {
jedis = pool.getResource();
for (int i : offset) {
if(!jedis.getbit(key, i)){
return false;
}
}
}finally{
if(jedis != null){
if(connected){
pool.returnResource(jedis);
}else{
pool.returnBrokenResource(jedis);
}
}
}
return true;
}
/**
* Check if a bizId is part of the set
* @param key
* @param bizId
*/
public boolean includeWithPipe(String key, long bizId){
int[] offset = HashUtils.murmurHashOffset(bizId, hashFunctionCount, bitSize);
ShardedJedis jedis = null;
boolean connected = true;
try {
jedis = pool.getResource();
ShardedJedisPipeline pipeline = jedis.pipelined();
for (int i : offset) {
pipeline.getbit(key, i);
}
List<Object> responses = pipeline.syncAndReturnAll();
for (Object object : responses) {
if(object instanceof Boolean){
Boolean contains = (Boolean) object;
if(!contains){
return false;
}
}
}
}finally{
if(jedis != null){
if(connected){
pool.returnResource(jedis);
}else{
pool.returnBrokenResource(jedis);
}
}
}
return true;
}
public long count(String key){
ShardedJedis jedis = null;
boolean connected = true;
try {
jedis = pool.getResource();
return jedis.bitcount(key);
}finally{
if(jedis != null){
if(connected){
pool.returnResource(jedis);
}else{
pool.returnBrokenResource(jedis);
}
}
}
}
public String getRedisData(String key){
ShardedJedis jedis = null;
boolean connected = true;
try {
jedis = pool.getResource();
String redisData = jedis.get(key);
return redisData;
}finally{
if(jedis != null){
if(connected){
pool.returnResource(jedis);
}else{
pool.returnBrokenResource(jedis);
}
}
}
}
/**
* Calculate M and K
* See http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives for more details
* @param maxKey
* @param errorRate
* @return
*/
public int calcOptimalM(int maxKey, float errorRate){
return (int) Math.ceil(maxKey
* (Math.log(errorRate) / Math.log(0.6185)));
}
/**
* Calculate M and K
* See http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives for more details
* @param bitSize
* @param maxKey
* @return
*/
public int calcOptimalK(int bitSize, int maxKey){
return (int) Math.ceil(Math.log(2) * (bitSize / maxKey));
}
public String getHosts() {
return hosts;
}
public void setHosts(String hosts) {
this.hosts = hosts;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public int getMaxKey() {
return maxKey;
}
public void setMaxKey(int maxKey) {
this.maxKey = maxKey;
}
public float getErrorRate() {
return errorRate;
}
public void setErrorRate(float errorRate) {
this.errorRate = errorRate;
}
public int getHashFunctionCount() {
return hashFunctionCount;
}
public void setHashFunctionCount(int hashFunctionCount) {
this.hashFunctionCount = hashFunctionCount;
}
public int getBitSize() {
return bitSize;
}
public void setBitSize(int bitSize) {
this.bitSize = bitSize;
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
BloomFilter bloomFilter = new BloomFilter(hostConfig, 1000, 0.00000001f, (int)Math.pow(2, 31));
System.out.println(bloomFilter.getBitSize()/8/1024/2014);
}
}