Class: RedisClient::Cluster::Transaction

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

Constant Summary collapse

ConsistencyError =
Class.new(::RedisClient::Cluster::Error)

Instance Method Summary collapse

Constructor Details

#initialize(router, command_builder, node: nil, slot: nil, asking: false) ⇒ Transaction

Returns a new instance of Transaction.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redis_client/cluster/transaction.rb', line 18

def initialize(router, command_builder, node: nil, slot: nil, asking: false)
  @router = router
  @command_builder = command_builder
  @retryable = true
  @pipeline = ::RedisClient::Pipeline.new(::RedisClient::Cluster::NoopCommandBuilder)
  @pending_commands = []
  @node = node
  prepare_tx unless @node.nil?
  @watching_slot = slot
  @asking = asking
end

Instance Method Details

#call(*command, **kwargs, &block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/redis_client/cluster/transaction.rb', line 30

def call(*command, **kwargs, &block)
  command = @command_builder.generate(command, kwargs)
  if prepare(command)
    @pipeline.call_v(command, &block)
  else
    defer { @pipeline.call_v(command, &block) }
  end
end

#call_once(*command, **kwargs, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/redis_client/cluster/transaction.rb', line 48

def call_once(*command, **kwargs, &block)
  @retryable = false
  command = @command_builder.generate(command, kwargs)
  if prepare(command)
    @pipeline.call_once_v(command, &block)
  else
    defer { @pipeline.call_once_v(command, &block) }
  end
end

#call_once_v(command, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/redis_client/cluster/transaction.rb', line 58

def call_once_v(command, &block)
  @retryable = false
  command = @command_builder.generate(command)
  if prepare(command)
    @pipeline.call_once_v(command, &block)
  else
    defer { @pipeline.call_once_v(command, &block) }
  end
end

#call_v(command, &block) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/redis_client/cluster/transaction.rb', line 39

def call_v(command, &block)
  command = @command_builder.generate(command)
  if prepare(command)
    @pipeline.call_v(command, &block)
  else
    defer { @pipeline.call_v(command, &block) }
  end
end

#executeObject



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

def execute
  @pending_commands.each(&:call)

  return EMPTY_ARRAY if @pipeline._empty?
  raise ConsistencyError.new("couldn't determine the node: #{@pipeline._commands}").with_config(@router.config) if @node.nil?

  commit
end