lib/sunspot/indexer.rb in sunspot-2.0.0.pre.111215 vs lib/sunspot/indexer.rb in sunspot-2.0.0.pre.120415
- old
+ new
@@ -1,5 +1,7 @@
+require 'sunspot/batcher'
+
module Sunspot
#
# This class presents a service for adding, updating, and removing data
# from the Solr index. An Indexer instance is associated with a particular
# setup, and thus is capable of indexing instances of a certain class (and its
@@ -20,14 +22,14 @@
#
# model<Object>:: the model to index
#
def add(model)
documents = Util.Array(model).map { |m| prepare(m) }
- if @batch.nil?
- add_documents(documents)
+ if batcher.batching?
+ batcher.concat(documents)
else
- @batch.concat(documents)
+ add_documents(documents)
end
end
#
# Remove the given model from the Solr index
@@ -67,21 +69,24 @@
#
# Start batch processing
#
def start_batch
- @batch = []
+ batcher.start_new
end
#
# Write batch out to Solr and clear it
#
def flush_batch
- add_documents(@batch)
- @batch = nil
+ add_documents(batcher.end_current)
end
private
+
+ def batcher
+ @batcher ||= Batcher.new
+ end
#
# Convert documents into hash of indexed properties
#
def prepare(model)