
    &g+D                    >   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Jr  SSKJrJr  SSKJr  SSK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 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      r0g)z4CommandCursor class to iterate over command results.    )annotations)deque)	TYPE_CHECKINGAnyAsyncIteratorGenericMappingNoReturnOptionalSequenceUnion)CodecOptions&_convert_raw_document_lists_to_streams)_csot)_ConnectionManager)_CURSOR_CLOSED_ERRORS)ConnectionFailureInvalidOperationOperationFailure)_CursorAddress_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5       r\S)S j5       r\S*S j5       r\S+S j5       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 jrS/S jrS0S jrS1S2S jjrS3S jrS4S jr S5S jr!\"RF                  S1S6S jj5       r$Sr%g)7AsyncCommandCursor6   z7An asynchronous cursor / iterator over command cursors.Nc	                n   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        Xpl        U R                  S:H  U l        Xl        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postBatchResumeTokenr   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_explicit_session_killed_comment_end_session_ns	full_name
batch_size
isinstanceint	TypeErrortype)	self
collectioncursor_infoaddressr?   max_await_time_mssessionexplicit_sessioncomments	            hC:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\pymongo/asynchronous/command_cursor.py__init__AsyncCommandCursor.__init__;   s    #;Et$;|45
BM//"C
"  %"3((1188@@HH!1xx1}<<;"4(DH!++DH
#+S116G6SDTJ[E\D]^  7T1    c                $    U R                  5         g N)_die_no_lockrD   s    rL   __del__AsyncCommandCursor.__del__e   s    rO   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@   rA   rB   rC   
ValueErrorr1   )rD   r?   s     rL   r?   AsyncCommandCursor.batch_sizeh   sX     *c**A$zBRASTUU>677%?0q>JrO   c                2    [        U R                  5      S:  $ )zNReturns `True` if the cursor has documents remaining from the
previous batch.
r   )lenr-   rS   s    rL   	_has_nextAsyncCommandCursor._has_next   s     4::""rO   c                    U R                   $ )z\Retrieve the postBatchResumeToken from the response to a
changeStream aggregate or getMore.
)r/   rS   s    rL   _post_batch_resume_token+AsyncCommandCursor._post_batch_resume_token   s    
 )))rO   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+   r3   r4   _should_pin_cursorr8   r*   
pin_cursorr   r,   close)rD   connr4   conn_mgrs       rL   _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      $ rQ   )unpack_response)rD   response	cursor_idcodec_optionsuser_fieldslegacy_responses         rL   _unpack_response#AsyncCommandCursor._unpack_response   s     ''	+__rO   c                n    [        [        U R                  5      =(       d    U R                  (       + 5      $ )a  Does this cursor have the potential to return more data?

Even if :attr:`alive` is ``True``, :meth:`next` can raise
:exc:`StopIteration`. Best to use a for loop::

    async for doc in collection.aggregate(pipeline):
        print(doc)

.. note:: :attr:`alive` can be True while iterating a cursor from
  a failed server. In this case :attr:`alive` will return False after
  :meth:`next` fails to retrieve the next batch of results from the
  server.
)boolr\   r-   r:   rS   s    rL   aliveAsyncCommandCursor.alive   s#     C

O9DLL(8::rO   c                    U R                   $ )zReturns the id of the cursor.)r,   rS   s    rL   rm   AsyncCommandCursor.cursor_id   s     xxrO   c                    U R                   $ )zEThe (host, port) of the server used, or None.

.. versionadded:: 3.0
)r0   rS   s    rL   rG   AsyncCommandCursor.address   s     }}rO   c                >    U R                   (       a  U R                  $ g)zoThe cursor's :class:`~pymongo.asynchronous.client_session.AsyncClientSession`, or None.

