Module: Oxblood::Commands::SortedSets

Included in:
Oxblood::Commands
Defined in:
lib/oxblood/commands/sorted_sets.rb

Instance Method Summary collapse

Instance Method Details

#zadd(key, *args, **opts) ⇒ Integer

Add one or more members to a sorted set, or update its score if it already exists.

Parameters:

  • key (String)

    under which store set

  • args ([Float, String], Array<[Float, String]>)

    scores and members

  • opts (Hash)

Options Hash (**opts):

  • :xx (Boolean)

    Only update elements that already exist. Never add elements.

  • :nx (Boolean)

    Don't update already existing elements. Always add new elements.

  • :ch (Boolean)

    Modify the return value from the number of new elements added, to the total number of elements changed.

  • :incr (Boolean)

    When this option is specified ZADD acts like ZINCRBY. Only one score-element pair can be specified in this mode.

Returns:

  • (Integer)

    The number of elements added to the sorted sets, not including elements already existing for which the score was updated

See Also:


28
29
30
31
32
33
34
35
# File 'lib/oxblood/commands/sorted_sets.rb', line 28

def zadd(key, *args, **opts)
  args.unshift(:XX) if opts[:xx]
  args.unshift(:NX) if opts[:nx]
  args.unshift(:CH) if opts[:ch]
  args.unshift(:INCR) if opts[:incr]

  run(*args.unshift(:ZADD, key))
end

#zcard(key) ⇒ Integer

Get the number of members in a sorted set

Parameters:

  • key (String)

Returns:

  • (Integer)

    the cardinality (number of elements) of the sorted set, or 0 if key does not exists

See Also:


44
45
46
# File 'lib/oxblood/commands/sorted_sets.rb', line 44

def zcard(key)
  run(:ZCARD, key)
end

#zcount(key, min, max) ⇒ Integer

Count the members in a sorted set with scores within the given values

Parameters:

  • key (String)
  • min (String)
  • max (String)

Returns:

  • (Integer)

    the number of elements in the specified score range

See Also:


56
57
58
# File 'lib/oxblood/commands/sorted_sets.rb', line 56

def zcount(key, min, max)
  run(:ZCOUNT, key, min, max)
end

#zincrby(key, increment, member) ⇒ String

Increment the score of a member in a sorted set

Parameters:

  • key (String)
  • increment (Float)
  • member (String)

Returns:

  • (String)

    the new score of member (a double precision floating point number), represented as string

See Also:


69
70
71
# File 'lib/oxblood/commands/sorted_sets.rb', line 69

def zincrby(key, increment, member)
  run(:ZINCRBY, key, increment, member)
end

#zinterstore(destination, numkeys, *keys, **opts) ⇒ Integer

Intersect multiple sorted sets and store the resulting sorted set in

a new key.

Parameters:

  • destination (String)

    key

  • numkeys (Integer)

    number of sorted sets

  • keys (String, Array<String>)
  • opts (Hash)

Options Hash (**opts):

  • :weights (Array<Float>)

    multiplication factor for each input sorted set.

  • :aggregate (Symbol)

    how the results of the union are aggregated.

Returns:

  • (Integer)

    the number of elements in the resulting sorted set at destination.

See Also:


375
376
377
# File 'lib/oxblood/commands/sorted_sets.rb', line 375

def zinterstore(destination, numkeys, *keys, **opts)
  common_store(:ZINTERSTORE, destination, numkeys, keys, opts)
end

#zlexcount(key, min, max) ⇒ Object

Count the number of members in a sorted set between a given lexicographical range

Parameters:

  • key (String)
  • min (String)
  • max (String)

Returns:

  • the number of elements in the specified score range

See Also:


127
128
129
# File 'lib/oxblood/commands/sorted_sets.rb', line 127

def zlexcount(key, min, max)
  run(:ZLEXCOUNT, key, min, max)
end

#zrange(key, start, stop, opts = {}) ⇒ Array

Return a range of members in a sorted set, by index

Examples:

session.zrange('myzset', 0, -1)
# => ['one', 'two']
session.zrange('myzset', 0, -1, withscores: true)
# => [['one', '1'], ['two', '2']]

Parameters:

  • key (String)
  • start (Integer)

    index

  • stop (Integer)

    index

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :withscores (Boolean) — default: false

    Return the scores of the elements together with the elements

Returns:

  • (Array)

    list of elements in the specified range (optionally with their scores, in case the :withscores option is given)

See Also:


