Class: Redis::ZSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redis/zsets.rb

Overview

WARN Time complexity may not match C version in this module yet. TODO Should add and delete manipulate @keys instead of clearing?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZSet

Returns a new instance of ZSet.



9
10
11
12
13
# File 'lib/redis/zsets.rb', line 9

def initialize
   @hash = Hash.new
  @keys = nil
  @keys_reverse = nil
end

Class Method Details

.aggregate(database, is_and, destination, numkeys, conversions, *args) ⇒ Object



123
124
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
# File 'lib/redis/zsets.rb', line 123

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



15
16
17
18
19
20
# File 'lib/redis/zsets.rb', line 15

def add(o, s = 0.0)
  @hash[o] = s
 @keys = nil
 @keys_reverse = nil
  self
end

#delete(o) ⇒ Object



22
23
24
25
26
27
# File 'lib/redis/zsets.rb', line 22

def delete(o)
  @keys = nil
  @keys_reverse = nil
  @hash.delete(o)
  self
end

#eachObject



45
46
47
48
49
# File 'lib/redis/zsets.rb', line 45

def each
  block_given? or return enum_for(__method__)
  to_a.each { |o| yield(o) }
  self
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/redis/zsets.rb', line 41

def empty?
  @hash.empty?
end

#include?(o) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/redis/zsets.rb', line 29

def include?(o)
  @hash.include?(o)
end

#range(reverse, start, stop, conversions, withscores = false) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/redis/zsets.rb', line 67

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



76
77
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
# File 'lib/redis/zsets.rb', line 76

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



33
34
35
# File 'lib/redis/zsets.rb', line 33

def score(o)
 @hash[o]
end

#sizeObject



37
38
39
# File 'lib/redis/zsets.rb', line 37

def size
  @hash.size
end

#to_aObject



51
52
53
54
55
56
57
58
# File 'lib/redis/zsets.rb', line 51

def to_a
  unless @keys
    (@keys = @hash.to_a).sort! do |a, b|
      a.reverse <=> b.reverse
    end
  end
  @keys
end

#to_a_reverseObject



60
61
62
63
64
65
# File 'lib/redis/zsets.rb', line 60

def to_a_reverse
 unless @keys_reverse
   @keys_reverse = to_a.reverse
  end
 @keys_reverse
end