Class: NoSE::Backend::CassandraBackend::InsertStatementStep

Inherits:
Backend::InsertStatementStep show all
Defined in:
lib/nose/backend/cassandra.rb

Overview

Insert data into an index on the backend

Instance Attribute Summary

Attributes inherited from Backend::StatementStep

#index

Instance Method Summary collapse

Methods included from Supertype

included

Constructor Details

#initialize(client, index, fields) ⇒ InsertStatementStep

Returns a new instance of InsertStatementStep.



178
179
180
181
182
183
184
# File 'lib/nose/backend/cassandra.rb', line 178

def initialize(client, index, fields)
  super

  @fields = fields.map(&:id) & index.all_fields.map(&:id)
  @prepared = client.prepare insert_cql
  @generator = Cassandra::Uuid::Generator.new
end

Instance Method Details

#process(results) ⇒ Object

Insert each row into the index



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/nose/backend/cassandra.rb', line 187

def process(results)
  results.each do |result|
    fields = @index.all_fields.select { |field| result.key? field.id }
    values = fields.map do |field|
      value = result[field.id]

      # If this is an ID, generate or construct a UUID object
      if field.is_a?(Fields::IDField)
        value = if value.nil?
                  @generator.uuid
                else
                  Cassandra::Uuid.new(value.to_i)
                end
      end

      # XXX Useful to test that we never insert null values
      # fail if value.nil?

      value
    end

    begin
      @client.execute(@prepared, arguments: values)
    rescue Cassandra::Errors::InvalidError
      # We hit a value which does not actually need to be
      # inserted based on the data since some foreign
      # key in the graph corresponding to this column
      # family does not exist
      nil
    end
  end
end