outoftime-sunspot_rails 0.9.12 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -236,9 +236,9 @@ http://outoftime.lighthouseapp.com/projects/20339-sunspot
236
236
 
237
237
  == Contributors
238
238
 
239
- Mat Brown (mat@patch.com)
240
- Peer Allan (peer.allan@gmail.com)
241
- Michael Moen (michael@underpantsgnome.com)
239
+ - Mat Brown (mat@patch.com)
240
+ - Peer Allan (peer.allan@gmail.com)
241
+ - Michael Moen (michael@underpantsgnome.com)
242
242
 
243
243
  == License
244
244
 
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 12
2
+ :patch: 0
3
3
  :major: 0
4
- :minor: 9
4
+ :minor: 10
@@ -147,32 +147,53 @@ module Sunspot #:nodoc:
147
147
  # database at a time. The default batch size is 500; if nil is passed,
148
148
  # records will not be indexed in batches. By default, a commit is issued
149
149
  # after each batch; passing +false+ for +batch_commit+ will disable
150
- # this, and only issue a commit at the end of the process.
150
+ # this, and only issue a commit at the end of the process. If associated
151
+ # objects need to indexed also, you can specify +include+ in format
152
+ # accepted by ActiveRecord to improve your sql select performance
151
153
  #
152
- # ==== Parameters
154
+ # ==== Options (passed as a hash)
153
155
  #
154
156
  # batch_size<Integer>:: Batch size with which to load records. Passing
155
157
  # 'nil' will skip batches. Default is 500.
156
- # batch_commit<Boolean>:: Flag singaling if a commit should be done after
158
+ # batch_commit<Boolean>:: Flag signalling if a commit should be done after
157
159
  # after each batch is indexed, default is 'true'
160
+ # include<Mixed>:: include option to be passed to the ActiveRecord find,
161
+ # used for including associated objects that need to be
162
+ # indexed with the parent object, accepts all formats
163
+ # ActiveRecord::Base.find does
158
164
  #
159
- def reindex(batch_size = 500, batch_commit = true)
165
+ # ==== Examples
166
+ #
167
+ # # reindex in batches of 500, commit after each
168
+ # Post.reindex
169
+ #
170
+ # # index all rows at once, then commit
171
+ # Post.reindex(:batch_size => nil)
172
+ #
173
+ # # reindex in batches of 500, commit when all batches complete
174
+ # Post.reindex(:batch_commit => false)
175
+ #
176
+ # # include the associated +author+ object when loading to index
177
+ # Post.reindex(:include => :author)
178
+ #
179
+ def reindex(opts={})
180
+ options = { :batch_size => 500, :batch_commit => true, :include => []}.merge(opts)
160
181
  remove_all_from_index
161
- unless batch_size
162
- Sunspot.index!(all)
182
+ unless options[:batch_size]
183
+ Sunspot.index!(all(:include => options[:include]))
163
184
  else
164
185
  record_count = count(:order => primary_key)
165
186
  counter = 1
166
187
  offset = 0
167
188
  while(offset < record_count)
168
- benchmark batch_size, counter do
169
- Sunspot.index(all(:offset => offset, :limit => batch_size, :order => primary_key))
189
+ benchmark options[:batch_size], counter do
190
+ Sunspot.index(all(:include => options[:include], :offset => offset, :limit => options[:batch_size], :order => primary_key))
170
191
  end
171
- Sunspot.commit if batch_commit
172
- offset += batch_size
192
+ Sunspot.commit if options[:batch_commit]
193
+ offset += options[:batch_size]
173
194
  counter += 1
174
195
  end
175
- Sunspot.commit unless batch_commit
196
+ Sunspot.commit unless options[:batch_commit]
176
197
  end
177
198
  end
178
199
 
data/spec/model_spec.rb CHANGED
@@ -158,10 +158,32 @@ describe 'ActiveRecord mixin' do
158
158
  end
159
159
 
160
160
  it 'should index all instances' do
161
+ Post.reindex(:batch_size => nil)
162
+ Sunspot.commit
163
+ Post.search.results.to_set.should == @posts.to_set
164
+ end
165
+
166
+ it 'should remove all currently indexed instances' do
167
+ old_post = Post.create!
168
+ old_post.index!
169
+ old_post.destroy
161
170
  Post.reindex
162
171
  Sunspot.commit
163
172
  Post.search.results.to_set.should == @posts.to_set
164
173
  end
174
+
175
+ end
176
+
177
+ describe 'reindex() with real data' do
178
+ before :each do
179
+ @posts = Array.new(2) { Post.create }
180
+ end
181
+
182
+ it 'should index all instances' do
183
+ Post.reindex(:batch_size => nil)
184
+ Sunspot.commit
185
+ Post.search.results.to_set.should == @posts.to_set
186
+ end
165
187
 
166
188
  it 'should remove all currently indexed instances' do
167
189
  old_post = Post.create!
@@ -173,11 +195,78 @@ describe 'ActiveRecord mixin' do
173
195
  end
174
196
 
175
197
  describe "using batch sizes" do
176
- it 'should index with a batch size' do
177
- Post.reindex(1)
198
+ it 'should index with a specified batch size' do
199
+ Post.reindex(:batch_size => 1)
178
200
  Sunspot.commit
179
201
  Post.search.results.to_set.should == @posts.to_set
180
202
  end
181
203
  end
204
+
182
205
  end
206
+
207
+ describe "reindex()" do
208
+
209
+ before(:each) do
210
+ @posts = Array.new(10) { Post.create }
211
+ end
212
+
213
+ describe "when not using batches" do
214
+
215
+ it "should select all if the batch_size is nil" do
216
+ Post.should_receive(:all).with(:include => []).and_return([])
217
+ Post.reindex(:batch_size => nil)
218
+ end
219
+
220
+ it "should search for models with includes" do
221
+ Post.should_receive(:all).with(:include => :author).and_return([])
222
+ Post.reindex(:batch_size => nil, :include => :author)
223
+ end
224
+
225
+ end
226
+
227
+ describe "when using batches" do
228
+
229
+ it "should use the default options" do
230
+ Post.should_receive(:all).with do |params|
231
+ params[:limit].should == 500
232
+ params[:include].should == []
233
+ end.and_return(@posts)
234
+ Post.reindex
235
+ end
236
+
237
+ it "should count the number of records to index" do
238
+ Post.should_receive(:count).and_return(10)
239
+ Post.reindex
240
+ end
241
+
242
+ it "should override the batch_size" do
243
+ Post.should_receive(:all).with do |params|
244
+ params[:limit].should == 20
245
+ @posts
246
+ end.and_return(@posts)
247
+ Post.reindex(:batch_size => 20)
248
+ end
249
+
250
+ it "should set the include option" do
251
+ Post.should_receive(:all).with do |params|
252
+ params[:include].should == [{:author => :address}]
253
+ @posts
254
+ end.and_return(@posts)
255
+ Post.reindex(:include => [{:author => :address}])
256
+ end
257
+
258
+ it "should commit after indexing each batch" do
259
+ Sunspot.should_receive(:commit).twice
260
+ Post.reindex(:batch_size => 5)
261
+ end
262
+
263
+ it "should commit after indexing everything" do
264
+ Sunspot.should_receive(:commit).once
265
+ Post.reindex(:batch_commit => false)
266
+ end
267
+
268
+ end
269
+ end
270
+
271
+
183
272
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outoftime-sunspot_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.12
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat Brown
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-04 00:00:00 -07:00
12
+ date: 2009-06-05 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency