Skip to content

Commit 1894003

Browse files
authored
PYTHON-4921 Eliminate unnecessary killCursors command when batchSize == limit (mongodb#2004)
1 parent d2c1e18 commit 1894003

File tree

8 files changed

+368
-32
lines changed

8 files changed

+368
-32
lines changed

pymongo/message.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ def _gen_find_command(
252252
if limit < 0:
253253
cmd["singleBatch"] = True
254254
if batch_size:
255+
# When limit and batchSize are equal we increase batchSize by 1 to
256+
# avoid an unnecessary killCursors.
257+
if limit == batch_size:
258+
batch_size += 1
255259
cmd["batchSize"] = batch_size
256260
if read_concern.level and not (session and session.in_transaction):
257261
cmd["readConcern"] = read_concern.document

test/crud/unified/client-bulkWrite-replaceOne-sort.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"schemaVersion": "1.4",
44
"runOnRequirements": [
55
{
6-
"minServerVersion": "8.0"
6+
"minServerVersion": "8.0",
7+
"serverless": "forbid"
78
}
89
],
910
"createEntities": [

test/crud/unified/client-bulkWrite-updateOne-sort.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"schemaVersion": "1.4",
44
"runOnRequirements": [
55
{
6-
"minServerVersion": "8.0"
6+
"minServerVersion": "8.0",
7+
"serverless": "forbid"
78
}
89
],
910
"createEntities": [

test/crud/unified/distinct-hint.json

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
"description": "distinct-hint",
3+
"schemaVersion": "1.0",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "7.1.0"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client0",
13+
"observeEvents": [
14+
"commandStartedEvent"
15+
]
16+
}
17+
},
18+
{
19+
"database": {
20+
"id": "database0",
21+
"client": "client0",
22+
"databaseName": "distinct-hint-tests"
23+
}
24+
},
25+
{
26+
"collection": {
27+
"id": "collection0",
28+
"database": "database0",
29+
"collectionName": "coll0"
30+
}
31+
}
32+
],
33+
"initialData": [
34+
{
35+
"collectionName": "coll0",
36+
"databaseName": "distinct-hint-tests",
37+
"documents": [
38+
{
39+
"_id": 1,
40+
"x": 11
41+
},
42+
{
43+
"_id": 2,
44+
"x": 22
45+
},
46+
{
47+
"_id": 3,
48+
"x": 33
49+
}
50+
]
51+
}
52+
],
53+
"tests": [
54+
{
55+
"description": "distinct with hint string",
56+
"operations": [
57+
{
58+
"name": "distinct",
59+
"object": "collection0",
60+
"arguments": {
61+
"fieldName": "x",
62+
"filter": {
63+
"_id": 1
64+
},
65+
"hint": "_id_"
66+
},
67+
"expectResult": [
68+
11
69+
]
70+
}
71+
],
72+
"expectEvents": [
73+
{
74+
"client": "client0",
75+
"events": [
76+
{
77+
"commandStartedEvent": {
78+
"command": {
79+
"distinct": "coll0",
80+
"key": "x",
81+
"query": {
82+
"_id": 1
83+
},
84+
"hint": "_id_"
85+
},
86+
"commandName": "distinct",
87+
"databaseName": "distinct-hint-tests"
88+
}
89+
}
90+
]
91+
}
92+
]
93+
},
94+
{
95+
"description": "distinct with hint document",
96+
"operations": [
97+
{
98+
"name": "distinct",
99+
"object": "collection0",
100+
"arguments": {
101+
"fieldName": "x",
102+
"filter": {
103+
"_id": 1
104+
},
105+
"hint": {
106+
"_id": 1
107+
}
108+
},
109+
"expectResult": [
110+
11
111+
]
112+
}
113+
],
114+
"expectEvents": [
115+
{
116+
"client": "client0",
117+
"events": [
118+
{
119+
"commandStartedEvent": {
120+
"command": {
121+
"distinct": "coll0",
122+
"key": "x",
123+
"query": {
124+
"_id": 1
125+
},
126+
"hint": {
127+
"_id": 1
128+
}
129+
},
130+
"commandName": "distinct",
131+
"databaseName": "distinct-hint-tests"
132+
}
133+
}
134+
]
135+
}
136+
]
137+
}
138+
]
139+
}

test/crud/unified/estimatedDocumentCount.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
"name": "estimatedDocumentCount",
250250
"object": "collection0",
251251
"expectError": {
252-
"isError": true
252+
"isClientError": true
253253
}
254254
}
255255
],

test/crud/unified/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)