
    Iifo                       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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JrJrJr  SSKJ r J!r!J"r"  SSK#J$r$J%r%J&r&J'r'  SSK(J)r)  SSK*J+r+J,r,J-r-  SSK.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6  SSK7J8r8  SSK9J:r:J;r;  SSK<J=r=  SSK>J?r?  \(       a  SSK@JArA  SSKBJCrC  SSKDJErE  SSKFJGrGJHrHJIrI  SrJ " S S5      rKg)z<The bulk write operations interface.

.. versionadded:: 2.7
    )annotationsN)MutableMapping)islice)TYPE_CHECKINGAnyIteratorMappingOptionalTypeUnion)ObjectId)RawBSONDocument)_csotcommon)	_COMMANDS_DELETE_ALL_merge_command_raise_bulk_write_error_Run)validate_is_document_typevalidate_ok_for_replacevalidate_ok_for_update)ConfigurationErrorInvalidOperationNotPrimaryErrorOperationFailure)_RETRYABLE_ERROR_CODES)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_DELETE_INSERT_UPDATE_BulkWriteContext_convert_exception_convert_write_result_EncryptedBulkWriteContext_randint)ReadPreference)ClientSession_validate_session_write_concern)_handle_reauth)WriteConcern)
Collection)MongoClient)
Connection)_DocumentOut_DocumentType	_PipelineTc                  4   \ 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rS!S jr\              S"S j5       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+S jrSrg),_BulkS   z'The private guts of the bulk write API.Nc                   UR                  UR                  R                  S[        S9S9U l        XPl        U R
                  b!  [        R                  " SU R
                  5        X@l        X l	        / U l
        SU l        X0l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        g)zInitialize a _Bulk instance.replace)unicode_decode_error_handlerdocument_class)codec_optionsNletFT)with_optionsr;   _replacedict
collectionr<   r   r   commentorderedopsexecutedbypass_doc_valuses_collationuses_array_filtersuses_hint_updateuses_hint_deleteis_retryableretryingstarted_retryable_writecurrent_runnext_runis_encrypted)selfr@   rB   bypass_document_validationrA   r<   s         >/venv/lib/python3.13/site-packages/pymongo/synchronous/bulk.py__init___Bulk.__init__V   s     %11$22;;-6t <  2 

 88,,UDHH=&-8:8#"' % % ',$!    c                    U R                   R                  R                  R                  nU(       a  UR                  (       d  SU l        [        $ SU l        [        $ )NTF)r@   databaseclient
_encrypter_bypass_auto_encryptionrO   r'   r$   )rP   	encrypters     rR   bulk_ctx_class_Bulk.bulk_ctx_classx   sD    OO,,33>>	Y>> $D-- %D$$rU   c                    [        SU5        [        U[        5      (       d  SU;   d  [        5       US'   U R                  R                  [        U45        g)z*Add an insert document to the list of ops.document_idN)r   
isinstancer   r   rC   appendr"   )rP   r_   s     rR   
add_insert_Bulk.add_insert   s@    !*h78_55(9J&jHUO(+,rU   c                    [        U5        [        SU4SU4SU4SU4/5      nUb  SU l        XXS'   Ub  SU l        XhS'   Ub  SU l        XxS	'   U(       a  S
U l        U R                  R                  [        U45        g)z8Create an update document and add it to the list of ops.qumultiupsertNT	collationarrayFiltershintF)	r   r?   rF   rG   rH   rJ   rC   rb   r#   )	rP   selectorupdaterh   ri   rj   array_filtersrl   cmds	            rR   
add_update_Bulk.add_update   s     	v&"8_sFmgu-=&?QR
  "&D($&*D#"/$(D!K %D#'rU   c                    [        U5        XSUS.nUb  SU l        XFS'   Ub  SU l        XVS'   U R                  R	                  [
        U45        g)z8Create a replace document and add it to the list of ops.F)rf   rg   rh   ri   NTrj   rl   )r   rF   rH   rC   rb   r#   )rP   rm   replacementri   rj   rl   rp   s          rR   add_replace_Bulk.add_replace   sW     	 ,&Q "&D($(D!K#'rU   c                    XS.nUb  SU l         X5S'   Ub  SU l        XES'   U[        :X  a  SU l        U R                  R                  [        U45        g)z7Create a delete document and add it to the list of ops.)rf   limitNTrj   rl   F)rF   rI   r   rJ   rC   rb   r!   )rP   rm   rx   rj   rl   rp   s         rR   
add_delete_Bulk.add_delete   s[     - "&D($(D!KK %D#'rU   c              #     #    Sn[        U R                  5       HG  u  nu  p4Uc  [        U5      nOUR                  U:w  a  Uv   [        U5      nUR	                  X$5        MI     Uv   g7f)zYGenerate batches of operations, batched by type of
operation, in the order **provided**.
N)	enumeraterC   r   op_typeadd)rP   runidxr}   	operations        rR   gen_ordered_Bulk.gen_ordered   sb      )2488)<%C%'{7m'	7mGGC# *= 	s   A(A*c              #    #    [        [        5      [        [        5      [        [        5      /n[	        U R
                  5       H  u  nu  p4X   R                  X$5        M     U H  nUR
                  (       d  M  Uv   M     g7f)zRGenerate batches of operations, batched by type of
