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_idenabled_for_server_publish	_listener_events)selfr+   r,   r-   r.   r0   r2    r?   W/var/www/html/olx_land/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   optsload_balancedr8   openr>   r?   r?   r@   rD   R   s   
zServer.open
service_idc                   s   | j |I dH  dS )zClear the connection pool.N)r,   reset)r>   rF   r?   r?   r@   rG   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'   )message
topologyId
serverHost
serverPort)r;   r<   r=   putpublish_server_closedr6   addressr9   r   isEnabledForloggingDEBUGr   r   STOP_SERVERr8   closer7   rE   r?   r?   r@   rS   ^   s&   

zServer.closec                 C  s   | j   dS )zCheck the server's state soon.N)r8   request_checkrE   r?   r?   r@   rT   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)
as_commandclient
_encrypter_bypass_auto_encryptionencryptdbcodec_optionsupdate_command)r>   rU   rW   rX   cmdra   r?   r?   r@   operation_to_command|   s   


zServer.operation_to_commandread_preferencer#   
unpack_res!Callable[..., list[_DocumentOut]]r]   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'   )rH   clientIdcommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionIdrJ   rK   	serviceIdz$db)rF   FT)legacy_responseuser_fields)rH   ri   
durationMSfailurerk   rl   rm   rn   ro   rp   rJ   rK   rq   isServerSideError)rF   database_nameexplain)idns)r&   okfindr&   r(   r)   )rH   ri   rt   replyrk   rl   rm   rn   ro   rp   rJ   rK   rq   )datarN   rW   duration
request_idfrom_commanddocsmore_to_come)r~   rN   r   r   r   r   )<enabled_for_commandsr   nowuse_commandconn_mgrr   re   get_message_split_messager   rO   rP   rQ   r   r   STARTED_topology_settingsr9   nextiterry   server_connection_idrN   rF   publish_command_startreceive_messagesend_message_CURSOR_DOC_FIELDS	cursor_idrb   r]   _process_responsesessionr   max_wire_version	Exception
isinstancer   r   detailsr   FAILEDpublish_command_failurename	namespace	SUCCEEDEDpublish_command_successr^   decryptraw_command_responser
   _should_pin_cursorexhaust
pin_cursorr   rY   update_exhaustr   r6   r   )r>   rW   rU   rf   r0   rg   r]   publishstartuse_cmdr   rd   dbnr   rH   r~   max_doc_sizer}   rs   rr   r   firstexcr   ru   res	decryptedresponser?   r?   r@   run_operation   sR  

	

%

	zServer.run_operationhandler"Optional[_MongoClientErrorHandler]$AsyncContextManager[AsyncConnection]c                   s   | j |S r[   )r,   checkout)r>   r   r?   r?   r@   r   ^  s   zServer.checkoutc                 C     | j S r[   )r6   rE   r?   r?   r@   descriptionc     zServer.descriptionc                 C  s   |j | jj ks	J || _d S r[   )rN   r6   )r>   r+   r?   r?   r@   r   g  s   
c                 C  r   r[   )r7   rE   r?   r?   r@   r,   l  r   zServer.poolrH   ,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>   rH   r   r~   r?   r?   r@   r   p  s   
zServer._split_messagestrc                 C  s   d| j j d| jdS )N< >)	__class____name__r6   rE   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   r[   )rF   r/   r4   r5   )F)rU   rV   rW   r    rX   rY   r4   rZ   )rW   r    rU   rV   rf   r#   r0   r1   rg   rh   r]   r   r4   r   )r   r   r4   r   )r4   r$   )r+   r$   r4   r5   )r4   r!   )rH   r   r4   r   )r4   r   )r   
__module____qualname__rA   rD   rG   rS   rT   re   r   r   r   propertyr   setterr,   r   r   r?   r?   r?   r@   r*   =   s0    


 V
r*   )<__doc__
__future__r   rP   r   typingr   r   r   r   r   r	   bsonr
   pymongo.asynchronous.helpersr   pymongo.errorsr   r   pymongo.helpers_sharedr   pymongo.loggerr   r   r   r   r   pymongo.messager   r   r   r   pymongo.responser   r   queuer   weakrefr   bson.objectidr   !pymongo.asynchronous.mongo_clientr   r   pymongo.asynchronous.monitorr   pymongo.asynchronous.poolr    r!   pymongo.monitoringr"   pymongo.read_preferencesr#   pymongo.server_descriptionr$   pymongo.typingsr%   _IS_SYNCr   r*   r?   r?   r?   r@   <module>   s4    	