Sha256: 766e3dd6ed3e2c64e9263f9bbdcc02292b21c6e36f72c50f3dd1b255758fd146

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Chewy
  class Strategy
    # This strategy accumulates all the objects prepared for
    # indexing and fires index process when strategy is popped
    # from the strategies stack.
    #
    #   Chewy.strategy(:atomic) do
    #     User.all.map(&:save) # Does nothing here
    #     Post.all.map(&:save) # And here
    #     # It imports all the changed users and posts right here
    #     # before block leaving with bulk ES API, kinda optimization
    #   end
    #
    class Atomic < Base
      def initialize
        @stash = {}
      end

      def update type, objects, options = {}
        ActiveSupport::Deprecation.warn("`urgent: true` option is deprecated and is not effective inside `:atomic` strategy, use `Chewy.strategy(:urgent)` strategy instead") if options.key?(:urgent)

        @stash[type] ||= []
        @stash[type] |= type.send(:build_root).id ? Array.wrap(objects) : type.adapter.identify(objects)
      end

      def leave
        @stash.all? { |type, ids| type.import!(ids) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chewy-0.8.4 lib/chewy/strategy/atomic.rb
chewy-0.8.3 lib/chewy/strategy/atomic.rb