Skip to content

Instantly share code, notes, and snippets.

@R-omk
Last active August 27, 2019 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save R-omk/7a7f327d350b9c10acf7de7a754a4771 to your computer and use it in GitHub Desktop.
Save R-omk/7a7f327d350b9c10acf7de7a754a4771 to your computer and use it in GitHub Desktop.
tarantool bloomfilter concept
local bloom = require('bloomfilter')
local bloom_options = {size = 512, hash = 3}
local bf_instance = bloom.new(bloom_options)
local init_state = nil
local somedata = "Data"
local somedata2 = "Data2"
-- now the filter is filled with zeros
bf_instance:merge(init_state)
bf_instance:add(somedata, somedata2) -- same as bf_instance:merge(bf_instance.hash(somedata), bf_instance.hash(somedata))
-- exists : bool
local exists = bf_instance:intersect(bf_instance.hash("Data")) -- true
------------
-- Integrate with tarantool box
local bloom_box = require('bloomfilter.box')
-- create common space
local my_space = box.schema.space.create('my_bloom_filter', { temporary = false , if_not_exists = true })
my_space:create_index('primary', { type = 'TREE', unique = true, parts = { 1, 'unsigned' }, if_not_exists = true })
-- make space with bloomfilter index
local filed_num = 2
local index_name = 'bloom_bitset'
local my_bloom_space = bloom_box.create(bloom_options, my_space, filed_num, index_name) -- gererate: -- my_space:create_index('bloom_bitset', { unique=false, type='BITSET', parts = {2, 'string'}})
local tuple_primary_index_key = {1}
-- add new data to bloom filter
my_bloom_space:merge(tuple_primary_index_key, bf_instance) -- insert or update space -- box.space.my_bloom_filter:update( {id}, { {'|', 2, tostring(bf_instance) } } )
-- iterate over tuples which may contain "Data"
for _, tuple in my_bloom_space:pairs( bf_instance.hash("Data") , { iterator = box.index.BITS_ALL_SET }) do -- box.space.my_bloom_filter.index.bloom_bitset:pairs(...
-- do some with tuple
end
-- etc ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment