#199
[acts_as_ferret.git] / lib / bulk_indexer.rb
blobda318c860ad7cb86151fa1c2b670a7e4c28905dd
1 module ActsAsFerret
2   class BulkIndexer
3     def initialize(args = {})
4       @batch_size = args[:batch_size] || 1000
5       @logger = args[:logger]
6       @model = args[:model]
7       @work_done = 0
8       @index = args[:index]
9       if args[:reindex]
10         @reindex = true
11         @model_count  = @model.count.to_f
12       else
13         @model_count = args[:total]
14       end
15     end
17     def index_records(records, offset)
18       batch_time = measure_time {
19         records.each { |rec| @index.add_document(rec.to_doc, rec.ferret_analyzer) if rec.ferret_enabled?(true) }
20       }.to_f
21       @work_done = offset.to_f / @model_count * 100.0 if @model_count > 0
22       remaining_time = ( batch_time / @batch_size ) * ( @model_count - offset + @batch_size )
23       @logger.info "#{@reindex ? 're' : 'bulk '}index model #{@model.name} : #{'%.2f' % @work_done}% complete : #{'%.2f' % remaining_time} secs to finish"
25     end
27     def measure_time
28       t1 = Time.now
29       yield
30       Time.now - t1
31     end
33   end
35 end