class NoSE::Backend::CassandraBackend::InsertStatementStep

Insert data into an index on the backend

Public Class Methods

new(client, index, fields) click to toggle source
# 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

Public Instance Methods

process(results) click to toggle source

Insert each row into the index

# 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

Private Instance Methods

insert_cql() click to toggle source

The CQL used to insert the fields into the index

# File lib/nose/backend/cassandra.rb, line 223
def insert_cql
  insert = "INSERT INTO #{@index.key} ("
  insert += @fields.map { |f| "\"#{f}\"" }.join(', ')
  insert << ') VALUES (' << (['?'] * @fields.length).join(', ') + ')'

  insert
end