fRUdZddlmZddlZddlmZddlmZddlm Z m Z m Z m Z m Z mZmZmZddlmZddlmZdd lmZmZdd lmZmZdd lmZmZmZdd lm Z m!Z!m"Z"m#Z#dd l$m%Z%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-ddl.m/Z/ddl0m1Z1e rddl2m3Z3ddl4m5Z5ddl6m7Z7m8Z8m9Z9dZ:de;d<dZde;d<dZ?de;d<dZ@de;d <Gd!d"ZA d'd#ZBd(d$ZCGd%d&ZDy))z?G   # * *8 4  $ 2 $  #x*'< < #  #x / #K F;$77 ::m,LC((*Kg,'C#&99S>K # K   & - -k :  v C ()005 rBc|drM|djd|dd}|d}|d}|dk(r |jdrd }t|||t|) z5Raise a BulkWriteError from the full bulk api result.rZc |dS)NrErQ)errors r@z)_raise_bulk_write_error..s%.rB)keyrcodeerrmsgzTransaction numberszrThis MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.)sort startswithrr)rberrrqmsgrrs r@_raise_bulk_write_errorrxs}=!M"'',H'I-(+6{(m 2:#..)>?-  #64= =  %%rBcpeZdZdZ d ddZeddZddZ d ddZ d ddZ d ddZ dd Z dd Z d dd Z dd Zdd Z d dZ d dZ d!dZy)"_Bulkz'The private guts of the bulk write API.Nc|j|jjdt|_||_|j t jd|j ||_||_ g|_ d|_ ||_ d|_ d|_d|_d|_d|_d|_d|_d|_d|_y)zInitialize a _Bulk instance.replace)unicode_decode_error_handlerdocument_class) codec_optionsNletFT) with_optionsr_replacedict collectionrrrcommentorderedr=executedbypass_doc_valuses_collationuses_array_filtersuses_hint_updateuses_hint_delete is_retryableretryingstarted_retryable_write current_runnext_run)r?rrbypass_document_validationrrs r@rAz_Bulk.__init__s%11$22;;-6t<2   88   , ,UDHH =&-  8: 8#"' % %  ',$ rBc|jjjj}|r|jst St SN)rdatabaseclient _encrypter_bypass_auto_encryptionr!r )r? encrypters r@bulk_ctx_classz_Bulk.bulk_ctx_classs4OO,,33>> Y>>- -$ $rBctd|t|tsd|vs t|d<|jj t |fy)z*Add an insert document to the list of ops.document_idN)r isinstancerrr=rGr)r?rs r@ add_insertz_Bulk.add_inserts=!*h78_5(9J&jHUO (+,rBct|td|fd|fd|fd|fg}| d|_||d<| d|_||d<| d|_||d <|rd |_|j jt|fy) z8Create an update document and add it to the list of ops.qumultiupsertNT collation arrayFiltershintF) rrrrrrr=rGr) r?selectorr5rrr array_filtersrcmds r@ add_updatez_Bulk.add_updates v&"8_sFmgu-=&?Q R   "&D (C   $&*D #"/C   $(D !CK  %D  #'rBct|||d|d}| d|_||d<| d|_||d<|jj t |fy)z8Create a replace document and add it to the list of ops.F)rrrrNTrr)rrrr=rGr)r?rrirrrrs r@ add_replacez_Bulk.add_replaces] ,;&Q  "&D (C   $(D !CK #'rBc||d}| d|_||d<| d|_||d<|tk(rd|_|jj t |fy)z7Create a delete document and add it to the list of ops.)rlimitNTrrF)rrr+rr=rGr)r?rrrrrs r@ add_deletez_Bulk.add_delete sau-  "&D (C   $(D !CK K  %D  #'rBc#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) enumerater=r9r;rJ)r?rarDr;rIs r@ gen_orderedz_Bulk.gen_ordered sg)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)r9rrrrr=rJ)r? operationsrDr;rIras r@ gen_unorderedz_Bulk.gen_unordered.sj7mT']DMB )2488)< %C%'9 w  # #C 3*=Cww s A3A=6A=c |jjj} |jjj} | j} |j st ||_d|_|j } |j| |d} | r7|jst |d|_|jd} t| j}|j| |||| || j|jj}| jt| j krv| r+t| j | jz dk(r|xs|}||jjd|j"i}|j$r|j$|d<t'j(|||j*rd|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 |vrnU|jU||| }| xjt|z c_| jt| j krv|j"r|d ry|jx|_} | r6yy) NFTr,rrbypassDocumentValidationrwriteConcernErrorrqrrZ)+rrnamer_event_listenersrnextrvalidate_sessionrr7r;rrr>r^r=rrrapply_write_concernrrrrr_start_retryable_write _apply_tor#PRIMARYsend_cluster_timeadd_server_api apply_timeoutr acknowledgedexecuter]rr`deepcopyrkrx execute_unack)r? generator write_concernsessionconnop_id retryablerbfinal_write_concerndb_namer listenersralast_runcmd_namebwcrr=rdto_sendrjfulls r@_execute_commandz_Bulk._execute_command:s,//**//))00++ #ID  DM fg.== $Y 5 ==(#H -H%% -- C..3sww</SWW!>1 D$7$H=M!5!5y$,,O<<%)\\C N))#}=&&6:C2388'CKKGW;M,M!%CJ!)E)E6687;4%%c9n6L6LdS&&sGV<##C(""63/SWWcnnd;!--&)kk#sF&COFG!**%8"=Cwwvq)-CC $}}[9&sD#..&I/5"3 S^^VL%*DM38D0|| (?!//S&AG#g,.g..3sww</n|| M :%)]] 2D sYrBc  ggdddddgdt dfd }jjj}|j j |||dsdr t S)zExecute using write commands.rrZr\rTrWrXrYrUrVc 4j|||yr)r)rrrrbrrr?rs r@retryable_bulkz-_Bulk.execute_command..retryable_bulks(  ! ! rB)bulk operation_idrZr\)rOptional[ClientSession]rr&rboolrKrL)r"rrr_retryable_writerrx) r?rrrrIrrrbrs ``` @@r@execute_commandz_Bulk.execute_commands"$    , 4> KO    ))00         } %5I)J #K 0rBc T|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.NrF writeConcernwr)rrrrrr"rrr7r;rrr>r^r=rrr) r?rrrrrrrarrrr=rs r@execute_op_msg_no_resultsz_Bulk.execute_op_msg_no_resultss\//**//))00++  #ID  -H%% -- C..3sww</doo22u"S!H ##C(SWWcnnd;++Cf=#g,...3sww</&*)T%: :D s1rBc ggdddddgd}t}t} |j||d||d||y#t$rYywxYw)zAExecute write commands with OP_MSG and w=0 WriteConcern, ordered.rrNF)r$r"rr)r?rrrrbinitial_write_concernrs r@execute_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|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.r0zPMust be connected to MongoDB 4.2+ to use hint on unacknowledged update commands.zGCannot set bypass_document_validation with unacknowledged write concern) rrrrrmax_wire_versionrrrrrr)r?rrrunacks r@execute_no_resultsz_Bulk.execute_no_resultss   $%Z[ [  " "$%]^ ^@m&@&@"@ T**t/D/Dq/H$b  T**t/D/Dq/H$b    "Y  <<224MR R--dI>>rBc |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)r=rrrrrrrrrrr_conn_for_writesrr)r?rrrIrr connections r@rz _Bulk.execute9sxx"#=> > =="#OP P %F)F)F 1'=I <<((*I**,I))00))(()<  '' I}M  '' ='9U U   s C99D)NN) rzCollection[_DocumentType]rrrrrz Optional[str]rz Optional[Any]rKrL)rKzType[_BulkWriteContext])rr'rKrL)FFNNN)rMapping[str, Any]r5z#Union[Mapping[str, Any], _Pipeline]rrrrrOptional[Mapping[str, Any]]rz!Optional[list[Mapping[str, Any]]]r Union[str, dict[str, Any], None]rKrL)FNN) rrrirrrrrrrrKrL) rrrr*rrrrrKrL)rKzIterator[Optional[_Run]])rKzIterator[_Run]r)r Iterator[Any]rr$rrrr&rr*rrrbMutableMapping[str, Any]rzOptional[WriteConcern]rKrL) rrrr$rrrIstrrKzdict[str, Any])rr&rrrKrL)rr&rrrr$rKrL)rr$rrrIrrKr)rMrNrOrPrApropertyrrrrrrrrrrrrrrQrBr@rzrzs1"&! -%)      B%%-15;?15(#(4( (  ( / (9(/( (B1515 (#('( ( / ( / ( (.2615 (#((/ ( / (  ((  *7;e3 e3$e3) e3  e3  e3e3.e34e3 e3N. .$.) .  .  .`#;J""!"$ "  "H??!?$ ?  ?>V#V)V V VrBrz) rar9rbrrcr*rdrrKrL)rbr'rKr )ErP __future__rr`collections.abcr itertoolsrtypingrrrr r r r r bson.objectidr bson.raw_bsonrpymongorrpymongo.client_sessionrrpymongo.commonrrrpymongo.errorsrrrrpymongo.helpersrrpymongo.messagerrrr r!r"pymongo.read_preferencesr#pymongo.write_concernr$pymongo.collectionr% pymongo.poolr&pymongo.typingsr'r(r)r+__annotations__r-r/r1r3r7r9rkrxrzrQrBr@r s# *   #)!Q  A4.-'FF S S Cc"@ @##8)6 )6))6 )6  )6  )6X&$kVkVrB