valkey-rb 1.0.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 +7 -0
- data/.rubocop.yml +58 -0
- data/.rubocop_todo.yml +22 -0
- data/README.md +95 -0
- data/Rakefile +23 -0
- data/lib/valkey/bindings.rb +224 -0
- data/lib/valkey/commands/bitmap_commands.rb +86 -0
- data/lib/valkey/commands/cluster_commands.rb +259 -0
- data/lib/valkey/commands/connection_commands.rb +318 -0
- data/lib/valkey/commands/function_commands.rb +255 -0
- data/lib/valkey/commands/generic_commands.rb +525 -0
- data/lib/valkey/commands/geo_commands.rb +87 -0
- data/lib/valkey/commands/hash_commands.rb +587 -0
- data/lib/valkey/commands/hyper_log_log_commands.rb +51 -0
- data/lib/valkey/commands/json_commands.rb +389 -0
- data/lib/valkey/commands/list_commands.rb +348 -0
- data/lib/valkey/commands/module_commands.rb +125 -0
- data/lib/valkey/commands/pubsub_commands.rb +237 -0
- data/lib/valkey/commands/scripting_commands.rb +286 -0
- data/lib/valkey/commands/server_commands.rb +961 -0
- data/lib/valkey/commands/set_commands.rb +220 -0
- data/lib/valkey/commands/sorted_set_commands.rb +971 -0
- data/lib/valkey/commands/stream_commands.rb +636 -0
- data/lib/valkey/commands/string_commands.rb +359 -0
- data/lib/valkey/commands/transaction_commands.rb +175 -0
- data/lib/valkey/commands/vector_search_commands.rb +271 -0
- data/lib/valkey/commands.rb +68 -0
- data/lib/valkey/errors.rb +41 -0
- data/lib/valkey/libglide_ffi.so +0 -0
- data/lib/valkey/opentelemetry.rb +207 -0
- data/lib/valkey/pipeline.rb +20 -0
- data/lib/valkey/protobuf/command_request_pb.rb +51 -0
- data/lib/valkey/protobuf/connection_request_pb.rb +51 -0
- data/lib/valkey/protobuf/response_pb.rb +39 -0
- data/lib/valkey/pubsub_callback.rb +10 -0
- data/lib/valkey/request_error_type.rb +10 -0
- data/lib/valkey/request_type.rb +436 -0
- data/lib/valkey/response_type.rb +20 -0
- data/lib/valkey/utils.rb +253 -0
- data/lib/valkey/version.rb +5 -0
- data/lib/valkey.rb +551 -0
- metadata +119 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Valkey
|
|
4
|
+
module Commands
|
|
5
|
+
# This module contains commands related to RedisJSON / Valkey JSON.
|
|
6
|
+
#
|
|
7
|
+
# @see https://redis.io/docs/stack/json/
|
|
8
|
+
#
|
|
9
|
+
module JsonCommands
|
|
10
|
+
# Get JSON value at path.
|
|
11
|
+
#
|
|
12
|
+
# @example Get entire JSON document
|
|
13
|
+
# valkey.json_get("user:1")
|
|
14
|
+
# # => "{\"name\":\"John\",\"age\":30}"
|
|
15
|
+
# @example Get specific path
|
|
16
|
+
# valkey.json_get("user:1", "$.name")
|
|
17
|
+
# # => "[\"John\"]"
|
|
18
|
+
#
|
|
19
|
+
# @param [String] key the key
|
|
20
|
+
# @param [Array<String>] paths optional paths to retrieve
|
|
21
|
+
# @return [String, nil] JSON string or nil if key doesn't exist
|
|
22
|
+
#
|
|
23
|
+
# @see https://redis.io/commands/json.get/
|
|
24
|
+
def json_get(key, *paths)
|
|
25
|
+
args = [key] + paths
|
|
26
|
+
send_command(RequestType::JSON_GET, args)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Set JSON value at path.
|
|
30
|
+
#
|
|
31
|
+
# @example Set entire document
|
|
32
|
+
# valkey.json_set("user:1", "$", '{"name":"John","age":30}')
|
|
33
|
+
# # => "OK"
|
|
34
|
+
# @example Set specific path
|
|
35
|
+
# valkey.json_set("user:1", "$.age", "31")
|
|
36
|
+
# # => "OK"
|
|
37
|
+
#
|
|
38
|
+
# @param [String] key the key
|
|
39
|
+
# @param [String] path the JSON path
|
|
40
|
+
# @param [String] value the JSON value
|
|
41
|
+
# @return [String] "OK"
|
|
42
|
+
#
|
|
43
|
+
# @see https://redis.io/commands/json.set/
|
|
44
|
+
def json_set(key, path, value)
|
|
45
|
+
send_command(RequestType::JSON_SET, [key, path, value])
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Delete JSON value at path.
|
|
49
|
+
#
|
|
50
|
+
# @example Delete entire document
|
|
51
|
+
# valkey.json_del("user:1")
|
|
52
|
+
# # => 1
|
|
53
|
+
# @example Delete specific path
|
|
54
|
+
# valkey.json_del("user:1", "$.age")
|
|
55
|
+
# # => 1
|
|
56
|
+
#
|
|
57
|
+
# @param [String] key the key
|
|
58
|
+
# @param [String] path optional path (default: root)
|
|
59
|
+
# @return [Integer] number of paths deleted
|
|
60
|
+
#
|
|
61
|
+
# @see https://redis.io/commands/json.del/
|
|
62
|
+
def json_del(key, path = nil)
|
|
63
|
+
args = [key]
|
|
64
|
+
args << path if path
|
|
65
|
+
send_command(RequestType::JSON_DEL, args)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Alias for json_del (deprecated).
|
|
69
|
+
#
|
|
70
|
+
# @param [String] key the key
|
|
71
|
+
# @param [String] path optional path
|
|
72
|
+
# @return [Integer] number of paths deleted
|
|
73
|
+
#
|
|
74
|
+
# @see https://redis.io/commands/json.forget/
|
|
75
|
+
def json_forget(key, path = nil)
|
|
76
|
+
args = [key]
|
|
77
|
+
args << path if path
|
|
78
|
+
send_command(RequestType::JSON_FORGET, args)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Get multiple JSON values.
|
|
82
|
+
#
|
|
83
|
+
# @example Get from multiple keys
|
|
84
|
+
# valkey.json_mget("user:1", "user:2", "$.name")
|
|
85
|
+
# # => ["[\"John\"]", "[\"Jane\"]"]
|
|
86
|
+
#
|
|
87
|
+
# @param [Array<String>] keys_and_path keys followed by path
|
|
88
|
+
# @return [Array<String, nil>] array of JSON strings
|
|
89
|
+
#
|
|
90
|
+
# @see https://redis.io/commands/json.mget/
|
|
91
|
+
def json_mget(*keys_and_path)
|
|
92
|
+
send_command(RequestType::JSON_MGET, keys_and_path)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Get the type of JSON value at path.
|
|
96
|
+
#
|
|
97
|
+
# @example Get type
|
|
98
|
+
# valkey.json_type("user:1", "$.age")
|
|
99
|
+
# # => ["integer"]
|
|
100
|
+
#
|
|
101
|
+
# @param [String] key the key
|
|
102
|
+
# @param [String] path optional path
|
|
103
|
+
# @return [Array<String>] array of type names
|
|
104
|
+
#
|
|
105
|
+
# @see https://redis.io/commands/json.type/
|
|
106
|
+
def json_type(key, path = nil)
|
|
107
|
+
args = [key]
|
|
108
|
+
args << path if path
|
|
109
|
+
send_command(RequestType::JSON_TYPE, args)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Increment number at path.
|
|
113
|
+
#
|
|
114
|
+
# @example Increment age
|
|
115
|
+
# valkey.json_numincrby("user:1", "$.age", 1)
|
|
116
|
+
# # => "[31]"
|
|
117
|
+
#
|
|
118
|
+
# @param [String] key the key
|
|
119
|
+
# @param [String] path the JSON path
|
|
120
|
+
# @param [Numeric] value the increment value
|
|
121
|
+
# @return [String] JSON array of new values
|
|
122
|
+
#
|
|
123
|
+
# @see https://redis.io/commands/json.numincrby/
|
|
124
|
+
def json_numincrby(key, path, value)
|
|
125
|
+
send_command(RequestType::JSON_NUM_INCR_BY, [key, path, value.to_s])
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Multiply number at path.
|
|
129
|
+
#
|
|
130
|
+
# @example Multiply price
|
|
131
|
+
# valkey.json_nummultby("product:1", "$.price", 1.1)
|
|
132
|
+
# # => "[110.0]"
|
|
133
|
+
#
|
|
134
|
+
# @param [String] key the key
|
|
135
|
+
# @param [String] path the JSON path
|
|
136
|
+
# @param [Numeric] value the multiplier
|
|
137
|
+
# @return [String] JSON array of new values
|
|
138
|
+
#
|
|
139
|
+
# @see https://redis.io/commands/json.nummultby/
|
|
140
|
+
def json_nummultby(key, path, value)
|
|
141
|
+
send_command(RequestType::JSON_NUM_MULT_BY, [key, path, value.to_s])
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Append string to JSON string at path.
|
|
145
|
+
#
|
|
146
|
+
# @example Append to name
|
|
147
|
+
# valkey.json_strappend("user:1", "$.name", '" Jr."')
|
|
148
|
+
# # => [8]
|
|
149
|
+
#
|
|
150
|
+
# @param [String] key the key
|
|
151
|
+
# @param [String] path the JSON path
|
|
152
|
+
# @param [String] value the string to append
|
|
153
|
+
# @return [Array<Integer>] array of new string lengths
|
|
154
|
+
#
|
|
155
|
+
# @see https://redis.io/commands/json.strappend/
|
|
156
|
+
def json_strappend(key, path, value)
|
|
157
|
+
send_command(RequestType::JSON_STR_APPEND, [key, path, value])
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Get length of JSON string at path.
|
|
161
|
+
#
|
|
162
|
+
# @example Get string length
|
|
163
|
+
# valkey.json_strlen("user:1", "$.name")
|
|
164
|
+
# # => [4]
|
|
165
|
+
#
|
|
166
|
+
# @param [String] key the key
|
|
167
|
+
# @param [String] path optional path
|
|
168
|
+
# @return [Array<Integer, nil>] array of string lengths
|
|
169
|
+
#
|
|
170
|
+
# @see https://redis.io/commands/json.strlen/
|
|
171
|
+
def json_strlen(key, path = nil)
|
|
172
|
+
args = [key]
|
|
173
|
+
args << path if path
|
|
174
|
+
send_command(RequestType::JSON_STR_LEN, args)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Append values to JSON array at path.
|
|
178
|
+
#
|
|
179
|
+
# @example Append to array
|
|
180
|
+
# valkey.json_arrappend("user:1", "$.tags", '"ruby"', '"valkey"')
|
|
181
|
+
# # => [3]
|
|
182
|
+
#
|
|
183
|
+
# @param [String] key the key
|
|
184
|
+
# @param [String] path the JSON path
|
|
185
|
+
# @param [Array<String>] values JSON values to append
|
|
186
|
+
# @return [Array<Integer>] array of new array lengths
|
|
187
|
+
#
|
|
188
|
+
# @see https://redis.io/commands/json.arrappend/
|
|
189
|
+
def json_arrappend(key, path, *values)
|
|
190
|
+
send_command(RequestType::JSON_ARR_APPEND, [key, path] + values)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Get index of value in JSON array.
|
|
194
|
+
#
|
|
195
|
+
# @example Find index
|
|
196
|
+
# valkey.json_arrindex("user:1", "$.tags", '"ruby"')
|
|
197
|
+
# # => [0]
|
|
198
|
+
#
|
|
199
|
+
# @param [String] key the key
|
|
200
|
+
# @param [String] path the JSON path
|
|
201
|
+
# @param [String] value the value to search for
|
|
202
|
+
# @param [Integer] start optional start index
|
|
203
|
+
# @param [Integer] stop optional stop index
|
|
204
|
+
# @return [Array<Integer>] array of indices (-1 if not found)
|
|
205
|
+
#
|
|
206
|
+
# @see https://redis.io/commands/json.arrindex/
|
|
207
|
+
def json_arrindex(key, path, value, start = nil, stop = nil)
|
|
208
|
+
args = [key, path, value]
|
|
209
|
+
args << start.to_s if start
|
|
210
|
+
args << stop.to_s if stop
|
|
211
|
+
send_command(RequestType::JSON_ARR_INDEX, args)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Insert values into JSON array at index.
|
|
215
|
+
#
|
|
216
|
+
# @example Insert at index
|
|
217
|
+
# valkey.json_arrinsert("user:1", "$.tags", 1, '"python"')
|
|
218
|
+
# # => [3]
|
|
219
|
+
#
|
|
220
|
+
# @param [String] key the key
|
|
221
|
+
# @param [String] path the JSON path
|
|
222
|
+
# @param [Integer] index the index to insert at
|
|
223
|
+
# @param [Array<String>] values JSON values to insert
|
|
224
|
+
# @return [Array<Integer>] array of new array lengths
|
|
225
|
+
#
|
|
226
|
+
# @see https://redis.io/commands/json.arrinsert/
|
|
227
|
+
def json_arrinsert(key, path, index, *values)
|
|
228
|
+
send_command(RequestType::JSON_ARR_INSERT, [key, path, index.to_s] + values)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Get length of JSON array at path.
|
|
232
|
+
#
|
|
233
|
+
# @example Get array length
|
|
234
|
+
# valkey.json_arrlen("user:1", "$.tags")
|
|
235
|
+
# # => [2]
|
|
236
|
+
#
|
|
237
|
+
# @param [String] key the key
|
|
238
|
+
# @param [String] path optional path
|
|
239
|
+
# @return [Array<Integer, nil>] array of array lengths
|
|
240
|
+
#
|
|
241
|
+
# @see https://redis.io/commands/json.arrlen/
|
|
242
|
+
def json_arrlen(key, path = nil)
|
|
243
|
+
args = [key]
|
|
244
|
+
args << path if path
|
|
245
|
+
send_command(RequestType::JSON_ARR_LEN, args)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Pop element from JSON array.
|
|
249
|
+
#
|
|
250
|
+
# @example Pop last element
|
|
251
|
+
# valkey.json_arrpop("user:1", "$.tags")
|
|
252
|
+
# # => ["\"ruby\""]
|
|
253
|
+
# @example Pop at index
|
|
254
|
+
# valkey.json_arrpop("user:1", "$.tags", 0)
|
|
255
|
+
# # => ["\"python\""]
|
|
256
|
+
#
|
|
257
|
+
# @param [String] key the key
|
|
258
|
+
# @param [String] path optional path
|
|
259
|
+
# @param [Integer] index optional index (default: -1)
|
|
260
|
+
# @return [Array<String, nil>] array of popped values
|
|
261
|
+
#
|
|
262
|
+
# @see https://redis.io/commands/json.arrpop/
|
|
263
|
+
def json_arrpop(key, path = nil, index = nil)
|
|
264
|
+
args = [key]
|
|
265
|
+
args << path if path
|
|
266
|
+
args << index.to_s if index
|
|
267
|
+
send_command(RequestType::JSON_ARR_POP, args)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Trim JSON array to specified range.
|
|
271
|
+
#
|
|
272
|
+
# @example Trim array
|
|
273
|
+
# valkey.json_arrtrim("user:1", "$.tags", 0, 1)
|
|
274
|
+
# # => [2]
|
|
275
|
+
#
|
|
276
|
+
# @param [String] key the key
|
|
277
|
+
# @param [String] path the JSON path
|
|
278
|
+
# @param [Integer] start start index
|
|
279
|
+
# @param [Integer] stop stop index
|
|
280
|
+
# @return [Array<Integer>] array of new array lengths
|
|
281
|
+
#
|
|
282
|
+
# @see https://redis.io/commands/json.arrtrim/
|
|
283
|
+
def json_arrtrim(key, path, start, stop)
|
|
284
|
+
send_command(RequestType::JSON_ARR_TRIM, [key, path, start.to_s, stop.to_s])
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Get keys of JSON object at path.
|
|
288
|
+
#
|
|
289
|
+
# @example Get object keys
|
|
290
|
+
# valkey.json_objkeys("user:1", "$")
|
|
291
|
+
# # => [["name", "age"]]
|
|
292
|
+
#
|
|
293
|
+
# @param [String] key the key
|
|
294
|
+
# @param [String] path optional path
|
|
295
|
+
# @return [Array<Array<String>>] array of key arrays
|
|
296
|
+
#
|
|
297
|
+
# @see https://redis.io/commands/json.objkeys/
|
|
298
|
+
def json_objkeys(key, path = nil)
|
|
299
|
+
args = [key]
|
|
300
|
+
args << path if path
|
|
301
|
+
send_command(RequestType::JSON_OBJ_KEYS, args)
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Get number of keys in JSON object at path.
|
|
305
|
+
#
|
|
306
|
+
# @example Get object length
|
|
307
|
+
# valkey.json_objlen("user:1", "$")
|
|
308
|
+
# # => [2]
|
|
309
|
+
#
|
|
310
|
+
# @param [String] key the key
|
|
311
|
+
# @param [String] path optional path
|
|
312
|
+
# @return [Array<Integer, nil>] array of key counts
|
|
313
|
+
#
|
|
314
|
+
# @see https://redis.io/commands/json.objlen/
|
|
315
|
+
def json_objlen(key, path = nil)
|
|
316
|
+
args = [key]
|
|
317
|
+
args << path if path
|
|
318
|
+
send_command(RequestType::JSON_OBJ_LEN, args)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Clear container values at path.
|
|
322
|
+
#
|
|
323
|
+
# @example Clear array
|
|
324
|
+
# valkey.json_clear("user:1", "$.tags")
|
|
325
|
+
# # => 1
|
|
326
|
+
#
|
|
327
|
+
# @param [String] key the key
|
|
328
|
+
# @param [String] path optional path
|
|
329
|
+
# @return [Integer] number of paths cleared
|
|
330
|
+
#
|
|
331
|
+
# @see https://redis.io/commands/json.clear/
|
|
332
|
+
def json_clear(key, path = nil)
|
|
333
|
+
args = [key]
|
|
334
|
+
args << path if path
|
|
335
|
+
send_command(RequestType::JSON_CLEAR, args)
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# Toggle boolean value at path.
|
|
339
|
+
#
|
|
340
|
+
# @example Toggle boolean
|
|
341
|
+
# valkey.json_toggle("user:1", "$.active")
|
|
342
|
+
# # => [1]
|
|
343
|
+
#
|
|
344
|
+
# @param [String] key the key
|
|
345
|
+
# @param [String] path the JSON path
|
|
346
|
+
# @return [Array<Integer>] array of new boolean values (0 or 1)
|
|
347
|
+
#
|
|
348
|
+
# @see https://redis.io/commands/json.toggle/
|
|
349
|
+
def json_toggle(key, path)
|
|
350
|
+
send_command(RequestType::JSON_TOGGLE, [key, path])
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Get debug information about JSON value.
|
|
354
|
+
#
|
|
355
|
+
# @example Get memory usage
|
|
356
|
+
# valkey.json_debug("MEMORY", "user:1", "$")
|
|
357
|
+
# # => [120]
|
|
358
|
+
#
|
|
359
|
+
# @param [String] subcommand the debug subcommand
|
|
360
|
+
# @param [String] key the key
|
|
361
|
+
# @param [String] path optional path
|
|
362
|
+
# @return [Object] depends on subcommand
|
|
363
|
+
#
|
|
364
|
+
# @see https://redis.io/commands/json.debug/
|
|
365
|
+
def json_debug(subcommand, key, path = nil)
|
|
366
|
+
args = [subcommand, key]
|
|
367
|
+
args << path if path
|
|
368
|
+
send_command(RequestType::JSON_DEBUG, args)
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# Get JSON value in RESP format.
|
|
372
|
+
#
|
|
373
|
+
# @example Get as RESP
|
|
374
|
+
# valkey.json_resp("user:1", "$")
|
|
375
|
+
# # => [["name", "John"], ["age", 30]]
|
|
376
|
+
#
|
|
377
|
+
# @param [String] key the key
|
|
378
|
+
# @param [String] path optional path
|
|
379
|
+
# @return [Object] RESP representation
|
|
380
|
+
#
|
|
381
|
+
# @see https://redis.io/commands/json.resp/
|
|
382
|
+
def json_resp(key, path = nil)
|
|
383
|
+
args = [key]
|
|
384
|
+
args << path if path
|
|
385
|
+
send_command(RequestType::JSON_RESP, args)
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
end
|