
    [j8                    8   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZmZ ddlmZmZmZ ddlmZmZmZmZmZ dd	lm Z  dd
l!m"Z"m#Z#m$Z$ erddl%m&Z& ddl'm(Z( ddl)m*Z*  G d dee$                   Z+ G d de+ee$                   Z,dS )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)_CURSOR_CLOSED_ERRORS_ConnectionManager)ConnectionFailureInvalidOperationOperationFailure)_CursorAddress_GetMore_OpMsg_OpReply_RawBatchGetMore)PinnedResponse)_Address_DocumentOut_DocumentType)ClientSession)
Collection)
Connectionc                  .   e Zd ZdZeZ	 	 	 	 	 dCdDdZdEdZdFdGdZdGdZ	dEdZ
dHdZdIdZedJd!            ZdKd$ZdLd'Z	 	 dMdNd0ZdOd1ZedId2            ZedOd3            ZedPd4            ZedQd5            ZdRd7ZdSd9ZeZdTd<ZdUd=ZdVd>ZdWdBZdS )XCommandCursorz)A cursor / iterator over command cursors.r   NF
collectionCollection[_DocumentType]cursor_infoMapping[str, Any]addressOptional[_Address]
batch_sizeintmax_await_time_msOptional[int]sessionOptional[ClientSession]explicit_sessionboolcommentr   returnNonec	                   d| _         || _        |d         | _        t          |d                   | _        |                    d          | _        || _        || _        || _	        || _
        || _        | j        dk    | _        || _        | j        r|                     d           d|v r|d         | _        n|j        | _        |                     |           t%          |t&                    s|t)          d          dS dS )	zCreate a new command cursor.Nid
firstBatchpostBatchResumeTokenr   Tnsz,max_await_time_ms must be an integer or None)_CommandCursor__sock_mgr_CommandCursor__collection_CommandCursor__idr   _CommandCursor__dataget$_CommandCursor__postbatchresumetoken_CommandCursor__address_CommandCursor__batch_size!_CommandCursor__max_await_time_ms_CommandCursor__session _CommandCursor__explicit_session_CommandCursor__killed_CommandCursor__comment_CommandCursor__end_session_CommandCursor__ns	full_namer)   
isinstancer*   	TypeError)	selfr#   r%   r'   r)   r+   r-   r/   r1   s	            Z/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/pymongo/command_cursor.py__init__zCommandCursor.__init__1   s     $7A%	K566CN??"D
 D
# !&#4  "2	Q = 	%t$$$;#D)DII",DI
###+S11 	L6G6SJKKK	L 	L6S6S    c                .    |                                   d S N_CommandCursor__dierK   s    rL   __del__zCommandCursor.__del__X       

rN   synchronousc                ,   | j         }d| _         | j        r-|s+| j        }| j        J t          | j        | j                  }nd}d}| j        j        j                            |||| j	        | j
        | j                   | j        sd| _
        d| _	        dS )zCloses this cursor.TNr   )rD   r;   r?   r   rG   r:   databaseclient_cleanup_cursorr9   rB   rC   )rK   rV   already_killed	cursor_idr'   s        rL   __diezCommandCursor.__die[   s    9 	^ 		I>---$T^TY??GG IG")99ON#	
 	
 	
 & 	"!DNrN   c                n    | j         r+| j        s&| j                             |           d | _         d S d S d S )N)lock)rB   rC   _end_session)rK   rV   s     rL   __end_sessionzCommandCursor.__end_sessions   sN    > 	"$"9 	"N''['999!DNNN	" 	" 	" 	"rN   c                0    |                      d           dS )z$Explicitly close / kill this cursor.TNrQ   rS   s    rL   closezCommandCursor.closex   s    

4rN   CommandCursor[_DocumentType]c                    t          |t                    st          d          |dk     rt          d          |dk    rdp|| _        | S )a  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``.

        :Parameters:
          - `batch_size`: The size of each batch of results requested.
        zbatch_size must be an integerr   zbatch_size must be >= 0      )rI   r*   rJ   
