Sha256: 5a6bef90699a5d322810359b72af56d6abc0f54877210bfd55d104747da939b6

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module ActsAsFerret
  class BulkIndexer
    def initialize(args = {})
      @batch_size = args[:batch_size] || 1000
      @logger = args[:logger]
      @model = args[:model]
      @work_done = 0
      @index = args[:index]
      if args[:reindex]
        @reindex = true
        @model_count  = @model.count.to_f
      else
        @model_count = args[:total]
      end
    end

    def index_records(records, offset)
      batch_time = measure_time {
        docs = []
        records.each { |rec| docs << [rec.to_doc, rec.ferret_analyzer] if rec.ferret_enabled?(true) }
        @index.update_batch(docs)
        # records.each { |rec| @index.add_document(rec.to_doc, rec.ferret_analyzer) if rec.ferret_enabled?(true) }
      }.to_f
      @work_done = offset.to_f / @model_count * 100.0 if @model_count > 0
      remaining_time = ( batch_time / @batch_size ) * ( @model_count - offset + @batch_size )
      @logger.info "#{@reindex ? 're' : 'bulk '}index model #{@model.name} : #{'%.2f' % @work_done}% complete : #{'%.2f' % remaining_time} secs to finish"

    end

    def measure_time
      t1 = Time.now
      yield
      Time.now - t1
    end

  end

end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
cehoffman-acts_as_ferret-0.4.4 lib/bulk_indexer.rb
jkraemer-acts_as_ferret-0.4.4 lib/bulk_indexer.rb
acts_as_ferret-0.4.4 lib/bulk_indexer.rb