Class: RedisClient::Cluster::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/cluster/command.rb

Defined Under Namespace

Classes: Detail

Constant Summary collapse

EMPTY_STRING =
''
EMPTY_HASH =
{}.freeze
EMPTY_ARRAY =
[].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands) ⇒ Command

Returns a new instance of Command.



64
65
66
# File 'lib/redis_client/cluster/command.rb', line 64

def initialize(commands)
  @commands = commands || EMPTY_HASH
end

Class Method Details

.load(nodes, slow_command_timeout: -1)) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redis_client/cluster/command.rb', line 26

def load(nodes, slow_command_timeout: -1)
  cmd = errors = nil

  nodes&.each do |node|
    regular_timeout = node.read_timeout
    node.read_timeout = slow_command_timeout > 0.0 ? slow_command_timeout : regular_timeout
    reply = node.call('COMMAND')
    node.read_timeout = regular_timeout
    commands = parse_command_reply(reply)
    cmd = ::RedisClient::Cluster::Command.new(commands)
    break
  rescue ::RedisClient::Error => e
    errors ||= []
    errors << e
  end

  return cmd unless cmd.nil?

  raise ::RedisClient::Cluster::InitialSetupError, errors
end

Instance Method Details

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/redis_client/cluster/command.rb', line 96

def exists?(name)
  @commands.key?(::RedisClient::Cluster::NormalizedCmdName.instance.get_by_name(name))
end

#extract_all_keys(command) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/redis_client/cluster/command.rb', line 75

def extract_all_keys(command)
  keys_start = determine_first_key_position(command)
  keys_end = determine_last_key_position(command, keys_start)
  keys_step = determine_key_step(command)
  return EMPTY_ARRAY if [keys_start, keys_end, keys_step].any?(&:zero?)

  keys_end = [keys_end, command.size - 1].min
  # use .. inclusive range because keys_end is a valid index.
  (keys_start..keys_end).step(keys_step).map { |i| command[i] }
end

#extract_first_key(command) ⇒ Object



68
69
70
71
72
73
# File 'lib/redis_client/cluster/command.rb', line 68

def extract_first_key(command)
  i = determine_first_key_position(command)
  return EMPTY_STRING if i == 0

  (command[i].is_a?(Array) ? command[i].flatten.first : command[i]).to_s
end

#should_send_to_primary?(command) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/redis_client/cluster/command.rb', line 86

def should_send_to_primary?(command)
  name = ::RedisClient::Cluster::NormalizedCmdName.instance.get_by_command(command)
  @commands[name]&.write?
end

#should_send_to_replica?(command) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/redis_client/cluster/command.rb', line 91

def should_send_to_replica?(command)
  name = ::RedisClient::Cluster::NormalizedCmdName.instance.get_by_command(command)
  @commands[name]&.readonly?
end