Class: NoSE::Backend::FileBackend::InsertStatementStep

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

Overview

Insert data into an index on the backend

Instance Attribute Summary

Attributes inherited from Backend::StatementStep

#index

Instance Method Summary collapse

Methods inherited from Backend::InsertStatementStep

#initialize

Methods included from Supertype

included

Constructor Details

This class inherits a constructor from NoSE::Backend::Backend::InsertStatementStep

Instance Method Details

#process(results) ⇒ Object

Add new rows to the index



135
136
137
138
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
# File 'lib/nose/backend/file.rb', line 135

def process(results)
  key_ids = (@index.hash_fields + @index.order_fields).map(&:id).to_set

  results.each do |row|
    # Pick out primary key fields we can use to match
    conditions = row.select do |field_id|
      key_ids.include? field_id
    end

    # If we have all the primary keys, check for a match
    if conditions.length == key_ids.length
      # Try to find a row with this ID and update it
      matching_row = @client[index.key].find do |index_row|
        index_row.merge(conditions) == index_row
      end

      unless matching_row.nil?
        matching_row.merge! row
        next
      end
    end

    # Populate IDs as needed
    key_ids.each do |key_id|
      row[key_id] = SecureRandom.uuid if row[key_id].nil?
    end

    @client[index.key] << row
  end
end