Skip to content

Instantly share code, notes, and snippets.

@ybv
Created October 15, 2014 23:32
Show Gist options
  • Save ybv/c976cda4798641c93d3c to your computer and use it in GitHub Desktop.
Save ybv/c976cda4798641c93d3c to your computer and use it in GitHub Desktop.
Simple Bloom Filter api with Tornado.
import os
import hashlib
import tornado.ioloop
import tornado.web
from tornado import gen
class bv(object):
def __init__(self):
self.bit_vector = None
def make_vector(self,n):
self.bit_vector = [False]*int(n)
def add_string(self,str,k):
n = len(self.bit_vector)
for i in xrange(0,k):
hash = hashlib.md5(str).hexdigest()
self.bit_vector[int(hash,16)%n] = True
str = hash
def test_string(self,string,k):
for i in xrange(0,k):
hash = hashlib.md5(string).hexdigest()
if not self.bit_vector[int(hash,16)%len(self.bit_vector)]:
return False
str = hash
return True
class AddHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
string = self.get_argument('string') if 'string' in self.request.arguments else None
hash_times = int(self.get_argument('repeat')) if 'repeat' in self.request.arguments else 3
if string:
if bv.bit_vector:
bv.add_string(string,hash_times)
self.write("string is added")
else:
self.write("first set the bit vector using /set_vector/")
else:
self.write("Please provide a string")
class ResetHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
size = self.get_argument('size') if 'size' in self.request.arguments else None
if size:
bv.make_vector(size)
self.write("vector is reset")
else:
self.write("Please provide a size for the vector")
class InfoHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
self.write("use /set_vector/ to initialize bit vector; /add_string/ to add string and /test_string/ to test for it.")
class TestHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
string = self.get_argument('string') if 'string' in self.request.arguments else None
hash_times = int(self.get_argument('repeat')) if 'repeat' in self.request.arguments else 3
if string:
resp = bv.test_string(string,hash_times)
if resp:
self.write("string is present")
else:
self.write("string is NOT present")
else:
self.write("Please provide a string to test")
def make_app(bv):
app = tornado.web.Application([
(r"/",InfoHandler),
(r"/add_string/",AddHandler),
(r"/set_vector/",ResetHandler),
(r"/test_string/",TestHandler)
],bv)
return app
if __name__=="__main__":
bv = bv()
app = make_app(bv)
app.listen(int(os.environ.get("PORT", 5000)))
tornado.ioloop.IOLoop.current().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment