Skip to content

Commit 5521e58

Browse files
mikebaldrycomandeo-mongo
authored andcommitted
RUBY-2961 Treat 0 limit as no limit and negative limit as positive limit in query caching (#2452)
1 parent 3f5c2e0 commit 5521e58

File tree

4 files changed

+337
-3
lines changed

4 files changed

+337
-3
lines changed

lib/mongo/collection/view/iterable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def each
7070
# If a query with a limit is performed, the query cache will
7171
# re-use results from an earlier query with the same or larger
7272
# limit, and then impose the lower limit during iteration.
73-
limit_for_cached_query = respond_to?(:limit) ? limit : nil
73+
limit_for_cached_query = respond_to?(:limit) ? QueryCache.normalized_limit(limit) : nil
7474
end
7575

7676
if block_given?

lib/mongo/query_cache.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ def set(cursor, **opts)
179179
#
180180
# @api private
181181
def get(**opts)
182-
limit = opts[:limit]
182+
limit = normalized_limit(opts[:limit])
183+
183184
_namespace_key = namespace_key(**opts)
184185
_cache_key = cache_key(**opts)
185186

@@ -189,7 +190,7 @@ def get(**opts)
189190
caching_cursor = namespace_hash[_cache_key]
190191
return nil unless caching_cursor
191192

192-
caching_cursor_limit = caching_cursor.view.limit
193+
caching_cursor_limit = normalized_limit(caching_cursor.view.limit)
193194

194195
# There are two scenarios in which a caching cursor could fulfill the
195196
# query:
@@ -199,6 +200,7 @@ def get(**opts)
199200
#
200201
# Otherwise, return nil because the stored cursor will not satisfy
201202
# the query.
203+
202204
if limit && (caching_cursor_limit.nil? || caching_cursor_limit >= limit)
203205
caching_cursor
204206
elsif limit.nil? && caching_cursor_limit.nil?
@@ -208,6 +210,14 @@ def get(**opts)
208210
end
209211
end
210212

213+
def normalized_limit(limit)
214+
return nil unless limit
215+
# For the purposes of caching, a limit of 0 means no limit, as mongo treats it as such.
216+
return nil if limit == 0
217+
# For the purposes of caching, a negative limit is the same as as a positive limit.
218+
limit.abs
219+
end
220+
211221
private
212222

213223
def cache_key(**opts)

spec/integration/query_cache_spec.rb

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,69 @@
345345

346346
it 'uses the cache' do
347347
results_limit_5 = authorized_collection.find.limit(5).to_a
348+
results_limit_negative_5 = authorized_collection.find.limit(-5).to_a
348349
results_limit_3 = authorized_collection.find.limit(3).to_a
350+
results_limit_negative_3 = authorized_collection.find.limit(-3).to_a
349351
results_no_limit = authorized_collection.find.to_a
352+
results_limit_0 = authorized_collection.find.limit(0).to_a
353+
354+
355+
expect(results_limit_5.length).to eq(5)
356+
expect(results_limit_5.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4])
357+
358+
expect(results_limit_negative_5.length).to eq(5)
359+
expect(results_limit_negative_5.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4])
360+
361+
expect(results_limit_3.length).to eq(3)
362+
expect(results_limit_3.map { |r| r["test"] }).to eq([0, 1, 2])
363+
364+
expect(results_limit_negative_3.length).to eq(3)
365+
expect(results_limit_negative_3.map { |r| r["test"] }).to eq([0, 1, 2])
366+
367+
expect(results_no_limit.length).to eq(10)
368+
expect(results_no_limit.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
369+
370+
expect(results_limit_0.length).to eq(10)
371+
expect(results_limit_0.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
372+
373+
expect(events.length).to eq(1)
374+
end
375+
end
376+
377+
context 'when the first query has a 0 limit' do
378+
before do
379+
authorized_collection.find.limit(0).to_a
380+
end
381+
382+
it 'uses the cache' do
383+
results_limit_5 = authorized_collection.find.limit(5).to_a
384+
results_limit_negative_5 = authorized_collection.find.limit(-5).to_a
385+
results_limit_3 = authorized_collection.find.limit(3).to_a
386+
results_limit_negative_3 = authorized_collection.find.limit(-3).to_a
387+
results_no_limit = authorized_collection.find.to_a
388+
results_limit_0 = authorized_collection.find.limit(0).to_a
350389

351390
expect(results_limit_5.length).to eq(5)
352391
expect(results_limit_5.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4])
353392

393+
expect(results_limit_negative_5.length).to eq(5)
394+
expect(results_limit_negative_5.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4])
395+
396+
354397
expect(results_limit_3.length).to eq(3)
355398
expect(results_limit_3.map { |r| r["test"] }).to eq([0, 1, 2])
356399

