Skip to content

Commit 49d9abe

Browse files
authored
Merge pull request #537 from splitio/bloomfilter-clear-fix
Bloomfilter clear fix
2 parents 496265d + eb7509b commit 49d9abe

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/splitclient-rb/cache/filter/bloom_filter.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,35 @@ module Filter
88
class BloomFilter
99
def initialize(capacity, false_positive_probability = 0.001)
1010
@capacity = capacity.round
11-
m = best_m(capacity, false_positive_probability)
12-
@ba = BitArray.new(m.round)
11+
@m = best_m(capacity, false_positive_probability)
12+
reset_filter
1313
@k = best_k(capacity)
1414
end
1515

1616
def add(string)
1717
return false if contains?(string)
1818

1919
positions = hashes(string)
20-
2120
positions.each { |position| @ba[position] = 1 }
2221

2322
true
2423
end
25-
24+
2625
def contains?(string)
2726
!hashes(string).any? { |ea| @ba[ea] == 0 }
2827
end
2928

3029
def clear
31-
@ba.size.times { |i| @ba[i] = 0 }
30+
@ba = nil
31+
reset_filter
3232
end
33-
33+
3434
private
3535

36+
def reset_filter
37+
@ba = BitArray.new(@m.round)
38+
end
39+
3640
# m is the required number of bits in the array
3741
def best_m(capacity, false_positive_probability)
3842
-(capacity * Math.log(false_positive_probability)) / (Math.log(2) ** 2)

0 commit comments

Comments
 (0)