152
153
154
# File 'lib/oxblood/commands/sorted_sets.rb', line 152

def zrange(key, start, stop, opts = {})
  common_range(:ZRANGE, key, start, stop, opts)
end

#zrangebylex(key, min, max, opts = {}) ⇒ Array<String>

Return a range of members in a sorted set, by lexicographical range.

Parameters:

  • key (String)
  • min (String)
  • max (String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :limit (Array<Integer, Integer>)

    Get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL)

Returns:

  • (Array<String>)

    list of elements in the specified score range.

See Also:


85
86
87
# File 'lib/oxblood/commands/sorted_sets.rb', line 85

def zrangebylex(key, min, max, opts = {})
  common_rangebylex(:ZRANGEBYLEX, key, min, max, opts)
end

#zrangebyscore(key, min, max, opts = {}) ⇒ Array

Return a range of members in a sorted set, by score

Examples:

session.zrangebyscore('myzset', '-inf', '+inf')
# => ['one', 'two', 'three']
session.zrangebyscore('myzset', '(1', 2, withscores: true)
# => [['two', '2']]
opts = { withscores: true, limit: [1, 1] }
session.zrangebyscore('myzset', '-inf', '+inf', opts)
# => [['two', '2']]

Parameters:

  • key (String)

    under which set is stored

  • min (String)

    score

  • max (String)

    score

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :withscores (Boolean) — default: false

    Return the scores of the elements together with the elements

  • :limit (Array<Integer, Integer>)

    Get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL)

Returns:

  • (Array)

    list of elements in the specified score range (optionally with their scores, in case the :withscores option is given)

See Also:


184
185
186
# File 'lib/oxblood/commands/sorted_sets.rb', line 184

def zrangebyscore(key, min, max, opts = {})
  common_rangebyscore(:ZRANGEBYSCORE, key, min, max, opts)
end

#zrank(key, member) ⇒ Integer?

Determine the index of a member in a sorted set in the sorted set or key does not exist

Parameters:

  • key (String)
  • member (String)

Returns:

  • (Integer, nil)

    the rank of member or nil if member does not exist

See Also:


196
197
198
# File 'lib/oxblood/commands/sorted_sets.rb', line 196

def zrank(key, member)
  run(:ZRANK, key, member)
end

#zrem(key, *members) ⇒ Integer, RError

Remove one or more members from a sorted set

Parameters:

  • key (String)
  • members (Array<String>)

    to delete

Returns:

  • (Integer)

    number of deleted members

  • (RError)

    when key exists and does not hold a sorted set.

See Also:


208
209
210
# File 'lib/oxblood/commands/sorted_sets.rb', line 208

def zrem(key, *members)
  run(*members.unshift(:ZREM, key))
end

#zremrangebylex(key, min, max) ⇒ Integer

Remove all members in a sorted set between the given lexicographical range.

Parameters:

  • key (String)
  • min (String)
  • max (String)

Returns:

  • (Integer)

    the number of elements removed.

See Also:


97
98
99
# File 'lib/oxblood/commands/sorted_sets.rb', line 97

def zremrangebylex(key, min, max)
  run(:ZREMRANGEBYLEX, key, min, max)
end

#zremrangebyrank(key, start, stop) ⇒ Integer

Remove all members in a sorted set within the given indexes

Parameters:

  • key (String)
  • start (String)
  • stop (String)

Returns:

  • (Integer)

    the number of elements removed

See Also:


220
221
222
# File 'lib/oxblood/commands/sorted_sets.rb', line 220

def zremrangebyrank(key, start, stop)
  run(:ZREMRANGEBYRANK, key, start, stop)
end

#zremrangebyscore(key, min, max) ⇒ Integer

Remove all members in a sorted set within the given scores

Parameters:

  • key (String)
  • min (String)

    score

  • max (String)

    score

Returns:

  • (Integer)

    the number of elements removed

See Also:


232
233
234
# File 'lib/oxblood/commands/sorted_sets.rb', line 232

def zremrangebyscore(key, min, max)
  run(:ZREMRANGEBYSCORE, key, min, max)
end

#zrevrange(key, start, stop, opts = {}) ⇒ Array

Return a range of members in a sorted set, by index, with scores ordered from high to low

Examples:

session.zrevrange('myzset', 0, -1)
# => ['two', 'one']
session.zrevrange('myzset', 0, -1, withscores: true)
# => [['two', '2'], ['one', '1']]

Parameters:

  • key (String)
  • start (Integer)

    index

  • stop (Integer)

    index

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :withscores (Boolean) — default: false

    Return the scores of the elements together with the elements

