diff --git a/lib/splitclient-rb/cache/filter/bloom_filter.rb b/lib/splitclient-rb/cache/filter/bloom_filter.rb index b06a94ab..aa602807 100644 --- a/lib/splitclient-rb/cache/filter/bloom_filter.rb +++ b/lib/splitclient-rb/cache/filter/bloom_filter.rb @@ -8,8 +8,8 @@ module Filter class BloomFilter def initialize(capacity, false_positive_probability = 0.001) @capacity = capacity.round - m = best_m(capacity, false_positive_probability) - @ba = BitArray.new(m.round) + @m = best_m(capacity, false_positive_probability) + reset_filter @k = best_k(capacity) end @@ -17,22 +17,26 @@ def add(string) return false if contains?(string) positions = hashes(string) - positions.each { |position| @ba[position] = 1 } true end - + def contains?(string) !hashes(string).any? { |ea| @ba[ea] == 0 } end def clear - @ba.size.times { |i| @ba[i] = 0 } + @ba = nil + reset_filter end - + private + def reset_filter + @ba = BitArray.new(@m.round) + end + # m is the required number of bits in the array def best_m(capacity, false_positive_probability) -(capacity * Math.log(false_positive_probability)) / (Math.log(2) ** 2)