400+
expect(results_limit_negative_3.length).to eq(3)
401+
expect(results_limit_negative_3.map { |r| r["test"] }).to eq([0, 1, 2])
402+
403+
357404
expect(results_no_limit.length).to eq(10)
358405
expect(results_no_limit.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
359406

407+
408+
expect(results_limit_0.length).to eq(10)
409+
expect(results_limit_0.map { |r| r["test"] }).to eq([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
410+
360411
expect(events.length).to eq(1)
361412
end
362413
end
@@ -391,6 +442,21 @@
391442
end
392443
end
393444

445+
context 'and two queries are performed with a larger negative limit' do
446+
it 'uses the query cache for the third query' do
447+
results1 = authorized_collection.find.limit(-3).to_a
448+
results2 = authorized_collection.find.limit(-3).to_a
449+
450+
expect(results1.length).to eq(3)
451+
expect(results1.map { |r| r["test"] }).to eq([0, 1, 2])
452+
453+
expect(results2.length).to eq(3)
454+
expect(results2.map { |r| r["test"] }).to eq([0, 1, 2])
455+
456+
expect(events.length).to eq(2)
457+
end
458+
end
459+
394460
context 'and the second query has a smaller limit' do
395461
let(:results) { authorized_collection.find.limit(1).to_a }
396462

@@ -401,6 +467,99 @@
401467
end
402468
end
403469

470+
context 'and the second query has a smaller negative limit' do
471+
let(:results) { authorized_collection.find.limit(-1).to_a }
472+
473+
it 'uses the cached query' do
474+
expect(results.count).to eq(1)
475+
expect(results.first["test"]).to eq(0)
476+
expect(events.length).to eq(1)
477+
end
478+
end
479+
480+
context 'and the second query has no limit' do
481+
it 'queries again' do
482+
expect(authorized_collection.find.to_a.count).to eq(10)
483+
expect(events.length).to eq(2)
484+
end
485+
end
486+
end
487+
488+
context 'when the first query has a negative limit' do
489+
before do
490+
authorized_collection.find.limit(-2).to_a
491+
end
492+
493+
context 'and the second query has a larger limit' do
494+
let(:results) { authorized_collection.find.limit(3).to_a }
495+
496+
it 'queries again' do
497+
expect(results.length).to eq(3)
498+
expect(results.map { |result| result["test"] }).to eq([0, 1, 2])
499+
expect(events.length).to eq(2)
500+
end
501+
end
502+
503+
context 'and the second query has a larger negative limit' do
504+
let(:results) { authorized_collection.find.limit(-3).to_a }
505+
506+
it 'queries again' do
507+
expect(results.length).to eq(3)
508+
expect(results.map { |result| result["test"] }).to eq([0, 1, 2])
509+
expect(events.length).to eq(2)
510+
end
511+
end
512+
513+
context 'and two queries are performed with a larger limit' do
514+
it 'uses the query cache for the third query' do
515+
results1 = authorized_collection.find.limit(3).to_a
516+
results2 = authorized_collection.find.limit(3).to_a
517+
518+
expect(results1.length).to eq(3)
519+
expect(results1.map { |r| r["test"] }).to eq([0, 1, 2])
520+
521+
expect(results2.length).to eq(3)
522+
expect(results2.map { |r| r["test"] }).to eq([0, 1, 2])
523+
524+
expect(events.length).to eq(2)
525+
end
526+
end
527+
528+
context 'and two queries are performed with a larger negative limit' do
529+
it 'uses the query cache for the third query' do
530+
results1 = authorized_collection.find.limit(-3).to_a
531+
results2 = authorized_collection.find.limit(-3).to_a
532+
533+
expect(results1.length).to eq(3)
534+
expect(results1.map { |r| r["test"] }).to eq([0, 1, 2])
535+
536+
expect(results2.length).to eq(3)
537+
expect(results2.map { |r| r["test"] }).to eq([0, 1, 2])
538+
539+
expect(events.length).to eq(2)
540+
end
541+
end
542+
543+
context 'and the second query has a smaller limit' do
544+
let(:results) { authorized_collection.find.limit(1).to_a }
545+
546+
it 'uses the cached query' do
547+
expect(results.count).to eq(1)
548+
expect(results.first["test"]).to eq(0)
549+
expect(events.length).to eq(1)
550+
end
551+
end
552+
553+
context 'and the second query has a smaller negative limit' do
554+
let(:results) { authorized_collection.find.limit(-1).to_a }
555+
556+
it 'uses the cached query' do
557+
expect(results.count).to eq(1)
558+
expect(results.first["test"]).to eq(0)
559+
expect(events.length).to eq(1)
560+
end
561+
end
562+
404563
context 'and the second query has no limit' do
405564
it 'queries again' do
406565
expect(authorized_collection.find.to_a.count).to eq(10)

0 commit comments

Comments
 (0)