2121from typing import (
2222 TYPE_CHECKING ,
2323 Any ,
24- Callable ,
2524 Optional ,
26- Union ,
2725)
2826
29- from pymongo .asynchronous .command_runner import run_cursor_command
30- from pymongo .asynchronous .helpers import _handle_reauth
3127from pymongo .logger import (
3228 _SDAM_LOGGER ,
3329 _debug_log ,
3430 _SDAMStatusMessage ,
3531)
36- from pymongo .message import _GetMore , _OpMsg , _Query
37- from pymongo .response import PinnedResponse , Response
3832
3933if TYPE_CHECKING :
4034 from queue import Queue
4135 from weakref import ReferenceType
4236
4337 from bson .objectid import ObjectId
44- from pymongo .asynchronous .mongo_client import AsyncMongoClient , _MongoClientErrorHandler
38+ from pymongo .asynchronous .mongo_client import _MongoClientErrorHandler
4539 from pymongo .asynchronous .monitor import Monitor
4640 from pymongo .asynchronous .pool import AsyncConnection , Pool
4741 from pymongo .monitoring import _EventListeners
48- from pymongo .read_preferences import _ServerMode
4942 from pymongo .server_description import ServerDescription
50- from pymongo .typings import _DocumentOut
5143
5244_IS_SYNC = False
5345
54- _CURSOR_DOC_FIELDS = {"cursor" : {"firstBatch" : 1 , "nextBatch" : 1 }}
55-
5646
5747class Server :
5848 def __init__ (
@@ -117,112 +107,6 @@ def request_check(self) -> None:
117107 """Check the server's state soon."""
118108 self ._monitor .request_check ()
119109
120- async def operation_to_command (
121- self , operation : Union [_Query , _GetMore ], conn : AsyncConnection , apply_timeout : bool = False
122- ) -> tuple [dict [str , Any ], str ]:
123- cmd , db = operation .as_command (conn , apply_timeout )
124- # Support auto encryption
125- if operation .client ._encrypter and not operation .client ._encrypter ._bypass_auto_encryption :
126- cmd = await operation .client ._encrypter .encrypt ( # type: ignore[misc, assignment]
127- operation .db , cmd , operation .codec_options
128- )
129- operation .update_command (cmd )
130-
131- return cmd , db
132-
133- @_handle_reauth
134- async def run_operation (
135- self ,
136- conn : AsyncConnection ,
137- operation : Union [_Query , _GetMore ],
138- read_preference : _ServerMode ,
139- listeners : Optional [_EventListeners ],
140- unpack_res : Callable [..., list [_DocumentOut ]],
141- client : AsyncMongoClient [Any ],
142- ) -> Response :
143- """Run a _Query or _GetMore operation and return a Response object.
144-
145- This method is used only to run _Query/_GetMore operations from
146- cursors.
147- Can raise ConnectionFailure, OperationFailure, etc.
148-
149- :param conn: An AsyncConnection instance.
150- :param operation: A _Query or _GetMore object.
151- :param read_preference: The read preference to use.
152- :param listeners: Instance of _EventListeners or None.
153- :param unpack_res: A callable that decodes the wire protocol response.
154- :param client: An AsyncMongoClient instance.
155- """
156- assert listeners is not None
157-
158- use_cmd = operation .use_command (conn )
159- more_to_come = bool (operation .conn_mgr and operation .conn_mgr .more_to_come )
160- cmd , dbn = await self .operation_to_command (operation , conn , use_cmd )
161- if more_to_come :
162- request_id = 0
163- data = b""
164- max_doc_size = 0
165- else :
166- message = operation .get_message (read_preference , conn , use_cmd )
167- request_id , data , max_doc_size = self ._split_message (message )
168-
169- user_fields = _CURSOR_DOC_FIELDS if use_cmd else None
170-
171- docs , reply , duration = await run_cursor_command (
172- conn ,
173- cmd ,
174- dbn ,
175- request_id ,
176- data ,
177- client = client ,
178- session = operation .session , # type: ignore[arg-type]
179- listeners = listeners ,
180- codec_options = operation .codec_options ,
181- user_fields = user_fields ,
182- command_name = operation .name ,
183- pool_opts = conn .opts ,
184- max_doc_size = max_doc_size ,
185- more_to_come = more_to_come ,
186- unpack_res = unpack_res ,
187- cursor_id = operation .cursor_id ,
188- )
189- assert reply is not None
190-
191- response : Response
192-
193- if client ._should_pin_cursor (operation .session ) or operation .exhaust : # type: ignore[arg-type]
194- conn .pin_cursor ()
195- if isinstance (reply , _OpMsg ):
196- # In OP_MSG, the server keeps sending only if the
197- # more_to_come flag is set.
198- more_to_come = reply .more_to_come
199- else :
200- # In OP_REPLY, the server keeps sending until cursor_id is 0.
201- more_to_come = bool (operation .exhaust and reply .cursor_id )
202- if operation .conn_mgr :
203- operation .conn_mgr .update_exhaust (more_to_come )
204- response = PinnedResponse (
205- data = reply ,
206- address = self ._description .address ,
207- conn = conn ,
208- duration = duration ,
209- request_id = request_id ,
210- from_command = use_cmd ,
211- docs = docs , # type: ignore[arg-type]
212- more_to_come = more_to_come ,
213- )
214- else :
215- response = Response (
216- data = reply ,
217- address = self ._description .address ,
218- duration = duration ,
219- request_id = request_id ,
220- from_command = use_cmd ,
221- docs = docs , # type: ignore[arg-type]
222- )
223-
224- return response
225-
226110 async def checkout (
227111 self , handler : Optional [_MongoClientErrorHandler ] = None
228112 ) -> AbstractAsyncContextManager [AsyncConnection ]:
@@ -241,19 +125,5 @@ def description(self, server_description: ServerDescription) -> None:
241125 def pool (self ) -> Pool :
242126 return self ._pool
243127
244- def _split_message (
245- self , message : Union [tuple [int , Any ], tuple [int , Any , int ]]
246- ) -> tuple [int , Any , int ]:
247- """Return request_id, data, max_doc_size.
248-
249- :param message: (request_id, data, max_doc_size) or (request_id, data)
250- """
251- if len (message ) == 3 :
252- return message # type: ignore[return-value]
253- else :
254- # get_more and kill_cursors messages don't include BSON documents.
255- request_id , data = message # type: ignore[misc]
256- return request_id , data , 0
257-
258128 def __repr__ (self ) -> str :
259129 return f"<{ self .__class__ .__name__ } { self ._description !r} >"
0 commit comments