o
    hq7                     @  sd  d Z ddlmZ ddlZddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZmZmZmZ ddlmZmZmZmZ ddl m!Z!m"Z" erd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- ddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 dZ9ddddiZ:G dd dZ;dS )z2Communicate with one MongoDB server in a topology.    )annotationsN)datetime)TYPE_CHECKINGAnyAsyncContextManagerCallableOptionalUnion)_decode_all_selective)_handle_reauth)NotPrimaryErrorOperationFailure)_check_command_response)_COMMAND_LOGGER_SDAM_LOGGER_CommandStatusMessage
_debug_log_SDAMStatusMessage)_convert_exception_GetMore_OpMsg_Query)PinnedResponseResponse)Queue)ReferenceType)ObjectId)AsyncMongoClient_MongoClientErrorHandler)Monitor)AsyncConnectionPool)_EventListeners)_ServerMode)ServerDescription)_DocumentOutFcursor   )
firstBatch	nextBatchc                   @  s   e Zd Z			d@dAddZdBddZdCdDddZdBddZdBddZ	dEdFd#d$Ze	dGd,d-Z
	dCdHd1d2ZedId3d4ZejdJd5d4ZedKd6d7ZdLd;d<ZdMd>d?ZdS )NServerNserver_descriptionr$   poolr!   monitorr   topology_idOptional[ObjectId]	listenersOptional[_EventListeners]eventsOptional[ReferenceType[Queue]]returnNonec                 C  sJ   || _ || _|| _|| _|duo|j| _|| _d| _| jr#| | _dS dS )zRepresent one MongoDB server.N)_description_pool_monitor_topology_idZenabled_for_server_publish	_listener_events)selfr+   r,   r-   r.   r0   r2    r>   N/var/www/html/venv/lib/python3.10/site-packages/pymongo/asynchronous/server.py__init__>   s   
zServer.__init__c                   s   | j jjs| j  dS dS )z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r7   optsZload_balancedr8   openr=   r>   r>   r?   rB   R   s   
zServer.open
service_idc                   s   | j |I dH  dS )zClear the connection pool.N)r,   reset)r=   rD   r>   r>   r?   rE   Z   s   zServer.resetc                   s   | j r!| jdusJ | jdusJ | j| jj| jj| jff t	t
jr:tttj| j| jjd | jjd d | j I dH  | j I dH  dS )zXClear the connection pool and stop the monitor.

        Reconnect with open().
        Nr   r'   )messageZ
topologyId
serverHost
serverPort)r:   r;   r<   putZpublish_server_closedr6   addressr9   r   isEnabledForloggingDEBUGr   r   ZSTOP_SERVERr8   closer7   rC   r>   r>   r?   rN   ^   s&   

zServer.closec                 C  s   | j   dS )zCheck the server's state soon.N)r8   request_checkrC   r>   r>   r?   rO   x   s   zServer.request_checkF	operationUnion[_Query, _GetMore]connr    apply_timeoutbooltuple[dict[str, Any], str]c                   sR   | ||\}}|jjr |jjjs |jj|j||jI d H }|| ||fS N)Z
as_commandclient
_encrypterZ_bypass_auto_encryptionZencryptdbcodec_optionsZupdate_command)r=   rP   rR   rS   cmdrY   r>   r>   r?   operation_to_command|   s   


zServer.operation_to_commandread_preferencer#   
unpack_res!Callable[..., list[_DocumentOut]]rW   r   r   c                   s>  |dusJ |j }t }||}	|jo|jj}
| |||	I dH \}}|
r+d}n||||	}| |\}}}t	
tjratt	tj|jj|tt|||||j|j|jd |jd |jd |rd|vrk||d< |dusqJ |j||||j|j|jd zK|
r|dI dH }n|||I dH  ||I dH }|	rt}d}nd}d}|||j|j||d	}|	r|d }|j||j I dH  t!||j" W nk t#y5 } z^t | }t$|t%t&fr|j'}nt(|}t	
tjrtt	tj)|jj||tt|||||j|j|jd |jd |jt$|t&d
 |r0|dusJ |j*|||j+||j|j|j|d  d}~ww t | }|	rD|d }n.|j+dkrT|rQ|d ni }n|j|, ddd}|j+dkrl||d d< n||d d< t	
tjrtt	tj-|jj||tt|||||j|j|jd |jd |jd |r|dusJ |j.|||j+||j|j|j|d |j}|r|j/r|	r|j/0|1 I dH }t2||j|}|3|j s|j4r|5  t$|t6r|j}
n	t7|j4o|j}
|jr|j8|
 t9|| j:j||||	||
