
    i3                    .   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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#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AsyncIteratorMappingNoReturnOptionalSequenceUnion)CodecOptions&_convert_raw_document_lists_to_streams)_AsyncCursorBase_ConnectionManager)_CURSOR_CLOSED_ERRORS)ConnectionFailureInvalidOperationOperationFailure)_GetMore_OpMsg_OpReply_RawBatchGetMore)PinnedResponse)_Address_DocumentOut_DocumentType)AsyncClientSession)AsyncCollection)AsyncConnectionFc                     \ 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))AsyncCommandCursor.   z7An asynchronous 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           Z/home/ubuntu/wiki/venv/lib/python3.13/site-packages/pymongo/asynchronous/command_cursor.py__init__AsyncCommandCursor.__init__3   s%    #;E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!AsyncCommandCursor._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=   AsyncCommandCursor.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AsyncCommandCursor._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+AsyncCommandCursor._post_batch_resume_token}   s    
 )))rL   c                L  #    U R                   R                  R                  nUR                  U R                  5      (       d  g U R
                  (       dL  UR                  5         [        US5      nU R                  S:X  a  UR                  5       I S h  vN   g X0l        g g  N7f)NFr   )
r)   r1   r2   _should_pin_cursorr6   r(   
pin_cursorr   r*   close)rB   connr2   conn_mgrs       rI   _maybe_pin_connection(AsyncCommandCursor._maybe_pin_connection   s}     !!**11((77~~OO)$6H xx1}nn&&&!)  's   BB$B"B$c                &    UR                  X#XE5      $ rN   )unpack_response)rB   response	cursor_idcodec_optionsuser_fieldslegacy_responses         rI   _unpack_response#AsyncCommandCursor._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:   AsyncCommandCursor._end_session   s?    ==T]]4405DMM-MM//1 DM 5=rL   c                D  #    U R                   R                  R                  n UR                  XR                  U R
                  S9I Sh  vN 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       I Sh  vN   [;        U5      U l        g GN! [         a\  nUR                  [        ;   a  SU l	        UR                  (       a  U R                  5         e U R                  5       I Sh  vN    e SnAf[         a"    SU l	        U R                  5       I Sh  vN    e [         a    U R                  5       I Sh  vN    e f = f N7f)	z/Send a getmore message and handle the response.)rE   NTr   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 AsyncCommandCursor._send_message   s    !!**11	#2200$-- 3  H. h//>>!3HMM8CXCX!Y  ]]1%h/F{+I)/4J)KD&d|DH IhmmX6666}}..DH88q=**,9%
M   		xx00#{{!!#  jjl""  	DL**, 	**,	$ sl   !H 'E( E%E( C?H HH %E( (
H2AG	GG		'H0G31"HHHH 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      I Sh  vN   OU R'                  5       I Sh  vN   [        U R                  5      $  N2 N7f)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   FN)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AsyncCommandCursor._refresh   s     tzz??dlltzz?"88#xx~~c15F((==dllKI$$##$$HH$$22MM$$--44++NNMM  " .."""4::'" #s$   D1E*3E&4E*E(E*(E*c                    U $ rN    rO   s    rI   	__aiter__AsyncCommandCursor.__aiter__   s    rL   c                   #    U $ 7frN   r   rO   s    rI   
__aenter__AsyncCommandCursor.__aenter__   s
     s   c                   #    U R                   (       a1  U R                  S5      I Sh  vN nUb  U$ U R                   (       a  M1  [        e N"7f)zAdvance the cursor.TN)alive	_try_nextStopAsyncIteration)rB   docs     rI   nextAsyncCommandCursor.next   sA      jjt,,C
 jjj
 ! 	 -s   &AAAAc                >   #    U R                  5       I S h  vN $  N7frN   )r   rO   s    rI   	__anext__AsyncCommandCursor.__anext__   s     YY[   s   c                  #    [        U R                  5      (       d0  U R                  (       d  U(       a  U R                  5       I Sh  vN   [        U R                  5      (       a  U R                  R	                  5       $ g N97f)z<Advance the cursor blocking for at most one getMore command.N)rX   r+   r8   r   popleft)rB   get_more_alloweds     rI   r   AsyncCommandCursor._try_next  sR     4::t||8H--/!!tzz??::%%''	 "s   ABB	:Bc                  #    [        U R                  5      (       d)  U R                  (       d  U R                  5       I Sh  vN   [        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 N7f)z4Get all or some available documents from the cursor.NTF)
rX   r+   r8   r   extendclearrangeminappendr   )rB   resulttotal_s       rI   _next_batchAsyncCommandCursor._next_batch  s     4::t||--/!!tzz??}djj)

  "  s3tzz?E:;AMM$**"4"4"67 < "s   ?C5C3B2C5c                <   #    U R                  SS9I Sh  vN $  N7f)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   N)r   rO   s    rI   try_nextAsyncCommandCursor.try_next  s       ^^T^::::s   )r.   r/   r)   r9   r+   r*   r8   r0   r;   r-   r6   r(   r5   r   NNNrC   zAsyncCollection[_DocumentType]rD   zMapping[str, Any]rE   zOptional[_Address]r=   r?   rF   Optional[int]rG   zOptional[AsyncClientSession]rH   r   returnNone)r   str)r=   r?   r   !AsyncCommandCursor[_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AsyncIterator[_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   AN +/04(2( '( $	(
 ( )( .( ( 
(T.# * **& 48 %`)` !` 7	`
 1` ` 
 `!*&X D!!;rL   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
$ )AsyncRawBatchCommandCursori.  c           	     `   > UR                  S5      (       a   e[        TU ]	  UUUUUUU5        g)a6  Create a new cursor / iterator over raw batches of BSON data.

Should not be called directly by application developers -
see :meth:`~pymongo.asynchronous.collection.AsyncCollection.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   #AsyncRawBatchCommandCursor.__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   +AsyncRawBatchCommandCursor._unpack_responseN  s.      ,,Y,P 3<?CrL   c                    [        S5      e)Nz5Cannot call __getitem__ on AsyncRawBatchCommandCursor)r   )rB   indexs     rI   __getitem__&AsyncRawBatchCommandCursor.__getitem__]  s    VWWrL   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 +/04
2
 '
 $	

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