Returns:

  • (Array)

    list of elements in the specified range (optionally with their scores, in case the :withscores option is given)

See Also:


258
259
260
# File 'lib/oxblood/commands/sorted_sets.rb', line 258

def zrevrange(key, start, stop, opts = {})
  common_range(:ZREVRANGE, key, start, stop, opts)
end

#zrevrangebylex(key, min, max, opts = {}) ⇒ Array<String>

Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.

Parameters:

  • key (String)
  • min (String)
  • max (String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :limit (Array<Integer, Integer>)

    Get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL)

Returns:

  • (Array<String>)

    list of elements in the specified score range.

See Also:


114
115
116
# File 'lib/oxblood/commands/sorted_sets.rb', line 114

def zrevrangebylex(key, min, max, opts = {})
  common_rangebylex(:ZREVRANGEBYLEX, key, min, max, opts)
end

#zrevrangebyscore(key, min, max, opts = {}) ⇒ Array

Return a range of members in a sorted set, by score, with scores ordered from high to low

Examples:

session.zrevrangebyscore('myzset', '+inf', '-inf')
# => ['three', 'two', 'one']
session.zrevrangebyscore('myzset', 2, '(1', withscores: true)
# => [['two', '2']]
opts = { withscores: true, limit: [1, 1] }
session.zrevrangebyscore('myzset', '+inf', '-inf', opts)
# => [['two', '2']]

Parameters:

  • key (String)

    under which set is stored

  • min (String)

    score

  • max (String)

    score

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :withscores (Boolean) — default: false

    Return the scores of the elements together with the elements

  • :limit (Array<Integer, Integer>)

    Get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL)

Returns:

  • (Array)

    list of elements in the specified score range (optionally with their scores, in case the :withscores option is given)

See Also:


291
292
293
# File 'lib/oxblood/commands/sorted_sets.rb', line 291

def zrevrangebyscore(key, min, max, opts = {})
  common_rangebyscore(:ZREVRANGEBYSCORE, key, min, max, opts)
end

#zrevrank(key, member) ⇒ Integer?

Determine the index of a member in a sorted set, with scores ordered from high to low exists in the sorted set or key does not exists

Parameters:

  • key (String)
  • member (String)

Returns:

  • (Integer, nil)

    the rank of member, or nil if member does not

See Also:


304
305
306
# File 'lib/oxblood/commands/sorted_sets.rb', line 304

def zrevrank(key, member)
  run(:ZREVRANK, key, member)
end

#zscan(key, cursor, opts = {}) ⇒ Array

Incrementally iterate sorted sets elements and associated scores

Parameters:

  • cursor (Integer)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :count (Integer)

    Amount of work that should be done at every call in order to retrieve elements from the collection.

  • :match (String)

Returns:

  • (Array)

    two elements array, where the first element is String representing an unsigned 64 bit number (the cursor), and the second element is an Array of elements.

See Also:


334
335
336
337
338
# File 'lib/oxblood/commands/sorted_sets.rb', line 334

def zscan(key, cursor, opts = {})
  args = [:ZSCAN, key, cursor]
  Scan.merge_opts!(args, opts)
  run(*args)
end

#zscore(key, member) ⇒ String?

Get the score associated with the given member in a sorted set point number), represented as string, or nil if member does not exist in the sorted set, or key does not exists

Parameters:

  • key (String)
  • member (String)

Returns:

  • (String, nil)

    the score of member (a double precision floating

See Also:


317
318
319
# File 'lib/oxblood/commands/sorted_sets.rb', line 317

def zscore(key, member)
  run(:ZSCORE, key, member)
end

#zunionstore(destination, numkeys, *keys, **opts) ⇒ Integer

Add multiple sorted sets and store the resulting sorted set in a new key

Parameters:

  • destination (String)

    key

  • numkeys (Integer)

    number of sorted sets

  • keys (String, Array<String>)
  • opts (Hash)

Options Hash (**opts):

  • :weights (Array<Float>)

    multiplication factor for each input sorted set.

  • :aggregate (Symbol)

    how the results of the union are aggregated.

Returns:

  • (Integer)

    the number of elements in the resulting sorted set at destination.

See Also:


355
356
357
# File 'lib/oxblood/commands/sorted_sets.rb', line 355

def zunionstore(destination, numkeys, *keys, **opts)
  common_store(:ZUNIONSTORE, destination, numkeys, keys, opts)
end