d}|S t;|| j:j|||	|d}|S )aH  Run a _Query or _GetMore operation and return a Response object.

        This method is used only to run _Query/_GetMore operations from
        cursors.
        Can raise ConnectionFailure, OperationFailure, etc.

        :param conn: An AsyncConnection instance.
        :param operation: A _Query or _GetMore object.
        :param read_preference: The read preference to use.
        :param listeners: Instance of _EventListeners or None.
        :param unpack_res: A callable that decodes the wire protocol response.
        :param client: An AsyncMongoClient instance.
        Nr   r'   )rF   clientIdcommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionIdrG   rH   	serviceIdz$db)rD   FT)legacy_responseuser_fields)rF   r`   
durationMSfailurerb   rc   rd   re   rf   rg   rG   rH   rh   ZisServerSideError)rD   Zdatabase_nameZexplain)idns)r&   okfindr&   r(   r)   )rF   r`   rk   replyrb   rc   rd   re   rf   rg   rG   rH   rh   )datarJ   rR   duration
request_idfrom_commanddocsmore_to_come)rr   rJ   rs   rt   ru   rv   )<Zenabled_for_commandsr   nowZuse_commandZconn_mgrrw   r\   Zget_message_split_messager   rK   rL   rM   r   r   ZSTARTEDZ_topology_settingsr9   nextiterrm   Zserver_connection_idrJ   rD   Zpublish_command_startZreceive_messagesend_message_CURSOR_DOC_FIELDSZ	cursor_idrZ   rW   _process_responsesessionr   Zmax_wire_version	Exception
isinstancer   r   detailsr   ZFAILEDZpublish_command_failurename	namespaceZ	SUCCEEDEDZpublish_command_successrX   ZdecryptZraw_command_responser
   Z_should_pin_cursorZexhaustZ
pin_cursorr   rT   Zupdate_exhaustr   r6   r   )r=   rR   rP   r]   r0   r^   rW   publishstartZuse_cmdrw   r[   Zdbnrt   rF   rr   Zmax_doc_sizerq   rj   ri   rv   firstexcrs   rl   resZ	decryptedresponser>   r>   r?   run_operation   sR  

	

%

	zServer.run_operationhandler"Optional[_MongoClientErrorHandler]$AsyncContextManager[AsyncConnection]c                   s   | j |S rV   )r,   checkout)r=   r   r>   r>   r?   r   ^  s   zServer.checkoutc                 C     | j S rV   )r6   rC   r>   r>   r?   descriptionc     zServer.descriptionc                 C  s   |j | jj ks	J || _d S rV   )rJ   r6   )r=   r+   r>   r>   r?   r   g  s   
c                 C  r   rV   )r7   rC   r>   r>   r?   r,   l  r   zServer.poolrF   ,Union[tuple[int, Any], tuple[int, Any, int]]tuple[int, Any, int]c                 C  s"   t |dkr|S |\}}||dfS )zReturn request_id, data, max_doc_size.

        :param message: (request_id, data, max_doc_size) or (request_id, data)
           r   )len)r=   rF   rt   rr   r>   r>   r?   ry   p  s   
zServer._split_messagestrc                 C  s   d| j j d| jdS )N< >)	__class____name__r6   rC   r>   r>   r?   __repr__~  s   zServer.__repr__)NNN)r+   r$   r,   r!   r-   r   r.   r/   r0   r1   r2   r3   r4   r5   )r4   r5   rV   )rD   r/   r4   r5   )F)rP   rQ   rR   r    rS   rT   r4   rU   )rR   r    rP   rQ   r]   r#   r0   r1   r^   r_   rW   r   r4   r   )r   r   r4   r   )r4   r$   )r+   r$   r4   r5   )r4   r!   )rF   r   r4   r   )r4   r   )r   
__module____qualname__r@   rB   rE   rN   rO   r\   r   r   r   propertyr   setterr,   ry   r   r>   r>   r>   r?   r*   =   s0    


 V
r*   )<__doc__
__future__r   rL   r   typingr   r   r   r   r   r	   Zbsonr
   Zpymongo.asynchronous.helpersr   Zpymongo.errorsr   r   Zpymongo.helpers_sharedr   Zpymongo.loggerr   r   r   r   r   Zpymongo.messager   r   r   r   Zpymongo.responser   r   queuer   weakrefr   Zbson.objectidr   Z!pymongo.asynchronous.mongo_clientr   r   Zpymongo.asynchronous.monitorr   Zpymongo.asynchronous.poolr    r!   Zpymongo.monitoringr"   Zpymongo.read_preferencesr#   Zpymongo.server_descriptionr$   Zpymongo.typingsr%   Z_IS_SYNCr}   r*   r>   r>   r>   r?   <module>   s4    	