Skip to content

Commit 13568b1

Browse files
committed
Move re-order logic for client bulkWrite into _client_batched_op_msg_impl
1 parent 0e07e18 commit 13568b1

File tree

5 files changed

+15
-25
lines changed

5 files changed

+15
-25
lines changed

pymongo/_client_bulk_shared.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"""Constants, types, and classes shared across Client Bulk Write API implementations."""
1717
from __future__ import annotations
1818

19-
from typing import TYPE_CHECKING, Any, ChainMap, Mapping, MutableMapping, NoReturn
19+
from collections import ChainMap
20+
from typing import TYPE_CHECKING, Any, Mapping, MutableMapping, NoReturn
2021

2122
from pymongo.errors import ClientBulkWriteException, OperationFailure
2223
from pymongo.helpers_shared import _get_wce_doc

pymongo/asynchronous/client_bulk.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import copy
2222
import datetime
2323
import logging
24-
from collections import ChainMap
2524
from collections.abc import MutableMapping
2625
from itertools import islice
2726
from typing import (
@@ -133,16 +132,8 @@ def add_insert(self, namespace: str, document: _DocumentOut) -> None:
133132
"""Add an insert document to the list of ops."""
134133
validate_is_document_type("document", document)
135134
# Generate ObjectId client side.
136-
if not isinstance(document, RawBSONDocument):
137-
# Since the data document itself is nested within the insert document
138-
# it won't be automatically re-ordered by the BSON conversion.
139-
# We use ChainMap here to make the _id field the first field instead.
140-
if "_id" in document:
141-
document = ChainMap(document, {"_id": document["_id"]})
142-
else:
143-
id = ObjectId()
144-
document["_id"] = id
145-
document = ChainMap(document, {"_id": id})
135+
if not (isinstance(document, RawBSONDocument) or "_id" in document):
136+
document["_id"] = ObjectId()
146137
cmd = {"insert": -1, "document": document}
147138
self.ops.append(("insert", cmd))
148139
self.namespaces.append(namespace)

pymongo/message.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import datetime
2525
import random
2626
import struct
27+
from collections import ChainMap
2728
from io import BytesIO as _BytesIO
2829
from typing import (
2930
TYPE_CHECKING,
@@ -1111,6 +1112,12 @@ def _check_doc_size_limits(
11111112
# key and the index of its namespace within ns_info as its value.
11121113
op_doc[op_type] = ns_info[namespace] # type: ignore[index]
11131114

1115+
# Since the data document itself is nested within the insert document
1116+
# it won't be automatically re-ordered by the BSON conversion.
1117+
# We use ChainMap here to make the _id field the first field instead.
1118+
if real_op_type == "insert":
1119+
op_doc["document"] = ChainMap(op_doc["document"], {"_id": op_doc["document"]["_id"]}) # type: ignore[index]
1120+
11141121
# Encode current operation doc and, if newly added, namespace doc.
11151122
op_doc_encoded = _dict_to_bson(op_doc, False, opts)
11161123
op_length = len(op_doc_encoded)

pymongo/monitoring.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ def connection_checked_in(self, event):
189189
from __future__ import annotations
190190

191191
import datetime
192-
from collections import abc, namedtuple
193-
from typing import TYPE_CHECKING, Any, ChainMap, Mapping, Optional, Sequence
192+
from collections import ChainMap, abc, namedtuple
193+
from typing import TYPE_CHECKING, Any, Mapping, Optional, Sequence
194194

195195
from bson.objectid import ObjectId
196196
from pymongo.hello import Hello, HelloCompat

pymongo/synchronous/client_bulk.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import copy
2222
import datetime
2323
import logging
24-
from collections import ChainMap
2524
from collections.abc import MutableMapping
2625
from itertools import islice
2726
from typing import (
@@ -133,16 +132,8 @@ def add_insert(self, namespace: str, document: _DocumentOut) -> None:
133132
"""Add an insert document to the list of ops."""
134133
validate_is_document_type("document", document)
135134
# Generate ObjectId client side.
136-
if not isinstance(document, RawBSONDocument):
137-
# Since the data document itself is nested within the insert document
138-
# it won't be automatically re-ordered by the BSON conversion.
139-
# We use ChainMap here to make the _id field the first field instead.
140-
if "_id" in document:
141-
document = ChainMap(document, {"_id": document["_id"]})
142-
else:
143-
id = ObjectId()
144-
document["_id"] = id
145-
document = ChainMap(document, {"_id": id})
135+
if not (isinstance(document, RawBSONDocument) or "_id" in document):
136+
document["_id"] = ObjectId()
146137
cmd = {"insert": -1, "document": document}
147138
self.ops.append(("insert", cmd))
148139
self.namespaces.append(namespace)

0 commit comments

Comments
 (0)