Class: FakeRedis::SortedSetStore

Inherits:
Object
  • Object
show all
Defined in:
lib/fakeredis/sorted_set_store.rb

Direct Known Subclasses

SortedSetIntersectStore, SortedSetUnionStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, data) ⇒ SortedSetStore

Returns a new instance of SortedSetStore.


5
6
7
8
9
10
# File 'lib/fakeredis/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

Instance Attribute Details

#aggregateObject

Returns the value of attribute aggregate


3
4
5
# File 'lib/fakeredis/sorted_set_store.rb', line 3

def aggregate
  @aggregate
end

#dataObject

Returns the value of attribute data


3
4
5
# File 'lib/fakeredis/sorted_set_store.rb', line 3

def data
  @data
end

#keysObject

Returns the value of attribute keys


3
4
5
# File 'lib/fakeredis/sorted_set_store.rb', line 3

def keys
  @keys
end

#weightsObject

Returns the value of attribute weights


3
4
5
# File 'lib/fakeredis/sorted_set_store.rb', line 3

def weights
  @weights
end

Instance Method Details

#aggregate_max(out) ⇒ Object


54
55
56
57
58
# File 'lib/fakeredis/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) ⇒ Object


48
49
50
51
52
# File 'lib/fakeredis/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) ⇒ Object


40
41
42
43
44
45
46
# File 'lib/fakeredis/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

#callObject


64
65
66
# File 'lib/fakeredis/sorted_set_store.rb', line 64

def call
  ZSet.new.tap {|out| send("aggregate_#{aggregate}", out) }
end

#computed_valuesObject

Apply the weightings to the hashes


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fakeredis/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

#hashesObject


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fakeredis/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_keysObject

Raises:

  • (NotImplemented)

60
61
62
# File 'lib/fakeredis/sorted_set_store.rb', line 60

def selected_keys
  raise NotImplemented, "subclass needs to implement #selected_keys"
end