+\hvdZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZddlmZddlmZdd lmZmZdd lmZmZdd lmZdd lmZdd lm Z ddl!m"Z"e r ddl#m$Z$ddl%m&Z&ddl'm(Z(m)Z)ddl*m+Z+m,Z,m-Z-ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4ddl5m6Z6ddl7m8Z8m9Z9m:Z:ddl;mZ>m?Z?m@Z@ddlAmBZBddlCmDZDmEZEmFZFmGZGddlHmIZImJZJddlKmLZLdZMGddZNy)zIThe client-level bulk write operations interface. .. versionadded:: 4.9 ) annotationsN)MutableMapping)islice) TYPE_CHECKINGAnyMappingOptionalTypeUnion)ObjectId)RawBSONDocument)_csotcommon) ClientSession_validate_session_write_concern) Collection) CommandCursor)Database)_handle_reauth) MongoClient) Connection)_merge_command"_throw_client_bulk_write_exception)validate_is_document_typevalidate_ok_for_replacevalidate_ok_for_update)ConfigurationErrorConnectionFailureInvalidOperationNotPrimaryErrorOperationFailureWaitQueueTimeoutError)_RETRYABLE_ERROR_CODES)_COMMAND_LOGGER_CommandStatusMessage _debug_log)_ClientBulkWriteContext_convert_client_bulk_exception_convert_exception_convert_write_result_randint)ReadPreference)ClientBulkWriteResult DeleteResultInsertOneResult UpdateResult) _DocumentOut _Pipeline) WriteConcernTc eZdZdZ d ddZeddZddZ d ddZ d ddZ d ddZ e dd Z dd Z dd Z d d Z d!d Z d" d#dZ d$dZ d%dZ d%dZ d&dZy)' _ClientBulkz4The private guts of the client-level bulk write API.Nc||_||_||_|j tjd|j||_||_||_||_g|_ g|_ d|_ d|_ d|_ d|_d|_|jj j"|_d|_d|_y)z"Initialize a _ClientBulk instance.NletrF)client write_concernr7rrorderedbypass_doc_valcommentverbose_resultsops namespaces idx_offset total_opsexecuteduses_collationuses_array_filtersoptions retry_writes is_retryableretryingstarted_retryable_write)selfr8r9r:bypass_document_validationr<r7r=s b/root/niggaflix-v3/playground/venv/lib/python3.12/site-packages/pymongo/synchronous/client_bulk.py__init__z_ClientBulk.__init__Ys * 88   , ,UDHH = 8 .8:%'  #"' KK//<< ',$ctSN)r')rJs rLbulk_ctx_classz_ClientBulk.bulk_ctx_classxs&&rNctd|t|tsd|vs t|d<d|d}|jj d|f|j j ||xjdz c_y)z*Add an insert document to the list of ops.document_id)insertrSrVN)r isinstancer r r>appendr?rA)rJ namespacerScmds rL add_insertz_ClientBulk.add_insert|sf!*h78_5(9J&jHUO2 3( y) !rNc 6t|d|||d} ||| d<| d|_|| d<||| d<| d|_|| d<| | | d <|rd |_|jj d | f|j j ||xjd z c_y) z8Create an update document and add it to the list of ops.rUupdatefilter updateModsmultiNupsertT arrayFiltershint collationsortFr_rW)rrDrCrGr>rYr?rA) rJrZselectorr_rbrcrf array_filtersrergr[s rL add_updatez_ClientBulk.add_updates v&     "CM  $&*D #"/C   CK  "&D (C   CK  %D  3( y) !rNct|d||dd}|||d<|||d<| d|_||d<|||d <|jjd |f|jj||xj d z c_y) z8Create a replace document and add it to the list of ops.rUFr^NrcreTrfrgreplacerW)rrCr>rYr?rA) rJrZrh replacementrcrfrergr[s rL add_replacez_ClientBulk.add_replaces ,%    "CM  CK  "&D (C   CK C() y) !rNcd||d}|||d<| d|_||d<|rd|_|jjd|f|jj||xj d z c_y) z7Create a delete document and add it to the list of ops.rU)deleter`rbNreTrfFrprW)rCrGr>rYr?rA)rJrZrhrbrfrer[s rL add_deletez_ClientBulk.add_deletesvx%@  CK  "&D (C   %D  3( y) !rNc ||d<||d<tjtjrt tt j |jj|tt||j|||jj|jj|jjd|jjd|jj  |j"r|j%|||| |jj'|||j(}t*j*j-|j.z } tjtjrt tt j0|jj| |tt||j|||jj|jj|jjd|jjd|jj |j"r|j3||| |j4j7||j8|S#t:$r} t*j*j-|j.z } t=| t>t@fr | jB} n tE| } tjtjrt tt jF|jj| | tt||j|||jj|jj|jjd|jjd|jj t=| t@|j"r|jI|| | d| i}t=| t@r1|j4j7| jB|j8n&|j4j7i|j8Yd } ~ |Sd } ~ wwxYw) zCA proxy for Connection.write_command that handles event publishing.r>nsInforrW messageclientIdcommand commandName databaseName requestId operationIddriverConnectionIdserverConnectionId serverHost serverPort serviceId rurv durationMSreplyrxryrzr{r|r}r~rrrurvrfailurerxryrzr{r|r}r~rrisServerSideErrorerrorN)%r$ isEnabledForloggingDEBUGr&r%STARTED_topology_settings _topology_idnextiterdb_nameconnidserver_connection_idaddress service_idpublish_start write_commandcodecdatetimenow start_time SUCCEEDED_succeedr8_process_responsesession ExceptionrXr r!detailsr)FAILED_fail) rJbwcr[ request_idmsgop_docsns_docsr8rdurationexcrs rLrz_ClientBulk.write_commands?E H  ' ' 6 -5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((--  ;; JJsJ 99 ?HH**:sCIIFE((,,.?H++GMM:#1;;#66CC' $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11 {{ Z9 KK ) )% =F E! ?((,,.?H#1ABC(+ ,S1++GMM:#188#66CC'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G${{ *gx8cNE#/0 --ckk3;;G --b#++> E! ?sE?J RGQ>>Rctjtjrt tt j |jj|tt||j|||jj|jj|jjd|jjd|jj  |j"r|j%||||} |jj'||j(}t*j*j-|j.z } |t1|j2||} nddi} tjtjrt tt j4|jj| | tt||j|||jj|jj|jjd|jjd|jj |j"r|j7|| | | S#t8$r} t*j*j-|j.z } t;| t<r"t1|j2|| j>} n(t;| t@r | j>} n tC| } tjtjrt tt jD|jj| | tt||j|||jj|jj|jjd|jjd|jj t;| t<|j"r!|j.J|jG|| | d| i} Yd} ~ | Sd} ~ wwxYw) zAA proxy for Connection.unack_write that handles event publishing.rrWrtNokrrr)$r$rrrr&r%rrrrrrrrrrrrr unack_write max_bson_sizerrrr*namerrrrXr!rr r)rr) rJrr[rrrrr8resultrrrrs rLrz_ClientBulk.unack_write?s#  ' ' 6 -5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((--  ;;**S*gw?C9 #XX))#s/@/@AF((,,.?H!-chhVDq "// >' 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 *gx8cNE ? #s E6J Q F-P>>Qc ||j|||\}}}}|j|||||||j||fS)z6Executes a batch of bulkWrite server commands (unack).) batch_commandrr8) rJrr[r>r?rr to_send_ops to_send_nss rL_execute_batch_unackz _ClientBulk._execute_batch_unacksJ473D3DS#z3Z0 Cj c:sKT[[YJ&&rNc ~|j|||\}}}}|j|||||||j} | ||fS)z4Executes a batch of bulkWrite server commands (ack).)rrr8) rJrr[r>r?rrrrrs rL_execute_batchz_ClientBulk._execute_batchsQ473D3DS#z3Z0 Cj##Cj#{JX\XcXcd{J..rNc|jdrtt|jdd}t ||d|j ||du|j }|j| |D]}|d|jz}|j|\} } |ds"|d j||jry|dsT|jsa| d k(r| d d } t| d } | dvrd} t|d d } | dk(r t|d }  || d|<yy#t $r4} |j"r|j%t'| |d<Yd} ~ yd} ~ wwxYw)z?Internal helper for processing the server reply command cursor.cursoradminz$cmd.bulkWrite)databaserN)rexplicit_sessionr<idxr writeErrorsrVrSrTT) acknowledged)r_rlr_)rin_client_bulkrpResultsr)getrrr8rrr<_maybe_pin_connectionr@r>rYr:r=r/r0r.ralivecloser()rJ full_resultrrrcoll cmd_cursordocoriginal_indexop_typeop inserted_idresrs rL_process_results_cursorz#_ClientBulk._process_results_cursors ::h !$++w7%D'x  !(!4 J  , ,T 2 F%OC%(Z$//%AN"&((>":KGRt9}-44S9<<"4yT%9%9"h.*,Z.*?K"1+D"QC"&;;&.G".sVZ"[C"h.".s"FCKN wiw$78H'O# J F##$$&"@"Ew  Fs,0AD"D" D"A D"" E+*EEc  d}d} |jj} |j|j||j|| ||| ||jj} |j |j kr_|j |j z | jkr|xs|}ddi} |j | d<|j| d<|xr |j } | s|stj| ||j|j| d<|jr|j| d<|jr|j| d <|rG|r#|j s|j#d |_|j%| |t&j(||j+| ||j|j-| |j/|j| t1|j2|j d}t1|j4|j d}|j6rJ|j9| | ||\}}}|}|j;d r|d }t=|d xr>t?|j@tBxr"|j@j;d dtDv}t?|tFxrt?|tHtJf }|rR|s|rNtMjN|}tQ|j2|j ||tS||jn8tQ|j2|j ||tS||jd|d <g|d<|j;ddtU|krd |d<|ds(||d <tQ|j2|j ||y|rw|j;di}|j;d dtDvrMtMjN|}tQ|j2|j ||tS||j|jW||||tQ|j2|j ||d|_,d|_n|j[| | ||\}}|xj tU|z c_|d s|jr|dry|j |j kr^yy)zr?rrrhasattrrXrdictr#rr r"copydeepcopyrrlenrrHr)rJr9rrop_id retryablerfinal_write_concernrcmd_name listenersrr[not_in_transactionr>r? raw_resultr_rrretryable_top_level_errorretryable_network_errorfullwces rL_execute_commandz_ClientBulk._execute_commandsKKK00  dkk73!!       KK % % oo.~~/33K3KK 3 D} "C$($8$8 8C !\\C N!(!G1G1G-G !))#}="".262E2E./||!%Ixx!XXE T%A%A22437D0!!#y.2H2H$O  " "3 =    $   t{{C 04??D9C$GJ))-1-@-@c3PZ-[* K#::g&"7OEy1S&u}}d;S!MM--fa8>37K0d|&0F7O"488T__k6R **%8"=Cwwvq)-CC $}}[9&txx$O:4AUAUV,,[&$Ptxx+vN!& /4,"&!:!:3S*!U Q OOs;/ /O7# ]9SIoo.rNc ddggdddddiiid t d fd }jjj|||ds dsd rt j S) z'Execute commands with w=1 WriteConcern.FNr) rrrwriteConcernErrors nInserted nUpsertednMatched nModifiednDeleted insertResults updateResults deleteResultscz|jdkr tdjj|||y)N.retryable_bulk|sK $$r)&R  ! !""  rN)bulk operation_idrrr)rOptional[ClientSession]rrrboolreturnNone)r+r8_retryable_writerGrr=)rJr operationrrrs` @@rLexecute_commandz_ClientBulk.execute_commandfs#"$ 1   ,     $ $$       %  w ;}#=MaAb .{Dr?rr) rJrrrrrrr[r>r?rrs rLexecute_command_unackz!_ClientBulk.execute_command_unacksP KK00  !!       KK % % oo."C $C "C N"".262E2E./#&(C ||!%Ixx!XXE    $4??D9C$GJ"66sCjQNK OOs;/ /O)oo.rNc|jr td|jr td|j t d|j |S)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)rCrrDr;r!r)rJrs rLexecute_no_resultsz_ClientBulk.execute_no_resultss]   $%Z[ [  " "$%]^ ^    *"Y ))$//rNc|js td|jr tdd|_t||j}|jj s^|j j||5}|jdkr td|j|tdddcdddS|j||}t||jj |jS#1swYFxYw)zExecute operations.zNo operations to executez*Bulk operations can only be executed once.TrrNF) r>rrBrr9rr8_conn_for_writesrrr-rr=)rJrr  connectionrs rLexecutez_ClientBulk.executes xx"#=> > =="#OP P 1'4;M;MN!!..--gyA AZ..3*V'' 3,T5%@  A A%%gy9$     + +     A As >8C==D)TNNNF)r8rr9r3r:r rKOptional[bool]r<z Optional[str]r7z Optional[Any]r=r r r )r zType[_ClientBulkWriteContext])rZstrrSr1r r )NNNNN)rZrrhMapping[str, Any]r_z#Union[Mapping[str, Any], _Pipeline]rbr rcrrfOptional[Mapping[str, Any]]riz!Optional[list[Mapping[str, Any]]]re Union[str, dict[str, Any], None]rgrr r )NNNN)rZrrhrrmrrcrrfrrerrgrr r )NN) rZrrhrrbr rfrrerr r )rr'r[MutableMapping[str, Any]rintrzUnion[bytes, dict[str, Any]]rlist[Mapping[str, Any]]rr r8rr dict[str, Any])rr'r[rrrrbytesrr rr r8rr r) rr'r[r!r>#list[tuple[str, Mapping[str, Any]]]r? list[str]r z7tuple[list[Mapping[str, Any]], list[Mapping[str, Any]]]) rr'r[r!r>r#r?r$r zGtuple[dict[str, Any], list[Mapping[str, Any]], list[Mapping[str, Any]]]) rrrrrrrrr r rP)r9r3rrrrrrrr rrrzOptional[WriteConcern]r r )rrr rr r)rrr r )rrr rr r)__name__ __module__ __qualname____doc__rMpropertyrQr\rjrnrqrrrrrrrrrrrrNrLr5r5Vs> 59!%! %--$- - %3 -  --- ->'' ""&15;?15,0%%$%4 %  %  %/%9%/%*% %X"&1515,0$'    / /* H2615 $  /  /  ,Y $Y&Y Y * Y ) Y)YY YYvW $W&W W  W ) W)WW %Wr ' $ ' '1 '  ' A ' / $ / /1 /  / Q /1F-1F)1F 1F ) 1F  1Fv7;A#A)A A  A  A.A4A AF3(33 " 3j(0(0 (0T00 0" (    rNr5)Or( __future__rrrrcollections.abcr itertoolsrtypingrrrr r r bson.objectidr bson.raw_bsonr pymongorr"pymongo.synchronous.client_sessionrrpymongo.synchronous.collectionr"pymongo.synchronous.command_cursorrpymongo.synchronous.databaserpymongo.synchronous.helpersr pymongo.synchronous.mongo_clientrpymongo.synchronous.poolrpymongo._client_bulk_sharedrrpymongo.commonrrrpymongo.errorsrrrr r!r"pymongo.helpers_sharedr#pymongo.loggerr$r%r&pymongo.messager'r(r)r*r+pymongo.read_preferencesr,pymongo.resultsr-r.r/r0pymongo.typingsr1r2pymongo.write_concernr3_IS_SYNCr5r*rNrLrDs# *#)!]5<16<3 :MM4 4. [  [  rN