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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +114 -0
  3. data/README.md +172 -5
  4. data/lib/redis/client.rb +5 -1
  5. data/lib/redis/commands/arrays.rb +358 -0
  6. data/lib/redis/commands/bitmaps.rb +24 -0
  7. data/lib/redis/commands/bless.rb +115 -0
  8. data/lib/redis/commands/cluster.rb +9 -2
  9. data/lib/redis/commands/hashes.rb +250 -7
  10. data/lib/redis/commands/keys.rb +48 -11
  11. data/lib/redis/commands/modules/json.rb +27 -2
  12. data/lib/redis/commands/modules/search/aggregation.rb +2 -1
  13. data/lib/redis/commands/modules/search/field.rb +11 -2
  14. data/lib/redis/commands/modules/search/hybrid.rb +3 -0
  15. data/lib/redis/commands/modules/search/index.rb +2 -1
  16. data/lib/redis/commands/modules/search/miscellaneous.rb +2 -1
  17. data/lib/redis/commands/modules/search/query.rb +2 -2
  18. data/lib/redis/commands/modules/search/schema.rb +4 -0
  19. data/lib/redis/commands/scripting.rb +109 -0
  20. data/lib/redis/commands/server.rb +158 -7
  21. data/lib/redis/commands/sets.rb +270 -0
  22. data/lib/redis/commands/sorted_sets.rb +131 -6
  23. data/lib/redis/commands/streams.rb +284 -33
  24. data/lib/redis/commands/strings.rb +232 -6
  25. data/lib/redis/commands/transactions.rb +6 -1
  26. data/lib/redis/commands/vector_sets.rb +407 -0
  27. data/lib/redis/commands.rb +169 -6
  28. data/lib/redis/distributed.rb +539 -22
  29. data/lib/redis/hash_ring.rb +2 -1
  30. data/lib/redis/keyspace_notifications/channels.rb +121 -0
  31. data/lib/redis/keyspace_notifications/manager.rb +1121 -0
  32. data/lib/redis/keyspace_notifications/notification.rb +95 -0
  33. data/lib/redis/keyspace_notifications/parser.rb +170 -0
  34. data/lib/redis/keyspace_notifications.rb +59 -0
  35. data/lib/redis/pipeline.rb +17 -5
  36. data/lib/redis/ractorize.rb +6 -0
  37. data/lib/redis/subscribe.rb +80 -2
  38. data/lib/redis/version.rb +1 -1
  39. data/lib/redis/xxh3.rb +354 -0
  40. data/lib/redis.rb +29 -8
  41. metadata +16 -6
@@ -22,6 +22,7 @@ class Redis
22
22
  # @return [Integer] The number of fields that were added to the hash
23
23
  def hset(key, *attrs)
24
24
  attrs = attrs.first.flatten if attrs.size == 1 && attrs.first.is_a?(Hash)
25
+ return 0 if attrs.empty?
25
26
 
26
27
  send_command([:hset, key, *attrs])
27
28
  end
@@ -158,6 +159,106 @@ class Redis
158
159
  send_command([:hdel, key].concat(fields))
159
160
  end
160
161
 
