
    ix                       S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
JrJrJrJrJrJr  SSKJr  SSKJr  SS	KJrJr  SS
KJrJr  SSKJr  SSKJr  SSKJ r   SSK!J"r"  \(       a  SSK#J$r$  SSK%J&r&  SSK'J(r(J)r)  SSK*J+r+J,r,J-r-  SSK.J/r/J0r0J1r1J2r2J3r3J4r4J5r5  SSK6J7r7  SSK8J9r9J:r:J;r;  SSK<J=r=J>r>J?r?J@r@JArA  SSKBJCrC  SSKDJErEJFrFJGrGJHrH  SSKIJJrJJKrK  SSKLJMrM  SrN " S S5      rOg)zIThe client-level bulk write operations interface.

.. versionadded:: 4.9
    )annotationsN)MutableMapping)islice)TYPE_CHECKINGAnyMappingOptionalTypeUnion)ObjectId)RawBSONDocument)_csotcommon)ClientSession_validate_session_write_concern)
Collection)CommandCursor)Database)_handle_reauth)MongoClient)
Connection)_merge_command"_throw_client_bulk_write_exception)validate_is_document_typevalidate_ok_for_replacevalidate_ok_for_update)ConfigurationErrorConnectionFailureInvalidOperationNotPrimaryErrorOperationFailurePyMongoErrorWaitQueueTimeoutError)_RETRYABLE_ERROR_CODES)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_ClientBulkWriteContext_convert_client_bulk_exception_convert_exception_convert_write_result_randint)ReadPreference)ClientBulkWriteResultDeleteResultInsertOneResultUpdateResult)_DocumentOut	_Pipeline)WriteConcernTc                  <   \ rS rSrSr     S               SS jjr\SS j5       rSS jr     S                   SS jjr	    S               SS jjr
  S           SS	 jjr\                SS
 j5       r                S S jr          S!S jr          S"S jr          S#S jr S$               S%S jjr      S&S jr    S'S jr    S'S jr      S(S jrSrg))_ClientBulkW   z4The private guts of the client-level bulk write API.Nc                |   Xl         X l        X`l        U R                  b!  [        R                  " SU R                  5        X0l        X@l        XPl        Xpl        / U l	        / U l
        SU l        SU l        SU l        SU l        SU l        U R                   R                   R"                  U l        SU l        SU l        g)z"Initialize a _ClientBulk instance.Nletr   F)clientwrite_concernr9   r   r   orderedbypass_doc_valcommentverbose_resultsops
namespaces
idx_offset	total_opsexecuteduses_collationuses_array_filtersoptionsretry_writesis_retryableretryingstarted_retryable_write)selfr:   r;   r<   bypass_document_validationr>   r9   r?   s           V/home/ubuntu/wiki/venv/lib/python3.13/site-packages/pymongo/synchronous/client_bulk.py__init___ClientBulk.__init__Z   s     *88,,UDHH=8.8:%' #"' KK//<<',$    c                    [         $ N)r(   )rL   s    rN   bulk_ctx_class_ClientBulk.bulk_ctx_classy   s    &&rQ   c                   [        SU5        [        U[        5      (       d  SU;   d  [        5       US'   SUS.nU R                  R                  SU45        U R                  R                  U5        U =R                  S-  sl        g)z*Add an insert document to the list of ops.document_id)insertrW   rZ      N)r   
isinstancer   r   r@   appendrA   rC   )rL   	namespacerW   cmds       rN   
add_insert_ClientBulk.add_insert}   si    !*h78_55(9J&jHUO23(y)!rQ   c
                @   [        U5        SUUUS.n
Ub  XZS'   Ub  SU l        XzS'   Ub  XS'   Ub  SU l        XjS'   U	b  XS	'   U(       a  S
U l        U R                  R                  SU
45        U R                  R                  U5        U =R                  S-  sl        g)z8Create an update document and add it to the list of ops.rY   updatefilter
updateModsmultiNupsertTarrayFiltershint	collationsortFrd   r[   )r   rF   rE   rI   r@   r]   rA   rC   )rL   r^   selectorrd   rg   rh   rk   array_filtersrj   rl   r_   s              rN   
add_update_ClientBulk.add_update   s     	v& 	
 "M$&*D#"/K "&D(K %D3(y)!rQ   c                   [        U5        SUUSS.nUb  XHS'   Ub  XhS'   Ub  SU l        XXS'   Ub  XxS	'   U R                  R                  S