ValueErrorr@   )rK   r)   s     rL   r)   zCommandCursor.batch_size|   sX      *c** 	=;<<<>>6777&!O1?ZrN   c                2    t          | j                  dk    S )z^Returns `True` if the cursor has documents remaining from the
        previous batch.
        r   )lenr<   rS   s    rL   	_has_nextzCommandCursor._has_next   s     4;!##rN   Optional[Mapping[str, Any]]c                    | j         S )zlRetrieve the postBatchResumeToken from the response to a
        changeStream aggregate or getMore.
        )r>   rS   s    rL   _post_batch_resume_tokenz&CommandCursor._post_batch_resume_token   s    
 **rN   connr    c                
   | j         j        j        }|                    | j                  sd S | j        sN|                                 t          |d          }| j        dk    r|	                                 d S || _        d S d S )NFr   )
r:   rX   rY   _should_pin_cursorrB   r9   
pin_cursorr   r;   rc   )rK   ro   rY   conn_mgrs       rL   _maybe_pin_connectionz#CommandCursor._maybe_pin_connection   s    "+2((88 	F 	+OO)$66H yA~~     "*	+ 	+rN   	operationr   c                   | j         j        j        }	 |                    || j        | j                  }n# t          $ rL}|j        t          v rd| _	        |j
        r|                     d           n|                                   d}~wt          $ r d| _	        |                                   t          $ r |                                   w xY wt          |t                     r&| j        st%          |j        |j                  | _        |j        rC|j        d         d         }|d         }|                    d          | _        |d	         | _        n4|j        }t          |j        t6                    sJ |j        j        | _        | j        dk    r|                                  t;          |          | _        dS )
z/Send a getmore message and handle the response.)r'   TFNr   cursor	nextBatchr7   r5   )r:   rX   rY   _run_operation_unpack_responser?   r   coder   rD   timeoutrR   rc   r   	ExceptionrI   r   r9   r   ro   more_to_comefrom_commanddocsr=   r>   r;   datar   r\   r   r<   )rK   ru   rY   responseexcrw   	documentss          rL   __send_messagezCommandCursor.__send_message   s   "+2	,,40$. -  HH   		 		 		x000 ${ 

5!!!! 

  	 	 	 DMJJLLL 	 	 	JJLLL	 h// 	[? ["4X]HDY"Z"Z  	0]1%h/F{+I*0**5K*L*LD'tDII IhmX66666 /DI9>>JJLLLI&&s   "6 
