class LambdaStoreHLL::Counter

Constants

ADD_SCRIPT
ADD_SCRIPT_MIN
COUNT_SCRIPT
COUNT_SCRIPT_MIN

Public Class Methods

new(redis, b: 10) click to toggle source
# File lib/lambda-store-hll/counter.rb, line 6
def initialize(redis, b: 10)
    raise "Accuracy not supported. Please choose a value of b between 4 and 16" if b < 4 || b > 16
    @b = b
    @m = 2 ** b
    @redis = redis
    @hash_len = 32 - b
    @alpha = 0.7213/(1 + 1.079/@m)
end

Public Instance Methods

add(k, v) click to toggle source

do the hashing on this end to save excessive amounts of script being sent

# File lib/lambda-store-hll/counter.rb, line 68
def add(k, v)
    #hash the string
    hash = Mmh3.hash32(v)
    #remove negatives but keep info
    hash = ((hash.abs << 1) | (hash.negative? ? 1 : 0))

    #get the 10 bit bucket id
    int_bucket = (hash & (@m - 1))

    @redis.eval(ADD_SCRIPT_MIN, :keys => [k, int_bucket], :argv => [rho(hash / @m).chr, @m])
end
count(k) click to toggle source
# File lib/lambda-store-hll/counter.rb, line 80
def count(k)
    @redis.eval(COUNT_SCRIPT_MIN, :keys => [k], :argv => [@m, @alpha])
end
del(k) click to toggle source
# File lib/lambda-store-hll/counter.rb, line 84
def del(k)
    @redis.del(k)
end

Private Instance Methods

rho(i) click to toggle source
# File lib/lambda-store-hll/counter.rb, line 90
def rho(i)
    return @hash_len + 1 if i == 0
    @hash_len - Math.log(i, 2).floor
end