162
+ # Get the value of one or more hash fields and delete them.
163
+ #
164
+ # @example
165
+ # redis.hset("hash", "f1", "v1", "f2", "v2")
166
+ # redis.hgetdel("hash", "f1", "f2") # => ["v1", "v2"]
167
+ # redis.hgetdel("hash", "f1", "f3") # => [nil, nil]
168
+ #
169
+ # @param [String] key
170
+ # @param [Array<String>] fields
171
+ # @return [Array<String, nil>] the value of each requested field, `nil` for fields that
172
+ # did not exist
173
+ def hgetdel(key, *fields)
174
+ fields.flatten!(1)
175
+ send_command([:hgetdel, key, 'FIELDS', fields.length, *fields])
176
+ end
177
+
178
+ # Get the value of one or more hash fields and optionally set their expiration. HGETEX is
179
+ # similar to HMGET, but is a write command with additional options. When no options are
180
+ # provided, HGETEX behaves like HMGET.
181
+ #
182
+ # @example
183
+ # redis.hset("hash", "f1", "v1", "f2", "v2")
184
+ # redis.hgetex("hash", "f1", "f2") # => ["v1", "v2"]
185
+ # redis.hgetex("hash", "f1", ex: 60) # => ["v1"]
186
+ #
187
+ # @param [String] key
188
+ # @param [Array<String>] fields
189
+ # @param [Hash] options
190
+ # - `:ex => Integer`: Set the specified expire time on the fields, in seconds.
191
+ # - `:px => Integer`: Set the specified expire time on the fields, in milliseconds.
192
+ # - `:exat => Integer`: Set the specified Unix time at which the fields will expire, in
193
+ # seconds.
194
+ # - `:pxat => Integer`: Set the specified Unix time at which the fields will expire, in
195
+ # milliseconds.
196
+ # - `:persist => true`: Remove the time to live associated with the fields.
197
+ # @return [Array<String, nil>] the value of each requested field, `nil` for fields that
198
+ # did not exist
199
+ def hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false)
200
+ if [ex, px, exat, pxat, persist].count { |option| option } > 1
201
+ raise ArgumentError, "ex, px, exat, pxat, and persist are mutually exclusive"
202
+ end
203
+
204
+ fields.flatten!(1)
205
+ args = [:hgetex, key]
206
+ args << "EX" << Integer(ex) if ex
207
+ args << "PX" << Integer(px) if px
208
+ args << "EXAT" << Integer(exat) if exat
209
+ args << "PXAT" << Integer(pxat) if pxat
210
+ args << "PERSIST" if persist
211
+ args.concat(['FIELDS', fields.length, *fields])
212
+
213
+ send_command(args)
214
+ end
215
+
216
+ # Set the value of one or more hash fields, and optionally their expiration. Existing
217
+ # values for the given fields are overwritten, and any previous TTL on those fields is
218
+ # discarded unless `:keepttl` is given.
219
+ #
220
+ # @example
221
+ # redis.hsetex("hash", "f1", "v1", "f2", "v2") # => 1
222
+ # redis.hsetex("hash", { "f1" => "v1", "f2" => "v2" }, ex: 60) # => 1
223
+ #
224
+ # @param [String] key
225
+ # @param [Array<String> | Hash<String, String>] attrs array or hash of fields and values
226
+ # @param [Hash] options
227
+ # - `:fnx => true`: Only set the fields if none of them already exist. Mutually
228
+ # exclusive with `:fxx`.
229
+ # - `:fxx => true`: Only set the fields if all of them already exist. Mutually
230
+ # exclusive with `:fnx`.
231
+ # - `:ex => Integer`: Set the specified expire time on the fields, in seconds.
232
+ # - `:px => Integer`: Set the specified expire time on the fields, in milliseconds.
233
+ # - `:exat => Integer`: Set the specified Unix time at which the fields will expire, in
234
+ # seconds.
235
+ # - `:pxat => Integer`: Set the specified Unix time at which the fields will expire, in
236
+ # milliseconds.
237
+ # - `:keepttl => true`: Retain the time to live already associated with the fields.
238
+ # @return [Integer] `1` if all the fields were set, `0` if none were set (e.g. the
239
+ # `:fnx`/`:fxx` condition was not met)
240
+ def hsetex(key, *attrs, fnx: nil, fxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false)
241
+ raise ArgumentError, "fnx and fxx are mutually exclusive" if fnx && fxx
242
+ if [ex, px, exat, pxat, keepttl].count { |option| option } > 1
243
+ raise ArgumentError, "ex, px, exat, pxat, and keepttl are mutually exclusive"
244
+ end
245
+
246
+ attrs = attrs.first.flatten if attrs.size == 1 && attrs.first.is_a?(Hash)
247
+ attrs.flatten!(1)
248
+
249
+ args = [:hsetex, key]
250
+ args << "FNX" if fnx
251
+ args << "FXX" if fxx
252
+ args << "EX" << Integer(ex) if ex
253
+ args << "PX" << Integer(px) if px
254
+ args << "EXAT" << Integer(exat) if exat
255
+ args << "PXAT" << Integer(pxat) if pxat
256
+ args << "KEEPTTL" if keepttl
257
+ args.concat(['FIELDS', attrs.length / 2, *attrs])
258
+
259
+ send_command(args)
260
+ end
261
+
161
262
  # Determine if a hash field exists.
162
263
  #
163
264
  # @param [String] key
@@ -267,15 +368,27 @@ class Redis
267
368
  # @example
268
369
  # redis.hset("hash", "f1", "v1")
269
370
  # redis.hexpire("hash", 10, "f1", "f2") # => [1, -2]
371
+ # redis.hexpire("hash", 10, "f1", "f2", nx: true) # => [0, -2]
270
372
  #
271
373
  # @param [String] key
272
374
  # @param [Integer] ttl
375
+ # @param [Hash] options
376
+ # - `:nx => true`: Set expiry only when the field has no expiry.
377
+ # - `:xx => true`: Set expiry only when the field has an existing expiry.
378
+ # - `:gt => true`: Set expiry only when the new expiry is greater than current one.
379
+ # - `:lt => true`: Set expiry only when the new expiry is less than current one.
273
380
  # @param [Array<String>] fields
