Class: HyperLogLog::Counter

Inherits:
Object
  • Object
show all
Includes:
Algorithm
Defined in:
lib/counter.rb

Instance Method Summary collapse

Methods included from Algorithm

#initialize, #intersection

Instance Method Details

#add(counter_name, value) ⇒ Object

This is the implementation of the standard HyperLogLog algorithm, storing counts in each byte of a string of length 2 ** b.



8
9
10
11
12
# File 'lib/counter.rb', line 8

def add(counter_name, value)
  hash, function_name, new_value = hash_info(value)
  existing_value = @redis.getrange(counter_name, function_name, function_name).unpack('C').first.to_i
  @redis.setrange(counter_name, function_name, new_value.chr) if new_value > existing_value
end

#count(counter_name) ⇒ Object

Estimate the cardinality of a single set



15
16
17
# File 'lib/counter.rb', line 15

def count(counter_name)
  union_helper([counter_name])
end

#union(counter_names) ⇒ Object

Estimate the cardinality of the union of several sets



20
21
22
# File 'lib/counter.rb', line 20

def union(counter_names)
  union_helper(counter_names)
end

#union_store(destination, counter_names) ⇒ Object

Store the union of several sets in destination so that it can be used as a HyperLogLog counter later.



26
27
28
# File 'lib/counter.rb', line 26

def union_store(destination, counter_names)
  @redis.set(destination, raw_union(counter_names).inject('') {|a, e| a << e.chr})
end