+\hqdZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZddlmZddlmZdd lmZmZdd lmZmZmZmZmZdd lm Z m!Z!m"Z"dd l#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)ddl*m+Z+m,Z,m-Z-ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6ddl7m8Z8ddl9m:Z:m;Z;ddlm?Z?e rddl@mAZAddlBmCZCddlDmEZEddlFmGZGmHZHmIZIdZJGddZKy)z> Y>> $D - - %D $ $rTctd|t|tsd|vs t|d<|jj t |fy)z*Add an insert document to the list of ops.document_idN)r isinstancerr rBappendr")rPr]s rR add_insertz_Bulk.add_inserts=!*h78_5(9J&jHUO (+,rTc t||||d} ||| d<| d|_|| d<| d|_|| d<| d|_|| d<| d|_|| d<|rd |_|j jt| fy) z8Create an update document and add it to the list of ops.)qumultiNupsertT collation arrayFiltershintsortF) rrErFrGrIrJrBr`r#) rPselectorupdatererfrg array_filtersrirjcmds rR add_updatez_Bulk.add_updates v&$,6EJ  "CM  "&D (C   $&*D #"/C   $(D !CK  !DNCK  %D  #'rTct|||d}|||d<| d|_||d<| d|_||d<| d|_||d<|jj t |fy)z8Create a replace document and add it to the list of ops.)rcrdNrfTrgrirj)rrErGrIrBr`r#)rPrk replacementrfrgrirjrns rR add_replacez_Bulk.add_replaces~ ,$,;?  "CM  "&D (C   $(D !CK  !DNCK #'rTc||d}| d|_||d<| d|_||d<|tk(rd|_|jj t |fy)z7Create a delete document and add it to the list of ops.)rclimitNTrgriF)rErHrrJrBr`r!)rPrkrtrgrirns rR add_deletez_Bulk.add_deletesa%-u=  "&D (C   $(D !CK K  %D  #'rTc#Kd}t|jD]F\}\}}| t|}n|j|k7r|t|}|j ||H|yw)ziGenerate batches of operations, batched by type of operation, in the order **provided**. N) enumeraterBrop_typeadd)rPrunidxrx operations rR gen_orderedz_Bulk.gen_orderedsl)2488)< $ %C%'9{7m' 7m GGC #  $ sA&A(c#Kttttttg}t |j D]\}\}}||j |||D]}|j s|yw)zbGenerate batches of operations, batched by type of operation, in arbitrary order. N)rr"r#r!rwrBry)rP operationsr{rxr|rzs rR gen_unorderedz_Bulk.gen_unorderedst7mT']DMB )2488)< 4 %C%'9 w  # #C 3 4 Cww  s A3A=6A=c|||j<tjtjrt tt j|jj|tt||j|||jj|jj|jj d|jj d|jj" |j$r|j'||| |jj)|||j*}t,j,j/|j0z }tjtjrt tt j2|jj||tt||j|||jj|jj|jj d|jj d|jj"|j$r|j5||||j7||j8|S#t:$r} t,j,j/|j0z }t=| t>t@fr | jB} n tE| } tjtjrt tt jF|jj|| tt||j|||jj|jj|jj d|jj d|jj"t=| t@|j$r|jI|| |t=| t>t@fr&|j7| jB|j8d} ~ wwxYw)zCA proxy for SocketInfo.write_command that handles event publishing.r messageclientIdcommand commandName databaseName requestId operationIddriverConnectionIdserverConnectionId serverHost serverPort serviceId rr durationMSreplyrrrrrrrrrrrrfailurerrrrrrrrrisServerSideErrorN)%fieldr isEnabledForloggingDEBUGr rSTARTED_topology_settings _topology_idnextiterdb_nameconnidserver_connection_idaddress service_idpublish_start write_commandcodecdatetimenow start_time SUCCEEDED_succeed_process_responsesession Exceptionr_rrdetailsr%FAILED_fail) rPbwcrn request_idmsgdocsrWrdurationexcrs rRrz_Bulk.write_commandsCII  ' ' 6 -5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((--  ;; JJsJ -5 HH**:sCIIFE((,,.?H++GMM:#1;;#66CC' $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11 {{ Z9  $ $UCKK 8@ ? ((,,.?H#1ABC(+ ,S1++GMM:#188#66CC'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G${{ *gx8#1ABC((ckkB = sE5J Q F,QQ cdtjtjrt tt j |jj|tt||j|||jj|jj|jjd|jjd|jj  |j"r|j%|||} |jj'||}t(j(j+|j,z } |t/|j0||} nddi} tjtjrt tt j2|jj| | tt||j|||jj|jj|jjd|jjd|jj |j"r|j5|| | |S#t6$r} t(j(j+|j,z } t9| t:r"t/|j0|| j<} n(t9| t>r | j<} n tA| } tjtjrt tt jB|jj| | tt||j|||jj|jj|jjd|jjd|jj t9| t:|j"r!|j,J|jE|| | d} ~ wwxYw)zAA proxy for Connection.unack_write that handles event publishing.rrrNokrr)#rrrrr rrrrrrrrrrrrrr unack_writerrrr&namerrrr_rrrr%rr) rPrrnrr max_doc_sizerrWresultrrrrs rRrz_Bulk.unack_writeIs  ' ' 6 -5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((--  ;;**S*d3C8 XX))#|' 5 ? ?!'!:!:!G!G#+#$(cO%([[",$.+.88;;+.88+H+H#&88#3#3A#6#&88#3#3A#6"%(("5"5 {{ Z9> = ((,,.?H#/0(=chhS[[(YC1++,S1++GMM:#188#66CC'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G"{{~~111 *gx8 ; sE,I66 P/F*P**P/c $|jrV|j||\}}}|jj|j|t d|j ||S|j||\}} }|j|||| d|||S)Nr)w) write_concernrrW)rO batch_commandrrrr-rr) rPrrnrBrW_ batched_cmdto_sendrrs rR_execute_batch_unackz_Bulk._execute_batch_unacks   &)&7&7S&A #A{G HH   *Q/    (+'8'8c'B $JW   S#z37F KrTc*|jrX|j||\}}}|jj|j||j |j |}||fS|j||\} } }|j||| | ||}||fS)N)r:rrW)rOrrrrrrr) rPrrnrBrWrrrrrrs rR_execute_batchz_Bulk._execute_batchs   &)&7&7S&A #A{GXX%% !ii  &Fw(+'8'8c'B $JW''S*c7FSFwrTc |jjj} |jjj} | j} |j st ||_d|_|j } |j| |d} | rC|jst |d|_|jd} t| j}|j| |||| || j|jj}| jt| j kr| r+t| j | jz dk(r|xs|}||jjd|j"i}|j$r|j$|d<t'j(|||j*|j*|d<|j,'| jt.t0fvr|j,|d<|rG|r#|j2s|j5d|_|j7||t8j:||j=||| |j?||jA| |tC| j | jd}|jDr|jG|||| \}}|jIdi}|jId d tJvr8tMjN|}tQ| || j|tS|tQ| || j|d|_ d|_|j"rd |vrnV|jU|||| }| xjt|z c_| jt| j kr|j"r|d ry|jx|_} | rByy) NFTrrAr@bypassDocumentValidationr;writeConcernErrorcoder writeErrors)+r?rVrrW_event_listenersrMrrNvalidate_sessionrKrrxr[r: idx_offsetlenrBrAr@rapply_write_concernrDr;r!r#rL_start_retryable_write _apply_tor)PRIMARYsend_cluster_timeadd_server_api apply_timeoutr acknowledgedrgetrcopydeepcopyrrr)rP generatorrrrop_id retryable full_resultfinal_write_concernrrW listenersrzlast_runcmd_namerrnrBrrwcefulls rR_execute_commandz_Bulk._execute_commands;//**//))00++ #ID  DM fg.== $Y 5 ==(#H -H%% -- C..3sww</SWW!>1 D$7$H=M!5!5y$,,O<<%)\\C N))#}=&&26:6I6IC2388'CKKGW;M,M!%CJ!)E)E6687;4%%c9n6L6LdS&&sGV<##C(""63/SWWcnnd;!--&*&9&9#sC&POFG!**%8"=Cwwvq)-CC $}}[9&sD#..&I/5"3 S^^VL%*DM38D0|| (?"77S#vNG#g,.g..3sww</n|| M :%)]] 2D sYrTc   ggdddddgdt d fd }jjj}|j j ||| }dsdr t S)zExecute using write commands.rrwriteConcernErrors nInserted nUpsertednMatched nModifiednRemovedupsertedc 4j|||yN)r)rrrrrrrPrs rRretryable_bulkz-_Bulk.execute_command..retryable_bulkNs(  ! ! rT)bulk operation_idrr)rOptional[ClientSession]rr0rboolreturnNone)r(r?rVrW_retryable_writerJr) rPrrrr|rrWrrrs ``` @@rRexecute_commandz_Bulk.execute_command9s"$    , 4> KO    ))00  # #       $  } %5I)J #K 0rTc V|jjj}|jjj}|j}t }|j st||_|j }|r(t|j}|j|||||d|j|jj} |jt|jkr||jjdddddii} |j| t!|j|jd} |j#| | | |} |xjt| z c_ |jt|jkrt|dx|_}|r'yy)zCExecute write commands with OP_MSG and w=0 writeConcern, unordered.NrAF writeConcernrr)r?rVrrWrr(rMrrrxr[r:rrrBrrr) rPrrrrWrrrzrrrnrBrs rRexecute_op_msg_no_resultsz_Bulk.execute_op_msg_no_resultsis^//**//))00++  #ID  -H%% -- C..3sww</doo22u"S!H ##C(SWWcnnd;33Cc6J#g,...3sww</&*)T%: :D s1rTc ggdddddgd}t}t} |j||d||d||y#t$rYywxYw)zAExecute write commands with OP_MSG and w=0 WriteConcern, ordered.rrNF)r-r(rr)rPrrrrinitial_write_concernrs rRexecute_command_no_resultsz _Bulk.execute_command_no_resultsss"$  !-    ! !%     s: AAc|jr td|jr td|xr |j }|r&|jr|j dkr td|r&|j r|j dkr td|r&|jr|j dkr td|jr td |jr|j|||S|j||S) 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.zPMust be connected to MongoDB 8.0+ to use sort on unacknowledged update commands.zGCannot set bypass_document_validation with unacknowledged write concern) rErrFrrHmax_wire_versionrGrIrDrrArr)rPrrrunacks rRexecute_no_resultsz_Bulk.execute_no_resultss   $%Z[ [  " "$%]^ ^@m&@&@"@ T**t/D/Dq/H$b  T**t/D/Dq/H$b  T^^(=(=(B$b    "Y  <<224MR R--dI>>rTc |js td|jr tdd|_|xs|jj}t ||}|j r|j}n|j}|jjj}|js0|j||5}|j||| dddy|j||||S#1swYyxYw)zExecute operations.zNo operations to executez*Bulk operations can only be executed once.TN)rBrrCr?rr+rAr}rrVrWr_conn_for_writesr r)rPrrr|rrW connections rRexecutez _Bulk.executesxx"#=> > =="#OP P %F)F)F 1'=I <<((*I**,I))00))(()<  '' I}M  '' ='9U U   s C99D)NN) r?zCollection[_DocumentType]rArrQOptional[bool]r@z Optional[str]r;z Optional[Any]rr)rzType[_BulkWriteContext])r]r1rr)NNNN)rkMapping[str, Any]rlz#Union[Mapping[str, Any], _Pipeline]rerrfrrgOptional[Mapping[str, Any]]rmz!Optional[list[Mapping[str, Any]]]ri Union[str, dict[str, Any], None]rjrrr)NNN)rkrrqrrfrrgrrirrjrrr) rkrrtintrgrrirrr)rzIterator[Optional[_Run]])rzIterator[_Run])rr$rnMutableMapping[str, Any]rrrbytesrlist[Mapping[str, Any]]rWr/rdict[str, Any])rr$rnrrrrrrrrrrWr/rr) r4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rnrrBrrWr/rr) rrrnrrBrrWr/rz.tuple[dict[str, Any], list[Mapping[str, Any]]]r)r Iterator[Any]rr-rrrr0rrrrrrrzOptional[WriteConcern]rr) rrrr-rrr|strrr)rr0rrrr)rr0rrrr-rr)rr-rrr|rrr)__name__ __module__ __qualname____doc__rSpropertyr[rarorrrur}rr,rrrrrrrrr r rTrRr5r5Ss1"&! !"-!"!"%3 !"  !"  !" !"F%%-26;?15,0(#(4( (  ( / (9(/(*( (L2615,0(#('( ( / ( / (*( (:2615 (#((/ ( / (  ((  S S&S S  S & SS SSjV V&V V  V  V&VV %Vp A%    ! 6 A%    8 >7;e3 e3$e3) e3  e3  e3e3.e34e3 e3N. .$.) .  .  .`#;J""!"$ "  "H!?!?!!?$ !?  !?FV#V)V V VrTr5)Lr __future__rrrrcollections.abcr itertoolsrtypingrrrr r r r bson.objectidr bson.raw_bsonrpymongorrpymongo.bulk_sharedrrrrrpymongo.commonrrrpymongo.errorsrrrrpymongo.helpers_sharedrpymongo.loggerrrr 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.poolr0pymongo.typingsr1r2r3_IS_SYNCr5rrTrRr6s# *#)!  :MM   4]6.9<3FF \ V\ VrT