Class: Redis::Bloomfilter
- Inherits:
-
Object
- Object
- Redis::Bloomfilter
- Defined in:
- lib/redis/bloomfilter.rb,
lib/redis/bloomfilter/version.rb
Constant Summary collapse
- VERSION =
"0.0.3"
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.
- #remove(key) ⇒ Object
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%
9 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 |
# File 'lib/redis/bloomfilter.rb', line 9 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'] if Gem::Version.new(ver) >= Gem::Version.new('2.6.0') @options[:driver] = 'lua' else @options[:driver] = '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.
5 6 7 |
# File 'lib/redis/bloomfilter.rb', line 5 def driver @driver end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/redis/bloomfilter.rb', line 4 def @options end |
Class Method Details
.optimal_k(num_of_elements, bf_size) ⇒ Object
52 53 54 55 56 |
# File 'lib/redis/bloomfilter.rb', line 52 def self.optimal_k num_of_elements, bf_size h = (Math.log(2) * (bf_size / num_of_elements)).round h+=1 if h == 0 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
48 49 50 |
# File 'lib/redis/bloomfilter.rb', line 48 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
4 5 6 |
# File 'lib/redis/bloomfilter/version.rb', line 4 def self.version "redis-bloomfilter version #{VERSION}" end |
Instance Method Details
#clear ⇒ Object
It deletes a bloomfilter
73 74 75 |
# File 'lib/redis/bloomfilter.rb', line 73 def clear @driver.clear end |
#include?(key) ⇒ Boolean
It checks if a key is part of the set
64 65 66 |
# File 'lib/redis/bloomfilter.rb', line 64 def include?(key) @driver.include?(key) end |
#insert(data) ⇒ Object
Insert a new element
59 60 61 |
# File 'lib/redis/bloomfilter.rb', line 59 def insert(data) @driver.insert data end |
#remove(key) ⇒ Object
68 69 70 |
# File 'lib/redis/bloomfilter.rb', line 68 def remove(key) @driver.remove key if @driver.respond_to? :remove end |