Sha256: 2fdfe4a5d64f3234dd3b74179e286ce6aaa9f23eb4f41c7a49fb8243fa2df6ad

Contents?: true

Size: 1.4 KB

Versions: 11

Compression:

Stored size: 1.4 KB

Contents

module Friendly
  class Indexer
    class << self
      attr_accessor :objects_per_iteration

      def populate(klass, *fields)
        instance.populate(klass, klass.storage_proxy.index_for_fields(fields))
      end

      def instance
        @instance ||= new
      end
    end

    self.objects_per_iteration = 100

    attr_reader :datastore, :translator

    def initialize(datastore  = Friendly.datastore, translator = Translator.new)
      @datastore  = datastore
      @translator = translator
    end

    def populate(klass, index)
      count  = 0
      loop do
        rows = datastore.all(klass, Query.new(:offset! => count, 
                                              :limit!  => objects_per_iteration, 
                                              :order!  => :added_id.asc))
        rows.each do |attrs|
          begin
            index.create(translator.to_object(klass, attrs))
          rescue Sequel::DatabaseError
            # we can safely swallow this exception because if we've gotten
            # to this point, we can be pretty sure that it's a duplicate
            # key error, which just means that the object already exists
            # in the index
          end
        end
        break if rows.length < objects_per_iteration
        count += objects_per_iteration
      end
    end

    protected
      def objects_per_iteration
        self.class.objects_per_iteration
      end
  end
end

Version data entries

11 entries across 11 versions & 7 rubygems

Version Path
ihoka-friendly-0.6.3 lib/friendly/indexer.rb
ihoka-friendly-0.6.2 lib/friendly/indexer.rb
friendly-0.6.0 lib/friendly/indexer.rb
honkster-friendly-0.5.3 lib/friendly/indexer.rb
honkster-friendly-0.5.2 lib/friendly/indexer.rb
honkster-friendly-0.5.1 lib/friendly/indexer.rb
wayne-friendly-0.5.1 lib/friendly/indexer.rb
wego-friendly-0.5.1 lib/friendly/indexer.rb
arunthampi-friendly-0.5.1 lib/friendly/indexer.rb
friendly_postgres-0.5.1 lib/friendly/indexer.rb
friendly-0.5.1 lib/friendly/indexer.rb