.. versionadded:: 3.6
N)r9   r8   rS   s    rL   rI   AsyncCommandCursor.session   s     !!== rO   c                    U R                   nSU l         U R                  (       aE  U(       d>  U R                  nU R                  c   e[        U R                  U R                  5      nX#4$ SnS nX#4$ )NTr   )r:   r,   r0   r   r=   )rD   already_killedrm   rG   s       rL   _prepare_to_die"AsyncCommandCursor._prepare_to_die   sh    88NI==,,,$T]]DHH=G
 !! IG!!rO   c                   U R                  5       u  pU R                  R                  R                  R	                  XU R
                  U R                  U R                  5        U R                  (       d  SU l        SU l        g)z,Closes this cursor without acquiring a lock.N)r   r+   r3   r4   _cleanup_cursor_no_lockr*   r8   r9   rD   rm   rG   s      rL   rR   AsyncCommandCursor._die_no_lock   sc    !113	!!((@@t?U?U	
 %% DMrO   c                $  #    U R                  5       u  pU R                  R                  R                  R	                  UUU R
                  U R                  U R                  5      I Sh  vN   U R                  (       d  SU l        SU l        g N$7f)zCloses this cursor.N)r   r+   r3   r4   _cleanup_cursor_lockr*   r8   r9   r   s      rL   	_die_lockAsyncCommandCursor._die_lock   s{     !113	''..CCNNMM""
 	
 	
 %% DM	
s   A'B)B*%Bc                    U R                   (       a4  U R                  (       d"  U R                   R                  5         S U l         g g g rQ   )r8   r9   _end_implicit_sessionrS   s    rL   r<   AsyncCommandCursor._end_session   s/    ==!7!7MM//1 DM "8=rO   c                @   #    U R                  5       I Sh  vN   g N7f)z$Explicitly close / kill this cursor.N)r   rS   s    rL   re   AsyncCommandCursor.close   s     nn   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.)rG   NTr   cursor	nextBatchr(   r&   )r+   r3   r4   _run_operationrq   r0   r   coder   r:   r6   rR   re   r   	Exceptionr@   r   r*   r   rf   more_to_comefrom_commanddocsr.   r/   r,   datar   rm   r   r-   )rD   	operationr4   rl   excr   	documentss          rL   _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.
.rW   FN)r\   r-   r:   r,   r=   splitr+   _read_preference_forrI   r   _getmore_classr1   rn   r8   r3   r4   r2   r*   r;   r   )rD   dbnamecollname	read_prefs       rL   _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 $ rQ    rS   s    rL   	__aiter__AsyncCommandCursor.__aiter__H  s    rO   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)ru   	_try_nextStopAsyncIteration)rD   docs     rL   nextAsyncCommandCursor.nextK  sA      jjt,,C
 jjj
 ! 	 -s   &AAAAc                >   #    U R                  5       I S h  vN $  N7frQ   )r   rS   s    rL   	__anext__AsyncCommandCursor.__anext__U  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)r\   r-   r:   r   popleft)rD   get_more_alloweds     rL   r   AsyncCommandCursor._try_nextX  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)
r\   r-   r:   r   extendclearrangeminappendr   )rD   resulttotal_s       rL   _next_batchAsyncCommandCursor._next_batcha  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   rS   s    rL   try_nextAsyncCommandCursor.try_nextp  s       ^^T^::::s   c                   #    U $ 7frQ   r   rS   s    rL   
__aenter__AsyncCommandCursor.__aenter__  s
     s   c                @   #    U R                  5       I S h  vN   g  N7frQ   )re   )rD   exc_typeexc_valexc_tbs       rL   	__aexit__AsyncCommandCursor.__aexit__  s     jjlr   c                $  #    / nUn[        U[        5      (       a  US:  a  [        S5      eU R                  (       aN  U R	                  X#5      I Sh  vN (       d   U$ Ub  U[        U5      -
  nUS:X  a   U$ U R                  (       a  MN  U$  N;7f)aE  Converts the contents of this cursor to a list more efficiently than ``[doc async for doc in cursor]``.

To use::

  >>> await cursor.to_list()

