
    =h5                       d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZmZmZmZmZ ddlmZ ddl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m Z m!Z! dd
l"m#Z#m$Z$m%Z% ddl&m'Z' ddl(m)Z) e	rFddlm*Z* ddl+m,Z, ddlm-Z-m.Z.m/Z/ ddl0m1Z1 ddl&m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z;m<Z<m=Z= ddl>m?Z?  ej                  d      j                  ZB	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZC ej                  d      j                  ZDef	 	 	 	 	 	 	 d dZEdZFd!dZGeHgej                  ZId"dZJy)#z&Internal network layer helper methods.    )annotationsN)TYPE_CHECKINGAnyMappingMutableMappingOptionalSequenceUnioncast)_decode_all_selective)_csothelpersmessagessl_support)MAX_MESSAGE_SIZE)_NO_COMPRESSION
decompress)NotPrimaryErrorOperationFailureProtocolError_OperationCancelled)_UNPACK_REPLY_OpMsg_OpReply)_is_speculative_authenticate)_errno_from_exception)CodecOptions)ClientSession)SnappyContextZlibContextZstdContext)MongoClient)_EventListeners)
Connection)ReadConcern)_ServerMode)_Address_CollationIn_DocumentOut_DocumentType)WriteConcernz<iiiic           
        t        t        |            }|dz   }d}|}|r|s|J t        j                  ||      }|r=|r|j                  s/|j
                  r|j                  |d<   |r|j                  ||        |||d<   |duxr |j                  }|r*t        j                  j                         }t        ||      }|r|j                         t        v rd}|rA|j                  r5|j                  j                  s|j                  j!                  |||      x}}|r| j#                  ||       t%        j&                  ||       |rl|rt(        j*                  nd}||rt(        j,                  ndz  }t        j.                  ||||||      \  }}}} |r?|=| |kD  r8t        j0                  |||       n t        j2                  d|dd|d||      \  }}}|>||t        j4                  z   kD  r(t        j0                  |||t        j4                  z          |rgt        j                  j                         z
  }!|J |
J |j7                  ||||
| j8                  	       t        j                  j                         }	 | j:                  j=                  |       |r	|rd}"d
di}#nnt?        | |      }"|"j@                  | _         |"jC                  ||      }$|$d   }#|r|jE                  |#|       |r#tG        jH                  |#| jJ                  |	|       |rOt        j                  j                         z
  !z   }&|J |
J |j[                  |&|#|||
| j8                  ||       |rQ|j                  rE|"rC|j                  j]                  |"j_                               }(ta        dtc        |(||      d         }#|#S # tL        $ r}%|rt        j                  j                         z
  !z   }&tO        |%tP        tR        f      r|%jT                  }'nt        jV                  |%      }'|J |
J |jY                  |&|'|||
| j8                  |        d}%~%ww xY w)a  Execute a command over the socket, or raise socket.error.

    :Parameters:
      - `conn`: a Connection instance
      - `dbname`: name of the database on which to run the command
      - `spec`: a command document as an ordered dict type, eg SON.
      - `is_mongos`: are we connected to a mongos?
      - `read_preference`: a read preference
      - `codec_options`: a CodecOptions instance
      - `session`: optional ClientSession instance.
      - `client`: optional MongoClient instance for updating $clusterTime.
      - `check`: raise OperationFailure if there are errors
      - `allowable_errors`: errors to ignore if `check` is True
      - `address`: the (host, port) of `conn`
      - `listeners`: An instance of :class:`~pymongo.monitoring.EventListeners`
      - `max_bson_size`: The maximum encoded bson size for this server
      - `read_concern`: The read concern for this command.
      - `parse_write_concern_error`: Whether to parse the ``writeConcernError``
        field in the command response.
      - `collation`: The collation for this command.
      - `compression_ctx`: optional compression Context.
      - `use_op_msg`: True if we should use OP_MSG.
      - `unacknowledged`: True if this is an unacknowledged command.
      - `user_fields` (optional): Response fields that should be decoded
        using the TypeDecoders from codec_options, passed to
        bson._decode_all_selective.
      - `exhaust_allowed`: True if we should enable OP_MSG exhaustAllowed.
    z.$cmdFNreadConcern	collationr   )ctx)
service_idok   )codec_optionsuser_fields)parse_write_concern_error)r1   database_name)r1   speculative_hellor7   r)   )2nextiterr   _maybe_add_read_preferencein_transactionleveldocument_update_read_concernenabled_for_commandsdatetimenowr   lowerr   
_encrypter_bypass_auto_encryptionencryptapply_timeoutr   apply_write_concernr   MORE_TO_COMEEXHAUST_ALLOWED_op_msg_raise_document_too_large_query_COMMAND_OVERHEADpublish_command_startr1   connsendallreceive_messagemore_to_comeunpack_response_process_responser   _check_command_responsemax_wire_version	Exception
isinstancer   r   details_convert_exceptionpublish_command_failurepublish_command_successdecryptraw_command_responser   r   ))rP   dbnamespec	is_mongosread_preferencer4   sessionclientcheckallowable_errorsaddress	listenersmax_bson_sizeread_concernr6   r.   compression_ctx
use_op_msgunacknowledgedr5   exhaust_allowedwrite_concernnamensr8   origpublishstartflags
request_idmsgsizemax_doc_sizeencoding_durationreplyresponse_docunpacked_docsexcdurationfailure	decrypteds)                                            _/var/www/html/phonemate/phone_mate_backend/venv/lib/python3.12/site-packages/pymongo/network.pycommandr   ?   s   h T
D	'	B D***11$HW)?)?"."7"7D((t4%[t#F	(F(FG!!%%'8tD4::<?:&##F,=,=,U,U''//mLLt 64(	dM2'5##1?''A.5oo4-_/
+
C|
 m7L=<X--dD-H 'r1b$m_!

