
    =h(                    6   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
 ddl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mZ dd	lmZmZ er>dd
lmZ ddlmZ ddlm 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+ ddl,m-Z- ddl.m/Z/ ddddiZ0 G d d      Z1y)z2Communicate with one MongoDB server in a topology.    )annotations)datetime)TYPE_CHECKINGAnyCallableContextManagerOptionalUnion)_decode_all_selective)NotPrimaryErrorOperationFailure)_check_command_response_handle_reauth)_convert_exception_GetMore_OpMsg_Query)PinnedResponseResponse)Queue)ReferenceType)ObjectId)_MongoClientErrorHandler)Monitor)_EventListeners)
ConnectionPool)_ServerMode)ServerDescription)_DocumentOutcursor   )
firstBatch	nextBatchc                      e Zd Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZdddZddZddZe	 	 	 	 	 	 	 	 	 	 	 	 dd       Z		 d	 	 	 ddZ
edd	       Zej                  dd
       Zedd       Z	 	 	 	 ddZddZy)ServerNc                    || _         || _        || _        || _        |duxr |j                  | _        || _        d| _        | j
                  r |       | _        yy)zRepresent one MongoDB server.N)_description_pool_monitor_topology_idenabled_for_server_publish	_listener_events)selfserver_descriptionpoolmonitortopology_id	listenerseventss          ^/var/www/html/phonemate/phone_mate_backend/venv/lib/python3.12/site-packages/pymongo/server.py__init__zServer.__init__,   s\     /
'!-N)2N2N"==!8DL     c                z    | j                   j                  j                  s| j                  j	                          yy)z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r)   optsload_balancedr*   openr0   s    r7   r=   zServer.open@   s*    
 zz,,MM  -r9   c                :    | j                   j                  |       y)zClear the connection pool.N)r2   reset)r0   
service_ids     r7   r@   zServer.resetH   s    		
#r9   c                ^   | j                   rm| j                  J | j                  J | j                  j                  | j                  j                  | j
                  j                  | j                  ff       | j                  j                          | j                  j                          y)zXClear the connection pool and stop the monitor.

        Reconnect with open().
        N)r-   r.   r/   putpublish_server_closedr(   addressr+   r*   closer)   reset_without_pauser>   s    r7   rF   zServer.closeL   s    
 ==>>---<<+++LLNN88&&..0A0AB 	

&&(r9   c                8    | j                   j                          y)zCheck the server's state soon.N)r*   request_checkr>   s    r7   rI   zServer.request_check]   s    ##%r9   c           
     P   d}|J |j                   }|rt        j                         }|j                  |      }	|j                  xr |j                  j
                  }
|
rd}n(|j                  |||	      }| j                  |      \  }}}|r_|j                  |      \  }}d|vr||d<   |J |j                  ||||j                  |j                         t        j                         }	 |
r|j                  d      }n#|j                         |j                  |      }|	r	t        }d}nd}d} |||j                  |j                   ||      }|	rA|d   }|j"                  j%                  ||j&                         t)        ||j*                         |rt        j                         z
  }|	r|d   }nY|j:                  d	k(  r
|r|d   ni }n@|j                  |j=                         d
dd}|j:                  dk(  r	||d   d<   n||d   d<   |J |j?                  |||j:                  ||j                  |j                         |j"                  }|rN|j@                  rB|	r@|j@                  jC                  |jE                               }tG        ||j                   |      }|jI                  |j&                        s|jJ                  r|jM                          t/        |tN              r|j
                  }
n#tQ        |jJ                  xr |j                        }
|j                  r|j                  jS                  |
       tU        || jV                  j                  ||||	||
      }|S tY        || jV                  j                  |||	|      }|S # t,        $ r}|rt        j                         z
  }t/        |t0        t2        f      r|j4                  }nt7        |      }|J |j9                  |||j:                  ||j                  |j                          d}~ww xY w)a  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.

        :Parameters:
          - `conn`: A Connection instance.
          - `operation`: A _Query or _GetMore object.
          - `read_preference`: The read preference to use.
          - `listeners`: Instance of _EventListeners or None.
          - `unpack_res`: A callable that decodes the wire protocol response.
        Nr   z$db)rA   FT)legacy_responseuser_fields)rA   database_nameexplain)idnsr"   )r!   okfindr!   r#   r$   )datarE   connduration
