
    l8'hp                       d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z, ddl-m.Z. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= ddl>m?Z? erddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZGmHZHmIZI dZJ G d d      ZKy)z<The bulk write operations interface.

.. versionadded:: 2.7
    )annotationsN)MutableMapping)islice)TYPE_CHECKINGAnyIteratorMappingOptionalTypeUnion)ObjectId)RawBSONDocument)_csotcommon)AsyncClientSession_validate_session_write_concern)_handle_reauth)	_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)WriteConcern)AsyncCollection)AsyncMongoClient)AsyncConnection)_DocumentOut_DocumentType	_PipelineFc                  
   e Zd ZdZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZedd       ZddZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ		 	 d	 	 	 	 	 	 	 	 	 ddZ
dd	Zdd
Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 d d       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!dZ	 	 	 	 	 	 	 	 	 	 d"dZ	 	 	 	 	 	 	 	 	 	 d#dZ	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%dZ	 	 	 	 	 	 	 	 	 	 d&dZ	 	 	 	 	 	 d'dZ	 	 	 	 	 	 	 	 d(dZ	 	 	 	 	 	 	 	 d(dZ	 	 	 	 	 	 	 	 d)dZy)*
_AsyncBulkz'The private guts of the bulk write API.Nc                   |j                  |j                  j                  dt                    | _        || _        | j
                   t        j                  d| j
                         || _        || _	        g | _
        d| _        || _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        y)z!Initialize a _AsyncBulk 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?   rA   bypass_document_validationr@   r;   s         U/var/www/html/trade_iq/venv/lib/python3.12/site-packages/pymongo/asynchronous/bulk.py__init__z_AsyncBulk.__init__V   s     %11$22;;-6t <  2 

 88,,UDHH=&-8:8#"' % % ',$!    c                    | j                   j                  j                  j                  }|r|j                  sd| _        t        S d| _        t        S )NTF)r?   databaseclient
_encrypter_bypass_auto_encryptionrN   r*   r'   )rO   	encrypters     rQ   bulk_ctx_classz_AsyncBulk.bulk_ctx_classx   sD    OO,,33>>	Y>> $D-- %D$$rS   c                    t        d|       t        |t              sd|v st               |d<   | j                  j                  t        |f       y)z*Add an insert document to the list of ops.document_idN)r   
isinstancer   r   rB   appendr%   )rO   r\   s     rQ   
add_insertz_AsyncBulk.add_insert   s=    !*h78_5(9J&jHUO(+,rS   c                    t        |       t        d|fd|fd|fd|fg      }|d| _        ||d<   |d| _        ||d<   |d| _        ||d	<   |rd
| _        | j                  j                  t        |f       y)z8Create an update document and add it to the list of ops.qumultiupsertNT	collationarrayFiltershintF)	r   r>   rE   rF   rG   rI   rB   r_   r&   )	rO   selectorupdaterd   re   rf   array_filtersrh   cmds	            rQ   
add_updatez_AsyncBulk.add_update   s     	v&"8_sFmgu-=&?QR
  "&D(C$&*D#"/C$(D!CK %D#'rS   c                    t        |       ||d|d}|d| _        ||d<   |d| _        ||d<   | j                  j	                  t
        |f       y)z8Create a replace document and add it to the list of ops.F)rb   rc   rd   re   NTrf   rh   )r   rE   rG   rB   r_   r&   )rO   ri   replacementre   rf   rh   rl   s          rQ   add_replacez_AsyncBulk.add_replace   s]     	 ,;&Q "&D(C$(D!CK#'rS   c                    ||d}|d| _         ||d<   |d| _        ||d<   |t        k(  rd| _        | j                  j                  t        |f       y)z7Create a delete document and add it to the list of ops.)rb   limitNTrf   rh   F)rE   rH   r   rI   rB   r_   r$   )rO   ri   rr   rf   rh   rl   s         rQ   
add_deletez_AsyncBulk.add_delete   sa     u- "&D(C$(D!CKK %D#'rS   c              #     K   d}t        | j                        D ]F  \  }\  }}|t        |      }n|j                  |k7  r| t        |      }|j	                  ||       H | yw)ziGenerate batches of operations, batched by type of
        operation, in the order **provided**.
        N)	enumeraterB   r   op_typeadd)rO   runidxrv   	operations        rQ   gen_orderedz_AsyncBulk.gen_ordered   sl      )2488)< 	$%C%'9{7m'	7mGGC#	$ 	s   A&A(c              #     K   t        t              t        t              t        t              g}t	        | j
                        D ]  \  }\  }}||   j                  ||        |D ]  }|j
                  s|  yw)zbGenerate batches of operations, batched by type of
        operation, in arbitrary order.
        N)r   r%   r&   r$   ru   rB   rw   )rO   
