Class: Cql::Client::AsynchronousPreparedStatement

Inherits:
PreparedStatement show all
Defined in:
lib/cql/client/asynchronous_prepared_statement.rb

Instance Attribute Summary

Attributes inherited from PreparedStatement

#metadata

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cql, execute_options_decoder, connection_manager, logger) ⇒ AsynchronousPreparedStatement

Returns a new instance of AsynchronousPreparedStatement.



8
9
10
11
12
13
14
# File 'lib/cql/client/asynchronous_prepared_statement.rb', line 8

def initialize(cql, execute_options_decoder, connection_manager, logger)
  @cql = cql
  @execute_options_decoder = execute_options_decoder
  @connection_manager = connection_manager
  @logger = logger
  @request_runner = RequestRunner.new
end

Class Method Details

.prepare(cql, execute_options_decoder, connection_manager, logger) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/cql/client/asynchronous_prepared_statement.rb', line 16

def self.prepare(cql, execute_options_decoder, connection_manager, logger)
  statement = new(cql, execute_options_decoder, connection_manager, logger)
  futures = connection_manager.map do |connection|
    statement.prepare(connection)
  end
  Future.all(*futures).map { statement }
rescue => e
  Future.failed(e)
end

Instance Method Details

#execute(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cql/client/asynchronous_prepared_statement.rb', line 26

def execute(*args)
  connection = @connection_manager.random_connection
  if connection[self]
    run(args, connection)
  else
    prepare(connection).flat_map do
      run(args, connection)
    end
  end
rescue => e
  Future.failed(e)
end

#prepare(connection) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cql/client/asynchronous_prepared_statement.rb', line 40

def prepare(connection)
  prepare_request = Protocol::PrepareRequest.new(@cql)
  f = @request_runner.execute(connection, prepare_request) do |response|
    connection[self] = response.id
    unless @raw_metadata
      # NOTE: this is not thread safe, but the worst that could happen
      # is that we assign the same data multiple times
      @raw_metadata = response.
      @metadata = ResultMetadata.new(@raw_metadata)
    end
    @logger.debug('Statement prepared on new connection')
  end
  f.map { self }
end