Skip to content

Commit e9a7168

Browse files
authored
refactor: remove redundant or unused codes (#407)
1 parent 5817a0b commit e9a7168

File tree

4 files changed

+25
-116
lines changed

4 files changed

+25
-116
lines changed

lib/redis_client/cluster/command.rb

+3-51
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Command
1717
Detail = Struct.new(
1818
'RedisCommand',
1919
:first_key_position,
20-
:last_key_position,
2120
:key_step,
2221
:write?,
2322
:readonly?,
@@ -54,7 +53,6 @@ def parse_command_reply(rows)
5453

5554
acc[row[0].downcase] = ::RedisClient::Cluster::Command::Detail.new(
5655
first_key_position: row[3],
57-
last_key_position: row[4],
5856
key_step: row[5],
5957
write?: row[2].include?('write'),
6058
readonly?: row[2].include?('readonly')
@@ -71,18 +69,7 @@ def extract_first_key(command)
7169
i = determine_first_key_position(command)
7270
return EMPTY_STRING if i == 0
7371

74-
(command[i].is_a?(Array) ? command[i].flatten.first : command[i]).to_s
75-
end
76-
77-
def extract_all_keys(command)
78-
keys_start = determine_first_key_position(command)
79-
keys_end = determine_last_key_position(command, keys_start)
80-
keys_step = determine_key_step(command)
81-
return EMPTY_ARRAY if [keys_start, keys_end, keys_step].any?(&:zero?)
82-
83-
keys_end = [keys_end, command.size - 1].min
84-
# use .. inclusive range because keys_end is a valid index.
85-
(keys_start..keys_end).step(keys_step).map { |i| command[i] }
72+
command[i]
8673
end
8774

8875
def should_send_to_primary?(command)
@@ -116,45 +103,10 @@ def determine_first_key_position(command) # rubocop:disable Metrics/CyclomaticCo
116103
end
117104
end
118105

119-
# IMPORTANT: this determines the last key position INCLUSIVE of the last key -
120-
# i.e. command[determine_last_key_position(command)] is a key.
121-
# This is in line with what Redis returns from COMMANDS.
122-
def determine_last_key_position(command, keys_start) # rubocop:disable Metrics/AbcSize
123-
case name = ::RedisClient::Cluster::NormalizedCmdName.instance.get_by_command(command)
124-
when 'eval', 'evalsha', 'zinterstore', 'zunionstore'
125-
# EVALSHA sha1 numkeys [key [key ...]] [arg [arg ...]]
126-
# ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE <SUM | MIN | MAX>]
127-
command[2].to_i + 2
128-
when 'object', 'memory'
129-
# OBJECT [ENCODING | FREQ | IDLETIME | REFCOUNT] key
130-
# MEMORY USAGE key [SAMPLES count]
131-
keys_start
132-
when 'migrate'
133-
# MIGRATE host port <key | ""> destination-db timeout [COPY] [REPLACE] [AUTH password | AUTH2 username password] [KEYS key [key ...]]
134-
command[3].empty? ? (command.length - 1) : 3
135-
when 'xread', 'xreadgroup'
136-
# XREAD [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] id [id ...]
137-
keys_start + ((command.length - keys_start) / 2) - 1
138-
else
139-
# If there is a fixed, non-variable number of keys, don't iterate past that.
140-
if @commands[name].last_key_position >= 0
141-
@commands[name].last_key_position
142-
else
143-
command.length + @commands[name].last_key_position
144-
end
145-
end
146-
end
147-
148-
def determine_optional_key_position(command, option_name) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
149-
idx = command&.flatten&.map(&:to_s)&.map(&:downcase)&.index(option_name&.downcase)
106+
def determine_optional_key_position(command, option_name)
107+
idx = command.map { |e| e.to_s.downcase }.index(option_name&.downcase)
150108
idx.nil? ? 0 : idx + 1
151109
end
152-
153-
def determine_key_step(command)
154-
name = ::RedisClient::Cluster::NormalizedCmdName.instance.get_by_command(command)
155-
# Some commands like EVALSHA have zero as the step in COMMANDS somehow.
156-
@commands[name].key_step == 0 ? 1 : @commands[name].key_step
157-
end
158110
end
159111
end
160112
end

lib/redis_client/cluster/errors.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ def self.from_command(command)
3434
end
3535

3636
class ErrorCollection < Error
37+
EMPTY_HASH = {}.freeze
38+
39+
private_constant :EMPTY_HASH
3740
attr_reader :errors
3841

3942
def self.with_errors(errors)
4043
if !errors.is_a?(Hash) || errors.empty?
41-
new(errors.to_s).with_errors({})
44+
new(errors.to_s).with_errors(EMPTY_HASH)
4245
else
4346
messages = errors.map { |node_key, error| "#{node_key}: (#{error.class}) #{error.message}" }
4447
new(messages.join(', ')).with_errors(errors)

lib/redis_client/cluster/router.rb

+12-9
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,20 @@ def send_watch_command(command)
367367
end
368368
end
369369

370-
MULTIPLE_KEYS_COMMAND_TO_SINGLE = {
371-
'mget' => ['get', 1].freeze,
372-
'mset' => ['set', 2].freeze,
373-
'del' => ['del', 1].freeze
374-
}.freeze
375-
376-
private_constant :MULTIPLE_KEYS_COMMAND_TO_SINGLE
377-
378370
def send_multiple_keys_command(cmd, method, command, args, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
379371
# This implementation is prioritized performance rather than readability or so.
380-
single_key_cmd, keys_step = MULTIPLE_KEYS_COMMAND_TO_SINGLE.fetch(cmd)
372+
case cmd
373+
when 'mget'
374+
single_key_cmd = 'get'
375+
keys_step = 1
376+
when 'mset'
377+
single_key_cmd = 'set'
378+
keys_step = 2
379+
when 'del'
380+
single_key_cmd = 'del'
381+
keys_step = 1
382+
else raise NotImplementedError, cmd
383+
end
381384

382385
return try_send(assign_node(command), method, command, args, &block) if command.size <= keys_step + 1 || ::RedisClient::Cluster::KeySlotConverter.hash_tag_included?(command[1])
383386

test/redis_client/cluster/test_command.rb

+6-55
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ def test_parse_command_reply
5353
['set', -3, Set['write', 'denyoom', 'movablekeys'], 1, -1, 2, Set['@write', '@string', '@slow'], Set[], Set[], Set[]]
5454
],
5555
want: {
56-
'get' => { first_key_position: 1, last_key_position: -1, key_step: 1, write?: false, readonly?: true },
57-
'set' => { first_key_position: 1, last_key_position: -1, key_step: 2, write?: true, readonly?: false }
56+
'get' => { first_key_position: 1, key_step: 1, write?: false, readonly?: true },
57+
'set' => { first_key_position: 1, key_step: 2, write?: true, readonly?: false }
5858
}
5959
},
6060
{
6161
rows: [
6262
['GET', 2, Set['readonly', 'fast'], 1, -1, 1, Set['@read', '@string', '@fast'], Set[], Set[], Set[]]
6363
],
6464
want: {
65-
'get' => { first_key_position: 1, last_key_position: -1, key_step: 1, write?: false, readonly?: true }
65+
'get' => { first_key_position: 1, key_step: 1, write?: false, readonly?: true }
6666
}
6767
},
6868
{ rows: [[]], want: {} },
@@ -87,8 +87,6 @@ def test_extract_first_key
8787
{ command: %w[GET foo{bar}baz], want: 'foo{bar}baz' },
8888
{ command: %w[MGET foo bar baz], want: 'foo' },
8989
{ command: %w[UNKNOWN foo bar], want: '' },
90-
{ command: [['GET'], 'foo'], want: 'foo' },
91-
{ command: ['GET', ['foo']], want: 'foo' },
9290
{ command: [], want: '' },
9391
{ command: nil, want: '' }
9492
].each_with_index do |c, idx|
@@ -151,7 +149,6 @@ def test_determine_first_key_position
151149
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
152150
[
153151
{ command: %w[EVAL "return ARGV[1]" 0 hello], want: 3 },
154-
{ command: [['EVAL'], '"return ARGV[1]"', 0, 'hello'], want: 3 },
155152
{ command: %w[EVALSHA sha1 2 foo bar baz zap], want: 3 },
156153
{ command: %w[MIGRATE host port key 0 5 COPY], want: 3 },
157154
{ command: ['MIGRATE', 'host', 'port', '', '0', '5', 'COPY', 'KEYS', 'key'], want: 8 },
@@ -164,7 +161,7 @@ def test_determine_first_key_position
164161
{ command: %w[XREADGROUP GROUP group consumer STREAMS key id], want: 5 },
165162
{ command: %w[SET foo 1], want: 1 },
166163
{ command: %w[set foo 1], want: 1 },
167-
{ command: [['SET'], 'foo', 1], want: 1 },
164+
{ command: ['SET', 'foo', 1], want: 1 },
168165
{ command: %w[GET foo], want: 1 }
169166
].each_with_index do |c, idx|
170167
msg = "Case: #{idx}"
@@ -179,62 +176,16 @@ def test_determine_optional_key_position
179176
{ params: { command: %w[XREAD COUNT 2 STREAMS mystream writers 0-0 0-0], option_name: 'streams' }, want: 4 },
180177
{ params: { command: %w[XREADGROUP GROUP group consumer STREAMS key id], option_name: 'streams' }, want: 5 },
181178
{ params: { command: %w[GET foo], option_name: 'bar' }, want: 0 },
182-
{ params: { command: ['FOO', ['BAR'], 'BAZ'], option_name: 'bar' }, want: 2 },
179+
{ params: { command: %w[FOO BAR BAZ], option_name: 'bar' }, want: 2 },
183180
{ params: { command: %w[FOO BAR BAZ], option_name: 'BAR' }, want: 2 },
184181
{ params: { command: [], option_name: nil }, want: 0 },
185-
{ params: { command: [], option_name: '' }, want: 0 },
186-
{ params: { command: nil, option_name: nil }, want: 0 }
182+
{ params: { command: [], option_name: '' }, want: 0 }
187183
].each_with_index do |c, idx|
188184
msg = "Case: #{idx}"
189185
got = cmd.send(:determine_optional_key_position, c[:params][:command], c[:params][:option_name])
190186
assert_equal(c[:want], got, msg)
191187
end
192188
end
193-
194-
def test_determine_key_step
195-
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
196-
[
197-
{ name: 'MSET', want: 2 },
198-
{ name: 'MGET', want: 1 },
199-
{ name: 'DEL', want: 1 },
200-
{ name: 'EVALSHA', want: 1 }
201-
].each_with_index do |c, idx|
202-
msg = "Case: #{idx}"
203-
got = cmd.send(:determine_key_step, c[:name])
204-
assert_equal(c[:want], got, msg)
205-
end
206-
end
207-
208-
def test_extract_all_keys
209-
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
210-
[
211-
{ command: ['EVAL', 'return ARGV[1]', '0', 'hello'], want: [] },
212-
{ command: ['EVAL', 'return ARGV[1]', '3', 'key1', 'key2', 'key3', 'arg1', 'arg2'], want: %w[key1 key2 key3] },
213-
{ command: [['EVAL'], '"return ARGV[1]"', 0, 'hello'], want: [] },
214-
{ command: %w[EVALSHA sha1 2 foo bar baz zap], want: %w[foo bar] },
215-
{ command: %w[MIGRATE host port key 0 5 COPY], want: %w[key] },
216-
{ command: ['MIGRATE', 'host', 'port', '', '0', '5', 'COPY', 'KEYS', 'key1'], want: %w[key1] },
217-
{ command: ['MIGRATE', 'host', 'port', '', '0', '5', 'COPY', 'KEYS', 'key1', 'key2'], want: %w[key1 key2] },
218-
{ command: %w[ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3], want: %w[zset1 zset2] },
219-
{ command: %w[ZUNIONSTORE out 2 zset1 zset2 WEIGHTS 2 3], want: %w[zset1 zset2] },
220-
{ command: %w[OBJECT HELP], want: [] },
221-
{ command: %w[MEMORY HELP], want: [] },
222-
{ command: %w[MEMORY USAGE key], want: %w[key] },
223-
{ command: %w[XREAD COUNT 2 STREAMS mystream writers 0-0 0-0], want: %w[mystream writers] },
224-
{ command: %w[XREADGROUP GROUP group consumer STREAMS key id], want: %w[key] },
225-
{ command: %w[SET foo 1], want: %w[foo] },
226-
{ command: %w[set foo 1], want: %w[foo] },
227-
{ command: [['SET'], 'foo', 1], want: %w[foo] },
228-
{ command: %w[GET foo], want: %w[foo] },
229-
{ command: %w[MGET foo bar baz], want: %w[foo bar baz] },
230-
{ command: %w[MSET foo val bar val baz val], want: %w[foo bar baz] },
231-
{ command: %w[BLPOP foo bar 0], want: %w[foo bar] }
232-
].each_with_index do |c, idx|
233-
msg = "Case: #{idx}"
234-
got = cmd.send(:extract_all_keys, c[:command])
235-
assert_equal(c[:want], got, msg)
236-
end
237-
end
238189
end
239190
end
240191
end

0 commit comments

Comments
 (0)