class NoBrainer::Document::Index::Synchronizer

Public Class Methods

new(models) click to toggle source
# File lib/no_brainer/document/index/synchronizer.rb, line 2
def initialize(models)
  @models_indexes_map = Hash[models.map do |model|
    [model, model.indexes.values.reject { |index| index.name == model.pk_name }]
  end]
end

Public Instance Methods

_generate_plan_for(model, wanted_indexes) click to toggle source
# File lib/no_brainer/document/index/synchronizer.rb, line 18
def _generate_plan_for(model, wanted_indexes)
  current_indexes = NoBrainer.run(model.rql_table.index_status).map do |s|
    meta = meta_store.select { |i| i.table_name == model.table_name && i.index_name == s['index'] }.last
    NoBrainer::Document::Index::Index.new(
      model, s['index'], s['index'], nil, nil, nil, s['geo'], s['multi'], meta)
  end

  all_aliased_names = (wanted_indexes + current_indexes).map(&:aliased_name).uniq
  all_aliased_names.map do |aliased_name|
    wanted_index = wanted_indexes.select { |i| i.aliased_name == aliased_name }.first
    current_index = current_indexes.select { |i| i.aliased_name == aliased_name }.first

    next if wanted_index.try(:external)

    case [!wanted_index.nil?, !current_index.nil?]
    when [true, false] then Op.new(wanted_index, :create)
    when [false, true] then Op.new(current_index, :delete)
    when [true, true]  then
      case wanted_index.same_definition?(current_index)
      when true  then nil # up to date
      when false then Op.new(current_index, :update, [wanted_index])
      end
    end
  end.compact
end
generate_plan() click to toggle source
# File lib/no_brainer/document/index/synchronizer.rb, line 44
def generate_plan
  @models_indexes_map.flat_map { |model, indexes| _generate_plan_for(model, indexes) }
end
meta_store() click to toggle source
# File lib/no_brainer/document/index/synchronizer.rb, line 8
def meta_store
  @meta_store ||= NoBrainer::Document::Index::MetaStore.to_a
end
sync_indexes(options={}) click to toggle source
# File lib/no_brainer/document/index/synchronizer.rb, line 48
def sync_indexes(options={})
  lock = NoBrainer::Lock.new('nobrainer:sync_indexes')

  lock.synchronize do
    generate_plan.each { |op| op.run(options) }
  end

  unless options[:wait] == false
    # Waiting on all models due to possible races
    @models_indexes_map.each_key do |model|
      NoBrainer.run(model.rql_table.index_wait())
    end
  end

  true
end