Or, so read at most n items from the cursor::

  >>> await cursor.to_list(n)

If the cursor is empty or has no more results, an empty list will be returned.

.. versionadded:: 4.9
rW   z'to_list() length must be greater than 0Nr   )r@   rA   rY   ru   r   r\   )rD   lengthres	remainings       rL   to_listAsyncCommandCursor.to_list  s       $&	fc""vzFGGjj))#999
 
	 !"SX-	>
 jjj 
 :s   ABB7BB)r0   r1   r+   r;   r-   r9   r,   r:   r2   r=   r/   r8   r*   r7   r   NNFNrE   zAsyncCollection[_DocumentType]rF   zMapping[str, Any]rG   Optional[_Address]r?   rA   rH   Optional[int]rI   Optional[AsyncClientSession]rJ   rt   rK   r   returnNone)r   r   )r?   rA   r   !AsyncCommandCursor[_DocumentType])r   rt   )r   Optional[Mapping[str, Any]])rf   r!   r   r   NF)rl   Union[_OpReply, _OpMsg]rm   r   rn   zCodecOptions[Mapping[str, Any]]ro   r   rp   rt   r   zSequence[_DocumentOut])r   rA   )r   r   )r   r   )r   z$tuple[int, Optional[_CursorAddress]])r   r   r   r   )r   zAsyncIterator[_DocumentType])r   r   )r   rt   r   Optional[_DocumentType]rQ   )r   listr   r   r   rt   )r   r   )r   r   )r   r   r   r   r   r   r   r   )r   r   r   zlist[_DocumentType])&__name__
__module____qualname____firstlineno____doc__r   r   rM   rT   r?   r]   propertyr`   rh   rq   ru   rm   rG   rI   r   rR   r   r<   re   r   r   r   r   r   r   r   r   r   r   r   applyr   __static_attributes__r   rO   rL   r#   r#   6   s   AN +/04!&(2( '( $	(
 ( )( .( ( ( 
(T.# * **& 48 %`)` !` 7	`
 1` ` 
 ` ; ;       "!
*&X D!!;$ [[ rO   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	           
     b   > UR                  S5      (       a   e[        T	U ]	  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.   superrM   )
rD   rE   rF   rG   r?   rH   rI   rJ   rK   	__class__s
            rL   rM   #AsyncRawBatchCommandCursor.__init__  s?    & ??<0000		
rO   c                N    UR                  X$S9nU(       d  [        US   5        U$ )N)ro   r   )raw_responser   )rD   rl   rm   rn   ro   rp   r   s          rL   rq   +AsyncRawBatchCommandCursor._unpack_response  s.      ,,Y,P 3<?CrO   c                    [        S5      e)Nz5Cannot call __getitem__ on AsyncRawBatchCommandCursor)r   )rD   indexs     rL   __getitem__&AsyncRawBatchCommandCursor.__getitem__  s    VWWrO   r   r   r   r   )rl   r   rm   r   rn   r   ro   r   rp   rt   r   zlist[Mapping[str, Any]])r   rA   r   r
   )r   r   r   r   r   r   rM   rq   r   r   __classcell__)r   s   @rL   r   r     s    %N +/04!&
2
 '
 $	

 
 )
 .
 
 
 

 
H 48 %) ! $	
 1  
!X XrO   r   N)1r   
__future__r   collectionsr   typingr   r   r   r   r	   r
   r   r   r   bsonr   r   pymongor   pymongo.asynchronous.cursorr   pymongo.cursor_sharedr   pymongo.errorsr   r   r   pymongo.messager   r   r   r   r   pymongo.responser   pymongo.typingsr   r   r   #pymongo.asynchronous.client_sessionr   pymongo.asynchronous.collectionr    pymongo.asynchronous.poolr!   _IS_SYNCr#   r   r   rO   rL   <module>r     s    ; " 
 
 
 F  : 7 P P  , A AF?9m/ m`2X!3M!B 2XrO   