operation, in arbitrary order.
N)r   r"   r#   r!   r|   rC   r~   )rP   
operationsr   r}   r   r   s         rR   gen_unordered_Bulk.gen_unordered   se      7mT']DMB
)2488)<%C%'##C3 *= Cwww	 s   A5B;	Bc                   XRUR                   '   [        R                  " [        R                  5      (       a  [        [        UR                  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#U5         UR                  R)                  X4UR*                  5      n[,        R,                  R/                  5       UR0                  -
  n[        R                  " [        R                  5      (       a  [        [        UR                  R                  [        R2                  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R5                  X7U5        UR7                  XqR8                  5        U$ ! [:         Ga  n	[,        R,                  R/                  5       UR0                  -
  n[=        U	[>        [@        45      (       a  U	RB                  n
O[E        U	5      n
[        R                  " [        R                  5      (       a  [        [        UR                  R                  [        RF                  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        [=        U	[>        [@        45      (       a&  UR7                  U	RB                  UR8                  5        e Sn	A	ff = f)zCA proxy for SocketInfo.write_command that handles event publishing.r      clientIdmessage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   isServerSideErrorN)%fieldr   isEnabledForloggingDEBUGr    _topology_settings_topology_idr   STARTEDnextiterdb_nameconnidserver_connection_idaddress
service_idpublish_startwrite_commandcodecdatetimenow
start_time	SUCCEEDED_succeed_process_responsesession	Exceptionra   r   r   detailsr%   FAILED_fail)rP   bwcrp   
request_idmsgdocsrX   r   durationexcr   s              rR   r   _Bulk.write_command   s    CII''6622??-55 cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;JJs-5	HH**:CIIFE((,,.?H++GMM::##66CC1;;' $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11  {{Z9$$UKK8@ ?  	((,,.?H#1ABCC(+,S1++GMM::##66CC188'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G$ {{		*x8#1ABCC((ckkB=	s   E:J Q&$F=Q!!Q&c                   [         R                  " [        R                  5      (       a  [	        [         UR
                  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#U5      n UR                  R'                  XE5      n[(        R(                  R+                  5       UR,                  -
  n	Ub  [/        UR0                  X(5      n
OSS0n
[         R                  " [        R                  5      (       a  [	        [         UR
                  R                  [        R2                  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R5                  X:U	5        U$ ! [6         Ga  n[(        R(                  R+                  5       UR,                  -
  n	[9        U[:        5      (       a!  [/        UR0                  X+R<                  5      nO-[9        U[>        5      (       a  UR<                  nO[A        U5      n[         R                  " [        R                  5      (       a  [	        [         UR
                  R                  [        RB                  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                   [9        U[:        5      S9  UR"                  (       a!  UR,                  c   eURE                  X<U	5        e 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   unack_writer   r   r   r&   namer   r   r   ra   r   r   r   r%   r   r   )rP   r   rp   r   r   max_doc_sizer   rX   resultr   r   r   r   s                rR   r   _Bulk.unack_write>  s    ''6622??-55 cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;**Sd3C8	XX))#<F((,,.?H!-chhD q	"//>>'!'!:!:!G!G 5 ? ?#+#$(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::##66CC188'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G" {{~~111		*x8;	s   E2J QF;Q		Qc           	        U R                   (       aN  UR                  X#5      u  pVnUR                  R                  UR                  U[        SS9UR                  US9  U$ UR                  X#5      u  pnU R                  XXSXt5        U$ )Nr   )w)write_concernr   rX   )rO   batch_commandr   r   r   r-   r   r   )
rP   r   rp   rC   rX   _batched_cmdto_sendr   r   s
             rR   _execute_batch_unack_Bulk._execute_batch_unack  s     &)&7&7&A#AGHH*Q/     (+'8'8'B$JW Sz7KrU   c                   U R                   (       aR  UR                  X#5      u  pVnUR                  R                  UR                  UUR
                  UR                  US9nX4$ UR                  X#5      u  pnU R                  XXXt5      nX4$ )N)r;   r   rX   )rO   r   r   r   r   r   r   r   )rP   r   rp   rC   rX   r   r   r   r   r   r   s              rR   _execute_batch_Bulk._execute_batch  s     &)&7&7&A#AGXX%%!ii & F  (+'8'8'B$JW''*7SFrU   c	           
     H   U R                   R                  R                  n	U R                   R                  R                  n
U
R                  nU R
                  (       d  [        U5      U l        S U l        U R
                  nUR                  X5        SnU(       Ga  U R                  (       d   [        US 5      U l        U R                  c  Sn[        UR                     nU R                  U	UUUUUUR                  U R                   R                  5      nUR                  [        UR                   5      :  Ga  U(       a1  [        UR                   5      UR                  -
  S:X  a  U=(       d    UnXR                   R                  SU R"                  0nU R$                  (       a  U R$                  US'   [&        R(                  " UU5        U R*                  (       a  SUS'   U R,                  b)  UR                  [.        [0        4;   a  U R,                  US'   U(       aQ  U(       a(  U R2                  (       d  UR5                  5         SU l        UR7                  UU[8        R:                  U5        UR=                  UX:5        UR?                  U5        URA                  U
U5        [C        UR                   UR                  S 5      nURD                  (       a  U RG                  UUUU
5      u  nnURI                  S0 5      nURI                  S	S
5      [J        ;   a9  [L        RN                  " U5      n[Q        UUUR                  U5        [S        U5        [Q        XUR                  U5        SU l	        SU l        U R"                  (       a  SU;   a  OYOU RU                  UUUU
5      nU=R                  [        U5      -  sl        UR                  [        UR                   5      :  a  GM  U R"                  (       a  US   (       a  g U R                  =U l        nU(       a  GM  g g )NFTr   rB   rA   bypassDocumentValidationr<   writeConcernErrorcoder   writeErrors)+r@   rW   r   rX   _event_listenersrM   r   rN   validate_sessionrK   r   r}   r\   r;   
idx_offsetlenrC   rB   rA   r   apply_write_concernrE   r<   r!   r#   rL   _start_retryable_write	_apply_tor)   PRIMARYsend_cluster_timeadd_server_apiapply_timeoutr   acknowledgedr   getr   copydeepcopyr   r   r   )rP   	generatorr   r   r   op_id	retryablefull_resultfinal_write_concernr   rX   	listenersr   last_runcmd_namer   rp   rC   r   r   wcefulls                         rR   _execute_command_Bulk._execute_command  s-    //**//))00++	#ID DM 	f.== $Y 5==(#H -H%%--	C ..3sww</ SWW!>1 D$7$H=M!5!5y$,,O<<%)\\C	N))#}=&&6:C2388'CKKGW;M,M!%CJ !)E)E6687;4%%c9n6L6LdS&&sG<##C(""63/SWWcnnd; !--&*&9&9#sC&POFG !**%8"=Cwwvq)-CC  $}}[9&sD#..&I/5"3S^^VL %*DM38D0||(?"77S#vNG#g,.g ..3sww</n ||M :%)]]2DsY crU   c           	       ^ ^^^^	 / / SSSSS/ S.m[        5       m	        SUUU	U U4S jjnT R                  R                  R                  nUR	                  T R
                  UUUT T	S9nTS   (       d
  TS   (       a  [        T5        T$ )zExecute using write commands.r   r   writeConcernErrors	nInserted	nUpsertednMatched	nModifiednRemovedupsertedc           	     4   > TR                  TTU UTUT5        g N)r   )r   r   r   r   r   r   rP   r   s      rR   retryable_bulk-_Bulk.execute_command.<locals>.retryable_bulkC  s(     !!rU   )bulkoperation_idr   r   )r   Optional[ClientSession]r   r0   r   boolreturnNone)r(   r@   rW   rX   _retryable_writerJ   r   )
rP   r   r   r   r   r   rX   r   r   r   s
   ```     @@rR   execute_command_Bulk.execute_command.  s     "$	
 
	,	4>	KO		 	 ))00## $ 
 }%5I)J#K0rU   c           
     v   U R                   R                  R                  nU R                   R                  R                  nUR                  n[        5       nU R                  (       d  [        U5      U l        U R                  nU(       Ga.  [        UR                     nU R                  UUUUUSUR                  U R                   R                  5      n	UR                  [        UR                  5      :  a  XR                   R                  SSSSS00n
UR                  U
5        [!        UR                  UR                  S5      nU R#                  XX5      nU=R                  [        U5      -  sl        UR                  [        UR                  5      :  a  M  [        US5      =U l        nU(       a  GM-  gg)zCExecute write commands with OP_MSG and w=0 writeConcern, unordered.NrB   FwriteConcernr   r   )r@   rW   r   rX   r   r(   rM   r   r   r}   r\   r;   r   r   rC   r   r   r   )rP   r   r   r   rX   r   r   r   r   r   rp   rC   r   s                rR   execute_op_msg_no_results_Bulk.execute_op_msg_no_results^  sZ   //**//))00++	
#ID -H%%--	C ..3sww</oo22u"S!H
 ##C(SWWcnnd;33CcJ#g,. ..3sww</ &*)T%::Ds1 crU   c           
         / / SSSSS/ S.n[        5       n[        5       n U R                  UUSUUSUU5        g! [         a     gf = f)zAExecute write commands with OP_MSG and w=0 WriteConcern, ordered.r   r   NF)r-   r(   r   r   )rP   r   r   r   r   initial_write_concernr   s          rR   execute_command_no_results _Bulk.execute_command_no_results  ss     "$	
 !-
	!!%	   		s   : 
AAc                   U R                   (       a  [        S5      eU R                  (       a  [        S5      eU=(       a    UR                  (       + nU(       a,  U R                  (       a  UR
                  S:  a  [        S5      eU(       a,  U R                  (       a  UR
                  S:  a  [        S5      eU R                  (       a  [        S5      eU R                  (       a  U R                  XU5      $ U R                  X5      $ )z3Execute all operations, returning no results (w=0).z3Collation is unsupported for unacknowledged writes.z6arrayFilters is unsupported for unacknowledged writes.	   zPMust be connected to MongoDB 4.4+ to use hint on unacknowledged delete commands.   zPMust be connected to MongoDB 4.2+ to use hint on unacknowledged update commands.zGCannot set bypass_document_validation with unacknowledged write concern)rF   r   rG   r   rI   max_wire_versionrH   rE   r   rB   r  r  )rP   r   r   r   unacks        rR   execute_no_results_Bulk.execute_no_results  s     $%Z[[""$%]^^@m&@&@"@T**t/D/Dq/H$b  T**t/D/Dq/H$b  "Y  <<224MRR--d>>rU   c                <   U R                   (       d  [        S5      eU R                  (       a  [        S5      eSU l        U=(       d    U R                  R                  n[        X!5      nU R                  (       a  U R                  5       nOU R                  5       nU R                  R                  R                  nUR                  (       d.  UR                  X#5       nU R                  XdU5         SSS5        gU R                  XAX#5      $ ! , (       d  f       g= f)zExecute operations.zNo operations to executez*Bulk operations can only be executed once.TN)rC   r   rD   r@   r   r+   rB   r   r   rW   rX   r   _conn_for_writesr  r	  )rP   r   r   r   r   rX   
connections          rR   execute_Bulk.execute  s     xx"#=>>=="#OPP%F)F)F1'I<<((*I**,I))00))((<
''
}M =< ''	'UU	 =<s   D
D)rE   r@   rA   rM   rD   rO   rJ   r<   rN   rC   rB   rK   rL   rG   rF   rI   rH   )NN)r@   zCollection[_DocumentType]rB   r  rQ   r  rA   zOptional[str]r<   zOptional[Any]r  r  )r  zType[_BulkWriteContext])r_   r1   r  r  )FFNNN)rm   Mapping[str, Any]rn   z#Union[Mapping[str, Any], _Pipeline]rh   r  ri   r  rj   Optional[Mapping[str, Any]]ro   z!Optional[list[Mapping[str, Any]]]rl    Union[str, dict[str, Any], None]r  r  )FNN)rm   r  rt   r  ri   r  rj   r   rl   r!  r  r  )
rm   r  rx   intrj   r   rl   r!  r  r  )r  zIterator[Optional[_Run]])r  zIterator[_Run])r   r$   rp   MutableMapping[str, Any]r   r"  r   bytesr   list[Mapping[str, Any]]rX   r/   r  dict[str, Any])r   r$   rp   r#  r   r"  r   r$  r   r"  r   r%  rX   r/   r  r   )
r   4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rp   r&  rC   r%  rX   r/   r  r%  )
r   r'  rp   r&  rC   r%  rX   r/   r  z.tuple[dict[str, Any], list[Mapping[str, Any]]]r   )r   Iterator[Any]r   r-   r   r  r   r0   r   r"  r   r  r   r#  r   zOptional[WriteConcern]r  r  )
r   r(  r   r-   r   r  r   strr  r&  )r   r0   r   r(  r  r  )r   r0   r   r(  r   r-   r  r  )r   r-   r   r  r   r)  r  r   )__name__
__module____qualname____firstlineno____doc__rS   propertyr\   rc   rq   ru   ry   r   r   r,   r   r   r   r   r   r	  r  r  r  r  __static_attributes__ rU   rR   r5   r5   S   s   1 "&! "- "  " %)	 "
  "  " 
 "D % %- 15;?15(#( 4( 	(
 ( /( 9( /( 
(B 1515(#( '( 	(
 /( /( 
(. 2615(#( ( /	(
 /( 
((
 SS &S 	S
 S &S S 
S SjVV &V 	V
 V V &V V 
%VpA  %	
  
!6A  %	
  
8> 7;e3 e3 $e3 )	e3
 e3 e3 e3 .e3 4e3 
e3N. . $. )	.
 . 
.`#;J"" !" $	"
 
"H?? !? $	?
 
?>V#V )V 	V
 
VrU   r5   )Lr.  
__future__r   r   r   r   collections.abcr   	itertoolsr   typingr   r   r   r	   r
   r   r   bson.objectidr   bson.raw_bsonr   pymongor   r   pymongo.bulk_sharedr   r   r   r   r   pymongo.commonr   r   r   pymongo.errorsr   r   r   r   pymongo.helpers_sharedr   pymongo.loggerr   r   r    pymongo.messager!   r"   r#   r$   r%   r&   r'   r(   pymongo.read_preferencesr)   "pymongo.synchronous.client_sessionr*   r+   pymongo.synchronous.helpersr,   pymongo.write_concernr-   pymongo.synchronous.collectionr.    pymongo.synchronous.mongo_clientr/   pymongo.synchronous.poolr0   pymongo.typingsr1   r2   r3   _IS_SYNCr5   r1  rU   rR   <module>rH     s    #    *    # ) !  
  : M M	 	 	 4 ] 6 .9<3FFM
V M
VrU   