Skip to content

Commit 1090b3f

Browse files
Eliminate unnecessary killCursors command when batchSize == limit (mongodb#1656)
JAVA-5667
1 parent ace6104 commit 1090b3f

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,12 @@ private BsonDocument getCommand(final OperationContext operationContext, final i
390390
if (batchSize < 0 && Math.abs(batchSize) < limit) {
391391
commandDocument.put("limit", new BsonInt32(Math.abs(batchSize)));
392392
} else if (batchSize != 0) {
393-
commandDocument.put("batchSize", new BsonInt32(Math.abs(batchSize)));
393+
int effectiveBatchSize = Math.abs(batchSize);
394+
if (effectiveBatchSize == limit) {
395+
// avoid an open cursor on server side when batchSize and limit are equal
396+
effectiveBatchSize++;
397+
}
398+
commandDocument.put("batchSize", new BsonInt32(effectiveBatchSize));
394399
}
395400
}
396401
if (limit < 0 || batchSize < 0) {

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

Lines changed: 62 additions & 0 deletions
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
}

0 commit comments

Comments
 (0)