
    i2                    .   S r SSKJr  SSKJr  SSKJrJrJrJ	r	J
r
JrJrJr  SSKJrJr  SSKJr  SSKJrJrJr  SSKJrJrJrJr  SS	KJr  SS
KJrJ r   SSK!J"r"J#r#J$r$  \(       a  SSK%J&r&  SSK'J(r(  SSK)J*r*  Sr+ " S S\ \$   5      r, " S S\,\$   5      r-g)z4CommandCursor class to iterate over command results.    )annotations)deque)TYPE_CHECKINGAnyIteratorMappingNoReturnOptionalSequenceUnion)CodecOptions&_convert_raw_document_lists_to_streams)_CURSOR_CLOSED_ERRORS)ConnectionFailureInvalidOperationOperationFailure)_GetMore_OpMsg_OpReply_RawBatchGetMore)PinnedResponse)_ConnectionManager_CursorBase)_Address_DocumentOut_DocumentType)ClientSession)
Collection)
ConnectionTc                     \ rS rSrSr\r    S               SS jjrSS jrSS jr	SS jr
\SS j5       rSS	 jr  S           SS
 jjrSS jrS S jrS!S jrS"S jrS#S jrS$S jrS$S jrS%S jrS&S'S jjrS(S jrSrg))CommandCursor.   z)A cursor / iterator over command cursors.Nc                   SU l         Xl        US   U l        [        US   5      U l        UR                  S5      U l        X0l        X@l        XPl	        U R                  R                  R                  R                  R                  U l        X`l        U R                  b  SU R                  l        U R                  S:H  U l        Xpl        U R"                  (       a  U R'                  5         SU;   a  US   U l        OUR*                  U l        U R-                  U5        [/        U[0        5      (       d  Ub  [3        S[5        U5       35      egg)	zCreate a new command cursor.Nid
firstBatchpostBatchResumeTokenTr   nsz2max_await_time_ms must be an integer or None, not )	_sock_mgr_collection_idr   _dataget_postbatchresumetoken_address_batch_size_max_await_time_msdatabaseclientoptionstimeout_timeout_session_attached_to_cursor_killed_comment_end_session_ns	full_name
batch_size
isinstanceint	TypeErrortype)self
collectioncursor_infoaddressr=   max_await_time_mssessioncomments           Y/home/ubuntu/wiki/venv/lib/python3.13/site-packages/pymongo/synchronous/command_cursor.py__init__CommandCursor.__init__3   s%    #6@t$;|45
BM//"C
"  %"3((1188@@HH==$04DMM-xx1}<<;"4(DH!++DH
#+S116G6SDTJ[E\D]^  7T1    c                    U R                   $ N)r;   rB   s    rI   _get_namespaceCommandCursor._get_namespace]   s    xxrL   c                    [        U[        5      (       d  [        S[        U5       35      eUS:  a  [	        S5      eUS:H  =(       a    S=(       d    UU l        U $ )aa  Limits the number of documents returned in one batch. Each batch
requires a round trip to the server. It can be adjusted to optimize
performance and limit data transfer.

.. note:: batch_size can not override MongoDB's internal limits on the
   amount of data it will return to the client in a single batch (i.e
   if you set batch size to 1,000,000,000, MongoDB will currently only
   return 4-16MB of results per batch).

Raises :exc:`TypeError` if `batch_size` is not an integer.
Raises :exc:`ValueError` if `batch_size` is less than ``0``.

:param batch_size: The size of each batch of results requested.
z#batch_size must be an integer, not r   zbatch_size must be >= 0      )r>   r?   r@   rA   
ValueErrorr/   )rB   r=   s     rI   r=   CommandCursor.batch_size`   sX     *c**A$zBRASTUU>677%?0q>JrL   c                2    [        U R                  5      S:  $ )zNReturns `True` if the cursor has documents remaining from the
previous batch.
r   )lenr+   rO   s    rI   	_has_nextCommandCursor._has_nextw   s     4::""rL   c                    U R                   $ )z\Retrieve the postBatchResumeToken from the response to a
changeStream aggregate or getMore.
)r-   rO   s    rI   _post_batch_resume_token&CommandCursor._post_batch_resume_token}   s    
 )))rL   c                0   U R                   R                  R                  nUR                  U R                  5      (       d  g U R
                  (       dD  UR                  5         [        US5      nU R                  S:X  a  UR                  5         g X0l        g g )NFr   )
r)   r1   r2   _should_pin_cursorr6   r(   
pin_cursorr   r*   close)rB   connr2   conn_mgrs       rI   _maybe_pin_connection#CommandCursor._maybe_pin_connection   so    !!**11((77~~OO)$6H xx1} !) rL   c                &    UR                  X#XE5      $ rN   )unpack_response)rB   response	cursor_idcodec_optionsuser_fieldslegacy_responses         rI   _unpack_responseCommandCursor._unpack_response   s     ''	+__rL   c                    U R                   (       aO  U R                   R                  (       a3  SU R                   l        U R                   R                  5         S U l         g g g NF)r6   	_implicitr7   _end_implicit_sessionrO   s    rI   r:   CommandCursor._end_session   s?    ==T]]4405DMM-MM//1 DM 5=rL   c                   U R                   R                  R                  n UR                  XR                  U R
                  S9n[        U[         5      (       a6  U R"                  (       d%  [%        UR&                  UR(                  5      U l        UR*                  (       a8  UR,                  S   S   nUS   nUR/                  S5      U l        US   U l        OHUR,                  n[        UR4                  [6        5      (       d   eUR4                  R8                  U l        U R2                  S:X  a  U R                  5         [;        U5      U l        g! [         aS  nUR                  [        ;   a  SU l	        UR                  (       a  U R                  5         e U R                  5         e SnAf[         a    SU l	        U R                  5         e [         a    U R                  5         e f = f)	z/Send a getmore message and handle the response.)rE   TNr   cursor	nextBatchr&   r$   )r)   r1   r2   _run_operationrm   r.   r   coder   r8   r4   _die_no_lockra   r   	Exceptionr>   r   r(   r   rb   more_to_comefrom_commanddocsr,   r-   r*   datar   ri   r   r+   )rB   	operationr2   rh   excru   	documentss          rI   _send_messageCommandCursor._send_message   s   !!**11	,,00$-- - H. h//>>!3HMM8CXCX!Y  ]]1%h/F{+I)/4J)KD&d|DH IhmmX6666}}..DH88q=JJL9%
G   		xx00#{{!!#  

  	DLJJL 	JJL	s   $E 
G+AF++A G+c                   [        U R                  5      (       d  U R                  (       a  [        U R                  5      $ U R                  (       a  U R                  R                  SS5      u  pU R                  R                  U R                  5      nU R                  U R                  UUU R                  U R                  U R                  R                  UU R                  U R                  R                  R                  U R                   U R"                  SU R$                  5      5        OU R'                  5         [        U R                  5      $ )zRefreshes the cursor with more data from the server.

Returns the length of self._data after refresh. Will exit early if
self._data is already non-empty. Raises OperationFailure when the
cursor cannot be refreshed due to an error on the query.
.rS   F)rX   r+   r8   r*   r;   splitr)   _read_preference_forrG   r   _getmore_classr/   rj   r6   r1   r2   r0   r(   r9   	_die_lock)rB   dbnamecollname	read_prefs       rI   _refreshCommandCursor._refresh   s     tzz??dlltzz?"88#xx~~c15F((==dllKI##$$HH$$22MM$$--44++NNMM" NN4::rL   c                    U $ rN    rO   s    rI   __iter__CommandCursor.__iter__       rL   c                    U $ rN   r   rO   s    rI   	__enter__CommandCursor.__enter__   r   rL   c                    U R                   (       a)  U R                  S5      nUb  U$ U R                   (       a  M)  [        e)zAdvance the cursor.T)alive	_try_nextStopIteration)rB   docs     rI   nextCommandCursor.next   s6     jj..&C
 jjj
 rL   c                "    U R                  5       $ rN   )r   rO   s    rI   __next__CommandCursor.__next__   s    yy{rL   c                    [        U R                  5      (       d(  U R                  (       d  U(       a  U R                  5         [        U R                  5      (       a  U R                  R	                  5       $ g)z<Advance the cursor blocking for at most one getMore command.N)rX   r+   r8   r   popleft)rB   get_more_alloweds     rI   r   CommandCursor._try_next  sD    4::t||8HMMOtzz??::%%''rL   c                   [        U R                  5      (       d!  U R                  (       d  U R                  5         [        U R                  5      (       a  Uc6  UR	                  U R                  5        U R                  R                  5         g[        [        [        U R                  5      U5      5       H,  nUR                  U R                  R                  5       5        M.     gg)z4Get all or some available documents from the cursor.TF)
rX   r+   r8   r   extendclearrangeminappendr   )rB   resulttotal_s       rI   _next_batchCommandCursor._next_batch  s    4::t||MMOtzz??}djj)

  "  s3tzz?E:;AMM$**"4"4"67 <rL   c                     U R                  SS9$ )a"  Advance the cursor without blocking indefinitely.

This method returns the next document without waiting
indefinitely for data.

If no document is cached locally then this method runs a single
getMore command. If the getMore yields any documents, the next
document is returned, otherwise, if the getMore returns no documents
(because there is no additional data) then ``None`` is returned.

:return: The next document or ``None`` when no document is available
  after running a single getMore or when the cursor is closed.

.. versionadded:: 4.5
T)r   )r   rO   s    rI   try_nextCommandCursor.try_next  s      ~~t~44rL   )r.   r/   r)   r9   r+   r*   r8   r0   r;   r-   r6   r(   r5   r   NNNrC   zCollection[_DocumentType]rD   zMapping[str, Any]rE   zOptional[_Address]r=   r?   rF   Optional[int]rG   zOptional[ClientSession]rH   r   returnNone)r   str)r=   r?   r   CommandCursor[_DocumentType])r   bool)r   Optional[Mapping[str, Any]])rb   r   r   r   rp   )rh   Union[_OpReply, _OpMsg]ri   r   rj   zCodecOptions[Mapping[str, Any]]rk   r   rl   r   r   zSequence[_DocumentOut])r   r   )r   r   r   r   )r   r?   )r   zIterator[_DocumentType])r   r   )r   r   )r   r   r   Optional[_DocumentType]rN   )r   listr   r   r   r   )r   r   )__name__
__module____qualname____firstlineno____doc__r   r   rJ   rP   r=   rY   propertyr\   rd   rm   r:   r   r   r   r   r   r   r   r   r   __static_attributes__r   rL   rI   r!   r!   .   s   3N +/+/(-( '( $	(
 ( )( )( ( 
(T.# * **& 48 %`)` !` 7	`
 1` ` 
 `!*&X D5rL   r!   c                     ^  \ rS rSr\r    S               SU 4S jjjr  S           S	S jjrS
