Class: Redis::Bloomfilter
- Inherits:
-
Object
- Object
- Redis::Bloomfilter
- Defined in:
- lib/redis/bloomfilter.rb,
lib/redis/bloomfilter/version.rb
Constant Summary collapse
- VERSION =
'1.1.0'
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .optimal_k(num_of_elements, bf_size) ⇒ Object
-
.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.
- .version ⇒ Object
Instance Method Summary collapse
-
#clear ⇒ Object
It deletes a bloomfilter.
-
#include?(key) ⇒ Boolean
It checks if a key is part of the set.
-
#initialize(options = {}) ⇒ Bloomfilter
constructor
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%.
-
#insert(data) ⇒ Object
Insert a new element.
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%
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 = { size: 1000, error_rate: 0.01, key_name: 'redis-bloomfilter', hash_engine: 'md5', redis: Redis.current, driver: nil }.merge raise ArgumentError, 'options[:size] && options[:error_rate] cannot be nil' if [:error_rate].nil? || [:size].nil? # Size provided, compute hashes and bits @options[:size] = [:size] @options[:error_rate] = [:error_rate] ? [:error_rate] : @options[:error_rate] @options[:bits] = Bloomfilter.optimal_m [:size], @options[:error_rate] @options[:hashes] = Bloomfilter.optimal_k [:size], @options[:bits] @redis = @options[:redis] || Redis.current @options[:hash_engine] = [:hash_engine] if [: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
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
6 7 8 |
# File 'lib/redis/bloomfilter.rb', line 6 def driver @driver end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/redis/bloomfilter.rb', line 5 def @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 |
.version ⇒ Object
6 7 8 |
# File 'lib/redis/bloomfilter/version.rb', line 6 def self.version "redis-bloomfilter version #{VERSION}" end |
Instance Method Details
#clear ⇒ Object
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
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 |