Permalink
Browse files

Replace sunspot:reindex task

  • Loading branch information...
1 parent 6d2828b commit 56a1e9cebdd72e0dff452a5623f906794c69e6e5 @bkeepers committed Nov 18, 2011
View
20 lib/sunspot/mongo_mapper.rb
@@ -27,5 +27,25 @@ def load_all(ids)
@clazz.all(:id.in => ids)
end
end
+
+ module ClassMethods
+ def find_in_batches(options = {}, &block)
+ puts "finding in batches: #{options.inspect}"
+ batch_size = options.delete(:batch_size) || 1000
+ start = options.delete(:start) || 0
+ options.delete(:include)
+ records = []
+
+ query(options).skip(start).order(:id.asc).each do |record|
+ records << record
+ if records.size == batch_size
+ yield records
+ records = []
+ end
+ end
+
+ yield records unless records.blank?
+ end
+ end
end
end
View
11 lib/sunspot/mongo_mapper/railtie.rb
@@ -0,0 +1,11 @@
+require 'rails'
+
+module Sunspot
+ module MongoMapper
+ class Railtie < ::Rails::Railtie
+ rake_tasks do
+ load "sunspot/mongo_mapper/tasks.rb"
+ end
+ end
+ end
+end
View
24 lib/sunspot/mongo_mapper/tasks.rb
@@ -0,0 +1,24 @@
+Rake.application['sunspot:reindex'].clear
+
+namespace :sunspot do
+ desc "Reindex all solr models that are located in your application's models directory."
+ task :reindex, [:batch_size, :models] => :environment do |t, args|
+ reindex_options = {:batch_commit => false}
+ case args[:batch_size]
+ when 'false'
+ reindex_options[:batch_size] = nil
+ when /^\d+$/
+ reindex_options[:batch_size] = args[:batch_size].to_i if args[:batch_size].to_i > 0
+ end
+ unless args[:models]
+ all_files = Dir.glob(Rails.root.join('app', 'models', '*.rb'))
+ all_models = all_files.map { |path| File.basename(path, '.rb').camelize.constantize }
+ sunspot_models = all_models.select { |m| m.respond_to?(:searchable?) && m.searchable? }
+ else
+ sunspot_models = args[:models].split('+').map{|m| m.constantize}
+ end
+ sunspot_models.each do |model|
+ model.solr_reindex reindex_options
+ end
+ end
+end
View
1 lib/sunspot_mongo_mapper.rb
@@ -1,3 +1,4 @@
require 'sunspot/mongo_mapper'
+require 'sunspot/mongo_mapper/railtie'
MongoMapper::Document.plugin Sunspot::MongoMapper
View
11 spec/spec_helper.rb
@@ -0,0 +1,11 @@
+require 'bundler'
+Bundler.require :default, :test
+require 'sunspot_mongo_mapper'
+
+MongoMapper.database = 'sunspot_mongo_mapper'
+
+RSpec.configure do |config|
+ config.before do
+ MongoMapper.database.collections.each(&:remove)
+ end
+end
View
34 spec/sunspot_mongo_mapper_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe Sunspot::MongoMapper do
+ class Post
+ include MongoMapper::Document
+ end
+
+ describe '.find_in_batches' do
+ before do
+ 3.times {|i| Post.create!(:title => "Post #{i}") }
+ end
+
+ it 'should yield array of records in order' do
+ results = []
+
+ Post.find_in_batches(:batch_size => 2) do |records|
+ results << records
+ end
+
+ results[0].length.should == 2
+ results[1].length.should == 1
+ end
+
+ it 'should pass conditions' do
+ results = []
+ Post.find_in_batches(:title => 'Post 1') do |records|
+ results.concat records
+ end
+
+ results.size.should == 1
+ results.first.title.should == 'Post 1'
+ end
+ end
+end
View
1 sunspot_mongo_mapper.gemspec
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'mongo_mapper'
s.add_runtime_dependency 'sunspot', '~> 1.1'
s.add_runtime_dependency 'sunspot_rails', '~> 1.1'
+ s.add_runtime_dependency 'rails', '~> 3.0'
s.add_development_dependency 'rspec', '~> 2.0'
s.add_development_dependency 'rake'
end

0 comments on commit 56a1e9c

Please sign in to comment.