
    [j5                    *   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          jA        ZB	 	 	 	 	 	 	 	 	 	 	 	 	 	 dRdSdCZC ej@        dD          jA        ZDefdTdIZEdJZFdUdNZGeHgejI        R ZIdVdQZJdS )Wz&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<iiiiTFconnr$   dbnamestrspecMutableMapping[str, Any]	is_mongosboolread_preferenceOptional[_ServerMode]codec_optionsCodecOptions[_DocumentType]sessionOptional[ClientSession]clientOptional[MongoClient]checkallowable_errors#Optional[Sequence[Union[str, int]]]addressOptional[_Address]	listenersOptional[_EventListeners]max_bson_sizeOptional[int]read_concernOptional[ReadConcern]parse_write_concern_error	collationOptional[_CollationIn]compression_ctx4Union[SnappyContext, ZlibContext, ZstdContext, None]
use_op_msgunacknowledgeduser_fieldsOptional[Mapping[str, Any]]exhaust_allowedwrite_concernOptional[WriteConcern]returnr*   c           
     z   t          t          |                    }|dz   }d}|}|r|s|J t          j        ||          }|r2|r|j        s)|j        r
|j        |d<   |r|                    ||            |||d<   |duo|j        }|r.t          j	        
                                }t          ||          }|r|                                t          v rd}|r1|j        r*|j        j        s|j                            |||          x}}|r|                     ||           t%          j        ||           |rc|rt(          j        nd}||rt(          j        ndz  }t          j        ||||||          \  }}}} |r|| |k    rt          j        |||           nt          j        d|dd|d||          \  }}}|6||t          j        z   k    r#t          j        |||t          j        z              |rft          j	        
                                |z
  }!|J |
J |                    ||||
| j        	           t          j	        
                                }	 | j                            |           |r	|rd}"d
di}#nrt?          | |          }"|"j         | _         |"!                    ||          }$|$d         }#|r|"                    |#|           |rtG          j$        |#| j%        |	|           n# tL          $ r}%|rt          j	        
                                |z
  |!z   }&tO          |%tP          tR          f          r|%j*        }'nt          j+        |%          }'|J |
J |,                    |&|'|||
| j        |            d}%~%ww xY w|rNt          j	        
                                |z
  |!z   }&|J |
J |-                    |&|#|||
| j        ||           |rZ|j        rS|"rQ|j        .                    |"/                                          }(ta          dtc          |(||          d                   }#|#S )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readConcernrG   r   )ctx)
service_idok   )r5   rM   )rF   )rW   database_name)rW   speculative_hellorZ   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_startrW   r,   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   ))r,   r-   r/   r1   r3   r5   r7   r9   r;   r<   r>   r@   rB   rD   rF   rG   rI   rK   rL   rM   rO   rP   namensr[   origpublishstartflags
request_idmsgsizemax_doc_sizeencoding_durationreplyresponse_docunpacked_docsexcdurationfailure	decrypteds)                                            S/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/pymongo/network.pycommandr   ?   st   h T

D	'	B D I I***1$HH 5W 5)? 5 	8"."7D 	5((t444%[t#F	(FG E!%%''8tDD 4::<<?:: M&# MF,=,U M'//mLLLt  )64(((	dM222 
'5<##1?A''A.5o4-_/
 /
 /
+
C|
  	Im7L=<X<X-dD-HHH 'r1b$m_!
 !