C  TMG<U<U,U%U))$mgF_F_6_`$--113e;$$$"""''&*g$// 	( 	
 !!%%')		#.E*.L#D*5E % 2 2D!11+ 2 M )+L((w?// ))$.G	2 %%))+e37HH$$$"""))/  	* 		
 &##%%--e.H.H.JK	1)]KXYZ[
 Q   ))--/%7;LLH#1ABC(+!44S9(((&&&--??$ .  	%s   ?BN9 9	QB	QQz<iiBc                   t        j                         rt        j                         }n6| j                  j	                         }|rt        j                         |z   }nd}t        t        | d|            \  }}}}|||k7  rt        d|d|      |dk  rt        d|d      ||kD  rt        d|d|d      |d	k(  r5t        t        | d
|            \  }}}	t        t        | |dz
  |      |	      }
nt        | |dz
  |      }
	 t        |   } ||
      S # t        $ r% t        d|dt        j                               dw xY w)z1Receive a raw BSON message or raise socket.error.N   zGot response id z but expected zMessage length (z3) not longer than standard message header size (16)z*) is larger than server max message size ()i  	      zGot opcode )r   get_timeoutget_deadlinerP   
gettimeouttime	monotonic_UNPACK_HEADER_receive_data_on_socketr   _UNPACK_COMPRESSION_HEADERr   r   KeyErrorkeys)rP   rw   max_message_sizedeadlinetimeoutlength_response_toop_codecompressor_iddataunpack_replys               r   rR   rR      s    %%'))&&(~~''1HH&45LTSUW_5`&a#FA{G$"2;/PZ~ ^__|vj([\
 	
   vj )-03
 	
 $$>#D!X6%
!M 1$XNP]^&tVb[(C$W-
 	  'N=3E3E3G2JK
	s   		D .Eg      ?c                   | j                   }|r| j                  }d}	 t        |d      r|j                         dkD  rd}n_|r9|t	        j
                         z
  }|dk  rd}t        t        |t              d      }nt        }| j                  j                  |d|      }|j                  rt        d      |ry|rt        j                  d      y)	zABlock until at least one byte is read, or a timeout, or a cancel.FTpendingr   )readr   zhello cancelledN	timed out)cancel_contextrP   hasattrr   r   r   maxmin_POLL_TIMEOUTsocket_checkerselect	cancelledr   socketr   )rP   r   contextsock	timed_outreadable	remainingr   s           r   wait_for_readr   #  s    !!Gyy	tY'DLLNQ,>  (4>>+; ;I !A~$(	!#i"?CG+G..55dw5W  )*;<<nn[11/      c                   t        |      }t        |      }d}||k  r	 t        | |       t        j                         r2|0| j                  t        |t        j                         z
  d             | j                  j                  ||d        }|dk(  rt        d      ||z  }||k  r|S # t        $ r t        j                  d      d t        $ r'}t        |      t         j"                  k(  rY d }~ۂ d }~ww xY w)Nr   r   zconnection closed)	bytearray
memoryviewr   r   r   set_conn_timeoutr   r   r   rP   	recv_intoBLOCKING_IO_ERRORSr   r   OSErrorr   errnoEINTR)rP   r   r   bufmv
bytes_readchunk_lengthr   s           r   r   r   H  s    
F
C	CBJ
v
	$)   "x';%%c(T^^5E*Eq&IJ99..r*+?L 1-..l"
% v
( I " 	8..-47 	$S)U[[8	s   A0B, ,'C:C54C55C:)TNNNNNFNNFFNFN).rP   r$   r`   strra   zMutableMapping[str, Any]rb   boolrc   zOptional[_ServerMode]r4   zCodecOptions[_DocumentType]rd   zOptional[ClientSession]re   zOptional[MongoClient]rf   r   rg   z#Optional[Sequence[Union[str, int]]]rh   zOptional[_Address]ri   zOptional[_EventListeners]rj   Optional[int]rk   zOptional[ReadConcern]r6   r   r.   zOptional[_CollationIn]rl   z4Union[SnappyContext, ZlibContext, ZstdContext, None]rm   r   rn   r   r5   zOptional[Mapping[str, Any]]ro   r   rp   zOptional[WriteConcern]returnr*   )rP   r$   rw   r   r   intr   zUnion[_OpReply, _OpMsg])rP   r$   r   Optional[float]r   None)rP   r$   r   r   r   r   r   r   )K__doc__
__future__r   rA   r   r   structr   typingr   r   r   r   r   r	   r
   r   bsonr   pymongor   r   r   r   pymongo.commonr   pymongo.compression_supportr   r   pymongo.errorsr   r   r   r   pymongo.messager   r   r   pymongo.monitoringr   pymongo.socket_checkerr   r   pymongo.client_sessionr   r   r    r!   pymongo.mongo_clientr"   r#   pymongo.poolr$   pymongo.read_concernr%   pymongo.read_preferencesr&   pymongo.typingsr'   r(   r)   r*   pymongo.write_concernr+   Structunpackr   r   r   rR   r   r   BlockingIOErrorr   r    r   r   <module>r      s,   - "     	 	 	 ' 8 8 + C  < ; ; 8!4SS02'04SS2w'.. <@"&+/#'*.&+(,LP /3!,0-o
oo #o 	o
 +o /o %o "o o :o  o )o !o (o  $o  &!o" J#o$ %o& 'o( -)o* +o, *-o. /od +V]]6299  JZ)
)"/)CF))X 2D &G(F(FG r   