Class: Redis::Bloomfilter

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

Constant Summary collapse

VERSION =
'1.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bloomfilter

Usage: Redis::Bloomfilter.new :size => 1000, :error_rate => 0.01 It creates a bloomfilter with a capacity of 1000 items and an error rate of 1%

Raises:

  • (ArgumentError)


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
44
45
# File 'lib/redis/bloomfilter.rb', line 10

def initialize(options = {})
  @options = {
    size: 1000,
    error_rate: 0.01,
    key_name: 'redis-bloomfilter',
    hash_engine: 'md5',
    redis: Redis.current,
    driver: nil
  }.merge options

  raise ArgumentError, 'options[:size] && options[:error_rate] cannot be nil' if options[:error_rate].nil? || options[:size].nil?

  # Size provided, compute hashes and bits

  @options[:size]       = options[:size]
  @options[:error_rate] = options[:error_rate] ? options[:error_rate] : @options[:error_rate]
  @options[:bits]       = Bloomfilter.optimal_m options[:size], @options[:error_rate]
  @options[:hashes]     = Bloomfilter.optimal_k options[:size], @options[:bits]

  @redis = @options[:redis] || Redis.current
  @options[:hash_engine] = options[:hash_engine] if options[:hash_engine]

  if @options[:driver].nil?
    ver = @redis.info['redis_version']

    @options[:driver] = if Gem::Version.new(ver) >= Gem::Version.new('2.6.0')
                          'lua'
                        else
                          'ruby'
                        end
  end

  driver_class = Redis::BloomfilterDriver.const_get(driver_name)
  @driver = driver_class.new @options
  @driver.redis = @redis
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



6
7
8
# File 'lib/redis/bloomfilter.rb', line 6

def driver
  @driver
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/redis/bloomfilter.rb', line 5

def options
  @options
end

Class Method Details

.optimal_k(num_of_elements, bf_size) ⇒ Object



53
54
55
56
57
# File 'lib/redis/bloomfilter.rb', line 53

def self.optimal_k(num_of_elements, bf_size)
  h = (Math.log(2) * (bf_size / num_of_elements)).round
  h += 1 if h.zero?
  h
end

.optimal_m(num_of_elements, false_positive_rate = 0.01) ⇒ Object

Methods used to calculate M and K Taken from en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives



49
50
51
# File 'lib/redis/bloomfilter.rb', line 49

def self.optimal_m(num_of_elements, false_positive_rate = 0.01)
  (-1 * num_of_elements * Math.log(false_positive_rate) / (Math.log(2)**2)).round
end

.versionObject



6
7
8
# File 'lib/redis/bloomfilter/version.rb', line 6

def self.version
  "redis-bloomfilter version #{VERSION}"
end

Instance Method Details

#clearObject

It deletes a bloomfilter



70
71
72
# File 'lib/redis/bloomfilter.rb', line 70

def clear
  @driver.clear
end

#include?(key) ⇒ Boolean

It checks if a key is part of the set

Returns:

  • (Boolean)


65
66
67
# File 'lib/redis/bloomfilter.rb', line 65

def include?(key)
  @driver.include?(key)
end

#insert(data) ⇒ Object

Insert a new element



60
61
62
# File 'lib/redis/bloomfilter.rb', line 60

def insert(data)
  @driver.insert data
end