operationsry   rv   rz   rx   s         rQ   gen_unorderedz_AsyncBulk.gen_unordered   st      7mT']DMB
)2488)< 	4%C%'9w##C3	4  	Cww		s   A3A=6A=c                  K   |||j                   <   t        j                  t        j                        rt        t        |j                  j                  t        j                  |t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                   d   |j                  j                   d   |j                  j"                         |j$                  r|j'                  |||       	 |j                  j)                  |||j*                         d{   }t,        j,                  j/                         |j0                  z
  }t        j                  t        j                        rt        t        |j                  j                  t        j2                  ||t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                   d   |j                  j                   d   |j                  j"                         |j$                  r|j5                  |||       |j7                  ||j8                         d{    |S 7 ]7 	# t:        $ r}	t,        j,                  j/                         |j0                  z
  }t=        |	t>        t@        f      r|	jB                  }
ntE        |	      }
t        j                  t        j                        rt        t        |j                  j                  t        jF                  ||
t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                   d   |j                  j                   d   |j                  j"                  t=        |	t@                     |j$                  r|jI                  ||
|       t=        |	t>        t@        f      r/|j7                  |	jB                  |j8                         d{  7    d}	~	ww xY ww)zCA proxy for SocketInfo.write_command that handles event publishing.r      clientIdmessagecommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceIdN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)%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	Exceptionr^   r   r   detailsr(   FAILED_fail)rO   bwcrl   
request_idmsgdocsrV   r   durationexcr   s              rQ   r   z_AsyncBulk.write_command   s,     CII''622??-55 cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;JJsJ-5	((00S#))LLE((,,.?H++GMM:##66CC1;;' $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11  {{Z9**5#++>>>@ k M* ? 	((,,.?H#1AB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$ {{		*gx8#1ABC..s{{CKKHHH=	s\   DQ,*J% J EJ% J#J% Q, J% #J% %
Q)/F.Q$Q Q$$Q))Q,c                  K   t        j                  t        j                        rt	        t         |j
                  j                  t        j                  |t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                          |j"                  r|j%                  |||      }	 |j                  j'                  ||       d{   }t(        j(                  j+                         |j,                  z
  }	|t/        |j0                  ||      }
nddi}
t        j                  t        j                        rt	        t         |j
                  j                  t        j2                  |	|
t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                          |j"                  r|j5                  ||
|	       |S 7 W# t6        $ r}t(        j(                  j+                         |j,                  z
  }	t9        |t:              r"t/        |j0                  ||j<                        }n(t9        |t>              r|j<                  }ntA        |      }t        j                  t        j                        rt	        t         |j
                  j                  t        jB                  |	|t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                   t9        |t:                     |j"                  r!|j,                  J |jE                  |||	        d}~ww xY ww)zFA proxy for AsyncConnection.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   r^   r   r   r   r(   r   r   )rO   r   rl   r   r   max_doc_sizer   rV   resultr   r   r   r   s                rQ   r   z_AsyncBulk.unack_write>  s     ''622??-55 cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;**S*d3C8	88//\BBF((,,.?H!-chhVD q	"//>'!'!:!:!G!G 5 ? ?#+#$(cO%([[",$.+.88;;+.88+H+H#&88#3#3A#6#&88#3#3A#6"%(("5"5  {{Z9> q C4  	((,,.?H#/0(=chhS[[(YC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		*gx8;	s>   DP?
J )J *EJ >P? J 
P<F*P77P<<P?c           	     T  K   | j                   r^|j                  ||      \  }}}|j                  j                  |j                  |t        d      |j                  |       d {    |S |j                  ||      \  }}	}| j                  ||||	d||       d {    |S 7 =7 w)Nr   )w)write_concernr   rV   )rN   batch_commandr   r   r   r-   r   r   )
rO   r   rl   rB   rV   _batched_cmdto_sendr   r   s
             rQ   _execute_batch_unackz_AsyncBulk._execute_batch_unack  s      &)&7&7S&A#A{G((""*Q/ #      (+'8'8c'B$JW ""3Za&QQQ! Rs$   A$B(&B$'6B(B&B(&B(c                Z  K   | j                   r`|j                  ||      \  }}}|j                  j                  |j                  ||j
                  |j                  |       d {   }||fS |j                  ||      \  }	}
}| j                  |||	|
||       d {   }||fS 7 @7 
w)N)r:   r   rV   )rN   r   r   r   r   r   r   r   )rO   r   rl   rB   rV   r   r   r   r   r   r   s              rQ   _execute_batchz_AsyncBulk._execute_batch  s      &)&7&7S&A#A{G88++!ii ,  F w (+'8'8c'B$JW--c3
CRXYYFw Zs$   A$B+&B''7B+B)	B+)B+c	           
       K   | j                   j                  j                  }	| j                   j                  j                  }
