Module: Redis::Commands::Arrays
- Included in:
- Redis::Commands
- Defined in:
- lib/redis/commands/arrays.rb
Instance Method Summary collapse
-
#arcount(key) ⇒ Integer
Get the number of non-empty elements in an array.
-
#ardel(key, *indices) ⇒ Integer
Delete elements at the specified indices in an array.
-
#ardelrange(key, *ranges) ⇒ Integer
Delete elements in one or more inclusive index ranges.
-
#arget(key, index) ⇒ String?
Get the value at an index in an array.
-
#argetrange(key, start, stop) ⇒ Array<String, nil>
Get values in a range of indices.
-
#argrep(key, start, stop, exact: nil, match: nil, glob: nil, re: nil, logic: nil, limit: nil, with_values: nil, nocase: nil) ⇒ Array<Integer>, Array<Array(Integer, String)>
Search array elements within an inclusive index range using one or more textual predicates.
-
#arinfo(key, full: false) ⇒ Hash{String => Integer, Float}
Get metadata about an array.
-
#arinsert(key, *values) ⇒ Integer
Insert one or more values at consecutive indices, beginning at the array's insert cursor.
-
#arlastitems(key, count, rev: false) ⇒ Array<String>
Get the most recently inserted elements.
-
#arlen(key) ⇒ Integer
Get the length of an array (max index + 1).
-
#armget(key, *indices) ⇒ Array<String, nil>
Get values at multiple indices in an array.
-
#armset(key, *pairs) ⇒ Integer
Set multiple index-value pairs in an array.
-
#arnext(key) ⇒ Integer?
Get the next index #arinsert would use.
-
#arop(key, start, stop, operation, value: nil) ⇒ Float, ...
Perform an aggregate operation on the non-empty elements in a range.
-
#arring(key, size, *values) ⇒ Integer
Insert one or more values into an array used as a fixed-size ring buffer.
-
#arscan(key, start, stop, limit: nil) ⇒ Array<Array(Integer, String)>
Iterate existing elements in an index range.
-
#arseek(key, index) ⇒ Boolean
Set the insert cursor of an array to a specific index.
-
#arset(key, index, *values) ⇒ Integer
Set one or more contiguous values starting at an index in an array.
Instance Method Details
#arcount(key) ⇒ Integer
Get the number of non-empty elements in an array.
130 131 132 |
# File 'lib/redis/commands/arrays.rb', line 130 def arcount(key) send_command([:arcount, key]) end |
#ardel(key, *indices) ⇒ Integer
Delete elements at the specified indices in an array.
Deleting an index that is not set counts as zero elements deleted and does not modify the array.
142 143 144 145 |
# File 'lib/redis/commands/arrays.rb', line 142 def ardel(key, *indices) indices = indices.flatten(1).map { |index| Integer(index) } send_command([:ardel, key, *indices]) end |
#ardelrange(key, *ranges) ⇒ Integer
Delete elements in one or more inclusive index ranges.
Ranges may overlap; each element is counted at most once. A range
given with start > stop is processed in ascending order regardless.
163 164 165 166 167 168 |
# File 'lib/redis/commands/arrays.rb', line 163 def ardelrange(key, *ranges) ranges = ranges.flatten(1).map { |index| Integer(index) } raise ArgumentError, "ranges must be given as start/stop pairs" if ranges.empty? || ranges.size.odd? send_command([:ardelrange, key, *ranges]) end |
#arget(key, index) ⇒ String?
Get the value at an index in an array.
42 43 44 |
# File 'lib/redis/commands/arrays.rb', line 42 def arget(key, index) send_command([:arget, key, Integer(index)]) end |
#argetrange(key, start, stop) ⇒ Array<String, nil>
Get values in a range of indices.
Empty slots inside the range are returned as nil. When start is
greater than stop, elements are returned in reverse index order.
114 115 116 |
# File 'lib/redis/commands/arrays.rb', line 114 def argetrange(key, start, stop) send_command([:argetrange, key, Integer(start), Integer(stop)]) end |
#argrep(key, start, stop, exact: nil, match: nil, glob: nil, re: nil, logic: nil, limit: nil, with_values: nil, nocase: nil) ⇒ Array<Integer>, Array<Array(Integer, String)>
Search array elements within an inclusive index range using one or more textual predicates. Empty slots are skipped.
Each predicate keyword accepts a single value or an array of values;
multiple predicates are combined with logic: (:or by default).
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/redis/commands/arrays.rb', line 278 def argrep(key, start, stop, exact: nil, match: nil, glob: nil, re: nil, logic: nil, limit: nil, with_values: nil, nocase: nil) args = [:argrep, key, argrep_bound(start), argrep_bound(stop)] { "EXACT" => exact, "MATCH" => match, "GLOB" => glob, "RE" => re }.each do |predicate, values| Array(values).each { |value| args << predicate << value } end if logic operator = logic.to_s.upcase raise ArgumentError, "logic must be :and or :or" unless %w[AND OR].include?(operator) args << operator end args << "LIMIT" << Integer(limit) if limit args << "WITHVALUES" if with_values args << "NOCASE" if nocase send_command(args) end |
#arinfo(key, full: false) ⇒ Hash{String => Integer, Float}
Get metadata about an array.
342 343 344 345 346 |
# File 'lib/redis/commands/arrays.rb', line 342 def arinfo(key, full: false) args = [:arinfo, key] args << "FULL" if full send_command(args, &HashifyArrayInfo) end |
#arinsert(key, *values) ⇒ Integer
Insert one or more values at consecutive indices, beginning at the array's insert cursor. The cursor advances by one for each value.
179 180 181 |
# File 'lib/redis/commands/arrays.rb', line 179 def arinsert(key, *values) send_command([:arinsert, key, *values]) end |
#arlastitems(key, count, rev: false) ⇒ Array<String>
Get the most recently inserted elements.
209 210 211 212 213 |
# File 'lib/redis/commands/arrays.rb', line 209 def arlastitems(key, count, rev: false) args = [:arlastitems, key, Integer(count)] args << "REV" if rev send_command(args) end |
#arlen(key) ⇒ Integer
Get the length of an array (max index + 1).
122 123 124 |
# File 'lib/redis/commands/arrays.rb', line 122 def arlen(key) send_command([:arlen, key]) end |
#armget(key, *indices) ⇒ Array<String, nil>
Get values at multiple indices in an array.
The reply preserves the order of the requested indices and contains
nil for any index that is not set.
93 94 95 96 |
# File 'lib/redis/commands/arrays.rb', line 93 def armget(key, *indices) indices = indices.flatten(1).map { |index| Integer(index) } send_command([:armget, key, *indices]) end |
#armset(key, *pairs) ⇒ Integer
Set multiple index-value pairs in an array.
Pairs may be non-contiguous and given in any order, as a flat list or a Hash.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/redis/commands/arrays.rb', line 66 def armset(key, *pairs) pairs = if pairs.size == 1 && pairs.first.is_a?(Hash) pairs.first.flatten else pairs.flatten(1) end raise ArgumentError, "wrong number of arguments" if pairs.empty? || pairs.size.odd? args = pairs.each_slice(2).flat_map { |index, value| [Integer(index), value] } send_command([:armset, key, *args]) end |
#arnext(key) ⇒ Integer?
Get the next index #arinsert would use.
197 198 199 |
# File 'lib/redis/commands/arrays.rb', line 197 def arnext(key) send_command([:arnext, key]) end |
#arop(key, start, stop, operation, value: nil) ⇒ Float, ...
Perform an aggregate operation on the non-empty elements in a range.
Supported operations: :sum, :min, :max (numeric, returned as
Float), :and, :or, :xor (bitwise, floats truncated toward
zero), :match (count of elements equal to value) and :used
(count of non-empty elements).
320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/redis/commands/arrays.rb', line 320 def arop(key, start, stop, operation, value: nil) operation = operation.to_s.upcase raise ArgumentError, "value is required for the MATCH operation" if operation == "MATCH" && value.nil? args = [:arop, key, Integer(start), Integer(stop), operation] args << value if operation == "MATCH" if %w[SUM MIN MAX].include?(operation) send_command(args, &Floatify) else send_command(args) end end |
#arring(key, size, *values) ⇒ Integer
Insert one or more values into an array used as a fixed-size ring buffer. Each value is placed at the next position in the ring and the cursor advances, wrapping around to index 0 once the ring is full.
223 224 225 |
# File 'lib/redis/commands/arrays.rb', line 223 def arring(key, size, *values) send_command([:arring, key, Integer(size), *values]) end |
#arscan(key, start, stop, limit: nil) ⇒ Array<Array(Integer, String)>
Iterate existing elements in an index range.
Empty slots are excluded. When start is greater than stop the
iteration is reversed.
243 244 245 246 247 |
# File 'lib/redis/commands/arrays.rb', line 243 def arscan(key, start, stop, limit: nil) args = [:arscan, key, Integer(start), Integer(stop)] args << "LIMIT" << Integer(limit) if limit send_command(args) end |
#arseek(key, index) ⇒ Boolean
Set the insert cursor of an array to a specific index.
188 189 190 |
# File 'lib/redis/commands/arrays.rb', line 188 def arseek(key, index) send_command([:arseek, key, Integer(index)], &Boolify) end |
#arset(key, index, *values) ⇒ Integer
Set one or more contiguous values starting at an index in an array.
When multiple values are given they are stored at consecutive indices
beginning at index. Writing past the current end of the array
extends it; slots that are skipped over stay empty. Only non-negative
indices are valid.
25 26 27 |
# File 'lib/redis/commands/arrays.rb', line 25 def arset(key, index, *values) send_command([:arset, key, Integer(index), *values]) end |