def get(self, haystack, pin): global db haystack = str(haystack) pin = str(pin) response = {} logger = logging.getLogger('bloom-api') if len(pin) != 24 or len(haystack) != 24: response['completed'] = 0 response['description'] = "Incorrect inputs" logger.info('EXISTS - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:Bad inputs') self.write(json.dumps(response)) self.finish() else: response['completed'] = 1 response['description'] = 'Completed' # Check the existence try: result = db.bloom.find_one({'_id': ObjectId(haystack)}) except: response['completed'] = 0 response['description'] = "Couldn't connect to database" logger.error('EXISTS - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:DB down') self.write(json.dumps(response)) self.finish() return if result is None: logger.info('EXISTS - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:New document') response['exists'] = 0 bf = Bloom.Bloom(new_filter_capacity) serialized = Bloom.BloomSerializer.serialize(bf) serialized['_id'] = ObjectId(haystack) db.bloom.save(serialized) else: bf = Bloom.BloomSerializer.deserialize(result) if pin in bf: response['exists'] = 1 else: response['exists'] = 0 logger.info('EXISTS - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:' + str(response['exists'])) self.write(json.dumps(response)) self.finish
def insert(client, haystack, pin): db = client.patari result = db.bloom.find_one({'_id': ObjectId(haystack)}) if result is not None: bf = Bloom.BloomSerializer.deserialize(result) bf.add(pin) serialized = Bloom.BloomSerializer.serialize(bf) serialized['_id'] = ObjectId(haystack) db.bloom.save(serialized) else: bf = Bloom.Bloom(new_filter_capacity) bf.add(pin) serialized = Bloom.BloomSerializer.serialize(bf) serialized['_id'] = ObjectId(haystack) db.bloom.save(serialized)
def get(self, haystack, pin): global db haystack = str(haystack) pin = str(pin) response = {} logger = logging.getLogger('bloom-api') if len(pin) != 24 or (len(haystack)) != 24: response['completed'] = 0 response['description'] = "Incorrect inputs" logger.info('ADD - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:Bad inputs') self.write(json.dumps(response)) self.finish() else: response['completed'] = 1 response['description'] = "Completed" # Implementing the update-if-current pattern # Just don't ask right now why I'm doing this while True: # Pull out the bloom filter try: result = db.bloom.find_one({'_id': ObjectId(haystack)}) except: response['completed'] = 0 response['description'] = "Couldn't connect to database" logger.error('ADD - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:DB down') self.write(json.dumps(response)) self.finish() return # Add the PinID if result is not None: bf = Bloom.BloomSerializer.deserialize(result) oldcount = bf.count bf.add(pin) # Mentioning old count makes you only access the older filter entry = {'_id': ObjectId(haystack), 'count': oldcount} to_update = { '$set': { 'bitarray': Binary(bf.bitarray.tobytes()), 'count': bf.count } } # Perform the update updated = db.bloom.find_one_and_update( entry, to_update, return_document=ReturnDocument.AFTER) if updated is not None: logger.info('ADD - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:' + str(updated['count'])) break else: logger.info('ADD - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:Concurrency issue, trying again') # Try fetching and updating again after delay, hence the infinite loop sleep(1) pass else: # The filter never existed in the first place bf = Bloom.Bloom(new_filter_capacity) bf.add(pin) serialized = Bloom.BloomSerializer.serialize(bf) serialized['_id'] = ObjectId(haystack) db.bloom.save(serialized) logger.info('ADD - Haystack:' + haystack + ' - Pin:' + pin + ' - Msg:New document') break self.write(json.dumps(response)) self.finish()
def setUp(self): self.filter = Bloom.Bloom(capacity=testing_capacity, error_rate=error_prob)
def setUp(self): self.filter = Bloom.Bloom(capacity=testing_capacity, error_rate=error_prob) self.pin = 'ffffffffffffffffffffffff' self.haystack = '000000000000000000000000'