fakeredis 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -3
- data/fakeredis.gemspec +4 -3
- data/lib/fakeredis/version.rb +1 -1
- data/lib/redis/connection/memory.rb +119 -87
- data/spec/compatibility_spec.rb +2 -2
- data/spec/connection_spec.rb +3 -13
- data/spec/hashes_spec.rb +33 -44
- data/spec/keys_spec.rb +27 -40
- data/spec/lists_spec.rb +27 -42
- data/spec/server_spec.rb +2 -39
- data/spec/sets_spec.rb +16 -22
- data/spec/sorted_sets_spec.rb +58 -229
- data/spec/spec_helper.rb +0 -4
- data/spec/strings_spec.rb +45 -92
- metadata +9 -16
- data/lib/fake_redis.rb +0 -1
- data/lib/fakeredis/expiring_hash.rb +0 -56
- data/lib/fakeredis/sorted_set_argument_handler.rb +0 -74
- data/lib/fakeredis/sorted_set_store.rb +0 -80
- data/lib/fakeredis/zset.rb +0 -4
- data/spec/spec_helper_live_redis.rb +0 -14
@@ -1,80 +0,0 @@
|
|
1
|
-
module FakeRedis
|
2
|
-
class SortedSetStore
|
3
|
-
attr_accessor :data, :weights, :aggregate, :keys
|
4
|
-
|
5
|
-
def initialize params, data
|
6
|
-
self.data = data
|
7
|
-
self.weights = params.weights
|
8
|
-
self.aggregate = params.aggregate
|
9
|
-
self.keys = params.keys
|
10
|
-
end
|
11
|
-
|
12
|
-
def hashes
|
13
|
-
@hashes ||= keys.map do |src|
|
14
|
-
case data[src]
|
15
|
-
when ::Set
|
16
|
-
# Every value has a score of 1
|
17
|
-
Hash[data[src].map {|k,v| [k, 1]}]
|
18
|
-
when Hash
|
19
|
-
data[src]
|
20
|
-
else
|
21
|
-
{}
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Apply the weightings to the hashes
|
27
|
-
def computed_values
|
28
|
-
unless defined?(@computed_values) && @computed_values
|
29
|
-
# Do nothing if all weights are 1, as n * 1 is n
|
30
|
-
@computed_values = hashes if weights.all? {|weight| weight == 1 }
|
31
|
-
# Otherwise, multiply the values in each hash by that hash's weighting
|
32
|
-
@computed_values ||= hashes.each_with_index.map do |hash, index|
|
33
|
-
weight = weights[index]
|
34
|
-
Hash[hash.map {|k, v| [k, (v * weight)]}]
|
35
|
-
end
|
36
|
-
end
|
37
|
-
@computed_values
|
38
|
-
end
|
39
|
-
|
40
|
-
def aggregate_sum out
|
41
|
-
selected_keys.each do |key|
|
42
|
-
out[key] = computed_values.inject(0) do |n, hash|
|
43
|
-
n + (hash[key] || 0)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def aggregate_min out
|
49
|
-
selected_keys.each do |key|
|
50
|
-
out[key] = computed_values.map {|h| h[key] }.compact.min
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def aggregate_max out
|
55
|
-
selected_keys.each do |key|
|
56
|
-
out[key] = computed_values.map {|h| h[key] }.compact.max
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def selected_keys
|
61
|
-
raise NotImplemented, "subclass needs to implement #selected_keys"
|
62
|
-
end
|
63
|
-
|
64
|
-
def call
|
65
|
-
ZSet.new.tap {|out| send("aggregate_#{aggregate}", out) }
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
class SortedSetIntersectStore < SortedSetStore
|
70
|
-
def selected_keys
|
71
|
-
@values ||= hashes.inject([]) { |r, h| r.empty? ? h.keys : (r & h.keys) }
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
class SortedSetUnionStore < SortedSetStore
|
76
|
-
def selected_keys
|
77
|
-
@values ||= hashes.map(&:keys).flatten.uniq
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
data/lib/fakeredis/zset.rb
DELETED