U45        U R                  R                  U5        U =R
                  S-  sl        g)z8Create a replace document and add it to the list of ops.rY   Frc   Nrh   rj   Trk   rl   replacer[   )r   rE   r@   r]   rA   rC   )	rL   r^   rm   replacementrh   rk   rj   rl   r_   s	            rN   add_replace_ClientBulk.add_replace   s     	 ,%	
 "MK "&D(KC()y)!rQ   c                    SX#S.nUb  XVS'   Ub  SU l         XFS'   U(       a  SU l        U R                  R                  SU45        U R                  R                  U5        U =R
                  S	-  sl        g)
z7Create a delete document and add it to the list of ops.rY   )deletere   rg   Nrj   Trk   Frw   r[   )rE   rI   r@   r]   rA   rC   )rL   r^   rm   rg   rk   rj   r_   s          rN   
add_delete_ClientBulk.add_delete   sp     x@K "&D( %D3(y)!rQ   c                L	   XRS'   XbS'   [         R                  " [        R                  5      (       a  [	        [         [
        R                  UR                  R                  U[        [        U5      5      UR                  UUUR                  R                  UR                  R                  UR                  R                  S   UR                  R                  S   UR                  R                   S9  UR"                  (       a  UR%                  X#XV5         UR                  R'                  X4UR(                  5      n[*        R*                  R-                  5       UR.                  -
  n	[         R                  " [        R                  5      (       a  [	        [         [
        R0                  UR                  R                  U	U[        [        U5      5      UR                  UUUR                  R                  UR                  R                  UR                  R                  S   UR                  R                  S   UR                  R                   S9  UR"                  (       a  UR3                  X8U	5        U R4                  R7                  XR8                  5        U$ ! [:         Ga  n
[*        R*                  R-                  5       UR.                  -
  n	[=        U
[>        [@        45      (       a  U
RB                  nO[E        U
5      n[         R                  " [        R                  5      (       a  [	        [         [
        RF                  UR                  R                  U	U[        [        U5      5      UR                  UUUR                  R                  UR                  R                  UR                  R                  S   UR                  R                  S   UR                  R                   [=        U
[@        5      S9  UR"                  (       a  URI                  X;U	5        SU
0n[=        U
[@        5      (       a6  U R4                  R7                  U
RB                  UR8                  5         S	n
A
U$ U R4                  R7                  0 UR8                  5         S	n
A
U$ S	n
A
ff = f)
zCA proxy for Connection.write_command that handles event publishing.r@   nsInfor   r[   messageclientIdcommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceIdr}   r~   
durationMSreplyr   r   r   r   r   r   r   r   r   r}   r~   r   failurer   r   r   r   r   r   r   r   r   isServerSideErrorerrorN)%r%   isEnabledForloggingDEBUGr'   r&   STARTED_topology_settings_topology_idnextiterdb_nameconnidserver_connection_idaddress
service_idpublish_startwrite_commandcodecdatetimenow
start_time	SUCCEEDED_succeedr:   _process_responsesession	Exceptionr\   r    r!   detailsr*   FAILED_fail)rL   bwcr_   
request_idmsgop_docsns_docsr:   r   durationexcr   s               rN   r   _ClientBulk.write_command   sL    E
H''66-5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;JJs99	?HH**:CIIFE((,,.?H++GMM::#1;;#66CC' $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11  {{Z9KK))%=F E  !	?((,,.?H#1ABCC(+,S1++GMM::#188#66CC'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G$ {{		*x8cNE#/00--ckk3;;GG  --b#++>>E!	?s    FJ R#(GR2&RR#c                   [         R                  " [        R                  5      (       a  [	        [         [
        R                  UR                  R                  U[        [        U5      5      UR                  UUUR                  R                  UR                  R                  UR                  R                  S   UR                  R                  S   UR                  R                   S9  UR"                  (       a  UR%                  X#XV5      n UR                  R'                  XAR(                  5      n[*        R*                  R-                  5       UR.                  -
  n	Ub  [1        UR2                  X(5      n
OSS0n
[         R                  " [        R                  5      (       a  [	        [         [
        R4                  UR                  R                  U	U
[        [        U5      5      UR                  UUUR                  R                  UR                  R                  UR                  R                  S   UR                  R                  S   UR                  R                   S9  UR"                  (       a  UR7                  X:U	5        U
$ ! [8         Ga  n[*        R*                  R-                  5       UR.                  -
  n	[;        U[<        5      (       a!  [1        UR2                  X+R>                  5      nO-[;        U[@        5      (       a  UR>                  nO[C        U5      n[         R                  " [        R                  5      (       a  [	        [         [
        RD                  UR                  R                  U	U[        [        U5      5      UR                  UUUR                  R                  UR                  R                  UR                  R                  S   UR                  R                  S   UR                  R                   [;        U[<        5      S9  UR"                  (       a!  UR.                  c   eURG                  X<U	5        SU0n
 SnAU
$ SnAff = f)	zAA proxy for Connection.unack_write that handles event publishing.r   r[   r|   Nokr   r   r   )$r%   r   r   r   r'   r&   r   r   r   r   r   r   r   r   r   r   r   r   r   unack_writemax_bson_sizer   r   r   r+   namer   r   r   r\   r!   r   r    r*   r   r   )rL   r   r_   r   r   r   r   r:   resultr   r   r   r   s                rN   r   _ClientBulk.unack_write@  s$    ''66-5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;**Sg?C9	#XX))#/@/@AF((,,.?H!-chhD q	"//>>' 5 ? ?!'!:!:!G!G#+#$(cO%([[",$.+.88;;+.88+H+H#&88#3#3A#6#&88#3#3A#6"%(("5"5  {{Z9@ ?  	#((,,.?H#/00(=chh[[(YC11++,S1++GMM::#188#66CC'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G" {{~~111		*x8cNE?	#s   E<J Q!F>QQ!c           	     n    UR                  X#U5      u  pVpxU R                  XXVXxU R                  5        Xx4$ )z6Executes a batch of bulkWrite server commands (unack).)batch_commandr   r:   )	rL   r   r_   r@   rA   r   r   to_send_ops
to_send_nss	            rN   _execute_batch_unack _ClientBulk._execute_batch_unack  s<     473D3DSz3Z0
:KT[[Y&&rQ   c           	     p    UR                  X#U5      u  pVpxU R                  XXVXxU R                  5      n	XU4$ )z4Executes a batch of bulkWrite server commands (ack).)r   r   r:   )
rL   r   r_   r@   rA   r   r   r   r   r   s
             rN   _execute_batch_ClientBulk._execute_batch  sC     473D3DSz3Z0
##Cj{X\XcXcdJ..rQ   c                   UR                  S5      (       Ga$  U(       a  SUl        [        [        U R                  S5      SS9n[        UUS   UR                  UU R                  S9nUR                  U5         U H  nUS   U R                  -   nU R                  U   u  pUS   (       d'  US	   R                  U5        U R                  (       a    g
US   (       d  Mc  U R                  (       d  Mv  U	S:X  a  U
S   S   n[        USS9nU	S;   a  Sn	[        USSS9nU	S:X  a
  [!        USS9nWX S3   U'   M     g
g
! ["         a9  nUR$                  (       a  UR'                  5         [)        U5      US'    S
nAg
S
nAff = f)z?Internal helper for processing the server reply command cursor.cursorTadminz$cmd.bulkWrite)databaser   )r   r>   idxr   writeErrorsNrZ   rW   rX   )acknowledged)rd   rr   rd   )r   in_client_bulkrw   Resultsr   )get_leave_aliver   r   r:   r   r   r>   _maybe_pin_connectionrB   r@   r]   r<   r?   r0   r1   r/   r   alivecloser)   )rL   full_resultr   r   r   coll
cmd_cursordocoriginal_indexop_typeopinserted_idresr   s                 rN   _process_results_cursor#_ClientBulk._process_results_cursor  s    ::h'+$!$++w7%D 'x J ,,T2F%C%(Z$//%AN"&((>":KGt9}-44S9<<" 4yyT%9%9%9"h.*,Z.*?K"1+D"QC"&;;&.G".sVZ"[C"h.".s"FCKNiw$78H' &%  L  F##$$&"@"Ew	Fs,   :AD= D= D= 2A	D= =
F /E;;F c           	        SnSn	U R                   R                  n
UR                  U R                   U5        U R                  UU	UUU
UU R                   R                  5      nU R
                  U R                  :  Ga
  U R                  U R
                  -
  UR                  ::  a  U=(       d    UnSS0nU R                  (       + US'   U R                  US'   U=(       a    UR                  (       + nU(       d  U(       d  [        R                  " X5        U R                  b  U R                  US'   U R                  (       a  U R                  US'   U R                  (       a  U R                  US	'   U(       aP  U(       a(  U R                   (       d  UR#                  5         S
U l        UR%                  X[&        R(                  U5        UR+                  XU R                   5        UR-                  U5        UR/                  U R                   U5        [1        U R2                  U R
                  S5      n[1        U R4                  U R
                  S5      nUR6                  (       Ga  U R9                  XX5      u  nnnUnUR;                  S5      (       GaX  US   n[=        US5      =(       aD    [?        UR@                  [B        5      =(       a#    UR@                  R;                  SS5      [D        ;   n[?        U[F        5      =(       a    [?        U[H        [J        45      (       + n[?        U[L        5      =(       a    URO                  S5      nU(       ad  U(       d  U(       d  U(       aO  [P        RR                  " U5      n[U        U R2                  U R
                  UU5        [W        UU R                  5        O7[U        U R2                  U R
                  UU5        [W        X`R                  5        SUS'   / US'   UR;                  SS5      [Y        U5      :  a  S
US'   US   (       d(  UUS'   [U        U R2                  U R
                  UU5        gU(       az  UR;                  S0 5      nUR;                  SS5      [D        ;   aN  [P        RR                  " U5      n[U        U R2                  U R
                  UU5        [W        UU R                  5        U R[                  UUX25        [U        U R2                  U R
                  UU5        SU l.        SU l        OU R_                  XX5      u  nnU =R
                  [Y        U5      -  sl        US   (       d  U R                  (       a  US   (       a  gU R
                  U R                  :  a  GM	  gg)z<Internal helper for executing batches of bulkWrite commands.r   	bulkWriter[   
errorsOnlyr<   NbypassDocumentValidationr>   r9   Tr   r   coder   RetryableErrorr   nErrorsanySuccessfulr   writeConcernErrorF)0r:   _event_listenersvalidate_sessionrT   codec_optionsrB   rC   max_write_batch_sizer?   r<   in_transactionr   apply_write_concernr=   r>   r9   rK   _start_retryable_write	_apply_tor-   PRIMARYsend_cluster_timeadd_server_apiapply_timeoutr   r@   rA   r   r   r   hasattrr\   r   dictr$   r   r    r#   r"   has_error_labelcopydeepcopyr   r   lenr   rJ   r   )rL   r;   r   r   op_id	retryabler   final_write_concernr   cmd_name	listenersr   r_   not_in_transactionr@   rA   
raw_resultr   _r   r   retryable_top_level_errorretryable_network_errorretryable_label_errorfullwces                             rN   _execute_command_ClientBulk._execute_command  sm    KK00	 	dkk73!!KK%%
 oo. ~~/33K3KK 3 D} "C$($8$8 8C!\\C	N!(!G1G1G-G!))#="".262E2E./||!%Ixx!XXE
 T%A%A22437D0!!#.2H2H$O""3=$t{{C04??D9C$GJ )))-1-@-@3-[*
K# ::g&&"7OEy1 S&u}}d;S!MM--fa8<RR .
 /90/ /Z(BW0XYY , -7|- -B//0@A * !120#}}[9&txx$O:4AUAUV&txx+vV:;H\H\]"&w(*}%::i+c+.>>37K0 d|&0F7O"488T__k6R **%8"=Cwwvq)-CC  $}}[9&txx$O:4AUAUV ,,[&$P txx+vN !&/4, "&!:!:3S!UQOOs;//O 7#]9SY oo..rQ   c                  ^ ^^ SS/ / SSSSS0 0 0 S.m[        5       m        S
UUU 4S jjnT R                  R                  T R                  UUUT TS9  TS   (       d  TS   (       d
  TS	   (       a  [	        TT R
                  5        T$ )z'Execute commands with w=1 WriteConcern.FNr   )r   r   r   writeConcernErrors	nInserted	nUpsertednMatched	nModifiednDeletedinsertResultsupdateResultsdeleteResultsc                |   > UR                   S:  a  [        S5      eTR                  TR                  U UTUT5        g )N   <MongoClient.bulk_write requires MongoDB server version 8.0+.)max_wire_versionr   r  r;   )r   r   r   r   r   rL   s      rN   retryable_bulk3_ClientBulk.execute_command.<locals>.retryable_bulk  sK    
 $$r)&R  !!""rQ   )bulkoperation_idr   r   r	  )r   Optional[ClientSession]r   r   r   boolreturnNone)r,   r:   _retryable_writerI   r   r?   )rL   r   	operationr  r   r   s   `   @@rN   execute_command_ClientBulk.execute_commandp  s     #"$1
 
	,		 	 		 	$ 	$$ 	% 	
 w;}#=MaAb.{D<P<PQrQ   c           	         SnSnU R                   R                  n[        5       nU R                  UUUUUSU R                   R                  5      nU R
                  U R                  :  Ga  SS0nSUS'   SUS'   U R                  b  U R                  US	'   S
S0US'   U R                  (       a  U R                  US'   U R                  (       a  U R                  US'   UR                  U5        [        U R                  U R
                  S5      n[        U R                  U R
                  S5      n	U R                  XgX5      u  pU =R
                  [        U
5      -  sl        U R
                  U R                  :  a  GM  gg)zDExecute commands with OP_MSG and w=0 writeConcern. Always unordered.r   r   Nr[   Tr   Fr<   r   wr   writeConcernr>   r9   )r:   r   r,   rT   r   rB   rC   r=   r>   r9   r   r   r@   rA   r   r   )rL   r   r   r   r   r   r   r_   r@   rA   r   r   s               rN   execute_command_unack!_ClientBulk.execute_command_unack  sM   
 KK00	
!!KK%%
 oo."C $C"C	N"".262E2E./#&(C||!%Ixx!XXE
$4??D9C$GJ "66sQNKOOs;//O) oo..rQ   c                    U R                   (       a  [        S5      eU R                  (       a  [        S5      eU R                  b  [	        S5      eU R                  U5      $ )z3Execute all operations, returning no results (w=0).z3Collation is unsupported for unacknowledged writes.z6arrayFilters is unsupported for unacknowledged writes.zGCannot set bypass_document_validation with unacknowledged write concern)rE   r   rF   r=   r!   r%  )rL   r   s     rN   execute_no_results_ClientBulk.execute_no_results  s]    
 $%Z[[""$%]^^*"Y  ))$//rQ   c                6   U R                   (       d  [        S5      eU R                  (       a  [        S5      eSU l        [        XR                  5      nU R                  R
                  (       d^  U R                  R                  X5       nUR                  S:  a  [        S5      eU R                  U5        [        SSS5      sSSS5        $ U R                  X5      n[        UU R                  R
                  U R                  5      $ ! , (       d  f       NJ= f)zExecute operations.zNo operations to executez*Bulk operations can only be executed once.Tr  r  NF)r@   r   rD   r   r;   r   r:   _conn_for_writesr  r(  r.   r   r?   )rL   r   r  
connectionr   s        rN   execute_ClientBulk.execute  s     xx"#=>>=="#OPP1';M;MN!!..--gAZ..3*V  ''
3,T5%@ BA %%g9$++  
 	
 BAs   9D


D)r=   r:   r>   rD   rB   rI   r9   rA   r@   r<   rJ   rK   rC   rF   rE   r?   r;   )TNNNF)r:   MongoClient[Any]r;   r4   r<   r  rM   Optional[bool]r>   zOptional[str]r9   zOptional[Any]r?   r  r  r  )r  zType[_ClientBulkWriteContext])r^   strrW   r2   r  r  )NNNNN)r^   r1  rm   Mapping[str, Any]rd   z#Union[Mapping[str, Any], _Pipeline]rg   r  rh   r0  rk   Optional[Mapping[str, Any]]rn   z!Optional[list[Mapping[str, Any]]]rj    Union[str, dict[str, Any], None]rl   r3  r  r  )NNNN)r^   r1  rm   r2  rs   r2  rh   r0  rk   r3  rj   r4  rl   r3  r  r  )NN)r^   r1  rm   r2  rg   r  rk   r3  rj   r4  r  r  )r   r(   r_   MutableMapping[str, Any]r   intr   zUnion[bytes, dict[str, Any]]r   list[Mapping[str, Any]]r   r7  r:   r/  r  dict[str, Any])r   r(   r_   r5  r   r6  r   bytesr   r7  r   r7  r:   r/  r  r3  )
r   r(   r_   r8  r@   #list[tuple[str, Mapping[str, Any]]]rA   	list[str]r  z7tuple[list[Mapping[str, Any]], list[Mapping[str, Any]]])
r   r(   r_   r8  r@   r:  rA   r;  r  zGtuple[dict[str, Any], list[Mapping[str, Any]], list[Mapping[str, Any]]])
r   r5  r   r5  r   r   r   r  r  r  rS   )r;   r4   r   r  r   r   r   r6  r   r  r   r5  r   zOptional[WriteConcern]r  r  )r   r  r  r1  r  r5  )r   r   r  r  )r   r  r  r1  r  r   )__name__
__module____qualname____firstlineno____doc__rO   propertyrT   r`   ro   rt   rx   r   r   r   r   r   r   r  r   r%  r(  r-  __static_attributes__ rQ   rN   r6   r6   W   s   > 59!%! %- - $- 	-
 %3- - - - 
-> ' '	" "&15;?15,0%% $% 4	%
 % % /% 9% /% *% 
%X "&1515,0 $ '	
  / / * 
H 2615 $ 	
 / / 
, Y$Y &Y 	Y
 *Y )Y )Y !Y 
Y YvW$W &W 	W
 W )W )W !W 
%Wr
'$
' 
' 1	
'
 
' 
A
'
/$
/ 
/ 1	
/
 
/ 
Q
/2F-2F )2F 	2F
 )2F 
2Fx 7;I#I )I 	I
 I I .I 4I 
IV3(3 3 
"	3j(0(0 
(0T00 
0"
(
 
 
	
rQ   r6   )Pr@  
__future__r   r   r   r   collections.abcr   	itertoolsr   typingr   r   r   r	   r
   r   bson.objectidr   bson.raw_bsonr   pymongor   r   "pymongo.synchronous.client_sessionr   r   pymongo.synchronous.collectionr   "pymongo.synchronous.command_cursorr   pymongo.synchronous.databaser   pymongo.synchronous.helpersr    pymongo.synchronous.mongo_clientr   pymongo.synchronous.poolr   pymongo._client_bulk_sharedr   r   pymongo.commonr   r   r   pymongo.errorsr   r   r   r    r!   r"   r#   pymongo.helpers_sharedr$   pymongo.loggerr%   r&   r'   pymongo.messager(   r)   r*   r+   r,   pymongo.read_preferencesr-   pymongo.resultsr.   r/   r0   r1   pymongo.typingsr2   r3   pymongo.write_concernr4   _IS_SYNCr6   rC  rQ   rN   <module>r]     s    #    *   # ) ! ] 5 < 1 6<3 
   : M M  4  4 .d

 d

rQ   