module Jekyll::Algolia::Indexer

def self.update_records(old_records_ids, new_records)

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

new_records - Records to add to the index
old_records_ids - Ids of records to delete from the index

Public: Update records of the index
def self.update_records(old_records_ids, new_records)
  # Stop if nothing to change
  if old_records_ids.empty? && new_records.empty?
    Logger.log('I:Content is already up to date.')
    return
  end
  Logger.log("I:Updating records in index #{index.name}...")
  Logger.log("I:Records to delete: #{old_records_ids.length}")
  Logger.log("I:Records to add:    #{new_records.length}")
  return if Configurator.dry_run?
  # We group delete and add operations into the same batch. Delete
  # operations should still come first, to avoid hitting an overquota too
  # soon
  operations = []
  old_records_ids.each do |object_id|
    operations << {
      action: 'deleteObject', indexName: index.name,
      body: { objectID: object_id }
    }
  end
  operations += new_records.map do |new_record|
    { action: 'addObject', indexName: index.name, body: new_record }
  end
  # Run the batches in slices if they are too large
  batch_size = Configurator.algolia('indexing_batch_size')
  slices = operations.each_slice(batch_size).to_a
  should_have_progress_bar = (slices.length > 1)
  if should_have_progress_bar
    progress_bar = ProgressBar.create(
      total: slices.length,
      format: 'Pushing records (%j%%) |%B|'
    )
  end
  slices.each do |slice|
    begin
      ::Algolia.batch!(slice)
      progress_bar.increment if should_have_progress_bar
    rescue StandardError => error
      records = slice.map do |record|
        record[:body]
      end
      ErrorHandler.stop(error, records: records)
    end
  end
end