class Rediska::SortedSetStore

Attributes

aggregate[RW]
data[RW]
keys[RW]
weights[RW]

Public Class Methods

new(params, data) click to toggle source
# File lib/rediska/sorted_set_store.rb, line 5
def initialize(params, data)
  @data = data
  @weights = params.weights
  @aggregate = params.aggregate
  @keys = params.keys
end

Public Instance Methods

aggregate_max(out) click to toggle source
# File lib/rediska/sorted_set_store.rb, line 54
def aggregate_max(out)
  selected_keys.each do |key|
    out[key] = computed_values.map {|h| h[key] }.compact.max
  end
end
aggregate_min(out) click to toggle source
# File lib/rediska/sorted_set_store.rb, line 48
def aggregate_min(out)
  selected_keys.each do |key|
    out[key] = computed_values.map {|h| h[key] }.compact.min
  end
end
aggregate_sum(out) click to toggle source
# File lib/rediska/sorted_set_store.rb, line 40
def aggregate_sum(out)
  selected_keys.each do |key|
    out[key] = computed_values.inject(0) do |n, hash|
      n + (hash[key] || 0)
    end
  end
end
call() click to toggle source
# File lib/rediska/sorted_set_store.rb, line 64
def call
  ZSet.new.tap {|out| send("aggregate_#{aggregate}", out) }
end
computed_values() click to toggle source
# File lib/rediska/sorted_set_store.rb, line 25
def computed_values
  @computed_values ||= begin
    # Do nothing if all weights are 1, as n * 1 is n.
    if weights.all? {|weight| weight == 1 }
      values = hashes
    # Otherwise, multiply the values in each hash by that hash's weighting
    else
      values = hashes.each_with_index.map do |hash, index|
        weight = weights[index]
        Hash[hash.map {|k, v| [k, (v * weight)]}]
      end
    end
  end
end
hashes() click to toggle source
# File lib/rediska/sorted_set_store.rb, line 12
def hashes
  @hashes ||= keys.map do |src|
    case data[src]
    when ::Set
      Hash[data[src].map {|k,v| [k, 1]}]
    when Hash
      data[src]
    else
      {}
    end
  end
end
selected_keys() click to toggle source
# File lib/rediska/sorted_set_store.rb, line 60
def selected_keys
  raise NotImplemented, "subclass needs to implement #selected_keys"
end