274
381
  # @return [Array<Integer>] Feedback on if the fields have been updated.
382
+ # @raise [ArgumentError] when more than one of `:nx`, `:xx`, `:gt`, `:lt` is given
275
383
  #
276
384
  # See https://redis.io/docs/latest/commands/hexpire/#return-information for array reply.
277
- def hexpire(key, ttl, *fields)
278
- send_command([:hexpire, key, ttl, 'FIELDS', fields.length, *fields])
385
+ def hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
386
+ fields.flatten!(1)
387
+ args = [:hexpire, key, Integer(ttl)]
388
+ args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
389
+ args.concat(['FIELDS', fields.length, *fields])
390
+
391
+ send_command(args)
279
392
  end
280
393
 
281
394
  # Returns the time to live in seconds for one or more fields.
@@ -291,6 +404,7 @@ class Redis
291
404
  #
292
405
  # See https://redis.io/docs/latest/commands/httl/#return-information for array reply.
293
406
  def httl(key, *fields)
407
+ fields.flatten!(1)
294
408
  send_command([:httl, key, 'FIELDS', fields.length, *fields])
295
409
  end
296
410
 
@@ -310,14 +424,13 @@ class Redis
310
424
  # - `:lt => true`: Set expiry only when the new expiry is less than current one.
311
425
  # @param [Array<String>] fields
312
426
  # @return [Array<Integer>] Feedback on if the fields have been updated.
427
+ # @raise [ArgumentError] when more than one of `:nx`, `:xx`, `:gt`, `:lt` is given
313
428
  #
314
429
  # See https://redis.io/docs/latest/commands/hpexpire/#return-information for array reply.
315
430
  def hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
316
- args = [:hpexpire, key, ttl]
317
- args << "NX" if nx
318
- args << "XX" if xx
319
- args << "GT" if gt
320
- args << "LT" if lt
431
+ fields.flatten!(1)
432
+ args = [:hpexpire, key, Integer(ttl)]
433
+ args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
321
434
  args.concat(['FIELDS', fields.length, *fields])
322
435
 
323
436
  send_command(args)
@@ -336,9 +449,124 @@ class Redis
336
449
  #
337
450
  # See https://redis.io/docs/latest/commands/hpttl/#return-information for array reply.
338
451
  def hpttl(key, *fields)
452
+ fields.flatten!(1)
339
453
  send_command([:hpttl, key, 'FIELDS', fields.length, *fields])
340
454
  end
341
455
 
