-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinit_redis.py
48 lines (42 loc) · 1.34 KB
/
init_redis.py
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
__author__ = 'multiangle'
from DB_Interface import MySQL_Interface
import redis
class SimpleHash():
def __init__(self,cap,seed):
self.cap=cap
self.seed=seed
def hash(self,value):
ret=0
for i in range(value.__len__()):
ret+=self.seed*ret+ord(value[i])
return ((self.cap-1) & ret)
class BloomFilter():
def __init__(self):
self.bit_size=1<<15
self.seeds=[5,7,11,13,31,37,61]
self.r=redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
self.hashFunc=[]
for i in range(self.seeds.__len__()):
self.hashFunc.append(SimpleHash(self.bit_size,self.seeds[i]))
def isContains(self,str_input,name):
if str_input==None:
return False
if str_input.__len__()==0:
return False
ret=True
for f in self.hashFunc:
loc=f.hash(str_input)
ret=ret & self.r.getbit(name,loc)
return ret
def insert(self,str_input,name):
for f in self.hashFunc:
loc=f.hash(str_input)
self.r.setbit(name,loc,1)
dbi=MySQL_Interface(dbname='microblog_spider')
r=redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
query='select uid from user_info_table ;'
uid=dbi.select_asQuery(query)
uid=[x[0] for x in uid]
bf=BloomFilter()
for id in uid:
bf.insert(id,'user_info_table')