class Cql::Client::AsynchronousBatch

@private

Constants

BATCH_TYPES

Public Class Methods

new(type, execute_options_decoder, connection_manager, options=nil) click to toggle source
# File lib/cql/client/batch.rb, line 72
def initialize(type, execute_options_decoder, connection_manager, options=nil)
  raise ArgumentError, "Unknown batch type: #{type}" unless BATCH_TYPES.include?(type)
  @type = type
  @execute_options_decoder = execute_options_decoder
  @connection_manager = connection_manager
  @options = options
  @request_runner = RequestRunner.new
  @parts = []
end

Public Instance Methods

add(*args) click to toggle source
# File lib/cql/client/batch.rb, line 82
def add(*args)
  @parts << args
  nil
end
execute(options=nil) click to toggle source
# File lib/cql/client/batch.rb, line 87
def execute(options=nil)
  options = @execute_options_decoder.decode_options(@options, options)
  connection = @connection_manager.random_connection
  request = Protocol::BatchRequest.new(BATCH_TYPES[@type], options[:consistency], options[:trace])
  unprepared_statements = nil
  @parts.each do |part, *bound_args|
    if part.is_a?(String) || part.prepared?(connection)
      add_part(connection, request, part, bound_args)
    else
      unprepared_statements ||= []
      unprepared_statements << [part, bound_args]
    end
  end
  @parts = []
  if unprepared_statements.nil?
    @request_runner.execute(connection, request, options[:timeout])
  else
    fs = unprepared_statements.map do |statement, _|
      if statement.respond_to?(:async)
        statement.async.prepare(connection)
      else
        statement.prepare(connection)
      end
    end
    Future.all(*fs).flat_map do
      unprepared_statements.each do |statement, bound_args|
        add_part(connection, request, statement, bound_args)
      end
      @request_runner.execute(connection, request, options[:timeout])
    end
  end
end

Private Instance Methods

add_part(connection, request, part, bound_args) click to toggle source
# File lib/cql/client/batch.rb, line 128
def add_part(connection, request, part, bound_args)
  if part.is_a?(String)
    type_hints = nil
    if bound_args.last.is_a?(Hash) && bound_args.last.include?(:type_hints)
      bound_args = bound_args.dup
      type_hints = bound_args.pop[:type_hints]
    end
    request.add_query(part, bound_args, type_hints)
  else
    part.add_to_batch(request, connection, bound_args)
  end
end