redis 6.0.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +114 -0
- data/README.md +172 -5
- data/lib/redis/client.rb +5 -1
- data/lib/redis/commands/arrays.rb +358 -0
- data/lib/redis/commands/bitmaps.rb +24 -0
- data/lib/redis/commands/bless.rb +115 -0
- data/lib/redis/commands/cluster.rb +9 -2
- data/lib/redis/commands/hashes.rb +250 -7
- data/lib/redis/commands/keys.rb +48 -11
- data/lib/redis/commands/modules/json.rb +27 -2
- data/lib/redis/commands/modules/search/aggregation.rb +2 -1
- data/lib/redis/commands/modules/search/field.rb +11 -2
- data/lib/redis/commands/modules/search/hybrid.rb +3 -0
- data/lib/redis/commands/modules/search/index.rb +2 -1
- data/lib/redis/commands/modules/search/miscellaneous.rb +2 -1
- data/lib/redis/commands/modules/search/query.rb +2 -2
- data/lib/redis/commands/modules/search/schema.rb +4 -0
- data/lib/redis/commands/scripting.rb +109 -0
- data/lib/redis/commands/server.rb +158 -7
- data/lib/redis/commands/sets.rb +270 -0
- data/lib/redis/commands/sorted_sets.rb +131 -6
- data/lib/redis/commands/streams.rb +284 -33
- data/lib/redis/commands/strings.rb +232 -6
- data/lib/redis/commands/transactions.rb +6 -1
- data/lib/redis/commands/vector_sets.rb +407 -0
- data/lib/redis/commands.rb +169 -6
- data/lib/redis/distributed.rb +539 -22
- data/lib/redis/hash_ring.rb +2 -1
- data/lib/redis/keyspace_notifications/channels.rb +121 -0
- data/lib/redis/keyspace_notifications/manager.rb +1121 -0
- data/lib/redis/keyspace_notifications/notification.rb +95 -0
- data/lib/redis/keyspace_notifications/parser.rb +170 -0
- data/lib/redis/keyspace_notifications.rb +59 -0
- data/lib/redis/pipeline.rb +17 -5
- data/lib/redis/ractorize.rb +6 -0
- data/lib/redis/subscribe.rb +80 -2
- data/lib/redis/version.rb +1 -1
- data/lib/redis/xxh3.rb +354 -0
- data/lib/redis.rb +29 -8
- metadata +16 -6
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
class Redis
|
|
6
|
+
module Commands
|
|
7
|
+
module VectorSets
|
|
8
|
+
# Add a new element into a vector set, or update its vector if it
|
|
9
|
+
# already exists.
|
|
10
|
+
#
|
|
11
|
+
# The vector can be given as an Array of numbers (sent as `VALUES`), or
|
|
12
|
+
# as a String holding a little-endian FP32 blob (sent as `FP32`), e.g.
|
|
13
|
+
# `values.pack("e*")`.
|
|
14
|
+
#
|
|
15
|
+
# @example Add an element with an array of values
|
|
16
|
+
# redis.vadd("mykey", [0.1, 1.2, 0.5], "my-element")
|
|
17
|
+
# # => true
|
|
18
|
+
# @example Add an element with an FP32 blob
|
|
19
|
+
# redis.vadd("mykey", [0.1, 1.2, 0.5].pack("e*"), "my-element")
|
|
20
|
+
# # => true
|
|
21
|
+
# @example Reduce dimensionality and attach attributes
|
|
22
|
+
# redis.vadd("mykey", [0.1, 1.2, 0.5], "my-element", reduce: 2, attributes: { size: "large" })
|
|
23
|
+
# # => true
|
|
24
|
+
#
|
|
25
|
+
# @param [String] key
|
|
26
|
+
# @param [Array<Numeric>, String] vector the vector as an array of
|
|
27
|
+
# numbers, or a little-endian FP32 blob
|
|
28
|
+
# @param [String] element name of the element being added
|
|
29
|
+
# @param [Hash] options
|
|
30
|
+
# @option options [Integer] :reduce project the vector down to this
|
|
31
|
+
# number of dimensions
|
|
32
|
+
# @option options [Boolean] :cas collect neighbor candidates in a
|
|
33
|
+
# background thread, check-and-set style
|
|
34
|
+
# @option options [Symbol, String] :quantization one of `:noquant`,
|
|
35
|
+
# `:q8` (the default) or `:bin`
|
|
36
|
+
# @option options [Integer] :ef build-time exploration factor (default 200)
|
|
37
|
+
# @option options [Hash, String] :attributes attributes associated with
|
|
38
|
+
# the element, as an object serialized to JSON or a pre-encoded JSON string
|
|
39
|
+
# @option options [Integer] :m maximum number of connections per graph
|
|
40
|
+
# node (default 16)
|
|
41
|
+
# @return [Boolean] whether the element was added
|
|
42
|
+
def vadd(key, vector, element, reduce: nil, cas: nil, quantization: nil, ef: nil, attributes: nil, m: nil)
|
|
43
|
+
args = [:vadd, key]
|
|
44
|
+
args << "REDUCE" << Integer(reduce) if reduce
|
|
45
|
+
|
|
46
|
+
if vector.is_a?(String)
|
|
47
|
+
args << "FP32" << vector
|
|
48
|
+
else
|
|
49
|
+
args << "VALUES" << vector.size
|
|
50
|
+
args.concat(vector.map { |value| Float(value) })
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
args << element
|
|
54
|
+
args << "CAS" if cas
|
|
55
|
+
|
|
56
|
+
if quantization
|
|
57
|
+
case quantization.to_s.downcase
|
|
58
|
+
when "noquant" then args << "NOQUANT"
|
|
59
|
+
when "q8" then args << "Q8"
|
|
60
|
+
when "bin" then args << "BIN"
|
|
61
|
+
else raise ArgumentError, "unknown quantization: #{quantization.inspect}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
args << "EF" << Integer(ef) if ef
|
|
66
|
+
|
|
67
|
+
if attributes
|
|
68
|
+
args << "SETATTR" << (attributes.is_a?(String) ? attributes : ::JSON.generate(attributes))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
args << "M" << Integer(m) if m
|
|
72
|
+
|
|
73
|
+
send_command(args, &BoolifyBoolean)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Return the number of elements in a vector set.
|
|
77
|
+
#
|
|
78
|
+
# @example
|
|
79
|
+
# redis.vcard("mykey")
|
|
80
|
+
# # => 2
|
|
81
|
+
#
|
|
82
|
+
# @param [String] key
|
|
83
|
+
# @return [Integer] the number of elements in the vector set, or 0 if
|
|
84
|
+
# the key does not exist
|
|
85
|
+
def vcard(key)
|
|
86
|
+
send_command([:vcard, key])
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Return the number of dimensions of the vectors in a vector set.
|
|
90
|
+
#
|
|
91
|
+
# For a vector set created with the `REDUCE` option this reports the
|
|
92
|
+
# reduced dimension, although full-size vectors must still be used when
|
|
93
|
+
# querying with VSIM.
|
|
94
|
+
#
|
|
95
|
+
# @example
|
|
96
|
+
# redis.vdim("mykey")
|
|
97
|
+
# # => 3
|
|
98
|
+
#
|
|
99
|
+
# @param [String] key
|
|
100
|
+
# @return [Integer] the dimension of the vectors in the set
|
|
101
|
+
# @raise [Redis::CommandError] if the key does not exist
|
|
102
|
+
def vdim(key)
|
|
103
|
+
send_command([:vdim, key])
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Return the approximate vector associated with an element in a vector
|
|
107
|
+
# set. The round trip is approximate because vectors are normalized and
|
|
108
|
+
# (by default) quantized on insertion.
|
|
109
|
+
#
|
|
110
|
+
# @example
|
|
111
|
+
# redis.vemb("mykey", "my-element")
|
|
112
|
+
# # => [0.1004752591252327, 1.2000000476837158, 0.5023762512207031]
|
|
113
|
+
# @example Raw internal representation
|
|
114
|
+
# redis.vemb("mykey", "my-element", raw: true)
|
|
115
|
+
# # => { "quantization" => "int8", "raw" => "\x0b\x7f5", "l2" => 1.3038404, "range" => 0.009448819 }
|
|
116
|
+
#
|
|
117
|
+
# @param [String] key
|
|
118
|
+
# @param [String] element name of the element whose vector to retrieve
|
|
119
|
+
# @param [Boolean] raw return the raw internal representation instead
|
|
120
|
+
# @return [Array<Float>, Hash, nil] the vector as an array of Floats; with
|
|
121
|
+
# `raw: true` a Hash with `"quantization"`, `"raw"` (blob), `"l2"` and,
|
|
122
|
+
# for q8 sets, `"range"` keys; nil if the key or element does not exist
|
|
123
|
+
def vemb(key, element, raw: nil)
|
|
124
|
+
if raw
|
|
125
|
+
send_command([:vemb, key, element, "RAW"], &HashifyVectorEmbeddingRaw)
|
|
126
|
+
else
|
|
127
|
+
send_command([:vemb, key, element], &FloatifyArray)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Return the neighbors of an element in a vector set, one entry per
|
|
132
|
+
# layer of the HNSW graph.
|
|
133
|
+
#
|
|
134
|
+
# @example
|
|
135
|
+
# redis.vlinks("mykey", "elem-1")
|
|
136
|
+
# # => [["elem-2"]]
|
|
137
|
+
# @example With similarity scores
|
|
138
|
+
# redis.vlinks("mykey", "elem-1", with_scores: true)
|
|
139
|
+
# # => [{ "elem-2" => 0.9989262223243713 }]
|
|
140
|
+
#
|
|
141
|
+
# @param [String] key
|
|
142
|
+
# @param [String] element name of the element whose neighbors to inspect
|
|
143
|
+
# @param [Boolean] with_scores include similarity scores for each neighbor
|
|
144
|
+
# @return [Array<Array<String>>, Array<Hash>, nil] one array of neighbor
|
|
145
|
+
# names per layer; with scores, one `{ name => Float }` Hash per layer;
|
|
146
|
+
# nil if the key or element does not exist
|
|
147
|
+
def vlinks(key, element, withscores: false, with_scores: withscores)
|
|
148
|
+
if with_scores
|
|
149
|
+
send_command([:vlinks, key, element, "WITHSCORES"], &HashifyVectorLinksWithScores)
|
|
150
|
+
else
|
|
151
|
+
send_command([:vlinks, key, element])
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Return elements similar to a given vector or element, by approximate
|
|
156
|
+
# (HNSW) or exact (`truth: true`) similarity search.
|
|
157
|
+
#
|
|
158
|
+
# The query is either a vector — an Array of numbers (sent as `VALUES`)
|
|
159
|
+
# or a little-endian FP32 String blob (sent as `FP32`) — or the name of
|
|
160
|
+
# an existing element (sent as `ELE`). Exactly one of `vector:` and
|
|
161
|
+
# `element:` must be given.
|
|
162
|
+
#
|
|
163
|
+
# @example Query by element
|
|
164
|
+
# redis.vsim("mykey", element: "apple", count: 3)
|
|
165
|
+
# # => ["apple", "apples", "pear"]
|
|
166
|
+
# @example Query by vector, with similarity scores
|
|
167
|
+
# redis.vsim("mykey", vector: [0.1, 1.2, 0.5], with_scores: true)
|
|
168
|
+
# # => { "apple" => 0.9998867657923256, ... }
|
|
169
|
+
# @example With scores and attributes
|
|
170
|
+
# redis.vsim("mykey", element: "apple", with_scores: true, with_attribs: true)
|
|
171
|
+
# # => { "apple" => [0.9998867657923256, { "len" => 5 }], ... }
|
|
172
|
+
#
|
|
173
|
+
# @param [String] key
|
|
174
|
+
# @param [Hash] options
|
|
175
|
+
# @option options [Array<Numeric>, String] :vector the query vector
|
|
176
|
+
# @option options [String] :element name of an existing element to use
|
|
177
|
+
# as the similarity reference
|
|
178
|
+
# @option options [Boolean] :with_scores include the similarity score
|
|
179
|
+
# (1 identical … 0 opposite) for each result
|
|
180
|
+
# @option options [Boolean] :with_attribs include the attributes for
|
|
181
|
+
# each result, parsed with JSON.parse (nil when absent)
|
|
182
|
+
# @option options [Boolean] :raw return attribute JSON strings as stored
|
|
183
|
+
# instead of parsing them
|
|
184
|
+
# @option options [Integer] :count limit the number of results
|
|
185
|
+
# @option options [Float] :epsilon only return elements with a distance
|
|
186
|
+
# no further than this delta (similarity better than 1 - delta)
|
|
187
|
+
# @option options [Integer] :ef search exploration factor; higher values
|
|
188
|
+
# improve recall at the cost of speed
|
|
189
|
+
# @option options [String] :filter filter expression over element attributes
|
|
190
|
+
# @option options [Integer] :filter_ef cap on filtering attempts
|
|
191
|
+
# @option options [Boolean] :truth exact linear scan instead of HNSW (O(N))
|
|
192
|
+
# @option options [Boolean] :nothread run in the main thread
|
|
193
|
+
# @return [Array<String>, Hash] element names; with `with_scores:` a
|
|
194
|
+
# `{ name => Float }` Hash; with `with_scores:` and `with_attribs:` a
|
|
195
|
+
# `{ name => [Float, Object] }` Hash; with `with_attribs:` alone a
|
|
196
|
+
# `{ name => Object }` Hash (the attributes as JSON strings when
|
|
197
|
+
# `raw: true`). Empty when the key does not exist.
|
|
198
|
+
# @raise [Redis::CommandError] if an `element:` reference does not exist
|
|
199
|
+
def vsim(key, vector: nil, element: nil, withscores: false, with_scores: withscores,
|
|
200
|
+
withattribs: false, with_attribs: withattribs, raw: false, count: nil,
|
|
201
|
+
epsilon: nil, ef: nil, filter: nil, filter_ef: nil, truth: nil, nothread: nil)
|
|
202
|
+
unless vector.nil? ^ element.nil?
|
|
203
|
+
raise ArgumentError, "must provide exactly one of vector or element"
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
args = [:vsim, key]
|
|
207
|
+
if element
|
|
208
|
+
args << "ELE" << element
|
|
209
|
+
elsif vector.is_a?(String)
|
|
210
|
+
args << "FP32" << vector
|
|
211
|
+
else
|
|
212
|
+
args << "VALUES" << vector.size
|
|
213
|
+
args.concat(vector.map { |value| Float(value) })
|
|
214
|
+
end
|
|
215
|
+
args << "WITHSCORES" if with_scores
|
|
216
|
+
args << "WITHATTRIBS" if with_attribs
|
|
217
|
+
args << "COUNT" << Integer(count) if count
|
|
218
|
+
args << "EPSILON" << Float(epsilon) if epsilon
|
|
219
|
+
args << "EF" << Integer(ef) if ef
|
|
220
|
+
args << "FILTER" << filter if filter
|
|
221
|
+
args << "FILTER-EF" << Integer(filter_ef) if filter_ef
|
|
222
|
+
args << "TRUTH" if truth
|
|
223
|
+
args << "NOTHREAD" if nothread
|
|
224
|
+
|
|
225
|
+
if with_scores && with_attribs
|
|
226
|
+
send_command(args) do |reply|
|
|
227
|
+
reply = HashifyVectorScoresWithAttribs.call(reply)
|
|
228
|
+
if raw || !reply.is_a?(Hash)
|
|
229
|
+
reply
|
|
230
|
+
else
|
|
231
|
+
reply.transform_values { |(score, attribs)| [score, attribs && ::JSON.parse(attribs)] }
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
elsif with_scores
|
|
235
|
+
send_command(args, &HashifyVectorScores)
|
|
236
|
+
elsif with_attribs
|
|
237
|
+
send_command(args) do |reply|
|
|
238
|
+
reply = Hashify.call(reply)
|
|
239
|
+
if raw || !reply.is_a?(Hash)
|
|
240
|
+
reply
|
|
241
|
+
else
|
|
242
|
+
reply.transform_values { |attribs| attribs && ::JSON.parse(attribs) }
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
else
|
|
246
|
+
send_command(args)
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Remove an element from a vector set. Memory is reclaimed immediately.
|
|
251
|
+
#
|
|
252
|
+
# @example
|
|
253
|
+
# redis.vrem("mykey", "my-element")
|
|
254
|
+
# # => true
|
|
255
|
+
#
|
|
256
|
+
# @param [String] key
|
|
257
|
+
# @param [String] element name of the element to remove
|
|
258
|
+
# @return [Boolean] whether the element was removed; false if the key or
|
|
259
|
+
# element does not exist
|
|
260
|
+
def vrem(key, element)
|
|
261
|
+
send_command([:vrem, key, element], &BoolifyBoolean)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Check if an element exists in a vector set.
|
|
265
|
+
#
|
|
266
|
+
# @example
|
|
267
|
+
# redis.vismember("mykey", "my-element")
|
|
268
|
+
# # => true
|
|
269
|
+
#
|
|
270
|
+
# @param [String] key
|
|
271
|
+
# @param [String] element name of the element to check for membership
|
|
272
|
+
# @return [Boolean] whether the element exists in the vector set; false
|
|
273
|
+
# if the key does not exist
|
|
274
|
+
def vismember(key, element)
|
|
275
|
+
send_command([:vismember, key, element], &BoolifyBoolean)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Return one or more random elements from a vector set.
|
|
279
|
+
#
|
|
280
|
+
# Behaves like SRANDMEMBER: a positive count returns up to that many
|
|
281
|
+
# distinct elements, a negative count returns exactly that many elements
|
|
282
|
+
# possibly with duplicates, and a count exceeding the set size returns
|
|
283
|
+
# the whole set.
|
|
284
|
+
#
|
|
285
|
+
# @example
|
|
286
|
+
# redis.vrandmember("mykey")
|
|
287
|
+
# # => "elem-2"
|
|
288
|
+
# @example With a count
|
|
289
|
+
# redis.vrandmember("mykey", 2)
|
|
290
|
+
# # => ["elem-1", "elem-3"]
|
|
291
|
+
#
|
|
292
|
+
# @param [String] key
|
|
293
|
+
# @param [Integer] count number of elements to return; positive for
|
|
294
|
+
# distinct elements, negative to allow duplicates
|
|
295
|
+
# @return [String, Array<String>, nil] a single element (or nil for a
|
|
296
|
+
# missing key) without count; an array of elements (empty for a
|
|
297
|
+
# missing key) with count
|
|
298
|
+
def vrandmember(key, count = nil)
|
|
299
|
+
if count.nil?
|
|
300
|
+
send_command([:vrandmember, key])
|
|
301
|
+
else
|
|
302
|
+
send_command([:vrandmember, key, Integer(count)])
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# Return the elements of a vector set within a lexicographical range,
|
|
307
|
+
# in lexicographical (byte-by-byte) order.
|
|
308
|
+
#
|
|
309
|
+
# Boundaries follow the ZRANGEBYLEX syntax: `"[elem"` inclusive,
|
|
310
|
+
# `"(elem"` exclusive, `"-"` minimum, `"+"` maximum. A negative count
|
|
311
|
+
# returns all elements in the range. VRANGE is a stateless iterator: to
|
|
312
|
+
# paginate, pass the last returned element as an exclusive start on the
|
|
313
|
+
# next call.
|
|
314
|
+
#
|
|
315
|
+
# @example All elements
|
|
316
|
+
# redis.vrange("mykey", "-", "+")
|
|
317
|
+
# # => ["elem-1", "elem-2", "elem-3"]
|
|
318
|
+
# @example Paginate, two at a time
|
|
319
|
+
# redis.vrange("mykey", "-", "+", 2)
|
|
320
|
+
# # => ["elem-1", "elem-2"]
|
|
321
|
+
# redis.vrange("mykey", "(elem-2", "+", 2)
|
|
322
|
+
# # => ["elem-3"]
|
|
323
|
+
#
|
|
324
|
+
# @param [String] key
|
|
325
|
+
# @param [String] start range start: `"[elem"`, `"(elem"` or `"-"`
|
|
326
|
+
# @param [String] stop range end: `"[elem"`, `"(elem"` or `"+"`
|
|
327
|
+
# @param [Integer] count maximum number of elements to return; negative
|
|
328
|
+
# returns the whole range
|
|
329
|
+
# @return [Array<String>] the elements in the range, empty if the key
|
|
330
|
+
# does not exist
|
|
331
|
+
def vrange(key, start, stop, count = nil)
|
|
332
|
+
args = [:vrange, key, start, stop]
|
|
333
|
+
args << Integer(count) if count
|
|
334
|
+
send_command(args)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# Return metadata and internal details about a vector set, including
|
|
338
|
+
# size, dimensions, quantization type, and graph structure.
|
|
339
|
+
#
|
|
340
|
+
# @example
|
|
341
|
+
# redis.vinfo("mykey")
|
|
342
|
+
# # => { "quant-type" => "int8", "vector-dim" => 3, "size" => 1, ... }
|
|
343
|
+
#
|
|
344
|
+
# @param [String] key
|
|
345
|
+
# @return [Hash, nil] the vector set metadata, or nil if the key does
|
|
346
|
+
# not exist
|
|
347
|
+
def vinfo(key)
|
|
348
|
+
send_command([:vinfo, key], &Hashify)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# Associate JSON attributes with an element in a vector set, update
|
|
352
|
+
# them, or delete them. Attributes can be used in filtered similarity
|
|
353
|
+
# searches with VSIM.
|
|
354
|
+
#
|
|
355
|
+
# @example Set attributes from a Hash
|
|
356
|
+
# redis.vsetattr("mykey", "my-element", { type: "fruit", color: "red" })
|
|
357
|
+
# # => true
|
|
358
|
+
# @example Delete the attributes
|
|
359
|
+
# redis.vsetattr("mykey", "my-element", nil)
|
|
360
|
+
# # => true
|
|
361
|
+
#
|
|
362
|
+
# @param [String] key
|
|
363
|
+
# @param [String] element name of the element whose attributes to set
|
|
364
|
+
# @param [Hash, String, nil] attributes attributes as an object
|
|
365
|
+
# serialized to JSON or a pre-encoded JSON string; nil or an empty
|
|
366
|
+
# string deletes the attributes
|
|
367
|
+
# @return [Boolean] whether the attributes were set; false if the key or
|
|
368
|
+
# element does not exist
|
|
369
|
+
def vsetattr(key, element, attributes)
|
|
370
|
+
json = case attributes
|
|
371
|
+
when nil then ""
|
|
372
|
+
when String then attributes
|
|
373
|
+
else ::JSON.generate(attributes)
|
|
374
|
+
end
|
|
375
|
+
send_command([:vsetattr, key, element, json], &BoolifyBoolean)
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# Return the JSON attributes associated with an element in a vector set.
|
|
379
|
+
#
|
|
380
|
+
# By default the reply is parsed with JSON.parse; pass +raw: true+ to get
|
|
381
|
+
# the JSON string as stored.
|
|
382
|
+
#
|
|
383
|
+
# @example
|
|
384
|
+
# redis.vgetattr("mykey", "my-element")
|
|
385
|
+
# # => { "size" => "large" }
|
|
386
|
+
# @example Raw JSON string
|
|
387
|
+
# redis.vgetattr("mykey", "my-element", raw: true)
|
|
388
|
+
# # => "{\"size\": \"large\"}"
|
|
389
|
+
#
|
|
390
|
+
# @param [String] key
|
|
391
|
+
# @param [String] element name of the element whose attributes to retrieve
|
|
392
|
+
# @param [Boolean] raw return the JSON string without parsing it
|
|
393
|
+
# @return [Object, String, nil] the parsed attributes (or the JSON string
|
|
394
|
+
# with +raw: true+); nil if the key or element does not exist or has no
|
|
395
|
+
# attributes
|
|
396
|
+
def vgetattr(key, element, raw: false)
|
|
397
|
+
send_command([:vgetattr, key, element]) do |reply|
|
|
398
|
+
if reply.nil? || raw
|
|
399
|
+
reply
|
|
400
|
+
else
|
|
401
|
+
::JSON.parse(reply)
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
end
|
|
407
|
+
end
|
data/lib/redis/commands.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "redis/commands/arrays"
|
|
3
4
|
require "redis/commands/bitmaps"
|
|
5
|
+
require "redis/commands/bless"
|
|
4
6
|
require "redis/commands/cluster"
|
|
5
7
|
require "redis/commands/connection"
|
|
6
8
|
require "redis/commands/geo"
|
|
@@ -18,10 +20,13 @@ require "redis/commands/sorted_sets"
|
|
|
18
20
|
require "redis/commands/streams"
|
|
19
21
|
require "redis/commands/strings"
|
|
20
22
|
require "redis/commands/transactions"
|
|
23
|
+
require "redis/commands/vector_sets"
|
|
21
24
|
|
|
22
25
|
class Redis
|
|
23
26
|
module Commands
|
|
27
|
+
include Arrays
|
|
24
28
|
include Bitmaps
|
|
29
|
+
include Bless
|
|
25
30
|
include Cluster
|
|
26
31
|
include Connection
|
|
27
32
|
include Geo
|
|
@@ -39,6 +44,7 @@ class Redis
|
|
|
39
44
|
include Streams
|
|
40
45
|
include Strings
|
|
41
46
|
include Transactions
|
|
47
|
+
include VectorSets
|
|
42
48
|
|
|
43
49
|
# Commands returning 1 for true and 0 for false may be executed in a pipeline
|
|
44
50
|
# where the method call will return nil. Propagate the nil instead of falsely
|
|
@@ -47,6 +53,18 @@ class Redis
|
|
|
47
53
|
value != 0 unless value.nil?
|
|
48
54
|
}
|
|
49
55
|
|
|
56
|
+
# For commands whose reply is an integer under RESP2 but a native boolean
|
|
57
|
+
# under RESP3 (e.g. VADD): pass booleans through, boolify integers, and
|
|
58
|
+
# propagate nil for pipelined calls.
|
|
59
|
+
BoolifyBoolean = lambda { |value|
|
|
60
|
+
case value
|
|
61
|
+
when true, false, nil
|
|
62
|
+
value
|
|
63
|
+
else
|
|
64
|
+
value != 0
|
|
65
|
+
end
|
|
66
|
+
}
|
|
67
|
+
|
|
50
68
|
BoolifySet = lambda { |value|
|
|
51
69
|
case value
|
|
52
70
|
when "OK"
|
|
@@ -78,12 +96,16 @@ class Redis
|
|
|
78
96
|
end
|
|
79
97
|
}
|
|
80
98
|
|
|
99
|
+
# RESP2 carries doubles as bulk strings, including the non-finite ones Redis 7.2 normalizes
|
|
100
|
+
# to "inf", "-inf" and "nan"; RESP3 delivers native Floats, which pass through.
|
|
81
101
|
Floatify = lambda { |value|
|
|
82
102
|
case value
|
|
83
103
|
when "inf"
|
|
84
104
|
Float::INFINITY
|
|
85
105
|
when "-inf"
|
|
86
106
|
-Float::INFINITY
|
|
107
|
+
when "nan"
|
|
108
|
+
Float::NAN
|
|
87
109
|
when String
|
|
88
110
|
Float(value)
|
|
89
111
|
else
|
|
@@ -91,7 +113,19 @@ class Redis
|
|
|
91
113
|
end
|
|
92
114
|
}
|
|
93
115
|
|
|
94
|
-
|
|
116
|
+
# Number arrays (e.g. the VEMB vector) arrive as native doubles under
|
|
117
|
+
# RESP3 but as bulk strings under RESP2; converge on an array of Floats.
|
|
118
|
+
FloatifyArray = lambda { |value|
|
|
119
|
+
return value unless value.is_a?(Array)
|
|
120
|
+
|
|
121
|
+
value.first.is_a?(String) ? value.map(&Floatify) : value
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# A nil reply (e.g. ZRANK WITHSCORE on a missing member) stays nil instead of becoming [nil, nil].
|
|
125
|
+
FloatifyPair = lambda { |pair|
|
|
126
|
+
return if pair.nil?
|
|
127
|
+
|
|
128
|
+
first, score = pair
|
|
95
129
|
[first, Floatify.call(score)]
|
|
96
130
|
}
|
|
97
131
|
|
|
@@ -108,6 +142,68 @@ class Redis
|
|
|
108
142
|
end
|
|
109
143
|
}
|
|
110
144
|
|
|
145
|
+
# ARINFO: the avg-* slice statistics arrive as native doubles under RESP3
|
|
146
|
+
# but as bulk strings under RESP2; floatify them after hashifying so both
|
|
147
|
+
# protocols return the same Ruby types.
|
|
148
|
+
HashifyArrayInfo = lambda { |value|
|
|
149
|
+
reply = Hashify.call(value)
|
|
150
|
+
return reply unless reply.is_a?(Hash)
|
|
151
|
+
|
|
152
|
+
reply.each do |field, field_value|
|
|
153
|
+
reply[field] = Floatify.call(field_value) if field.start_with?("avg-")
|
|
154
|
+
end
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
# VEMB RAW: [quantization, blob, l2, (range, q8 only)] with the numeric
|
|
158
|
+
# fields as bulk strings under RESP2 but doubles under RESP3.
|
|
159
|
+
HashifyVectorEmbeddingRaw = lambda { |reply|
|
|
160
|
+
return reply unless reply.is_a?(Array)
|
|
161
|
+
|
|
162
|
+
result = {
|
|
163
|
+
"quantization" => reply[0],
|
|
164
|
+
"raw" => reply[1],
|
|
165
|
+
"l2" => Floatify.call(reply[2])
|
|
166
|
+
}
|
|
167
|
+
result["range"] = Floatify.call(reply[3]) if reply.size > 3
|
|
168
|
+
result
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
# Element => score pairs (VSIM WITHSCORES, one VLINKS layer): a native map
|
|
172
|
+
# with double scores under RESP3, a flat [name, score, ...] array of bulk
|
|
173
|
+
# strings under RESP2; converge on a Hash of name => Float.
|
|
174
|
+
HashifyVectorScores = lambda { |reply|
|
|
175
|
+
case reply
|
|
176
|
+
when Hash
|
|
177
|
+
reply
|
|
178
|
+
when Array
|
|
179
|
+
reply.each_slice(2).to_h { |name, score| [name, Floatify.call(score)] }
|
|
180
|
+
else
|
|
181
|
+
reply
|
|
182
|
+
end
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
# VSIM WITHSCORES WITHATTRIBS: a native map of name => [double, attrs]
|
|
186
|
+
# under RESP3, a flat [name, score, attrs, ...] triplet array under RESP2
|
|
187
|
+
# (attrs is nil for elements without attributes); converge on a Hash of
|
|
188
|
+
# name => [Float, attrs].
|
|
189
|
+
HashifyVectorScoresWithAttribs = lambda { |reply|
|
|
190
|
+
case reply
|
|
191
|
+
when Hash
|
|
192
|
+
reply
|
|
193
|
+
when Array
|
|
194
|
+
reply.each_slice(3).to_h { |name, score, attrs| [name, [Floatify.call(score), attrs]] }
|
|
195
|
+
else
|
|
196
|
+
reply
|
|
197
|
+
end
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
# VLINKS WITHSCORES: one name => score map per HNSW layer.
|
|
201
|
+
HashifyVectorLinksWithScores = lambda { |reply|
|
|
202
|
+
return reply unless reply.is_a?(Array)
|
|
203
|
+
|
|
204
|
+
reply.map(&HashifyVectorScores)
|
|
205
|
+
}
|
|
206
|
+
|
|
111
207
|
HashifyInfo = lambda { |reply|
|
|
112
208
|
lines = reply.split("\r\n").grep_v(/^(#|$)/)
|
|
113
209
|
lines.map! { |line| line.split(':', 2) }
|
|
@@ -127,25 +223,30 @@ class Redis
|
|
|
127
223
|
EMPTY_STREAM_RESPONSE = [nil].freeze
|
|
128
224
|
private_constant :EMPTY_STREAM_RESPONSE
|
|
129
225
|
|
|
226
|
+
# XREADGROUP CLAIM appends idle-time and delivery-count to each claimed entry; keep them.
|
|
130
227
|
HashifyStreamEntries = lambda { |reply|
|
|
131
|
-
reply.compact.map do |entry_id, values|
|
|
132
|
-
[entry_id, values&.each_slice(2)&.to_h]
|
|
228
|
+
reply.compact.map do |entry_id, values, *claim_info|
|
|
229
|
+
[entry_id, values&.each_slice(2)&.to_h, *claim_info]
|
|
133
230
|
end
|
|
134
231
|
}
|
|
135
232
|
|
|
233
|
+
# XAUTOCLAIM: Redis 7.0 appends the ids of entries that were deleted from the stream and
|
|
234
|
+
# dropped from the PEL; 6.2 has no third element and reports them as nils inside entries.
|
|
136
235
|
HashifyStreamAutoclaim = lambda { |reply|
|
|
137
236
|
{
|
|
138
237
|
'next' => reply[0],
|
|
139
238
|
'entries' => reply[1].compact.map do |entry, values|
|
|
140
239
|
[entry, values.each_slice(2)&.to_h]
|
|
141
|
-
end
|
|
240
|
+
end,
|
|
241
|
+
'deleted' => reply[2] || []
|
|
142
242
|
}
|
|
143
243
|
}
|
|
144
244
|
|
|
145
245
|
HashifyStreamAutoclaimJustId = lambda { |reply|
|
|
146
246
|
{
|
|
147
247
|
'next' => reply[0],
|
|
148
|
-
'entries' => reply[1]
|
|
248
|
+
'entries' => reply[1],
|
|
249
|
+
'deleted' => reply[2] || []
|
|
149
250
|
}
|
|
150
251
|
}
|
|
151
252
|
|
|
@@ -171,6 +272,12 @@ class Redis
|
|
|
171
272
|
|
|
172
273
|
HashifyClusterNodeInfo = lambda { |str|
|
|
173
274
|
arr = str.split(' ')
|
|
275
|
+
# The first slot field can be a range ("0-5460"), a single slot ("5460" —
|
|
276
|
+
# e.g. a scale-out primary's first migrated slot), or, on a node owning no
|
|
277
|
+
# range at all, a bracketed importing/migrating marker; only owned slots
|
|
278
|
+
# reshape into the Range.
|
|
279
|
+
slots = arr[8] unless arr[8].nil? || arr[8].start_with?('[')
|
|
280
|
+
first_slot, last_slot = slots&.split('-')
|
|
174
281
|
{
|
|
175
282
|
'node_id' => arr[0],
|
|
176
283
|
'ip_port' => arr[1],
|
|
@@ -180,7 +287,7 @@ class Redis
|
|
|
180
287
|
'pong_recv' => arr[5],
|
|
181
288
|
'config_epoch' => arr[6],
|
|
182
289
|
'link_state' => arr[7],
|
|
183
|
-
'slots' =>
|
|
290
|
+
'slots' => first_slot.nil? ? nil : Range.new(first_slot, last_slot || first_slot)
|
|
184
291
|
}
|
|
185
292
|
}
|
|
186
293
|
|
|
@@ -206,8 +313,64 @@ class Redis
|
|
|
206
313
|
reply.map { |str| HashifyClusterNodeInfo.call(str) }
|
|
207
314
|
}
|
|
208
315
|
|
|
316
|
+
# CLUSTER SLOT-STATS: each element is a [slot, metrics] pair. RESP2 replies with the
|
|
317
|
+
# per-slot metrics as a flat [metric, value, ...] array; RESP3 already maps them.
|
|
318
|
+
# Converge on a Hash of metrics for every slot.
|
|
319
|
+
HashifyClusterSlotStats = lambda { |reply|
|
|
320
|
+
reply.map do |slot, metrics|
|
|
321
|
+
[slot, metrics.is_a?(Hash) ? metrics : metrics.each_slice(2).to_h]
|
|
322
|
+
end
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
# HOTKEYS GET: the server wraps the reply map (RESP3) or flat [field, value, ...] array (RESP2)
|
|
326
|
+
# in a one-element array, and the `by-*` lists are flat [key, value, ...] under both protocols.
|
|
327
|
+
HashifyHotkeys = lambda { |reply|
|
|
328
|
+
return reply unless reply.is_a?(Array)
|
|
329
|
+
|
|
330
|
+
hash = Hashify.call(reply.first)
|
|
331
|
+
%w[by-cpu-time-us by-net-bytes].each do |field|
|
|
332
|
+
hash[field] = Pairify.call(hash[field]) if hash.key?(field)
|
|
333
|
+
end
|
|
334
|
+
hash
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
# FUNCTION LIST: RESP2 replies with a flat [k, v, ...] array per library (and per
|
|
338
|
+
# function within it); RESP3 already returns maps. Converge on nested Hashes.
|
|
339
|
+
HashifyFunctionList = lambda { |reply|
|
|
340
|
+
reply.map do |library|
|
|
341
|
+
library = library.each_slice(2).to_h unless library.is_a?(Hash)
|
|
342
|
+
library["functions"] = library["functions"].map do |function|
|
|
343
|
+
function.is_a?(Hash) ? function : function.each_slice(2).to_h
|
|
344
|
+
end
|
|
345
|
+
library
|
|
346
|
+
end
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
# FUNCTION STATS: RESP2 replies with nested flat [k, v, ...] arrays; RESP3 with maps.
|
|
350
|
+
HashifyFunctionStats = lambda { |reply|
|
|
351
|
+
reply = reply.each_slice(2).to_h unless reply.is_a?(Hash)
|
|
352
|
+
|
|
353
|
+
running = reply["running_script"]
|
|
354
|
+
reply["running_script"] = running.each_slice(2).to_h if running.is_a?(Array)
|
|
355
|
+
|
|
356
|
+
engines = reply["engines"]
|
|
357
|
+
engines = engines.each_slice(2).to_h unless engines.is_a?(Hash)
|
|
358
|
+
reply["engines"] = engines.transform_values do |stats|
|
|
359
|
+
stats.is_a?(Hash) ? stats : stats.each_slice(2).to_h
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
reply
|
|
363
|
+
}
|
|
364
|
+
|
|
209
365
|
Noop = ->(reply) { reply }
|
|
210
366
|
|
|
367
|
+
if defined?(Ractor)
|
|
368
|
+
constants(false).each do |name|
|
|
369
|
+
value = const_get(name, false)
|
|
370
|
+
Ractor.make_shareable(value) if value.is_a?(Proc)
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
211
374
|
# Sends a command to Redis and returns its reply.
|
|
212
375
|
#
|
|
213
376
|
# Replies are converted to Ruby objects according to the RESP protocol, so
|