456
+ # Sets the expiration for one or more fields as an absolute Unix
457
+ # timestamp in seconds. A timestamp in the past deletes the field
458
+ # immediately.
459
+ #
460
+ # @example
461
+ # redis.hset("hash", "f1", "v1")
462
+ # redis.hexpireat("hash", Time.now.to_i + 10, "f1", "f2") # => [1, -2]
463
+ # redis.hexpireat("hash", Time.now.to_i - 10, "f1") # => [2]
464
+ #
465
+ # @param [String] key
466
+ # @param [Integer] unix_time_seconds absolute expiration timestamp in seconds since epoch
467
+ # @param [Hash] options
468
+ # - `:nx => true`: Set expiry only when the field has no expiry.
469
+ # - `:xx => true`: Set expiry only when the field has an existing expiry.
470
+ # - `:gt => true`: Set expiry only when the new expiry is greater than current one.
471
+ # - `:lt => true`: Set expiry only when the new expiry is less than current one.
472
+ # @param [Array<String>] fields
473
+ # @return [Array<Integer>] Feedback on if the fields have been updated.
474
+ # @raise [ArgumentError] when more than one of `:nx`, `:xx`, `:gt`, `:lt` is given
475
+ #
476
+ # See https://redis.io/docs/latest/commands/hexpireat/#return-information for array reply.
477
+ def hexpireat(key, unix_time_seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
478
+ fields.flatten!(1)
479
+ args = [:hexpireat, key, Integer(unix_time_seconds)]
480
+ args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
481
+ args.concat(['FIELDS', fields.length, *fields])
482
+
483
+ send_command(args)
484
+ end
485
+
486
+ # Sets the expiration for one or more fields as an absolute Unix
487
+ # timestamp in milliseconds. A timestamp in the past deletes the field
488
+ # immediately.
489
+ #
490
+ # @example
491
+ # redis.hset("hash", "f1", "v1")
492
+ # redis.hpexpireat("hash", (Time.now.to_f * 1000).to_i + 500, "f1", "f2") # => [1, -2]
493
+ # redis.hpexpireat("hash", (Time.now.to_f * 1000).to_i - 500, "f1") # => [2]
494
+ #
495
+ # @param [String] key
496
+ # @param [Integer] unix_time_milliseconds absolute expiration timestamp in milliseconds since epoch
497
+ # @param [Hash] options
498
+ # - `:nx => true`: Set expiry only when the field has no expiry.
499
+ # - `:xx => true`: Set expiry only when the field has an existing expiry.
500
+ # - `:gt => true`: Set expiry only when the new expiry is greater than current one.
501
+ # - `:lt => true`: Set expiry only when the new expiry is less than current one.
502
+ # @param [Array<String>] fields
503
+ # @return [Array<Integer>] Feedback on if the fields have been updated.
504
+ # @raise [ArgumentError] when more than one of `:nx`, `:xx`, `:gt`, `:lt` is given
505
+ #
506
+ # See https://redis.io/docs/latest/commands/hpexpireat/#return-information for array reply.
507
+ def hpexpireat(key, unix_time_milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
508
+ fields.flatten!(1)
509
+ args = [:hpexpireat, key, Integer(unix_time_milliseconds)]
510
+ args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
511
+ args.concat(['FIELDS', fields.length, *fields])
512
+
513
+ send_command(args)
514
+ end
515
+
516
+ # Returns the expiration time of one or more fields as an absolute Unix
517
+ # timestamp in seconds.
518
+ #
519
+ # @example
520
+ # redis.hset("hash", "f1", "v1", "f2", "v2")
521
+ # redis.hexpireat("hash", Time.now.to_i + 100, "f1")
522
+ # redis.hexpiretime("hash", "f1", "f2", "f3") # => [<unix timestamp in seconds>, -1, -2]
523
+ #
524
+ # @param [String] key
525
+ # @param [Array<String>] fields
526
+ # @return [Array<Integer>] Expiration Unix timestamps of the fields in seconds.
527
+ #
528
+ # See https://redis.io/docs/latest/commands/hexpiretime/#return-information for array reply.
529
+ def hexpiretime(key, *fields)
530
+ fields.flatten!(1)
531
+ send_command([:hexpiretime, key, 'FIELDS', fields.length, *fields])
532
+ end
533
+
534
+ # Returns the expiration time of one or more fields as an absolute Unix
535
+ # timestamp in milliseconds.
536
+ #
537
+ # @example
538
+ # redis.hset("hash", "f1", "v1", "f2", "v2")
539
+ # redis.hpexpireat("hash", (Time.now.to_f * 1000).to_i + 100_000, "f1")
540
+ # redis.hpexpiretime("hash", "f1", "f2", "f3") # => [<unix timestamp in milliseconds>, -1, -2]
541
+ #
542
+ # @param [String] key
543
+ # @param [Array<String>] fields
544
+ # @return [Array<Integer>] Expiration Unix timestamps of the fields in milliseconds.
545
+ #
546
+ # See https://redis.io/docs/latest/commands/hpexpiretime/#return-information for array reply.
547
+ def hpexpiretime(key, *fields)
548
+ fields.flatten!(1)
549
+ send_command([:hpexpiretime, key, 'FIELDS', fields.length, *fields])
550
+ end
551
+
552
+ # Removes the expiration from one or more fields, making them persistent.
553
+ #
554
+ # @example
555
+ # redis.hset("hash", "f1", "v1", "f2", "v2")
556
+ # redis.hexpire("hash", 100, "f1")
557
+ # redis.hpersist("hash", "f1", "f2", "f3") # => [1, -1, -2]
558
+ #
559
+ # @param [String] key
560
+ # @param [Array<String>] fields
561
+ # @return [Array<Integer>] `1` when the expiration was removed, `-1` when the
562
+ # field has no expiration, `-2` when the field or key does not exist.
563
+ #
564
+ # See https://redis.io/docs/latest/commands/hpersist/#return-information for array reply.
565
+ def hpersist(key, *fields)
566
+ fields.flatten!(1)
567
+ send_command([:hpersist, key, 'FIELDS', fields.length, *fields])
568
+ end
569
+
342
570
  # Register an ordered list of hash field names under +fieldset_name+ for
343
571
  # use by subsequent #himport_set calls on the same connection.
344
572
  #
@@ -417,6 +645,21 @@ class Redis
417
645
  def himport_discard_all
418
646
  send_command([:himport, "DISCARDALL"])
419
647
  end
648
+
649
+ private
650
+
651
+ # The HEXPIRE command family accepts a single optional condition token
652
+ # (`[NX | XX | GT | LT]`); the server rejects combinations, so fail fast in Ruby.
653
+ def hash_field_expiration_condition(nx, xx, gt, lt)
654
+ condition = []
655
+ condition << "NX" if nx
656
+ condition << "XX" if xx
657
+ condition << "GT" if gt
658
+ condition << "LT" if lt
659
+ raise ArgumentError, "only one of :nx, :xx, :gt, :lt can be specified" if condition.size > 1
660
+
661
+ condition
662
+ end
420
663
  end
421
664
  end
422
665
  end
@@ -412,7 +412,54 @@ class Redis
412
412
  # element specified in `:get`
413
413
  # - when `:store` is specified, the number of elements in the stored result
414
414
  def sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil)
415
- args = [:sort, key]
415
+ _sort(:sort, key, by: by, limit: limit, get: get, order: order, store: store)
416
+ end
417
+
418
+ # Sort the elements in a list, set or sorted set without storing the result.
419
+ #
420
+ # Read-only variant of `sort`: it has no `STORE` option, so it is flagged read-only on the
421
+ # server and can be executed on read-only replicas (a cluster replica in `READONLY` mode
422
+ # redirects plain `SORT` to the master).
423
+ #
424
+ # @example Retrieve the first 2 elements from an alphabetically sorted "list"
425
+ # redis.sort_ro("list", :order => "alpha", :limit => [0, 2])
426
+ # # => ["a", "b"]
427
+ # @example Sort by an external key and fetch each element together with its weight
428
+ # redis.sort_ro("list", :by => "weight_*", :get => ["#", "weight_*"])
429
+ # # => [["c", "10"], ["b", "20"], ["a", "30"]]
430
+ #
431
+ # @param [String] key
432
+ # @param [Hash] options
433
+ # - `:by => String`: use external key to sort elements by
434
+ # - `:limit => [offset, count]`: skip `offset` elements, return a maximum
435
+ # of `count` elements
436
+ # - `:get => [String, Array<String>]`: single key or array of keys to
437
+ # retrieve per element in the result
438
+ # - `:order => String`: combination of `ASC`, `DESC` and optionally `ALPHA`
439
+ #
440
+ # @return [Array<String>, Array<Array<String>>]
441
+ # - when `:get` is not specified, or holds a single element, an array of elements
442
+ # - when `:get` is specified, and holds more than one element, an array of
443
+ # elements where every element is an array with the result for every
444
+ # element specified in `:get`
445
+ #
446
+ # @see #sort
447
+ def sort_ro(key, by: nil, limit: nil, get: nil, order: nil)
448
+ _sort(:sort_ro, key, by: by, limit: limit, get: get, order: order)
449
+ end
450
+
451
+ # Determine the type stored at key.
452
+ #
453
+ # @param [String] key
454
+ # @return [String] `string`, `list`, `set`, `zset`, `hash` or `none`
455
+ def type(key)
456
+ send_command([:type, key])
457
+ end
458
+
459
+ private
460
+
461
+ def _sort(command, key, by:, limit:, get:, order:, store: nil)
462
+ args = [command, key]
416
463
  args << "BY" << by if by
417
464
 
418
465
  if limit
@@ -437,16 +484,6 @@ class Redis
437
484
  end
438
485
  end
439
486
 
440
- # Determine the type stored at key.
441
- #
442
- # @param [String] key
443
- # @return [String] `string`, `list`, `set`, `zset`, `hash` or `none`
444
- def type(key)
445
- send_command([:type, key])
446
- end
447
-
448
- private
449
-
450
487
  def _scan(command, cursor, args, match: nil, count: nil, type: nil, novalues: false, &block)
451
488
  # SSCAN/ZSCAN/HSCAN already prepend the key to +args+.
452
489
 
@@ -14,6 +14,10 @@ class Redis
14
14
  # JSON.NUMINCRBY — DO return differently shaped replies under RESP3 and will need
15
15
  # protocol-aware reshaping once RESP3 is supported at this layer.
16
16
  module Json
17
+ # Floating-point precisions accepted by JSON.SET's FPHA argument (Redis 8.8+).
18
+ JSON_SET_FPHA_TYPES = %w[BF16 FP16 FP32 FP64].freeze
19
+ private_constant :JSON_SET_FPHA_TYPES
20
+
17
21
  # Normalize a JSON.NUMINCRBY reply to a protocol-independent value: an array of numbers for
18
22
  # a JSONPath, a single number for a legacy path. Under RESP2 the result arrives as a
19
23
  # JSON-encoded string ("[3,4]" / "7"); under RESP3 it arrives as native numbers (an array,
@@ -51,6 +55,11 @@ class Redis
51
55
  end
52
56
  end
53
57
 
58
+ if defined?(Ractor)
59
+ Ractor.make_shareable(NumincrbyNormalize)
60
+ Ractor.make_shareable(TypeNormalize)
61
+ end
62
+
54
63
  # Set the JSON value at +path+ in the document stored under +key+.
55
64
  #
56
65
  # By default +value+ is a Ruby object that is serialized to JSON text with
@@ -76,16 +85,32 @@ class Redis
76
85
  # @param [Boolean] nx only set when the path does not already exist
77
86
  # @param [Boolean] xx only set when the path already exists
78
87
  # @param [Boolean] raw treat +value+ as an already-encoded JSON string and send it as-is
88
+ # @param [String, Symbol] fpha store a numeric array as a Floating-Point
89
+ # Homogeneous Array of the given precision — one of +:bf16+, +:fp16+,
90
+ # +:fp32+, +:fp64+ (case-insensitive; Redis 8.8+). All values of the array
91
+ # are forced into the fixed floating-point type: +:bf16+/+:fp16+ halve
92
+ # memory at reduced precision, while +:fp32+/+:fp64+ keep higher precision.
79
93
  # @return [Boolean, String] when +nx+ or +xx+ is given, +true+ on success and +false+
80
94
  # when the condition was not met; otherwise the raw +"OK"+ reply
81
- # @raise [ArgumentError] if both +nx+ and +xx+ are given (they are mutually exclusive)
82
- def json_set(key, path, value, nx: false, xx: false, raw: false)
95
+ # @raise [ArgumentError] if both +nx+ and +xx+ are given (they are mutually exclusive),
96
+ # or if +fpha+ is not one of the supported types
97
+ # @raise [Redis::CommandError] if a value in the array does not fit the chosen
98
+ # +fpha+ type ("value out of range for ...")
99
+ def json_set(key, path, value, nx: false, xx: false, raw: false, fpha: nil)
83
100
  raise ArgumentError, "nx and xx are mutually exclusive" if nx && xx
84
101
 
102
+ if fpha
103
+ fpha = fpha.to_s.upcase
104
+ unless JSON_SET_FPHA_TYPES.include?(fpha)
105
+ raise ArgumentError, "fpha accepts only: #{JSON_SET_FPHA_TYPES.join(', ')}"
106
+ end
107
+ end
108
+
85
109
  value = ::JSON.generate(value) unless raw
86
110
  args = [:"JSON.SET", key, path, value]
87
111
  args << "NX" if nx
88
112
  args << "XX" if xx
113
+ args << "FPHA" << fpha if fpha
89
114
 
90
115
  if nx || xx
91
116
  send_command(args, &BoolifySet)
@@ -108,7 +108,8 @@ class Redis
108
108
  @steps << if fields.empty?
109
109
  ["LOAD", "*"]
110
110
  else
111
- ["LOAD", fields.size, *fields.flatten]
111
+ fields.flatten!(1)
112
+ ["LOAD", fields.size, *fields]
112
113
  end
113
114
  self
114
115
  end
@@ -231,13 +231,22 @@ class Redis
231
231
  # Build a +VECTOR+ field.
232
232
  #
233
233
  # @example
234
- # Redis::Commands::Search::Field::VectorField.new(
234
+ # Redis::Commands::Search::VectorField.new(
235
235
  # "embedding", "HNSW", { type: "FLOAT32", dim: 4, distance_metric: "L2" }
236
236
  # )
237
237
  #
238
+ # @example HNSW with SQ8 scalar quantization (Redis 8.12+)
239
+ # Redis::Commands::Search::VectorField.new(
240
+ # "embedding", "HNSW",
241
+ # { type: "FLOAT32", dim: 4, distance_metric: "L2", compression: "SQ8", training_threshold: 4096 }
242
+ # )
243
+ #
238
244
  # @param [String, Symbol] name the document attribute the field indexes
239
245
  # @param [String, Symbol] algorithm the indexing method, one of +FLAT+, +HNSW+, +SVS-VAMANA+
240
- # @param [Hash] attributes the vector attributes (e.g. +type+, +dim+, +distance_metric+)
246
+ # @param [Hash] attributes the vector attributes (e.g. +type+, +dim+, +distance_metric+).
247
+ # Every pair is sent as-is, so algorithm-specific attributes such as +m+,
248
+ # +ef_construction+, +compression+ or +training_threshold+ need no special handling;
249
+ # an explicit +training_threshold: 0+ is sent (the server treats zero as meaningful).
241
250
  # @option options [String] :as an alias for the field, rendered as +AS <alias>+
242
251
  # @raise [ArgumentError] if +algorithm+ is not a supported indexing method
243
252
  # @raise [Redis::CommandError] if +:sortable+ or +:no_index+ is given
@@ -324,6 +324,9 @@ class Redis
324
324
  end
325
325
 
326
326
  # The +WITHCURSOR+ clause for paging through FT.HYBRID results.
327
+ #
328
+ # @note Only honored by Redis 8.4 through 8.10. Redis 8.11 and later reject the clause with
329
+ # a command error, as it was never part of the documented FT.HYBRID surface.
327
330
  class HybridCursorQuery
328
331
  # @param count [Integer] the cursor +COUNT+ (batch size, 0 to omit)
329
332
  # @param max_idle [Integer] the cursor +MAXIDLE+ in milliseconds (0 to omit)
@@ -267,7 +267,8 @@ class Redis
267
267
  # @param post_processing [Search::HybridPostProcessingConfig, nil] a post-fusion pipeline
268
268
  # @param params_substitution [Hash, nil] query parameter substitutions (+PARAMS+)
269
269
  # @param timeout [Integer, nil] query timeout in milliseconds (+TIMEOUT+)
270
- # @param cursor [Search::HybridCursorQuery, nil] cursor/pagination config (+WITHCURSOR+)
270
+ # @param cursor [Search::HybridCursorQuery, nil] cursor/pagination config (+WITHCURSOR+);
271
+ # only honored by Redis 8.4 through 8.10, rejected by 8.11 and later
271
272
  # @return [Search::HybridResult] the fused results (or per-leg cursor ids for WITHCURSOR)
272
273
  def hybrid_search(query:, combine_method: nil, post_processing: nil,
273
274
  params_substitution: nil, timeout: nil, cursor: nil)
@@ -394,7 +394,8 @@ class Redis
394
394
  # @param post_processing [Search::HybridPostProcessingConfig, nil] a post-fusion pipeline
395
395
  # @param params_substitution [Hash, nil] query parameter substitutions (+PARAMS+)
396
396
  # @param timeout [Integer, nil] query timeout in milliseconds (+TIMEOUT+)
397
- # @param cursor [Search::HybridCursorQuery, nil] cursor/pagination config (+WITHCURSOR+)
397
+ # @param cursor [Search::HybridCursorQuery, nil] cursor/pagination config (+WITHCURSOR+);
398
+ # only honored by Redis 8.4 through 8.10, rejected by 8.11 and later
398
399
  # @return [Search::HybridResult] the fused result rows, total, warnings and execution time
399
400
  # (or, for a WITHCURSOR query, the per-leg cursor ids)
400
401
  def ft_hybrid_search(
@@ -623,8 +623,8 @@ class Redis
623
623
  # TAG values are single literal tokens, so whitespace is significant and must be escaped
624
624
  # too. TEXT queries keep spaces, which separate terms (a multi-word match is an AND).
625
625
  TAG_SPECIAL_CHARACTERS = (TEXT_SPECIAL_CHARACTERS + [' ']).freeze
626
- TEXT_ESCAPE_PATTERN = Regexp.union(TEXT_SPECIAL_CHARACTERS)
627
- TAG_ESCAPE_PATTERN = Regexp.union(TAG_SPECIAL_CHARACTERS)
626
+ TEXT_ESCAPE_PATTERN = Regexp.union(TEXT_SPECIAL_CHARACTERS).freeze
627
+ TAG_ESCAPE_PATTERN = Regexp.union(TAG_SPECIAL_CHARACTERS).freeze
628
628
 
629
629
  # @param field [String] the field name
630
630
  def initialize(field)
@@ -157,6 +157,10 @@ class Redis
157
157
  # @example
158
158
  # vector_field "embedding", "HNSW", type: "FLOAT32", dim: 4, distance_metric: "L2"
159
159
  #
160
+ # @example HNSW with SQ8 scalar quantization (Redis 8.12+)
161
+ # vector_field "embedding", "HNSW", type: "FLOAT32", dim: 4, distance_metric: "L2",
162
+ # compression: "SQ8", training_threshold: 4096
163
+ #
160
164
  # @param [String, Symbol] name the document attribute the field indexes
161
165
  # @param [String, Symbol] algorithm the indexing method (+FLAT+, +HNSW+, +SVS-VAMANA+)
162
166
  # @param [Hash] attributes vector attributes and field-level options
@@ -97,6 +97,115 @@ class Redis
97
97
  _eval(:evalsha, args)
98
98
  end
99
99
 
100
+ # Manage Redis Functions libraries.
101
+ #
102
+ # @example Load a library
103
+ # redis.function(:load, "#!lua name=mylib\n...")
104
+ # # => "mylib"
105
+ # @example Replace an existing library
106
+ # redis.function(:load, code, replace: true)
107
+ # # => "mylib"
108
+ # @example List loaded libraries
109
+ # redis.function(:list)
110
+ # # => [{ "library_name" => "mylib", "engine" => "LUA", "functions" => [...] }]
111
+ # @example List one library including its source code
112
+ # redis.function(:list, libraryname: "mylib", withcode: true)
113
+ # @example Delete a library
114
+ # redis.function(:delete, "mylib")
115
+ # # => "OK"
116
+ # @example Dump and restore all libraries
117
+ # payload = redis.function(:dump)
118
+ # redis.function(:restore, payload, policy: :replace)
119
+ # # => "OK"
120
+ # @example Get engine and running-function statistics
121
+ # redis.function(:stats)
122
+ # # => { "running_script" => nil,
123
+ # # "engines" => { "LUA" => { "libraries_count" => 1, "functions_count" => 2 } } }
124
+ # @example Kill the currently running function
125
+ # redis.function(:kill)
126
+ # # => "OK"
127
+ #
128
+ # @param [String, Symbol] subcommand e.g. `load`, `delete`, `flush`, `list`, `dump`, `restore`, `stats`, `kill`
129
+ # @param [Array<String>] args depends on subcommand
130
+ # @param [Hash] options
131
+ # - `:replace => true`: (`load`) overwrite an existing library of the same name
132
+ # - `:libraryname => String`: (`list`) only list libraries matching this pattern
133
+ # - `:withcode => true`: (`list`) include each library's source code in the reply
134
+ # - `:policy => Symbol`: (`restore`) one of `:append` (default), `:flush`, `:replace`
135
+ # @return [String, Array<Hash>, Hash] depends on subcommand
136
+ #
137
+ # @see #fcall
138
+ # @see #fcall_ro
139
+ def function(subcommand, *args, **options)
140
+ subcommand = subcommand.to_s.downcase
141
+
142
+ case subcommand
143
+ when "load"
144
+ command = %i[function load]
145
+ command << "REPLACE" if options[:replace]
146
+ send_command(command + args)
147
+ when "list"
148
+ command = %i[function list] + args
149
+ command << "LIBRARYNAME" << options[:libraryname] if options[:libraryname]
150
+ command << "WITHCODE" if options[:withcode]
151
+ send_command(command, &HashifyFunctionList)
152
+ when "restore"
153
+ command = %i[function restore] + args
154
+ command << options[:policy].to_s.upcase if options[:policy]
155
+ send_command(command)
156
+ when "stats"
157
+ send_command(%i[function stats], &HashifyFunctionStats)
158
+ else
159
+ send_command([:function, subcommand] + args)
160
+ end
161
+ end
162
+
163
+ # Invoke a Redis Function loaded with FUNCTION LOAD.
164
+ #
165
+ # @example FCALL without keys nor arguments
166
+ # redis.fcall("myfunc")
167
+ # # => <depends on the function>
168
+ # @example FCALL with keys and arguments as array arguments
169
+ # redis.fcall("myfunc", ["k1", "k2"], ["a1", "a2"])
170
+ # # => <depends on the function>
171
+ # @example FCALL with keys and arguments in a hash argument
172
+ # redis.fcall("myfunc", :keys => ["k1", "k2"], :argv => ["a1", "a2"])
173
+ # # => <depends on the function>
174
+ #
175
+ # @param [Array<String>] keys optional array with keys to pass to the function
176
+ # @param [Array<String>] argv optional array with arguments to pass to the function
177
+ # @param [Hash] options
178
+ # - `:keys => Array<String>`: optional array with keys to pass to the function
179
+ # - `:argv => Array<String>`: optional array with arguments to pass to the function
180
+ # @return depends on the function
181
+ #
182
+ # @see #function
183
+ # @see #fcall_ro
184
+ def fcall(*args)
185
+ _eval(:fcall, args)
186
+ end
187
+
188
+ # Invoke a read-only Redis Function loaded with FUNCTION LOAD.
189
+ #
190
+ # The function must have been registered with the `no-writes` flag.
191
+ #
192
+ # @example FCALL_RO with a key
193
+ # redis.fcall_ro("myfunc", ["k1"])
194
+ # # => <depends on the function>
195
+ #
196
+ # @param [Array<String>] keys optional array with keys to pass to the function
197
+ # @param [Array<String>] argv optional array with arguments to pass to the function
198
+ # @param [Hash] options
199
+ # - `:keys => Array<String>`: optional array with keys to pass to the function
200
+ # - `:argv => Array<String>`: optional array with arguments to pass to the function
201
+ # @return depends on the function
202
+ #
203
+ # @see #function
204
+ # @see #fcall
205
+ def fcall_ro(*args)
206
+ _eval(:fcall_ro, args)
207
+ end
208
+
100
209
  private
101
210
 
102
211
  def _eval(cmd, args)