o
    nd;/                     @   s   d Z ddlmZ ddlmZ ddl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 ddlmZ G d	d
 d
eZG dd deZdS )z4CommandCursor class to iterate over command results.    )deque)&_convert_raw_document_lists_to_streams)integer_types)_SocketManager_CURSOR_CLOSED_ERRORS)ConnectionFailureInvalidOperationOperationFailure)_CursorAddress_GetMore_RawBatchGetMore)PinnedResponsec                   @   s   e Zd ZdZeZ			d/ddZdd Zd0d	d
Zdd Z	dd Z
dd Zdd Zedd Zdd Zdd Z	d1ddZdd Zedd Zedd  Zed!d" Zed#d$ Zd%d& Zd'd( ZeZd)d* Zd+d, Zd-d. ZdS )2CommandCursorz)A cursor / iterator over command cursors.r   NFc	           	      C   s   d| _ || _|d | _t|d | _|d| _|| _|| _|| _	|| _
|| _| jdk| _| jr5| d d|v r?|d | _n|j| _| | t|tsU|durWtddS dS )	zSCreate a new command cursor.

        The parameter 'retrieved' is unused.
        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__end_session_CommandCursor__ns	full_name
batch_size
isinstancer   	TypeError	self
collectioncursor_infoaddress	retrievedr"   max_await_time_mssessionexplicit_session r.   i/var/www/html/apis.geethamveg.com/public_html/venv/lib/python3.10/site-packages/pymongo/command_cursor.py__init__#   s,   



zCommandCursor.__init__c                 C      |    d S N_CommandCursor__dier&   r.   r.   r/   __del__C      zCommandCursor.__del__c                 C   sj   | j }d| _ | jr|s| j}t| j| j}nd}d}| jjj|||| j	| j
| j | js0d| _
d| _	dS )zCloses this cursor.
        Tr   N)r   r   r
   r   r    r   databaseclient_cleanup_cursorr   r   r   )r&   synchronousalready_killed	cursor_idr)   r.   r.   r/   __dieF   s(   


zCommandCursor.__diec                 C   s,   | j r| js| j j|d d | _ d S d S d S )N)lock)r   r   _end_session)r&   r;   r.   r.   r/   __end_session^   s   
zCommandCursor.__end_sessionc                 C   s   |  d dS )z-Explicitly close / kill this cursor.
        TNr3   r5   r.   r.   r/   closec      zCommandCursor.closec                 C   s8   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      )r#   r   r$   
ValueErrorr   )r&   r"   r.   r.   r/   r"   h   s   
zCommandCursor.batch_sizec                 C   s   t | jdkS )zUReturns `True` if the cursor has documents remaining from the
        previous batch.r   )lenr   r5   r.   r.   r/   	_has_next   rC   zCommandCursor._has_nextc                 C      | j S )zcRetrieve the postBatchResumeToken from the response to a
        changeStream aggregate or getMore.)r   r5   r.   r.   r/   _post_batch_resume_token   s   z&CommandCursor._post_batch_resume_tokenc                 C   sV   | j jj}|| jsd S | js)|  t|d}| jdkr$|	  d S || _d S d S )NFr   )
r   r8   r9   _should_pin_cursorr   r   
pin_cursorr   r   rB   )r&   	sock_infor9   sock_mgrr.   r.   r/   _maybe_pin_connection   s   



z#CommandCursor._maybe_pin_connectionc              
   C   s  | j jj}z|j|| j| jd}W n2 ty* } z|jtv r!d| _	| 
   d}~w ty8   d| _	| 
    tyC   | 
   w t|trT| jsTt|j|j| _|jrn|jd d }|d }|d| _|d | _n|j}|jj| _| jdkr| 
  t|| _dS )	z8Send a getmore message and handle the response.
        )r)   TNr   cursor	nextBatchr   r   )r   r8   r9   _run_operation_unpack_responser   r	   coder   r   rB   r   	Exceptionr#   r   r   r   socket_infomore_to_comefrom_commanddocsr   r   r   datar=   r   r   )r&   	operationr9   responseexcrP   	documentsr.   r.   r/   __send_message   sD   






zCommandCursor.__send_messagec                 C   s   | ||||S r2   )unpack_response)r&   r\   r=   codec_optionsuser_fieldslegacy_responser.   r.   r/   rS      s   
zCommandCursor._unpack_responsec                 C   s   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 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.
        .rD   FT)rG   r   r   r   r    splitr   _read_preference_forr,   _CommandCursor__send_message_getmore_classr   ra   r   r8   r9   r   r   r4   )r&   dbnamecollname	read_prefr.   r.   r/   _refresh   s(   


zCommandCursor._refreshc                 C   s   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.
        )boolrG   r   r   r5   r.   r.   r/   alive   s   zCommandCursor.alivec                 C   rI   )zReturns the id of the cursor.)r   r5   r.   r.   r/   r=      s   zCommandCursor.cursor_idc                 C   rI   )zUThe (host, port) of the server used, or None.

        .. versionadded:: 3.0
        )r   r5   r.   r.   r/   r)      s   zCommandCursor.addressc                 C   s   | j r| jS dS )zmThe cursor's :class:`~pymongo.client_session.ClientSession`, or None.

        .. versionadded:: 3.6
        N)r   r   r5   r.   r.   r/   r,     s   zCommandCursor.sessionc                 C      | S r2   r.   r5   r.   r.   r/   __iter__     zCommandCursor.__iter__c                 C   s&   | j r| d}|dur|S | j st)zAdvance the cursor.TN)rn   	_try_nextStopIteration)r&   docr.   r.   r/   next  s   
zCommandCursor.nextc                 C   sD   t | js| js|r|   t | jr | j}|j| j |S dS )z<Advance the cursor blocking for at most one getMore command.N)rG   r   r   rl   r   r8   _fix_outgoingpopleft)r&   get_more_allowedcollr.   r.   r/   rr     s   
zCommandCursor._try_nextc                 C   ro   r2   r.   r5   r.   r.   r/   	__enter__$  rq   zCommandCursor.__enter__c                 C   r1   r2   )rB   )r&   exc_typeexc_valexc_tbr.   r.   r/   __exit__'  r7   zCommandCursor.__exit__r   r   NNF)FNF)__name__
__module____qualname____doc__r   rh   r0   r6   r4   r   rB   r"   rH   propertyrJ   rO   rg   rS   rl   rn   r=   r)   r,   rp   ru   __next__rr   rz   r~   r.   r.   r.   r/   r      sD    
 

*






r   c                       s<   e Zd ZeZ			d
 fdd	Z	dddZdd	 Z  ZS )RawBatchCommandCursorr   NFc	           	   
      s0   | drJ t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>`_.
        r   N)r   superr   r0   r%   	__class__r.   r/   r0   .  s
   

zRawBatchCommandCursor.__init__c                 C   s"   |j ||d}|st|d  |S )N)rb   r   )raw_responser   )r&   r\   r=   ra   rb   rc   r   r.   r.   r/   rS   >  s   z&RawBatchCommandCursor._unpack_responsec                 C   s   t d)Nz)Cannot call __getitem__ on RawBatchCursor)r   )r&   indexr.   r.   r/   __getitem__H  s   z!RawBatchCommandCursor.__getitem__r   r   )	r   r   r   r   rh   r0   rS   r   __classcell__r.   r.   r   r/   r   +  s    

r   N)r   collectionsr   bsonr   bson.py3compatr   pymongo.cursorr   r   pymongo.errorsr   r   r	   pymongo.messager
   r   r   pymongo.responser   objectr   r   r.   r.   r.   r/   <module>   s     