
    &gB                    >   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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#  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GenericIteratorMappingNoReturnOptionalSequenceUnion)CodecOptions&_convert_raw_document_lists_to_streams)_csot)_CURSOR_CLOSED_ERRORS)ConnectionFailureInvalidOperationOperationFailure)_CursorAddress_GetMore_OpMsg_OpReply_RawBatchGetMore)PinnedResponse)_ConnectionManager)_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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CommandCursor6   z)A 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	            gC:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\pymongo/synchronous/command_cursor.py__init__CommandCursor.__init__;   s    #6@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__CommandCursor.__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?   CommandCursor.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CommandCursor._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&CommandCursor._post_batch_resume_token   s    
 )))rO   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+   r3   r4   _should_pin_cursorr8   r*   
pin_cursorr   r,   close)rD   connr4   conn_mgrs       rL   _maybe_pin_connection#CommandCursor._maybe_pin_connection   so    !!**11((77~~OO)$6H xx1} !) rO   c                &    UR                  X#XE5      $ rQ   )unpack_response)rD   response	cursor_idcodec_optionsuser_fieldslegacy_responses         rL   _unpack_responseCommandCursor._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::

    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CommandCursor.alive   s#     C

O9DLL(8::rO   c                    U R                   $ )zReturns the id of the cursor.)r,   rS   s    rL   rm   CommandCursor.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   CommandCursor.address   s     }}rO   c                >    U R                   (       a  U R                  $ g)z]The cursor's :class:`~pymongo.client_session.ClientSession`, or None.

.. versionadded:: 3.6
N)r9   r8   rS   s    rL   rI   CommandCursor.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CommandCursor._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   CommandCursor._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        U R                  (       d  SU l        SU l        g)zCloses this cursor.N)r   r+   r3   r4   _cleanup_cursor_lockr*   r8   r9   r   s      rL   	_die_lockCommandCursor._die_lock   si    !113	!!((==NNMM""	
 %% DMrO   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<   CommandCursor._end_session   s/    ==!7!7MM//1 DM "8=rO   c                $    U R                  5         g)z$Explicitly close / kill this cursor.N)r   rS   s    rL   re   CommandCursor.close   s    rO   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.)rG   TNr   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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.
.rW   F)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CommandCursor._refresh&  s     tzz??dlltzz?"88#xx~~c15F((==dllKI##$$HH$$22MM$$--44++NNMM" NN4::rO   c                    U $ rQ    rS   s    rL   __iter__CommandCursor.__iter__H      rO   c                    U R                   (       a)  U R                  S5      nUb  U$ U R                   (       a  M)  [        e)zAdvance the cursor.T)ru   	_try_nextStopIteration)rD   docs     rL   nextCommandCursor.nextK  s6     jj..&C
 jjj
 rO   c                "    U R                  5       $ rQ   )r   rS   s    rL   __next__CommandCursor.__next__U  s    yy{rO   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)r\   r-   r:   r   popleft)rD   get_more_alloweds     rL   r   CommandCursor._try_nextX  sD    4::t||8HMMOtzz??::%%''rO   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)
r\   r-   r:   r   extendclearrangeminappendr   )rD   resulttotal_s       rL   _next_batchCommandCursor._next_batcha  s    4::t||MMOtzz??}djj)

  "  s3tzz?E:;AMM$**"4"4"67 <rO   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   rS   s    rL   try_nextCommandCursor.try_nextp  s      ~~t~44rO   c                    U $ rQ   r   rS   s    rL   	__enter__CommandCursor.__enter__  r   rO   c                $    U R                  5         g rQ   )re   )rD   exc_typeexc_valexc_tbs       rL   __exit__CommandCursor.__exit__  s    

rO   c                   / nUn[        U[        5      (       a  US:  a  [        S5      eU R                  (       aF  U R	                  X#5      (       d   U$ Ub  U[        U5      -
  nUS:X  a   U$ U R                  (       a  MF  U$ )a3  Converts the contents of this cursor to a list more efficiently than ``[doc for doc in cursor]``.

To use::

  >>> cursor.to_list()

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

  >>> 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 0r   )r@   rA   rY   ru   r   r\   )rD   lengthres	remainings       rL   to_listCommandCursor.to_list  s      $&	fc""vzFGGjj##C33
 
	 !"SX-	>
 jjj 
rO   )r0   r1   r+   r;   r-   r9   r,   r:   r2   r=   r/   r8   r*   r7   r   NNFNrE   zCollection[_DocumentType]rF   zMapping[str, Any]rG   Optional[_Address]r?   rA   rH   Optional[int]rI   Optional[ClientSession]rJ   rt   rK   r   returnNone)r   r   )r?   rA   r   CommandCursor[_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Iterator[_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   3N +/+/!&(-( '( $	(
 ( )( )( ( ( 
(T.# * **& 48 %`)` !` 7	`
 1` ` 
 ` ; ;       "!
*&X D5$ [[ 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
$ )RawBatchCommandCursori  c	           
     b   > UR                  S5      (       a   e[        T	U ]	  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.   superrM   )
rD   rE   rF   rG   r?   rH   rI   rJ   rK   	__class__s
            rL   rM   RawBatchCommandCursor.__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   &RawBatchCommandCursor._unpack_response  s.      ,,Y,P 3<?CrO   c                    [        S5      e)Nz0Cannot call __getitem__ on RawBatchCommandCursor)r   )rD   indexs     rL   __getitem__!RawBatchCommandCursor.__getitem__  s    QRRrO   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 +/+/!&
-
 '
 $	

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