Class: Redpear::Store::SortedSet
- Inherits:
-
Enumerable
- Object
- Base
- Enumerable
- Redpear::Store::SortedSet
- Includes:
- Enumerable
- Defined in:
- lib/redpear/store/sorted_set.rb
Constant Summary
Constants inherited from Base
Base::IS_NIL, Base::IS_ONE, Base::IS_TRUE, Base::IS_ZERO, Base::PICK_FIRST, Base::TO_INT, Base::TO_SET
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Comparator.
-
#add(member, score) ⇒ Object
(also: #[]=)
Adds a single member.
-
#all(options = {}) ⇒ Array
(also: #to_a)
All elements.
-
#at(index, options = {}) ⇒ Array
Member + score for given ‘index`.
-
#count(range) ⇒ Integer
Returns items from range.
-
#decrement(member, by = 1) ⇒ Float
The new value.
- #delete(member) ⇒ Object
- #each {|field, score| ... } ⇒ Object
-
#empty? ⇒ Boolean
True, if empty.
-
#first(count = 0) ⇒ String
Member with the lowest index.
-
#include?(member) ⇒ Boolean
(also: #member?)
True, if member is included.
-
#increment(member, by = 1) ⇒ Float
The new value.
-
#index(member) ⇒ Integer
(also: #rank)
Determines the index of a member (based on ascending scores).
-
#interstore(target, *others) ⇒ Redpear::Store::SortedSet
Store the result of an intersection in a new
target
key. -
#last(count = 0) ⇒ String
Member with the highest index.
-
#length ⇒ Integer
The number of items in the set.
-
#maximum ⇒ Float
The higest score.
-
#minimum ⇒ Float
The lowest score.
-
#rindex(member) ⇒ Integer
(also: #rrank)
Determines the reverse index of a member (based on descending scores).
-
#rselect(range, options = {}) ⇒ Array
Selects members between a score
range
. -
#rslice(range, options = {}) ⇒ Array
(also: #bottom)
Returns a slice of members between rindex
range
, with the higher index returned first. -
#score(member) ⇒ Integer
(also: #[])
Determines the score of a member.
-
#select(range, options = {}) ⇒ Array
Selects members between a score
range
. -
#slice(range, options = {}) ⇒ Array
(also: #top)
Returns a slice of members between index
range
, with the lower index returned first. -
#unionstore(target, *others) ⇒ Redpear::Store::Set
Store the result of a union in a new
target
key.
Methods inherited from Enumerable
Methods inherited from Base
#clear, #exists?, #expire, #expire_at, #expire_in, #initialize, #inspect, #purge!, temporary, #ttl, #type, #value, #watch
Constructor Details
This class inherits a constructor from Redpear::Store::Base
Instance Method Details
#==(other) ⇒ Boolean
Comparator
152 153 154 |
# File 'lib/redpear/store/sorted_set.rb', line 152 def ==(other) other.respond_to?(:to_a) && other.to_a == to_a end |
#add(member, score) ⇒ Object Also known as: []=
Adds a single member.
37 38 39 40 |
# File 'lib/redpear/store/sorted_set.rb', line 37 def add(member, score) conn.zadd key, score, member self end |
#all(options = {}) ⇒ Array Also known as: to_a
Returns all elements.
15 16 17 |
# File 'lib/redpear/store/sorted_set.rb', line 15 def all( = {}) slice(0..-1, ) end |
#at(index, options = {}) ⇒ Array
Returns member + score for given ‘index`.
161 162 163 164 165 166 167 168 |
# File 'lib/redpear/store/sorted_set.rb', line 161 def at(index, = {}) case value = slice(index..index, ) when Redis::Future value.instance_eval { @transformation = PICK_FIRST } else value.first end end |
#count(range) ⇒ Integer
Returns items from range
28 29 30 |
# File 'lib/redpear/store/sorted_set.rb', line 28 def count(range) conn.zcount key, *range_pair(range) end |
#decrement(member, by = 1) ⇒ Float
Returns the new value.
240 241 242 |
# File 'lib/redpear/store/sorted_set.rb', line 240 def decrement(member, by = 1) increment member, -by end |
#delete(member) ⇒ Object
69 70 71 |
# File 'lib/redpear/store/sorted_set.rb', line 69 def delete(member) conn.zrem key, member end |
#each {|field, score| ... } ⇒ Object
7 8 9 |
# File 'lib/redpear/store/sorted_set.rb', line 7 def each(&block) all.each(&block) end |
#empty? ⇒ Boolean
Returns true, if empty.
85 86 87 88 89 90 91 92 |
# File 'lib/redpear/store/sorted_set.rb', line 85 def empty? case value = length when Redis::Future value.instance_eval { @transformation = IS_ZERO } else value.zero? end end |
#first(count = 0) ⇒ String
Returns member with the lowest index.
171 172 173 174 175 176 177 |
# File 'lib/redpear/store/sorted_set.rb', line 171 def first(count = 0) if count > 0 slice(0..(count-1), :with_scores => false) else at(0, :with_scores => false) end end |
#include?(member) ⇒ Boolean Also known as: member?
Returns true, if member is included.
74 75 76 77 78 79 80 81 |
# File 'lib/redpear/store/sorted_set.rb', line 74 def include?(member) case value = conn.zscore(key, member) when Redis::Future value.instance_eval { @transformation = IS_TRUE } else !!value end end |
#increment(member, by = 1) ⇒ Float
Returns the new value.
231 232 233 |
# File 'lib/redpear/store/sorted_set.rb', line 231 def increment(member, by = 1) conn.zincrby(key, by, member) end |
#index(member) ⇒ Integer Also known as: rank
Determines the index of a member (based on ascending scores)
54 55 56 |
# File 'lib/redpear/store/sorted_set.rb', line 54 def index(member) conn.zrank(key, member) end |
#interstore(target, *others) ⇒ Redpear::Store::SortedSet
Store the result of an intersection in a new target
key
220 221 222 223 224 |
# File 'lib/redpear/store/sorted_set.rb', line 220 def interstore(target, *others) opts = others.last.is_a?(Hash) ? others.pop : {} conn.zinterstore target.to_s, [key] + others.map(&:to_s), opts self.class.new target.to_s, conn end |
#last(count = 0) ⇒ String
Returns member with the highest index.
180 181 182 183 184 185 186 |
# File 'lib/redpear/store/sorted_set.rb', line 180 def last(count = 0) if count > 0 slice(-count..-1, :with_scores => false) else at(-1, :with_scores => false) end end |
#length ⇒ Integer
Returns the number of items in the set.
21 22 23 |
# File 'lib/redpear/store/sorted_set.rb', line 21 def length conn.zcard key end |
#maximum ⇒ Float
Returns the higest score.
194 195 196 |
# File 'lib/redpear/store/sorted_set.rb', line 194 def maximum _, score = at(-1); score end |
#minimum ⇒ Float
Returns the lowest score.
189 190 191 |
# File 'lib/redpear/store/sorted_set.rb', line 189 def minimum _, score = at(0); score end |
#rindex(member) ⇒ Integer Also known as: rrank
Determines the reverse index of a member (based on descending scores)
62 63 64 |
# File 'lib/redpear/store/sorted_set.rb', line 62 def rindex(member) conn.zrevrank(key, member) end |
#rselect(range) ⇒ Array #rselect(range) ⇒ Array
Selects members between a score range
. Higher scores returned first
145 146 147 148 |
# File 'lib/redpear/store/sorted_set.rb', line 145 def rselect(range, = {}) finish, start = range_pair(range, true) fetch_range :zrevrangebyscore, finish, start, end |
#rslice(range, options = {}) ⇒ Array Also known as: bottom
Returns a slice of members between rindex range
, with the higher index returned first
112 113 114 115 |
# File 'lib/redpear/store/sorted_set.rb', line 112 def rslice(range, = {}) start, finish = range_pair(range) fetch_range :zrevrange, start, finish, end |
#score(member) ⇒ Integer Also known as: []
Determines the score of a member
46 47 48 |
# File 'lib/redpear/store/sorted_set.rb', line 46 def score(member) conn.zscore(key, member) end |
#select(range) ⇒ Array #select(range) ⇒ Array
Selects members between a score range
. Lower scores returned first
129 130 131 132 |
# File 'lib/redpear/store/sorted_set.rb', line 129 def select(range, = {}) start, finish = range_pair(range) fetch_range :zrangebyscore, start, finish, end |
#slice(range, options = {}) ⇒ Array Also known as: top
Returns a slice of members between index range
, with the lower index returned first
100 101 102 103 |
# File 'lib/redpear/store/sorted_set.rb', line 100 def slice(range, = {}) start, finish = range_pair(range) fetch_range :zrange, start, finish, end |
#unionstore(target, *others) ⇒ Redpear::Store::Set
Store the result of a union in a new target
key
206 207 208 209 210 |
# File 'lib/redpear/store/sorted_set.rb', line 206 def unionstore(target, *others) opts = others.last.is_a?(Hash) ? others.pop : {} conn.zunionstore target.to_s, [key] + others.map(&:to_s), opts self.class.new target.to_s, conn end |