class RedisFile::SortedSetStore

Attributes

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

Public Class Methods

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

Public Instance Methods

aggregate_max(out) click to toggle source
# File lib/redis-file/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/redis-file/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/redis-file/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/redis-file/sorted_set_store.rb, line 64
def call
  ZSet.new.tap {|out| send("aggregate_#{aggregate}", out) }
end
computed_values() click to toggle source

Apply the weightings to the hashes

# File lib/redis-file/sorted_set_store.rb, line 27
def computed_values
  unless defined?(@computed_values) && @computed_values
    # Do nothing if all weights are 1, as n * 1 is n
    @computed_values = hashes if weights.all? {|weight| weight == 1 }
    # Otherwise, multiply the values in each hash by that hash's weighting
    @computed_values ||= hashes.each_with_index.map do |hash, index|
      weight = weights[index]
      Hash[hash.map {|k, v| [k, (v * weight)]}]
    end
  end
  @computed_values
end
hashes() click to toggle source
# File lib/redis-file/sorted_set_store.rb, line 12
def hashes
  @hashes ||= keys.map do |src|
    case data[src]
    when ::Set
      # Every value has a score of 1
      Hash[data[src].map {|k,v| [k, 1]}]
    when Hash
      data[src]
    else
      {}
    end
  end
end
selected_keys() click to toggle source
# File lib/redis-file/sorted_set_store.rb, line 60
def selected_keys
  raise NotImplemented, "subclass needs to implement #selected_keys"
end