|
j                  }| j
                  st        |      | _        d | _        | j
                  }|j                  |
|       d}|rI| j                  st        |d       | _        | j                  d}t        |j                     }| j                  |	||||||j                  | j                   j                        }|j                  t        |j                         k  r|r+t        |j                         |j                  z
  dk(  r|xs |}|| j                   j                  d| j"                  i}| j$                  r| j$                  |d<   t'        j(                  ||       | j*                  rd|d<   | j,                  '|j                  t.        t0        fv r| j,                  |d<   |rG|r#| j2                  s|j5                          d| _        |j7                  ||t8        j:                  |       |j=                  |||
       |j?                  |       |jA                  |
|       tC        |j                   |j                  d       }|jD                  r| jG                  ||||
       d {   \  }}|jI                  di       }|jI                  d	d
      tJ        v r8tM        jN                  |      }tQ        |||j                  |       tS        |       tQ        |||j                  |       d| _	        d| _        | j"                  r!d|v rn^| jU                  ||||
       d {   }|xj                  t        |      z  c_        |j                  t        |j                         k  r| j"                  r|d   ry | j                  x| _        }|rHy y 7 *7 tw)NFTr   rA   r@   bypassDocumentValidationr;   writeConcernErrorcoder   writeErrors)+r?   rU   r   rV   _event_listenersrL   r   rM   validate_sessionrJ   r   rv   rZ   r:   
idx_offsetlenrB   rA   r@   r   apply_write_concernrD   r;   r$   r&   rK   _start_retryable_write	_apply_tor,   PRIMARYsend_cluster_timeadd_server_apiapply_timeoutr   acknowledgedr   getr    copydeepcopyr   r   r   )rO   	generatorr   r   r   op_id	retryablefull_resultfinal_write_concernr   rV   	listenersrx   last_runcmd_namer   rl   rB   r   r   wcefulls                         rQ   _execute_commandz_AsyncBulk._execute_command  sJ     //**//))00++	#ID DM 	fg.== $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&&sGV<##C(""63/SWWcnnd; !--,0,?,?S#v,V&VOFG !**%8"=Cwwvq)-CC  $}}[9&sD#..&I/5"3S^^VL %*DM38D0||(?$($=$=c3V$TTG#g,.g ..3sww</n ||M :%)]]2DsY ^ 'W( Us2   J3O$5O6B7O$-O".AO$4'O$O$"O$c           	     &   	K   g g dddddg dt               		 	 	 	 	 	 	 	 d	 fd} j                  j                  j                  }|j	                   j
                  ||| 	       d{   }d   sd   rt               S 7 w)	zExecute using write commands.r   r   writeConcernErrors	nInserted	nUpsertednMatched	nModifiednRemovedupsertedc           	     P   K   j                  | ||       d {    y 7 wN)r   )r   r   r   r   r   r   rO   r   s      rQ   retryable_bulkz2_AsyncBulk.execute_command.<locals>.retryable_bulkC  s6      ''  s   &$&)bulkoperation_idNr   r   )r   Optional[AsyncClientSession]r   r0   r   boolreturnNone)r+   r?   rU   rV   _retryable_writerI   r   )
