Class: Bloomfilter::Bloomfilter

Inherits:
Object
  • Object
show all
Defined in:
lib/bloomfilter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bloomfilter

Returns a new instance of Bloomfilter.



15
16
17
18
19
20
21
22
23
# File 'lib/bloomfilter.rb', line 15

def initialize(options = {})
  if options[:size] && options[:false_positive_percentage]
    @filter = Jar::BloomFilter.new(options[:false_positive_percentage], options[:size])
  elsif options[:filter]
    @filter = options[:filter]
  end
  
  @lock = Jar::ReentrantLock.new
end

Instance Method Details

#<<(k) ⇒ Object



25
26
27
28
29
30
# File 'lib/bloomfilter.rb', line 25

def << (k)
  lock
  @filter.add(k)
ensure
  unlock
end

#countObject



39
40
41
# File 'lib/bloomfilter.rb', line 39

def count
  @filter.count
end

#include?(k) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/bloomfilter.rb', line 32

def include?(k)
  lock
  @filter.contains(k)
ensure
  unlock
end

#lockObject



43
44
45
# File 'lib/bloomfilter.rb', line 43

def lock
  @lock.lock
end

#unlockObject



47
48
49
# File 'lib/bloomfilter.rb', line 47

def unlock
  @lock.unlock
end