Module: Redis::Commands::Arrays
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Defined in: | lib/redis/commands/arrays.rb |
Instance Method Summary
-
#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)>
Searcharray 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.
-
#argrep_bound(index)
private
ARGREP accepts "-" / "+" as full-range bounds — unlike the other AR* range commands, which take numeric indices only (verified on
::Redis8.8).
Instance Method Details
#arcount(key) ⇒ Integer
Get the number of non-empty elements in an array.
# 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.
# 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.
# 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.
# 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.
# 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).
# 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
#argrep_bound(index) (private)
ARGREP accepts "-" / "+" as full-range bounds — unlike the other AR*
range commands, which take numeric indices only (verified on ::Redis
8.8). Anything else is coerced so typos still fail fast client-side.
# File 'lib/redis/commands/arrays.rb', line 353
def argrep_bound(index) index == "-" || index == "+" ? index : Integer(index) end
#arinfo(key, full: false) ⇒ Hash{String => Integer, Float}
Get metadata about an array.
# 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.
# 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.
# 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).
# 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.
# 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.
# 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.
# 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).
# 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.
# 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.
# 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.
# 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.
# File 'lib/redis/commands/arrays.rb', line 25
def arset(key, index, *values) send_command([:arset, key, Integer(index), *values]) end