C ABACr   Union[_OpReply, _OpMsg]r\   codec_optionsCodecOptions[Mapping[str, Any]]user_fieldslegacy_responseSequence[_DocumentOut]c                2    |                     ||||          S rP   )unpack_response)rK   r   r\   r   r   r   s         rL   rz   zCommandCursor._unpack_response   s     ''	=+___rN   c                   t          | j                  s| j        rt          | j                  S | j        r| j                            dd          \  }}| j                            | j                  }| 	                    | 
                    ||| j        | j        | j        j        || j        | j        j        j        | j        | j        d| j                             n|                     d           t          | j                  S )a  Refreshes 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.
        .rf   FT)rj   r<   rD   r;   rG   splitr:   _read_preference_forr-   _CommandCursor__send_message_getmore_classr@   r   rB   rX   rY   rA   r9   rE   rR   )rK   dbnamecollname	read_prefs       rL   _refreshzCommandCursor._refresh   s     t{ 	$t} 	$t{###9 	#ysA66FH)>>t|LLI##%I%3N%.5,ON    " JJt4;rN   c                T    t          t          | j                  p| j                   S )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.
        )r0   rj   r<   rD   rS   s    rL   alivezCommandCursor.alive  s&     C$$;T]):<<<rN   c                    | j         S )zReturns the id of the cursor.)r;   rS   s    rL   r\   zCommandCursor.cursor_id  s     yrN   c                    | j         S )zUThe (host, port) of the server used, or None.

        .. versionadded:: 3.0
        )r?   rS   s    rL   r'   zCommandCursor.address  s     ~rN   c                "    | j         r| j        S dS )zmThe cursor's :class:`~pymongo.client_session.ClientSession`, or None.

        .. versionadded:: 3.6
        N)rC   rB   rS   s    rL   r-   zCommandCursor.session%  s     " 	">!trN   Iterator[_DocumentType]c                    | S rP    rS   s    rL   __iter__zCommandCursor.__iter__/      rN   r   c                ^    | j         r |                     d          }||S | j          t          )zAdvance the cursor.T)r   	_try_nextStopIteration)rK   docs     rL   nextzCommandCursor.next2  s=     j 	..&&C
 j 	
 rN   get_more_allowedOptional[_DocumentType]c                    t          | j                  s| j        s|r|                                  t          | j                  r| j                                        S dS )z<Advance the cursor blocking for at most one getMore command.N)rj   r<   rD   r   popleft)rK   r   s     rL   r   zCommandCursor._try_next>  s]    4; 	 	:J 	MMOOOt{ 	;&&(((4rN   c                .    |                      d          S )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.

        :Returns:
          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_nextzCommandCursor.try_nextG  s    " ~~t~444rN   c                    | S rP   r   rS   s    rL   	__enter__zCommandCursor.__enter__Z  r   rN   exc_typeexc_valexc_tbc                .    |                                   d S rP   )rc   )rK   r   r   r   s       rL   __exit__zCommandCursor.__exit__]  rU   rN   r   NNFNr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   )r2   r3   )F)rV   r0   r2   r3   )r)   r*   r2   rd   )r2   r0   )r2   rl   )ro   r    r2   r3   )ru   r   r2   r3   NF)r   r   r\   r,   r   r   r   rl   r   r0   r2   r   )r2   r*   )r2   r(   )r2   r.   )r2   r   )r2   r   )r   r0   r2   r   )r2   r   )r2   rd   )r   r   r   r   r   r   r2   r3   )__name__
__module____qualname____doc__r   r   rM   rT   rR   rF   rc   r)   rk   propertyrn   rt   r   rz   r   r   r\   r'   r-   r   r   __next__r   r   r   r   r   rN   rL   r"   r"   ,   sO       33N +/+/!&%L %L %L %L %LN       0" " " "
      0$ $ $ $ + + + X++ + + +*' *' *' *'b 48 %` ` ` ` `           D = = = X=     X    X    X       H   5 5 5 5&        rN   r"   c                  F     e Zd ZeZ	 	 	 	 	 d$d% fdZ	 	 d&d'd Zd(d#Z xZS ))RawBatchCommandCursorr   NFr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   c	           
         |                     d          rJ t                                          ||||||||           dS )aL  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>`_.
        r6   N)r=   superrM   )
rK   r#   r%   r'   r)   r+   r-   r/   r1   	__class__s
            rL   rM   zRawBatchCommandCursor.__init__d  s[    & ??<00000		
 		
 		
 		
 		
rN   r   r   r\   r   r   r   rl   r   list[Mapping[str, Any]]c                b    |                     ||          }|st          |d                    |S )N)r   r   )raw_responser   )rK   r   r\   r   r   r   r   s          rL   rz   z&RawBatchCommandCursor._unpack_response  s?      ,,YK,PP 	D 3<?CCCrN   indexr
   c                     t          d          )Nz)Cannot call __getitem__ on RawBatchCursor)r   )rK   r   s     rL   __getitem__z!RawBatchCommandCursor.__getitem__  s    JKKKrN   r   r   r   )r   r   r\   r,   r   r   r   rl   r   r0   r2   r   )r   r*   r2   r
   )	r   r   r   r   r   rM   rz   r   __classcell__)r   s   @rL   r   r   a  s        %N +/+/!&
 
 
 
 
 
 
H 48 %    L L L L L L L LrN   r   N)-r   
__future__r   collectionsr   typingr   r   r   r   r	   r
   r   r   r   bsonr   r   pymongo.cursorr   r   pymongo.errorsr   r   r   pymongo.messager   r   r   r   r   pymongo.responser   pymongo.typingsr   r   r   pymongo.client_sessionr   pymongo.collectionr   pymongo.poolr    r"   r   r   rN   rL   <module>r      s   ; : " " " " " "      
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 F E E E E E E E D D D D D D D D P P P P P P P P P P X X X X X X X X X X X X X X + + + + + + A A A A A A A A A A (444444------''''''r r r r rGM* r r rj	2L 2L 2L 2L 2LM7=+A 2L 2L 2L 2L 2LrN   