Class: Cassanity::Executors::CqlRb

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cassanity/executors/cql_rb.rb

Constant Summary collapse

DefaultArgumentGenerators =

Private: Hash of commands to related argument generators.

{
  keyspaces: ArgumentGenerators::Keyspaces.new,
  keyspace_create: ArgumentGenerators::KeyspaceCreate.new,
  keyspace_drop: ArgumentGenerators::KeyspaceDrop.new,
  keyspace_use: ArgumentGenerators::KeyspaceUse.new,
  column_families: ArgumentGenerators::ColumnFamilies.new,
  column_family_create: ArgumentGenerators::ColumnFamilyCreate.new,
  column_family_drop: ArgumentGenerators::ColumnFamilyDrop.new,
  column_family_truncate: ArgumentGenerators::ColumnFamilyTruncate.new,
  column_family_select: ArgumentGenerators::ColumnFamilySelect.new,
  column_family_insert: ArgumentGenerators::ColumnFamilyInsert.new,
  column_family_update: ArgumentGenerators::ColumnFamilyUpdate.new,
  column_family_delete: ArgumentGenerators::ColumnFamilyDelete.new,
  column_family_alter: ArgumentGenerators::ColumnFamilyAlter.new,
  index_create: ArgumentGenerators::IndexCreate.new,
  index_drop: ArgumentGenerators::IndexDrop.new,
  batch: ArgumentGenerators::Batch.new,
  columns: ArgumentGenerators::Columns.new,
}
DefaultResultTransformers =

Private: Hash of commands to related result transformers.

{
  keyspaces: ResultTransformers::Keyspaces.new,
  column_families: ResultTransformers::ColumnFamilies.new,
  column_family_select: ResultTransformers::ResultToArray.new,
  columns: ResultTransformers::Columns.new,
}
DefaultRetryStrategy =

Private: Default retry strategy to retry N times.

RetryStrategies::RetryNTimes.new
Mirror =

Private: Default result transformer for commands that do not have one.

ResultTransformers::Mirror.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ CqlRb

Internal: Initializes a cassandra-cql based CQL executor.

args - The Hash of arguments.

:driver - The Cql::Client connection instance.
:instrumenter - What should be used to instrument all the things
                (default: Cassanity::Instrumenters::Noop).
:argument_generators - A Hash where each key is a command name
                       and each value is the related argument
                       generator that responds to `call`
                       (optional).
:result_transformers - A Hash where each key is a command name
                       and each value is the related result
                       transformer that responds to `call`
                       (optional).
:retry_strategy      - What retry strategy to use on failed
                       Cql::Client calls
                       (default: Cassanity::Instrumenters::RetryNTimes)

Examples

driver = Cql::Client.connect(hosts: ['cassandra.example.com'])
Cassanity::Executors::CqlRb.new(driver: driver)


112
113
114
115
116
117
118
# File 'lib/cassanity/executors/cql_rb.rb', line 112

def initialize(args = {})
  @driver = args.fetch(:driver)
  @instrumenter = args[:instrumenter] || Instrumenters::Noop
  @argument_generators = args.fetch(:argument_generators, DefaultArgumentGenerators)
  @result_transformers = args.fetch(:result_transformers, DefaultResultTransformers)
  @retry_strategy = args[:retry_strategy] || DefaultRetryStrategy
end

Instance Attribute Details

#argument_generatorsObject (readonly)

Private



78
79
80
# File 'lib/cassanity/executors/cql_rb.rb', line 78

def argument_generators
  @argument_generators
end

#driverObject (readonly)

Private



75
76
77
# File 'lib/cassanity/executors/cql_rb.rb', line 75

def driver
  @driver
end

#instrumenterObject (readonly)

Private: What should be used to instrument all the things.



84
85
86
# File 'lib/cassanity/executors/cql_rb.rb', line 84

def instrumenter
  @instrumenter
end

#result_transformersObject (readonly)

Private



81
82
83
# File 'lib/cassanity/executors/cql_rb.rb', line 81

def result_transformers
  @result_transformers
end

#retry_strategyObject (readonly)

Private: What strategy to use when retrying Cassandra commands



87
88
89
# File 'lib/cassanity/executors/cql_rb.rb', line 87

def retry_strategy
  @retry_strategy
end

Instance Method Details

#call(args = {}) ⇒ Object

Internal: Execute a CQL query.

args - One or more arguments to send to execute. First should always be

String CQL query. The rest should be the bound variables if any
are needed.

Examples

call({
  command: :keyspaces,
})

call({
  command: :keyspace_create,
  arguments: {keyspace_name: 'analytics'},
})

Returns the result of execution. Raises Cassanity::Error if anything goes wrong during execution.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cassanity/executors/cql_rb.rb', line 139

def call(args = {})
  instrument('cql.cassanity') do |payload|
    begin
      command = args.fetch(:command)
      payload[:command] = command
      generator = @argument_generators.fetch(command)
    rescue KeyError => e
      raise Cassanity::UnknownCommand
    end

    arguments = args[:arguments]

    send_use_command = false
    if arguments
      # TODO: As a temporary measure, we remove this deprecated option
      # while we have time to update each gem (e.g., adapter-cassanity)
      # that sets it. Consistency should be specified at the connection
      # level for now.
      if arguments[:using]
        arguments[:using].delete(:consistency)
      end

      # Instrumentation parameters
      if (keyspace_name = arguments[:keyspace_name])
        payload[:keyspace_name] = keyspace_name
      end
      if (column_family_name = arguments[:column_family_name])
        payload[:column_family_name] = column_family_name
      end

      # Select the correct keyspace before executing the CQL query
      if command != :keyspace_create && (keyspace_name = arguments[:keyspace_name])
        send_use_command = true
      end
    end

    begin
      cql, *variables = generator.call(arguments)
      payload[:cql] = cql
      payload[:cql_variables] = variables

      statement = Cassanity::Statement.new(cql)
      result = @retry_strategy.execute(payload) do
        @driver.use(keyspace_name) if send_use_command
        @driver.execute(statement.interpolate(variables))
      end

      transformer = @result_transformers.fetch(command, Mirror)
      transformed_result = transformer.call(result, args[:transformer_arguments])
      payload[:result] = transformed_result
    rescue StandardError => e
      raise Cassanity::Error
    end

    transformed_result
  end
end

#inspectObject

Public



198
199
200
201
202
203
# File 'lib/cassanity/executors/cql_rb.rb', line 198

def inspect
  attributes = [
    "driver=#{@driver.inspect}",
  ]
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
end