forked from erikdubbelboer/redis-lua-scaling-bloom-filter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlayer-add.lua
44 lines (35 loc) · 1.42 KB
/
layer-add.lua
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
local entries = ARGV[2]
local precision = ARGV[3]
local hash = redis.sha1hex(ARGV[4])
-- This uses a variation on:
-- 'Less Hashing, Same Performance: Building a Better Bloom Filter'
-- http://www.eecs.harvard.edu/~kirsch/pubs/bbbf/esa06.pdf
local h = { }
h[0] = tonumber(string.sub(hash, 1 , 8 ), 16)
h[1] = tonumber(string.sub(hash, 9 , 16), 16)
h[2] = tonumber(string.sub(hash, 17, 24), 16)
h[3] = tonumber(string.sub(hash, 25, 32), 16)
for layer=1,32 do
local key = ARGV[1] .. ':' .. layer .. ':'
local factor = math.ceil((entries + redis.call('INCR', key .. 'count')) / entries)
-- 0.69314718055995 = ln(2)
local index = math.ceil(math.log(factor) / 0.69314718055995)
local scale = math.pow(2, index - 1) * entries
key = key .. index
-- Based on the math from: http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives
-- Combined with: http://www.sciencedirect.com/science/article/pii/S0020019006003127
-- 0.4804530139182 = ln(2)^2
local bits = math.floor(-(scale * math.log(precision * math.pow(0.5, index))) / 0.4804530139182)
-- 0.69314718055995 = ln(2)
local k = math.floor(0.69314718055995 * bits / scale)
local found = true
for i=1, k do
if redis.call('SETBIT', key, (h[i % 2] + i * h[2 + (((i + (i % 2)) % 4) / 2)]) % bits, 1) == 0 then
found = false
end
end
-- if it wasn't found in this layer already break
if found == false then
break
end
end