Class: Redstruct::SortedSet

Inherits:
Struct
  • Object
show all
Includes:
Utils::Iterable
Defined in:
lib/redstruct/sorted_set.rb

Overview

Mapping between Redis and Ruby sorted sets (with scores). There is no caching mechanism in play, so most methods actually do access the underlying redis connection. Also, keep in mind Redis converts all values strings on the DB side

Instance Attribute Summary

Attributes inherited from Struct

#key

Instance Method Summary collapse

Methods inherited from Struct

#delete, #dump, #exists?, #expire, #expire_at, #inspectable_attributes, #persist, #restore, #ttl, #type

Constructor Details

#initialize(lex: false, **options) ⇒ SortedSet

Returns a new instance of SortedSet

Parameters:

  • lex (Boolean)

    if true, assumes the set is lexicographically sorted



14
15
16
17
# File 'lib/redstruct/sorted_set.rb', line 14

def initialize(lex: false, **options)
  super(**options)
  @lex = lex
end

Instance Method Details

#add(*values, exists: false, overwrite: true) ⇒ Integer

Returns the number of elements that have changed (includes new ones)

Parameters:

  • values (Array<#to_s>)

    the object to add to the set

  • exists (Boolean)

    if true, only update elements that exist (do not add new ones)

  • overwrite (Boolean)

    if false, do not update existing elements

Returns:

  • (Integer)

    the number of elements that have changed (includes new ones)



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/redstruct/sorted_set.rb', line 28

def add(*values, exists: false, overwrite: true)
  options = { xx: exists, nx: !overwrite, ch: true }

  if @lex
    values = values.map do |pair|
      member = pair.is_a?(Array) ? pair.last : pair
      [0.0, member]
    end
  end

  return self.connection.zadd(@key, values, options)
end

#clearObject

Removes all items from the set. Does this by simply deleting the key



58
59
60
# File 'lib/redstruct/sorted_set.rb', line 58

def clear
  delete
end

#contain?(item) ⇒ Boolean Also known as: include?

Relies on the score method, since it is O(1), whereas the index method is O(logn)

Parameters:

  • item (#to_s)

    the item to check for

Returns:

  • (Boolean)

    true if the item is in the set, false otherwise



93
94
95
# File 'lib/redstruct/sorted_set.rb', line 93

def contain?(item)
  return coerce_bool(score(item))
end

#decrement(member, by: 1.0) ⇒ Float

Returns the new score of the member

Parameters:

  • member (#to_s)

    the member of the set whose score to decrement

  • by (#to_f)

    the amount to decrement the score by

Returns:

  • (Float)

    the new score of the member



52
53
54
# File 'lib/redstruct/sorted_set.rb', line 52

def decrement(member, by: 1.0)
  return increment(member, by: -by.to_f)
end

#empty?Boolean

Checks if the set contains any items.

Returns:

  • (Boolean)

    true if the key exists (meaning it contains at least 1 item), false otherwise



85
86
87
# File 'lib/redstruct/sorted_set.rb', line 85

def empty?
  return !exists?
end

#increment(member, by: 1.0) ⇒ Float

Returns the new score of the member

Parameters:

  • member (#to_s)

    the member of the set whose score to increment

  • by (#to_f)

    the amount to increment the score by

Returns:

  • (Float)

    the new score of the member

Raises:

  • (NotImplementedError)


44
45
46
47
# File 'lib/redstruct/sorted_set.rb', line 44

def increment(member, by: 1.0)
  raise NotImplementedError, 'cannot increment the score of items in a lexicographically ordered set' if @lex
  return self.connection.zincrby(@key, by.to_f, member.to_s).to_f
end

#index(item) ⇒ Integer?

Returns the index of the item in the set, sorted ascending by score

Parameters:

  • item (#to_s)

    the item to check for

Returns:

  • (Integer, nil)

    the index of the item, or nil if not found



101
102
103
# File 'lib/redstruct/sorted_set.rb', line 101

def index(item)
  return self.connection.zrank(@key, item)
end

#lexicographic?Boolean

Returns true if this is a lexicographically sorted set

Returns:

  • (Boolean)

    true if this is a lexicographically sorted set



20
21
22
# File 'lib/redstruct/sorted_set.rb', line 20

def lexicographic?
  return @lex
end

#remove(*items) ⇒ Integer

Removes the items from the set.

Parameters:

  • items (Array<#to_s>)

    the items to remove from the set

Returns:

  • (Integer)

    the amount of items removed from the set



122
123
124
# File 'lib/redstruct/sorted_set.rb', line 122

def remove(*items)
  return self.connection.zrem(@key, items)
end

#rindex(item) ⇒ Integer?

Returns the index of the item in the set, sorted descending by score

Parameters:

  • item (#to_s)

    the item to check for

Returns:

  • (Integer, nil)

    the index of the item, or nil if not found



108
109
110
# File 'lib/redstruct/sorted_set.rb', line 108

def rindex(item)
  return self.connection.zrevrank(@key, item)
end

#score(item) ⇒ Float?

Returns the score of the given item.

Parameters:

  • item (#to_s)

    the item to check for

Returns:

  • (Float, nil)

    the score of the item, or nil if not found



115
116
117
# File 'lib/redstruct/sorted_set.rb', line 115

def score(item)
  return self.connection.zscore(@key, item)
end

#sizeInteger

Returns the number of items in the set. If you want to specify within a range, first get the slice and query its size.

Returns:

  • (Integer)

    the number of items in the set



65
66
67
# File 'lib/redstruct/sorted_set.rb', line 65

def size
  return self.connection.zcard(@key)
end

#slice(**options) ⇒ Redstruct::SortedSet::Slice

Returns a slice or partial selection of the set.

Returns:

  • (Redstruct::SortedSet::Slice)

    a newly created slice for this set

See Also:

  • Redstruct::SortedSet::Slice#initialize


72
73
74
75
76
77
78
79
80
81
# File 'lib/redstruct/sorted_set.rb', line 72

def slice(**options)
  defaults = {
    lower: nil,
    upper: nil,
    exclusive: false,
    lex: @lex
  }

  self.class::Slice.new(self, **defaults.merge(options))
end

#to_aArray<Redstruct::Utils::ScoredValue>

Returns an array representation of the set, sorted by score ascending NOTE: It pulls the whole set into memory, so use each if that's a concern, or use slices with pre-determined ranges.

Returns:

  • (Array<Redstruct::Utils::ScoredValue>)

    all the items in the set, sorted by score ascending



130
131
132
# File 'lib/redstruct/sorted_set.rb', line 130

def to_a
  return slice.to_a
end

#to_enum(match: '*', count: 10, with_scores: false) ⇒ Enumerator

Use redis-rb zscan_each method to iterate over particular keys

Returns:

  • (Enumerator)

    base enumerator to iterate of the namespaced keys



142
143
144
145
146
147
148
149
150
151
# File 'lib/redstruct/sorted_set.rb', line 142

def to_enum(match: '*', count: 10, with_scores: false)
  enumerator = self.connection.zscan_each(@key, match: match, count: count)
  return enumerator if with_scores
  return Enumerator.new do |yielder|
    loop do
      item, = enumerator.next
      yielder << item
    end
  end
end

#to_set::Set

TODO: Consider using ::SortedSet or some other data structure

Returns:

  • (::Set)

    an unordered set representation



136
137
138
# File 'lib/redstruct/sorted_set.rb', line 136

def to_set
  return slice.to_set
end