S jrSr	U =r
$ )RawBatchCommandCursori.  c           	     `   > UR                  S5      (       a   e[        TU ]	  UUUUUUU5        g)a$  Create a new cursor / iterator over raw batches of BSON data.

Should not be called directly by application developers -
see :meth:`~pymongo.collection.Collection.aggregate_raw_batches`
instead.

.. seealso:: The MongoDB documentation on `cursors <https://dochub.mongodb.org/core/cursors>`_.
r%   N)r,   superrJ   )	rB   rC   rD   rE   r=   rF   rG   rH   	__class__s	           rI   rJ   RawBatchCommandCursor.__init__1  s<    $ ??<0000	
rL   c                N    UR                  X$S9nU(       d  [        US   5        U$ )N)rk   r   )raw_responser   )rB   rh   ri   rj   rk   rl   r   s          rI   rm   &RawBatchCommandCursor._unpack_responseN  s.      ,,Y,P 3<?CrL   c                    [        S5      e)Nz0Cannot call __getitem__ on RawBatchCommandCursor)r   )rB   indexs     rI   __getitem__!RawBatchCommandCursor.__getitem__]  s    QRRrL   r   r   r   rp   )rh   r   ri   r   rj   zCodecOptions[dict[str, Any]]rk   r   rl   r   r   zlist[Mapping[str, Any]])r   r?   r   r	   )r   r   r   r   r   r   rJ   rm   r   r   __classcell__)r   s   @rI   r   r   .  s    %N +/+/
-
 '
 $	

 
 )
 )
 
 

 
D 48 %) ! 4	
 1  
!S SrL   r   N).r   
__future__r   collectionsr   typingr   r   r   r   r	   r
   r   r   bsonr   r   pymongo.cursor_sharedr   pymongo.errorsr   r   r   pymongo.messager   r   r   r   pymongo.responser   pymongo.synchronous.cursor_baser   r   pymongo.typingsr   r   r   "pymongo.synchronous.client_sessionr   pymongo.synchronous.collectionr   pymongo.synchronous.poolr   _IS_SYNCr!   r   r   rL   rI   <module>r      sx    ; " 	 	 	 F 7 P P H H + K A A@93}5K. }5@0SM-8 0SrL   