C  TMG<U,U%U%U)$mgF_6_``` ($-1133e;$$$"""''&*g$/ 	( 	
 	
 	
 !%%'')	# 	. 	E*.LL#D*55E % 2D!11+ 2  M )+L @((w??? / )$.G	        	 )--//%7;LLH#1ABCC :(+!4S99(((&&&--?$ .    	%&  
%))++e37HH$$$"""))/  	* 		
 		
 		
  
&# 
 
%--e.H.H.J.JKK	1)]KXXYZ[
 
 s   BK5 5
N?BNNz<iiBr   max_message_sizeintUnion[_OpReply, _OpMsg]c                   t          j                    rt          j                    }n4| j                                        }|rt          j                    |z   }nd}t          t          | d|                    \  }}}}|||k    rt          d|d|          |dk    rt          d|d          ||k    rt          d|d|d          |d	k    rEt          t          | d
|                    \  }}}	t          t          | |dz
  |          |	          }
nt          | |dz
  |          }
	 t          |         }n5# t          $ r( t          d|dt          j                              dw xY w ||
          S )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_deadliner,   
gettimeouttime	monotonic_UNPACK_HEADER_receive_data_on_socketr   _UNPACK_COMPRESSION_HEADERr   r   KeyErrorkeys)r,   r   r   deadlinetimeoutlength_response_toop_codecompressor_iddataunpack_replys               r   rt   rt      s     %'')&&(( 	~'''1HHH&45LTSUW_5`5`&a&a#FA{G$$ ^; ^ ^PZ ^ ^___||\v\\\
 
 	
    3v 3 3-3 3 3
 
 	
 $$>#D!X66%
 %
!M 1$XNNP]^^&tVb[(CC$W-   K'KK=3E3G3GKK
 
	 <s   1D? ?2E1g      ?r   Optional[float]Nonec                   | j         }|r| j        }d}	 t          |d          r|                                dk    rd}nh|rB|t	          j                    z
  }|dk    rd}t          t          |t                    d          }nt          }| j	        
                    |d|          }|j        rt          d          |rdS |rt          j        d          dS )	zABlock until at least one byte is read, or a timeout, or a cancel.FTpendingr   )readr   zhello cancelledN	timed out)cancel_contextr,   hasattrr   r   r   maxmin_POLL_TIMEOUTsocket_checkerselect	cancelledr   socketr   )r,   r   contextsock	timed_outreadable	remainingr   s           r   wait_for_readr   #  s   !G 2y		2tY'' XDLLNNQ,>,>  	, (4>+;+; ;I !A~~$(	!#i"?"?CCGG+G.55dw5WW  =)*;<<<  2n[111/	22 2    r   
memoryviewc                8   t          |          }t          |          }d}||k     r	 t          | |           t          j                    r9|7|                     t          |t          j                    z
  d                     | j	        
                    ||d                    }nS# t          $ r t          j        d          d t          $ r(}t          |          t           j        k    rY d }~Ђ d }~ww xY w|dk    rt          d          ||z  }||k     |S )Nr   r   zconnection closed)	bytearrayr   r   r   r   set_conn_timeoutr   r   r   r,   	recv_intoBLOCKING_IO_ERRORSr   r   OSErrorr   errnoEINTR)r,   r   r   bufmv
bytes_readchunk_lengthr   s           r   r   r   H  sB   
F

C	CBJ
v

	$)))  "" Kx';%%c(T^5E5E*Eq&I&IJJJ9..r*++??LL! 	8 	8 	8.--47 	 	 	$S))U[88	 1-...l"
% v

( Is   A>B' '(C7C21C22C7)TNNNNNFNNFFNFN).r,   r$   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r2   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   r2   rG   rH   rI   rJ   rK   r2   rL   r2   rM   rN   rO   r2   rP   rQ   rR   r*   )r,   r$   r   rC   r   r   rR   r   )r,   r$   r   r   rR   r   )r,   r$   r   r   r   r   rR   r   )K__doc__
__future__r   rd   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   rt   r   r   BlockingIOErrorr   r    r   r   <module>r      sS   - , " " " " " "     	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ' & & & & & 8 8 8 8 8 8 8 8 8 8 8 8 + + + + + + C C C C C C C C            < ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 8 8 8 8 8 8 
3!!!!!!444444SSSSSSSSSS000000222222''''''000000444444SSSSSSSSSSSS222222w''. <@"&+/#'*.&+(,LP /3!,0-o o o o od +V]6229  JZ) ) ) ) )X 2 2 2 2D &G(FGG      r   