Skip to content

Commit b8e9f14

Browse files
authored
refactor: steps to parse reply for several commands in bootstrap (#202)
1 parent 116c4d6 commit b8e9f14

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

lib/redis_client/cluster/command.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Command
1010
EMPTY_STRING = ''
1111
LEFT_BRACKET = '{'
1212
RIGHT_BRACKET = '}'
13+
EMPTY_HASH = {}.freeze
1314

1415
Detail = Struct.new(
1516
'RedisCommand',
@@ -41,16 +42,15 @@ def load(nodes)
4142
private
4243

4344
def parse_command_reply(rows)
44-
rows&.reject { |row| row[0].nil? }.to_h do |row|
45-
[
46-
row[0].downcase,
47-
::RedisClient::Cluster::Command::Detail.new(
48-
first_key_position: row[3],
49-
write?: row[2].include?('write'),
50-
readonly?: row[2].include?('readonly')
51-
)
52-
]
53-
end
45+
rows&.each_with_object({}) do |row, acc|
46+
next if row[0].nil?
47+
48+
acc[row[0].downcase] = ::RedisClient::Cluster::Command::Detail.new(
49+
first_key_position: row[3],
50+
write?: row[2].include?('write'),
51+
readonly?: row[2].include?('readonly')
52+
)
53+
end || EMPTY_HASH
5454
end
5555
end
5656

lib/redis_client/cluster/node.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def load_info(options, **kwargs) # rubocop:disable Metrics/AbcSize, Metrics/Cycl
135135
# @see https://redis.io/commands/cluster-nodes/
136136
# @see https://github.com/redis/redis/blob/78960ad57b8a5e6af743d789ed8fd767e37d42b8/src/cluster.c#L4660-L4683
137137
def parse_cluster_node_reply(reply) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
138-
reply.each_line("\n", chomp: true).with_object(Array.new(reply.count("\n"))) do |line, acc|
138+
reply.each_line("\n", chomp: true).filter_map do |line|
139139
fields = line.split
140140
flags = fields[2].split(',')
141141
next unless fields[7] == 'connected' && (flags & DEAD_FLAGS).empty?
@@ -149,7 +149,7 @@ def parse_cluster_node_reply(reply) # rubocop:disable Metrics/AbcSize, Metrics/C
149149
.map(&:sort)
150150
end
151151

152-
acc << ::RedisClient::Cluster::Node::Info.new(
152+
::RedisClient::Cluster::Node::Info.new(
153153
id: fields[0],
154154
node_key: fields[1].split('@').first,
155155
role: (flags & ROLE_FLAGS).first,
@@ -160,7 +160,7 @@ def parse_cluster_node_reply(reply) # rubocop:disable Metrics/AbcSize, Metrics/C
160160
link_state: fields[7],
161161
slots: slots
162162
)
163-
end.compact
163+
end
164164
end
165165
end
166166

0 commit comments

Comments
 (0)