Class: Redstruct::SortedSet
- 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
Instance Method Summary collapse
-
#add(*values, exists: false, overwrite: true) ⇒ Integer
The number of elements that have changed (includes new ones).
-
#clear ⇒ Object
Removes all items from the set.
-
#contain?(item) ⇒ Boolean
(also: #include?)
Relies on the score method, since it is O(1), whereas the index method is O(logn).
-
#decrement(member, by: 1.0) ⇒ Float
The new score of the member.
-
#empty? ⇒ Boolean
Checks if the set contains any items.
-
#increment(member, by: 1.0) ⇒ Float
The new score of the member.
-
#index(item) ⇒ Integer?
Returns the index of the item in the set, sorted ascending by score.
-
#initialize(lex: false, **options) ⇒ SortedSet
constructor
A new instance of SortedSet.
-
#lexicographic? ⇒ Boolean
True if this is a lexicographically sorted set.
-
#remove(*items) ⇒ Integer
Removes the items from the set.
-
#rindex(item) ⇒ Integer?
Returns the index of the item in the set, sorted descending by score.
-
#score(item) ⇒ Float?
Returns the score of the given item.
-
#size ⇒ Integer
Returns the number of items in the set.
-
#slice(**options) ⇒ Redstruct::SortedSet::Slice
Returns a slice or partial selection of the set.
-
#to_a ⇒ Array<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.
-
#to_enum(match: '*', count: 10, with_scores: false) ⇒ Enumerator
Use redis-rb zscan_each method to iterate over particular keys.
-
#to_set ⇒ ::Set
TODO: Consider using ::SortedSet or some other data structure.
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
14 15 16 17 |
# File 'lib/redstruct/sorted_set.rb', line 14 def initialize(lex: false, **) super(**) @lex = lex end |
Instance Method Details
#add(*values, exists: false, overwrite: true) ⇒ Integer
Returns 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) = { 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, ) end |
#clear ⇒ Object
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)
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
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.
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
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
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
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.
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
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.
115 116 117 |
# File 'lib/redstruct/sorted_set.rb', line 115 def score(item) return self.connection.zscore(@key, item) end |
#size ⇒ Integer
Returns the number of items in the set. If you want to specify within a range, first get the slice and query its size.
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.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/redstruct/sorted_set.rb', line 72 def slice(**) defaults = { lower: nil, upper: nil, exclusive: false, lex: @lex } self.class::Slice.new(self, **defaults.merge()) end |
#to_a ⇒ Array<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.
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
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
136 137 138 |
# File 'lib/redstruct/sorted_set.rb', line 136 def to_set return slice.to_set end |