Class: Redpear::Store::SortedSet

Inherits:
Enumerable show all
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

#conn, #key

Instance Method Summary collapse

Methods inherited from Enumerable

#size

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

Returns:

  • (Boolean)

    true if contains same members as other



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.

Parameters:

  • member (String)

    A member to add

  • score (Integer)

    The score



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.

Parameters:

  • options (Hash) (defaults to: {})
  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (Array)

    all elements



15
16
17
# File 'lib/redpear/store/sorted_set.rb', line 15

def all(options = {})
  slice(0..-1, options)
end

#at(index, options = {}) ⇒ Array

Returns member + score for given ‘index`.

Parameters:

  • index (Integer)
  • options (Hash) (defaults to: {})
  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (Array)

    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, options = {})
  case value = slice(index..index, options)
  when Redis::Future
    value.instance_eval { @transformation = PICK_FIRST }
  else
    value.first
  end
end

#count(range) ⇒ Integer

Returns items from range

Parameters:

  • range (Range)

Returns:

  • (Integer)

    the number of items within given score ‘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.

Parameters:

  • member (String)

    The member to decrement

  • by (Integer) (defaults to: 1)

    The decrement, defaults to 1

Returns:

  • (Float)

    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

Parameters:

  • member (String)

    The ‘member` to delete



69
70
71
# File 'lib/redpear/store/sorted_set.rb', line 69

def delete(member)
  conn.zrem key, member
end

#each {|field, score| ... } ⇒ Object

Yields:

  • over a field-score pair

Yield Parameters:

  • field (String)
  • score (String)


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.

Returns:

  • (Boolean)

    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.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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.

Parameters:

  • member (String)

    The member to increment

  • by (Integer) (defaults to: 1)

    The increment, defaults to 1

Returns:

  • (Float)

    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)

Parameters:

  • member (String)

Returns:

  • (Integer)

    the index for the given ‘member`



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

Parameters:

  • target (String)

    The target key

  • others (multiple)

    The other sets & options

  • [Array] (Hash)

    a customizable set of options

  • [Symbol] (Hash)

    a customizable set of options

Returns:



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.

Returns:

  • (String)

    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

#lengthInteger

Returns the number of items in the set.

Returns:

  • (Integer)

    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

#maximumFloat

Returns the higest score.

Returns:

  • (Float)

    the higest score



194
195
196
# File 'lib/redpear/store/sorted_set.rb', line 194

def maximum
  _, score = at(-1); score
end

#minimumFloat

Returns the lowest score.

Returns:

  • (Float)

    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)

Parameters:

  • member (String)

Returns:

  • (Integer)

    the index for the given ‘member`



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

Overloads:

  • #rselect(range) ⇒ Array

    Parameters:

    • range (Range)

      The score range of the elements

  • #rselect(range) ⇒ Array

    Parameters:

    • range (Array)

      as Array, e.g. [“-inf”, “+inf”]

Parameters:

  • options (Hash) (defaults to: {})
  • [Boolean] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options

Returns:

  • (Array)

    the members



145
146
147
148
# File 'lib/redpear/store/sorted_set.rb', line 145

def rselect(range, options = {})
  finish, start = range_pair(range, true)
  fetch_range :zrevrangebyscore, finish, start, options
end

#rslice(range, options = {}) ⇒ Array Also known as: bottom

Returns a slice of members between rindex range, with the higher index returned first

Parameters:

  • range (Range)

    The rindex range of the elements

  • options (Hash) (defaults to: {})
  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (Array)

    the members



112
113
114
115
# File 'lib/redpear/store/sorted_set.rb', line 112

def rslice(range, options = {})
  start, finish = range_pair(range)
  fetch_range :zrevrange, start, finish, options
end

#score(member) ⇒ Integer Also known as: []

Determines the score of a member

Parameters:

  • member (String)

Returns:

  • (Integer)

    the score for the given ‘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

Overloads:

  • #select(range) ⇒ Array

    Parameters:

    • range (Range)

      The score range of the elements

  • #select(range) ⇒ Array

    Parameters:

    • range (Array)

      as Array, e.g. [“-inf”, “+inf”]

Parameters:

  • options (Hash) (defaults to: {})
  • [Boolean] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options

Returns:

  • (Array)

    the members



129
130
131
132
# File 'lib/redpear/store/sorted_set.rb', line 129

def select(range, options = {})
  start, finish = range_pair(range)
  fetch_range :zrangebyscore, start, finish, options
end

#slice(range, options = {}) ⇒ Array Also known as: top

Returns a slice of members between index range, with the lower index returned first

Parameters:

  • range (Range)
  • options (Hash) (defaults to: {})
  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (Array)

    the members



100
101
102
103
# File 'lib/redpear/store/sorted_set.rb', line 100

def slice(range, options = {})
  start, finish = range_pair(range)
  fetch_range :zrange, start, finish, options
end

#unionstore(target, *others) ⇒ Redpear::Store::Set

Store the result of a union in a new target key

Parameters:

  • target (Redpear::Store::Set)

    The target set

  • others (multiple)

    The other sets & options

  • [Array] (Hash)

    a customizable set of options

  • [Symbol] (Hash)

    a customizable set of options

Returns:



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