Class: Redis::ZSets::ZSet
- Inherits:
-
Object
- Object
- Redis::ZSets::ZSet
- Includes:
- Enumerable
- Defined in:
- lib/redis/zsets.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(o, s = 0.0) ⇒ Object
- #delete(o) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #include?(o) ⇒ Boolean
-
#initialize ⇒ ZSet
constructor
A new instance of ZSet.
- #range(reverse, start, stop, conversions, withscores = false) ⇒ Object
- #range_by_score(reverse, min, max, conversions, *args) ⇒ Object
- #score(o) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_a_reverse ⇒ Object
Constructor Details
#initialize ⇒ ZSet
Returns a new instance of ZSet.
11 12 13 14 15 |
# File 'lib/redis/zsets.rb', line 11 def initialize @hash = Hash.new @keys = nil @keys_reverse = nil end |
Class Method Details
.aggregate(database, is_and, destination, numkeys, conversions, *args) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/redis/zsets.rb', line 125 def self.aggregate database, is_and, destination, numkeys, conversions, *args numkeys = numkeys.to_i aggregate = 'SUM' keys = [] keys << args.shift while (numkeys -= 1) >= 0 weights = Array.new keys.size, 1 until args.empty? case args.shift.upcase when 'WEIGHTS' weights = [] keys.size.times {weights << conversions.redis_f(args.shift, 'weight value is not a double')} when 'AGGREGATE' aggregate = args.shift.upcase else raise 'bad arguments' end end results = [] keys.zip(weights) do |key, weight| inner_result = ZSet.new record = database[key] || ZSet.new record.each do |member, score| inner_result.add member, (score||1) * weight end results << inner_result end result = results.reduce do |memo, result| n = is_and ? new : memo result.each do |member, score| next if is_and and !memo.include?(member) test = [score, memo.score(member)].compact text << 0 if test.empty? case aggregate when 'SUM' score = test.reduce :+ when 'MIN' score = test.min when 'MAX' score = test.max else raise 'bad arguments' end n.add member, score end n end database[destination] = result unless result.empty? result.size end |
Instance Method Details
#add(o, s = 0.0) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/redis/zsets.rb', line 17 def add(o, s = 0.0) @hash[o] = s @keys = nil @keys_reverse = nil self end |
#delete(o) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/redis/zsets.rb', line 24 def delete(o) @keys = nil @keys_reverse = nil @hash.delete(o) self end |
#each ⇒ Object
47 48 49 50 51 |
# File 'lib/redis/zsets.rb', line 47 def each block_given? or return enum_for(__method__) to_a.each { |o| yield(o) } self end |
#empty? ⇒ Boolean
43 44 45 |
# File 'lib/redis/zsets.rb', line 43 def empty? @hash.empty? end |
#include?(o) ⇒ Boolean
31 32 33 |
# File 'lib/redis/zsets.rb', line 31 def include?(o) @hash.include?(o) end |
#range(reverse, start, stop, conversions, withscores = false) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/redis/zsets.rb', line 69 def range reverse, start ,stop, conversions, withscores = false start = conversions.redis_i start stop = conversions.redis_i stop array = reverse ? to_a_reverse : to_a start = 0 if start < -size return array[start..stop].flatten(1) if withscores (array[start..stop]||[]).collect{|i|i.first} end |
#range_by_score(reverse, min, max, conversions, *args) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/redis/zsets.rb', line 78 def range_by_score reverse, min, max, conversions, *args withscores = offset = count = nil until args.empty? case args.shift.upcase when 'LIMIT' offset = args.shift.to_i count = args.shift.to_i when 'WITHSCORES' withscores = true else raise 'bad arguments' end end result = [] min_exclusive = false if min[0..0] == '(' min_exclusive = true min = min[1..-1] end min = conversions.redis_f min max_exclusive = false if max[0..0] == '(' max_exclusive = true max = max[1..-1] end max = conversions.redis_f max if reverse x = min; min = max; max = x end (reverse ? to_a_reverse : to_a).each do |member, score| next if min > score or (min_exclusive and min >= score) next if max < score or (max_exclusive and max <= score) if offset offset -= 1 next unless offset < 0 offset = nil end result << member result << score if withscores if count count -= 1 break if count == 0 end end result end |
#score(o) ⇒ Object
35 36 37 |
# File 'lib/redis/zsets.rb', line 35 def score(o) @hash[o] end |
#size ⇒ Object
39 40 41 |
# File 'lib/redis/zsets.rb', line 39 def size @hash.size end |
#to_a ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/redis/zsets.rb', line 53 def to_a unless @keys (@keys = @hash.to_a).sort! do |a, b| a.reverse <=> b.reverse end end @keys end |
#to_a_reverse ⇒ Object
62 63 64 65 66 67 |
# File 'lib/redis/zsets.rb', line 62 def to_a_reverse unless @keys_reverse @keys_reverse = to_a.reverse end @keys_reverse end |