redis-cluster-client 0.18.0 → 0.18.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7bd2629d1095d987406e994ca2339369e8e26cab68215494f80b586725fb912f
4
- data.tar.gz: b05931e7cca0a125dcb72965e2b163457dab180ed1de8f4d0e5d2cf8669c9690
3
+ metadata.gz: 56e0717cec7aea30679b6026ebf92f609da2f0dcbcbd7d64520ea26e9597e6d7
4
+ data.tar.gz: 136f4448e005be6a19cc0414edba88ef62470621054d0b3981b0a4db07235c50
5
5
  SHA512:
6
- metadata.gz: cdf11f947593f0575490ea199696a1da038b392bac8f2adf466979bb7dd4cff48414eb4df8da7cb070a5c0661f0bf6a33aed838aaf3ab0c17849787ecadd72b7
7
- data.tar.gz: d7578986769ddc6e1c50d552b13d417a9c051a1e85c121fe1daa4051db7bafd9a45c0f1932d1ba0c33ea5ddec93a0a64d2c789e5d16afdd6904e6589d5a8bbbe
6
+ metadata.gz: 01cb2a0e2514e73e856fd60bb9d5b291ee5becc0676740318f84cc2b215b2973685684a1b14e88dc2f87da3f5a3215ba9c90713deef8943efe66beec8149f4b6
7
+ data.tar.gz: 4e05413eac641be0b20c2d081b060b487f67ede2bedde2b07c7f433e8243837fd42894974c3a7f7092e2c6bdd111acbae0b54cf0c18f36deecfb25a3f5c23708
@@ -18,6 +18,7 @@ class RedisClient
18
18
 
19
19
  # @see https://redis.io/docs/latest/commands/command/ The reply of the COMMAND command
20
20
  # @see https://redis.io/docs/latest/develop/reference/command-tips/ Command tips
21
+ # It takes positional arguments because `keyword_init: true` allocates a transient hash for each instance.
21
22
  Spec = Struct.new(
22
23
  'RedisCommandSpec',
23
24
  :first_key_position,
@@ -26,8 +27,7 @@ class RedisClient
26
27
  :readonly?,
27
28
  :request_policy,
28
29
  :response_policy,
29
- :subcommands,
30
- keyword_init: true
30
+ :subcommands
31
31
  ) do
32
32
  def extract_first_key(command)
33
33
  i = first_key_position.to_i
@@ -96,23 +96,29 @@ class RedisClient
96
96
  private
97
97
 
98
98
  def parse_command_reply(rows)
99
+ specs = {}
99
100
  rows&.each_with_object({}) do |row, acc|
100
101
  next if row.first.nil?
101
102
 
102
- acc[row.first] = build_spec(row)
103
+ acc[row.first] = build_spec(row, specs)
103
104
  end.freeze || EMPTY_HASH
104
105
  end
105
106
 
