class Redis::BloomfilterDriver::Ruby

Attributes

redis[RW]

Faster Ruby version. This driver should be used if Redis version < 2.6

Public Class Methods

new(options = {}) click to toggle source
# File lib/bloomfilter_driver/ruby.rb, line 10
def initialize(options = {})
  @options = options
end

Public Instance Methods

clear() click to toggle source

It deletes a bloomfilter

# File lib/bloomfilter_driver/ruby.rb, line 33
def clear
  @redis.del @options[:key_name]
end
include?(key) click to toggle source

It checks if a key is part of the set

# File lib/bloomfilter_driver/ruby.rb, line 20
def include?(key)
  indexes = []
  indexes_for(key).each { |idx| indexes << idx }
  return false if @redis.getbit(@options[:key_name], indexes.shift).zero?

  result = @redis.pipelined do
    indexes.each { |idx| @redis.getbit(@options[:key_name], idx) }
  end

  !result.include?(0)
end
insert(data) click to toggle source

Insert a new element

# File lib/bloomfilter_driver/ruby.rb, line 15
def insert(data)
  set data
end

Protected Instance Methods

indexes_for(data) click to toggle source

Hashing strategy: www.eecs.harvard.edu/~michaelm/postscripts/tr-02-05.pdf

# File lib/bloomfilter_driver/ruby.rb, line 41
def indexes_for(data)
  sha = Digest::SHA1.hexdigest(data.to_s)
  h = []
  h[0] = sha[0...8].to_i(16)
  h[1] = sha[8...16].to_i(16)
  h[2] = sha[16...24].to_i(16)
  h[3] = sha[24...32].to_i(16)
  idxs = []

  (@options[:hashes]).times do |i|
    v = (h[i % 2] + i * h[2 + (((i + (i % 2)) % 4) / 2)]) % @options[:bits]
    idxs << v
  end
  idxs
end
set(key) click to toggle source
# File lib/bloomfilter_driver/ruby.rb, line 57
def set(key)
  @redis.pipelined do
    indexes_for(key).each { |i| @redis.setbit @options[:key_name], i, 1 }
  end
end