rO   r   r   r   rz   r   rV   r   r   r   s
   ```     @@rQ   execute_commandz_AsyncBulk.execute_command.  s      "$	
 
	1	9H	UY		 	 ))00)) * 
 
 }%5I)J#K0
s   A,B3B4Bc           
     r  K   | j                   j                  j                  }| j                   j                  j                  }|j                  }t               }| j                  st        |      | _        | j                  }|r0t        |j                     }| j                  |||||d|j                  | j                   j                        }	|j                  t        |j                        k  r|| j                   j                  dddddii}
|j                  |
       t!        |j                  |j                  d      }| j#                  |	|
||       d{   }|xj                  t        |      z  c_        |j                  t        |j                        k  rt        |d      x| _        }|r/yy7 ^w)zCExecute write commands with OP_MSG and w=0 writeConcern, unordered.NrA   FwriteConcernr   r   )r?   rU   r   rV   r   r+   rL   r   r   rv   rZ   r:   r   r   rB   r   r   r   )rO   r   r   r   rV   r   r   rx   r   r   rl   rB   r   s                rQ   execute_op_msg_no_resultsz$_AsyncBulk.execute_op_msg_no_results^  sm     //**//))00++	
#ID -H%%--	C ..3sww</doo22u"S!H
 ##C(SWWcnnd; $ 9 9#sC PP#g,. ..3sww</ &*)T%::Ds1 , Qs   EF7F5AF7F73F7c           
        K   g g dddddg d}t               }t               }	 | j                  ||d||d||       d{    y7 # t        $ r Y yw xY ww)zAExecute write commands with OP_MSG and w=0 WriteConcern, ordered.r   r   NF)r-   r+   r   r   )rO   r   r   r   r   initial_write_concernr   s          rQ   execute_command_no_resultsz%_AsyncBulk.execute_command_no_results  s      "$	
 !-
	''%	 	 	   		s8    AA AA AA 	AAAAc                  K   | j                   rt        d      | j                  rt        d      |xr |j                   }|r&| j                  r|j
                  dk  rt        d      |r&| j                  r|j
                  dk  rt        d      | j                  rt        d      | j                  r| j                  |||       d{   S | j                  ||       d{   S 7 7 w)	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 concernN)rE   r   rF   r   rH   max_wire_versionrG   rD   r   rA   r   r   )rO   r   r   r   unacks        rQ   execute_no_resultsz_AsyncBulk.execute_no_results  s      $%Z[[""$%]^^@m&@&@"@T**t/D/Dq/H$b  T**t/D/Dq/H$b  "Y  <<88y-XXX33D)DDD YDs$   C	C.C*C.%C,&C.,C.c                  K   | j                   st        d      | j                  rt        d      d| _        |xs | j                  j                  }t        ||      }| j                  r| j                         }n| j                         }| j                  j                  j                  }|j                  sP|j                  ||       d{   4 d{   }| j                  |||       d{    	 ddd      d{    y| j                  ||||       d{   S 7 V7 O7 67 '# 1 d{  7  sw Y   yxY w7 !w)zExecute operations.zNo operations to executez*Bulk operations can only be executed once.TN)rB   r   rC   r?   r   r   rA   r{   r~   rU   rV   r   _conn_for_writesr  r   )rO   r   r   rz   r   rV   
connections          rQ   executez_AsyncBulk.execute  s)     xx"#=>>=="#OPP%F)F)F1'=I<<((*I**,I))00))#44WiHH  J--j)]SSS   --iQZ[[[	 I S    \sx   C
ED#ED%ED+0D'1D+6ED)EE E%E'D+)E+D=1D42D=9E)NN)r?   zAsyncCollection[_DocumentType]rA   r   rP   r   r@   zOptional[str]r;   zOptional[Any]r   r   )r   zType[_BulkWriteContext])r\   r1   r   r   )FFNNN)ri   Mapping[str, Any]rj   z#Union[Mapping[str, Any], _Pipeline]rd   r   re   r   rf   Optional[Mapping[str, Any]]rk   z!Optional[list[Mapping[str, Any]]]rh    Union[str, dict[str, Any], None]r   r   )FNN)ri   r  ro   r  re   r   rf   r  rh   r  r   r   )
ri   r  rr   intrf   r  rh   r  r   r   )r   zIterator[Optional[_Run]])r   zIterator[_Run])r   r'   rl   MutableMapping[str, Any]r   r  r   bytesr   list[Mapping[str, Any]]rV   r/   r   dict[str, Any])r   r'   rl   r  r   r  r   r  r   r  r   r  rV   r/   r   r  )
r   4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rl   r  rB   r  rV   r/   r   r  )
r   r  rl   r  rB   r  rV   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   rz   strr   r  )r   r0   r   r  r   r   )r   r0   r   r  r   r-   r   r   )r   r-   r   r   rz   r  r   r   )__name__
__module____qualname____doc__rR   propertyrZ   r`   rm   rp   rs   r{   r~   r   r   r   r   r   r   r   r   r   r  r
   rS   rQ   r5   r5   S   s   1 "&! "2 "  " %)	 "
  "  " 
 "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. . $. .	.
 . 
.`%;#%;0=%;	%;N"" !" $	"
 
"HEE !E $	E
 
E>\#\ .\ 	\
 
\rS   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.asynchronous.client_sessionr   r   pymongo.asynchronous.helpersr   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.write_concernr-   pymongo.asynchronous.collectionr.   !pymongo.asynchronous.mongo_clientr/   pymongo.asynchronous.poolr0   pymongo.typingsr1   r2   r3   _IS_SYNCr5   r  rS   rQ   <module>r2     s    #    *    # ) ! c 7  
  : M M	 	 	 4 .?B9FFO
\ O
\rS   