106
- def build_spec(row)
107
- ::RedisClient::Cluster::Command::Spec.new(
108
- first_key_position: parse_first_key_position(row),
109
- key_step: row[5],
110
- write?: parse_writability(row),
111
- readonly?: row[2].include?('readonly'),
112
- request_policy: parse_policy_tip(row[7], REQUEST_POLICY_PREFIX),
113
- response_policy: parse_policy_tip(row[7], RESPONSE_POLICY_PREFIX),
114
- subcommands: parse_subcommands(row[9])
107
+ # Most commands share the same spec, so the identical ones are deduplicated to keep the table small.
108
+ # A spec with subcommands is left as it is because it is rarely identical to another.
109
+ def build_spec(row, specs)
110
+ spec = ::RedisClient::Cluster::Command::Spec.new(
111
+ parse_first_key_position(row), # first_key_position
112
+ row[5], # key_step
113
+ parse_writability(row), # write?
114
+ row[2].include?('readonly'), # readonly?
115
+ parse_policy_tip(row[7], REQUEST_POLICY_PREFIX), # request_policy
116
+ parse_policy_tip(row[7], RESPONSE_POLICY_PREFIX), # response_policy
117
+ parse_subcommands(row[9], specs) # subcommands
115
118
  ).freeze
119
+ return spec unless spec.subcommands.nil?
120
+
121
+ specs[spec] ||= spec
116
122
  end
117
123
 
118
124
  # The redis 6.2 or earlier doesn't include the information of the subcommands in the reply.
@@ -142,7 +148,7 @@ class RedisClient
142
148
 
143
149
  # The information of the subcommands is available in the redis 7.0 or later.
144
150
  # A container command such as XINFO reports its key positions per subcommand.
145
- def parse_subcommands(rows)
151
+ def parse_subcommands(rows, specs)
146
152
  return if rows.nil? || rows.empty?
147
153
 
148
154
  rows.each_with_object({}) do |row, acc|
@@ -151,7 +157,7 @@ class RedisClient
151
157
 
152
158
  # The server replies with a full name of the subcommand such as `xinfo|stream`.
153
159
  i = name.index(SUBCOMMAND_DELIMITER)
154
- acc[i.nil? ? name : name[i + 1, name.size]] = build_spec(row)
160
+ acc[i.nil? ? name : name[i + 1, name.size]] = build_spec(row, specs)
155
161
  end.freeze
156
162
  end
157
163
  end
@@ -53,10 +53,15 @@ class RedisClient
53
53
  end
54
54
 
55
55
  class CharArray
56
+ # A byte holds 256 values, and 0xff is reserved as the marker for an unassigned slot.
57
+ UNASSIGNED = 0xff
58
+ MAX_ELEMENTS = UNASSIGNED
59
+ private_constant :UNASSIGNED
60
+
56
61
  def initialize(size, elements)
57
62
  @elements = elements
58
63
  @string = String.new('', encoding: Encoding::BINARY, capacity: size)
59
- size.times { @string << 0xff }
64
+ size.times { @string << UNASSIGNED }
60
65
  end
61
66
 
62
67
  def [](index)
@@ -72,7 +77,7 @@ class RedisClient
72
77
 
73
78
  pos = @elements.find_index(element) # O(N)
74
79
  if pos.nil?
75
- raise(RangeError, 'full of elements') if @elements.size >= 256
80
+ raise(RangeError, 'full of elements') if @elements.size >= MAX_ELEMENTS
76
81
 
77
82
  pos = @elements.size
78
83
  @elements << element
@@ -263,7 +268,7 @@ class RedisClient
263
268
  end
264
269
 
265
270
  def make_array_for_slot_node_mappings(node_info_list)
266
- return Array.new(SLOT_SIZE) if !USE_CHAR_ARRAY_SLOT || node_info_list.count(&:primary?) > 256
271
+ return Array.new(SLOT_SIZE) if !USE_CHAR_ARRAY_SLOT || node_info_list.count(&:primary?) > ::RedisClient::Cluster::Node::CharArray::MAX_ELEMENTS
267
272
 
268
273
  primary_node_keys = node_info_list.select(&:primary?).map(&:node_key)
269
274
  ::RedisClient::Cluster::Node::CharArray.new(SLOT_SIZE, primary_node_keys)
@@ -7,6 +7,7 @@ require 'redis_client/cluster/errors'
7
7
  require 'redis_client/cluster/key_slot_converter'
8
8
  require 'redis_client/cluster/node'
9
9
  require 'redis_client/cluster/node_key'
10
+ require 'redis_client/cluster/noop_command_builder'
10
11
  require 'redis_client/cluster/transaction'
11
12
  require 'redis_client/cluster/optimistic_locking'
12
13
  require 'redis_client/cluster/pipeline'
@@ -484,27 +485,34 @@ class RedisClient
484
485
  raise NotImplementedError, cmd
485
486
  end
486
487
 
487
- return assign_node_and_send_command(method, command, args, &block) if command.size <= keys_step + 1 || ::RedisClient::Cluster::KeySlotConverter.hash_tag_included?(command[1])
488
+ if command.size <= keys_step + 1 ||
489
+ (command.size - 1) % keys_step != 0 ||
490
+ ::RedisClient::Cluster::KeySlotConverter.hash_tag_included?(command[1])
491
+ # An odd number of MSET arguments goes as is, so the server rejects it before any key is written.
492
+ return assign_node_and_send_command(method, command, args, &block)
493
+ end
488
494
 
495
+ # The command has already been built, so the pipeline must not build it again.
496
+ # The pipeline keeps each command array as is, so each one must be a new array.
489
497
  pipeline = ::RedisClient::Cluster::Pipeline.new(
490
498
  self,
491
- @command_builder,
499
+ ::RedisClient::Cluster::NoopCommandBuilder,
492
500
  @concurrent_worker,
493
501
  exception: true,
494
502
  seed: Random.new_seed
495
503
  )
496
504
 
497
- single_command = Array.new(keys_step + 1)
498
- single_command[0] = single_key_cmd
505
+ i = 1
506
+ size = command.size
499
507
  if keys_step == 1
500
- command[1..].each do |key|
501
- single_command[1] = key
502
- pipeline.call_v(single_command)
508
+ while i < size
509
+ pipeline.call_v([single_key_cmd, command[i]])
510
+ i += 1
503
511
  end
504
512
  else
505
- command[1..].each_slice(keys_step) do |v|
506
- keys_step.times { |i| single_command[i + 1] = v[i] }
507
- pipeline.call_v(single_command)
513
+ while i < size
514
+ pipeline.call_v([single_key_cmd, command[i], command[i + 1]])
515
+ i += 2
508
516
  end
509
517
  end
510
518
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-cluster-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.18.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taishi Kasuga