module Jekyll::Algolia::Indexer

def self.update_records(records)

Does nothing in dry run mode
update
Note: All operations will be done in one batch, assuring an atomic

records - All records extracted from Jekyll

Public: Update records of the index
def self.update_records(records)
  # Getting list of objectID in remote and locally
  remote_ids = remote_object_ids
  local_ids = local_object_ids(records)
  # Making a diff, to see what to add and what to delete
  ids_to_delete = remote_ids - local_ids
  ids_to_add = local_ids - remote_ids
  # What changes should we do to the indexes?
  has_records_to_update = !ids_to_delete.empty? || !ids_to_add.empty?
  has_object_id_index = index_exist?(index_object_ids)
  # Stop if nothing to change
  if !has_records_to_update && has_object_id_index
    Logger.log('I:Content is already up to date.')
    return
  end
  # We group all operations into one batch
  operations = []
  # We update records only if there are records to update
  if has_records_to_update
    Logger.log("I:Updating records in index #{index.name}...")
    Logger.log("I:Records to delete: #{ids_to_delete.length}")
    Logger.log("I:Records to add:    #{ids_to_add.length}")
    # Transforming ids into real records to add
    records_by_id = Hash[records.map { |r| [r[:objectID], r] }]
    records_to_add = ids_to_add.map { |id| records_by_id[id] }
    # Deletion operations come first, to avoid hitting an overquota too
    # soon if it can be avoided
    ids_to_delete.each do |object_id|
      operations << {
        action: 'deleteObject', indexName: index.name,
        body: { objectID: object_id }
      }
    end
    # Then we add the new records
    operations += records_to_add.map do |new_record|
      { action: 'addObject', indexName: index.name, body: new_record }
    end
  end
  # We update the dedicated index everytime we update records, but we also
  # create it if it does not exist
  should_update_object_id_index = has_records_to_update ||
                                  !has_object_id_index
  if should_update_object_id_index
    operations << { action: 'clear', indexName: index_object_ids.name }
    local_ids.each_slice(100).each do |ids|
      operations << {
        action: 'addObject', indexName: index_object_ids.name,
        body: { content: ids }
      }
    end
  end
  execute_operations(operations)
end