3
3
require 'redis_client'
4
4
require 'redis_client/cluster/errors'
5
5
require 'redis_client/cluster/key_slot_converter'
6
- require 'redis_client/cluster/normalized_cmd_name'
7
6
8
7
class RedisClient
9
8
class Cluster
@@ -30,7 +29,7 @@ def load(nodes, slow_command_timeout: -1) # rubocop:disable Metrics/AbcSize
30
29
nodes &.each do |node |
31
30
regular_timeout = node . read_timeout
32
31
node . read_timeout = slow_command_timeout > 0.0 ? slow_command_timeout : regular_timeout
33
- reply = node . call ( 'COMMAND ' )
32
+ reply = node . call ( 'command ' )
34
33
node . read_timeout = regular_timeout
35
34
commands = parse_command_reply ( reply )
36
35
cmd = ::RedisClient ::Cluster ::Command . new ( commands )
@@ -51,7 +50,7 @@ def parse_command_reply(rows)
51
50
rows &.each_with_object ( { } ) do |row , acc |
52
51
next if row [ 0 ] . nil?
53
52
54
- acc [ row [ 0 ] . downcase ] = ::RedisClient ::Cluster ::Command ::Detail . new (
53
+ acc [ row . first ] = ::RedisClient ::Cluster ::Command ::Detail . new (
55
54
first_key_position : row [ 3 ] ,
56
55
key_step : row [ 5 ] ,
57
56
write? : row [ 2 ] . include? ( 'write' ) ,
@@ -73,38 +72,59 @@ def extract_first_key(command)
73
72
end
74
73
75
74
def should_send_to_primary? ( command )
76
- name = ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_command ( command )
77
- @commands [ name ] &.write?
75
+ find_command_info ( command . first ) &.write?
78
76
end
79
77
80
78
def should_send_to_replica? ( command )
81
- name = ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_command ( command )
82
- @commands [ name ] &.readonly?
79
+ find_command_info ( command . first ) &.readonly?
83
80
end
84
81
85
82
def exists? ( name )
86
- @commands . key? ( :: RedisClient :: Cluster :: NormalizedCmdName . instance . get_by_name ( name ) )
83
+ @commands . key? ( name ) || @commands . key? ( name . to_s . downcase ( :ascii ) )
87
84
end
88
85
89
86
private
90
87
91
- def determine_first_key_position ( command ) # rubocop:disable Metrics/CyclomaticComplexity
92
- case name = ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_command ( command )
93
- when 'eval' , 'evalsha' , 'zinterstore' , 'zunionstore' then 3
94
- when 'object' then 2
95
- when 'memory'
88
+ def find_command_info ( name )
89
+ @commands [ name ] || @commands [ name . to_s . downcase ( :ascii ) ]
90
+ end
91
+
92
+ def determine_first_key_position ( command ) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity
93
+ if command . first . casecmp ( 'get' ) . zero?
94
+ find_command_info ( command . first ) &.first_key_position . to_i
95
+ elsif command . first . casecmp ( 'mget' ) . zero?
96
+ find_command_info ( command . first ) &.first_key_position . to_i
97
+ elsif command . first . casecmp ( 'set' ) . zero?
98
+ find_command_info ( command . first ) &.first_key_position . to_i
99
+ elsif command . first . casecmp ( 'mset' ) . zero?
100
+ find_command_info ( command . first ) &.first_key_position . to_i
101
+ elsif command . first . casecmp ( 'del' ) . zero?
102
+ find_command_info ( command . first ) &.first_key_position . to_i
103
+ elsif command . first . casecmp ( 'eval' ) . zero?
104
+ 3
105
+ elsif command . first . casecmp ( 'evalsha' ) . zero?
106
+ 3
107
+ elsif command . first . casecmp ( 'zinterstore' ) . zero?
108
+ 3
109
+ elsif command . first . casecmp ( 'zunionstore' ) . zero?
110
+ 3
111
+ elsif command . first . casecmp ( 'object' ) . zero?
112
+ 2
113
+ elsif command . first . casecmp ( 'memory' ) . zero?
96
114
command [ 1 ] . to_s . casecmp ( 'usage' ) . zero? ? 2 : 0
97
- when 'migrate'
115
+ elsif command . first . casecmp ( 'migrate' ) . zero?
98
116
command [ 3 ] . empty? ? determine_optional_key_position ( command , 'keys' ) : 3
99
- when 'xread' , 'xreadgroup'
117
+ elsif command . first . casecmp ( 'xread' ) . zero?
118
+ determine_optional_key_position ( command , 'streams' )
119
+ elsif command . first . casecmp ( 'xreadgroup' ) . zero?
100
120
determine_optional_key_position ( command , 'streams' )
101
121
else
102
- @commands [ name ] &.first_key_position . to_i
122
+ find_command_info ( command . first ) &.first_key_position . to_i
103
123
end
104
124
end
105
125
106
126
def determine_optional_key_position ( command , option_name )
107
- idx = command . map { |e | e . to_s . downcase } . index ( option_name &. downcase )
127
+ idx = command . map { |e | e . to_s . downcase ( :ascii ) } . index ( option_name )
108
128
idx . nil? ? 0 : idx + 1
109
129
end
110
130
end
0 commit comments