Skip to content

Commit

Permalink
Merge pull request sunspot#282 from culturecode/optimize-solr-index-o…
Browse files Browse the repository at this point in the history
…rphans

Optimized memory usage when calling solr_index_orphans.
  • Loading branch information
benmoss committed Sep 8, 2012
2 parents 4178b45 + b375ee7 commit 5a592a8
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions sunspot_rails/lib/sunspot/rails/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,17 @@ def solr_index(opts={})
#
# Array:: Collection of IDs that exist in Solr but not in the database
def solr_index_orphans(opts={})
batch_size = opts[:batch_size] || Sunspot.config.indexing.default_batch_size
count = self.count
indexed_ids = solr_search_ids { paginate(:page => 1, :per_page => count) }.to_set
find_each(:select => 'id', :batch_size => batch_size) do |object|
indexed_ids.delete(object.id)
batch_size = opts[:batch_size] || Sunspot.config.indexing.default_batch_size

solr_page = 0
solr_ids = []
while (solr_page = solr_page.next)
ids = solr_search_ids { paginate(:page => solr_page, :per_page => 1000) }.to_a
break if ids.empty?
solr_ids.concat ids
end
indexed_ids.to_a

return solr_ids - self.connection.select_values(select(:id).arel).collect(&:to_i)
end

#
Expand Down

0 comments on commit 5a592a8

Please sign in to comment.