request_idfrom_commanddocsmore_to_come)rS   rE   rU   rV   rW   rX   )-enabled_for_commandsr   nowuse_commandconn_mgrrY   get_message_split_message
as_commandpublish_command_startrE   rA   receive_messagesend_message_CURSOR_DOC_FIELDS	cursor_idcodec_optionsclient_process_responsesessionr   max_wire_version	Exception
isinstancer   r   detailsr   publish_command_failurename	namespacepublish_command_success
_encrypterdecryptraw_command_responser   _should_pin_cursorexhaust
pin_cursorr   boolupdate_exhaustr   r(   r   )r0   rT   	operationread_preferencer5   
unpack_resrU   publishstartuse_cmdrY   rV   messagerS   max_doc_sizecmddbnreplyrL   rK   rX   firstexcfailureresrg   	decryptedresponses                               r7   run_operationzServer.run_operationa   s   , $$$00LLNE''- ))Mi.@.@.M.MJ++OT7KG-1-@-@-I*Jl ++D1HCC E
(((++S*dllt ,  LLNE*	,,T2!!$5,,Z8 0"'""&##'' /'D Q  225):K:KL't/D/DE( ||~-H $(G9,!%d1g2(-y?R?R?T!U]^_>>V+26CM,/15CM+.(((--??! .  !!f''"--55e6P6P6RS	,Y	8O8OQ\] $$Y%6%679;L;LOO%(  %11  $I$5$5$I%//J!!""11,?%))11!%$)	H(   ))11!%$H i  	#<<>E1cO5E#FG,/KKG05G ,,,11NNLL#"% 2  #	s   -B*N 	P%BP  P%c                8    | j                   j                  |      S N)r2   checkout)r0   handlers     r7   r   zServer.checkout   s     yy!!'**r9   c                    | j                   S r   )r(   r>   s    r7   descriptionzServer.description  s       r9   c                \    |j                   | j                  j                   k(  sJ || _        y r   )rE   r(   )r0   r1   s     r7   r   zServer.description  s*    !))T->->-F-FFFF.r9   c                    | j                   S r   )r)   r>   s    r7   r2   zServer.pool  s    zzr9   c                6    t        |      dk(  r|S |\  }}||dfS )zReturn request_id, data, max_doc_size.

        :Parameters:
          - `message`: (request_id, data, max_doc_size) or (request_id, data)
           r   )len)r0   r   rV   rS   s       r7   r_   zServer._split_message  s.     w<1N  'JtQ&&r9   c                P    d| j                   j                   d| j                  dS )N< >)	__class____name__r(   r>   s    r7   __repr__zServer.__repr__  s)    4>>**+1T->->,ACCr9   )NNN)r1   r   r2   r   r3   r   r4   Optional[ObjectId]r5   Optional[_EventListeners]r6   zOptional[ReferenceType[Queue]]returnNone)r   r   r   )rA   r   r   r   )rT   r   rz   zUnion[_Query, _GetMore]r{   r   r5   r   r|   z!Callable[..., list[_DocumentOut]]r   r   )r   z"Optional[_MongoClientErrorHandler]r   zContextManager[Connection])r   r   )r1   r   r   r   )r   r   )r   z,Union[tuple[int, Any], tuple[int, Any, int]]r   ztuple[int, Any, int])r   str)r   
__module____qualname__r8   r=   r@   rF   rI   r   r   r   propertyr   setterr2   r_   r    r9   r7   r&   r&   +   s9    +//315$-$ $ 	$
 ($ -$ /$ 
$(!$)"& ZZ +Z %	Z
 -Z 6Z 
Z Zz =A+9+	#+
 ! ! / /  'C'	'Dr9   r&   N)2__doc__
__future__r   r   typingr   r   r   r   r	   r
   bsonr   pymongo.errorsr   r   pymongo.helpersr   r   pymongo.messager   r   r   r   pymongo.responser   r   queuer   weakrefr   bson.objectidr   pymongo.mongo_clientr   pymongo.monitorr   pymongo.monitoringr   pymongo.poolr   r   pymongo.read_preferencesr   pymongo.server_descriptionr   pymongo.typingsr    rd   r&   r   r9   r7   <module>r      sg    9 "  P P & < C H H 5%&='2-4<,qq AB uD uDr9   