Skip to content

Commit f8b7b2d

Browse files
increase batchSize when limit is equal to it
1 parent a3383f7 commit f8b7b2d

File tree

7 files changed

+90
-7
lines changed

7 files changed

+90
-7
lines changed

driver-core/src/main/com/mongodb/internal/operation/Operations.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,18 @@ <TResult> FindOperation<TResult> find(final MongoNamespace findNamespace, @Nulla
188188

189189
private <TResult> FindOperation<TResult> createFindOperation(final MongoNamespace findNamespace, @Nullable final Bson filter,
190190
final Class<TResult> resultClass, final FindOptions options) {
191+
final int effectiveBatchSize;
192+
if (options.getBatchSize() > 0 && options.getBatchSize() == options.getLimit()) {
193+
// Eliminate unnecessary killCursors command when batchSize == limit
194+
effectiveBatchSize = options.getBatchSize() + 1;
195+
} else {
196+
effectiveBatchSize = options.getBatchSize();
197+
}
191198
FindOperation<TResult> operation = new FindOperation<>(
192199
findNamespace, codecRegistry.get(resultClass))
193200
.retryReads(retryReads)
194201
.filter(filter == null ? new BsonDocument() : filter.toBsonDocument(documentClass, codecRegistry))
195-
.batchSize(options.getBatchSize())
202+
.batchSize(effectiveBatchSize)
196203
.skip(options.getSkip())
197204
.limit(options.getLimit())
198205
.projection(toBsonDocument(options.getProjection()))

driver-core/src/test/resources/unified-test-format/crud/find.json

+62
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,68 @@
237237
]
238238
}
239239
]
240+
},
241+
{
242+
"description": "Find with batchSize equal to limit",
243+
"operations": [
244+
{
245+
"object": "collection0",
246+
"name": "find",
247+
"arguments": {
248+
"filter": {
249+
"_id": {
250+
"$gt": 1
251+
}
252+
},
253+
"sort": {
254+
"_id": 1
255+
},
256+
"limit": 4,
257+
"batchSize": 4
258+
},
259+
"expectResult": [
260+
{
261+
"_id": 2,
262+
"x": 22
263+
},
264+
{
265+
"_id": 3,
266+
"x": 33
267+
},
268+
{
269+
"_id": 4,
270+
"x": 44
271+
},
272+
{
273+
"_id": 5,
274+
"x": 55
275+
}
276+
]
277+
}
278+
],
279+
"expectEvents": [
280+
{
281+
"client": "client0",
282+
"events": [
283+
{
284+
"commandStartedEvent": {
285+
"command": {
286+
"find": "coll0",
287+
"filter": {
288+
"_id": {
289+
"$gt": 1
290+
}
291+
},
292+
"limit": 4,
293+
"batchSize": 5
294+
},
295+
"commandName": "find",
296+
"databaseName": "find-tests"
297+
}
298+
}
299+
]
300+
}
301+
]
240302
}
241303
]
242304
}

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/FindPublisherImpl.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ Function<AsyncOperations<?>, TimeoutSettings> getTimeoutSettings() {
233233

234234
@Override
235235
AsyncReadOperation<AsyncBatchCursor<T>> asAsyncFirstReadOperation() {
236-
return getOperations().findFirst(filter, getDocumentClass(), findOptions);
236+
final FindOptions effectiveFindOptions;
237+
if (findOptions.getBatchSize() > 0 && findOptions.getBatchSize() == findOptions.getLimit()) {
238+
// Eliminate unnecessary killCursors command when batchSize == limit
239+
effectiveFindOptions = findOptions.withBatchSize(findOptions.getBatchSize() + 1);
240+
} else {
241+
effectiveFindOptions = findOptions;
242+
}
243+
return getOperations().findFirst(filter, getDocumentClass(), effectiveFindOptions);
237244
}
238245
}

driver-reactive-streams/src/test/unit/com/mongodb/reactivestreams/client/internal/FindPublisherImplTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void shouldBuildTheExpectedOperation() {
8888
.retryReads(true)
8989
.filter(new BsonDocument())
9090
.allowDiskUse(false)
91-
.batchSize(100)
91+
.batchSize(101)
9292
.collation(COLLATION)
9393
.comment(new BsonString("my comment"))
9494
.cursorType(CursorType.NonTailable)

driver-sync/src/main/com/mongodb/client/internal/FindIterableImpl.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,14 @@ private <E> E executeExplain(final Class<E> explainResultClass, @Nullable final
245245
}
246246

247247
public ExplainableReadOperation<BatchCursor<TResult>> asReadOperation() {
248-
return operations.find(filter, resultClass, findOptions);
248+
final FindOptions effectiveFindOptions;
249+
if (findOptions.getBatchSize() > 0 && findOptions.getBatchSize() == findOptions.getLimit()) {
250+
// Eliminate unnecessary killCursors command when batchSize == limit
251+
effectiveFindOptions = findOptions.withBatchSize(findOptions.getBatchSize() + 1);
252+
} else {
253+
effectiveFindOptions = findOptions;
254+
}
255+
return operations.find(filter, resultClass, effectiveFindOptions);
249256
}
250257

251258
}

driver-sync/src/test/unit/com/mongodb/client/gridfs/GridFSFindIterableSpecification.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class GridFSFindIterableSpecification extends Specification {
8888
expect operation, isTheSameAs(new FindOperation<GridFSFile>(namespace, gridFSFileCodec)
8989
.filter(new BsonDocument('filter', new BsonInt32(2)))
9090
.sort(new BsonDocument('sort', new BsonInt32(2)))
91-
.batchSize(99)
91+
.batchSize(100)
9292
.limit(99)
9393
.skip(9)
9494
.noCursorTimeout(true)

driver-sync/src/test/unit/com/mongodb/client/internal/FindIterableSpecification.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class FindIterableSpecification extends Specification {
8787
.filter(new BsonDocument('filter', new BsonInt32(1)))
8888
.sort(new BsonDocument('sort', new BsonInt32(1)))
8989
.projection(new BsonDocument('projection', new BsonInt32(1)))
90-
.batchSize(100)
90+
.batchSize(101)
9191
.limit(100)
9292
.skip(10)
9393
.cursorType(CursorType.NonTailable)
@@ -132,7 +132,7 @@ class FindIterableSpecification extends Specification {
132132
.filter(new BsonDocument('filter', new BsonInt32(2)))
133133
.sort(new BsonDocument('sort', new BsonInt32(2)))
134134
.projection(new BsonDocument('projection', new BsonInt32(2)))
135-
.batchSize(99)
135+
.batchSize(100)
136136
.limit(99)
137137
.skip(9)
138138
.cursorType(CursorType.Tailable)

0 commit comments

Comments
 (0)