o
    hq                     @  s  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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*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z? erddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZGmHZHmIZI dZJG dd dZKdS )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                   @  s   e Zd ZdZ		dhdiddZedjddZdkddZ				dldmd%d&Z			dndod(d)Z			dhdpd,d-Z
dqd/d0Zdrd2d3Zedsd@dAZdtdCdDZdudGdHZdvdJdKZ	dwdxdYdZZdyd]d^Zdzd_d`Zd{dadbZd{dcddZd|dfdgZdS )}_Bulkz'The private guts of the bulk write API.N
collectionCollection[_DocumentType]orderedboolbypass_document_validationOptional[bool]commentOptional[str]letOptional[Any]returnNonec                 C  s   |j |jjdtdd| _|| _| jdurtd| j || _|| _	g | _
d| _|| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _dS )zInitialize a _Bulk instance.replace)Zunicode_decode_error_handlerZdocument_class)codec_optionsNr<   FT)Zwith_optionsrA   _replacedictr4   r<   r   r   r:   r6   opsexecutedbypass_doc_valuses_collationuses_array_filtersuses_hint_updateuses_hint_delete	uses_sortis_retryableretryingstarted_retryable_writecurrent_runnext_runis_encrypted)selfr4   r6   r8   r:   r<    rS   K/var/www/html/venv/lib/python3.10/site-packages/pymongo/synchronous/bulk.py__init__V   s0   	

z_Bulk.__init__Type[_BulkWriteContext]c                 C  s*   | j jjj}|r|jsd| _tS d| _tS )NTF)r4   databaseclientZ
_encrypterZ_bypass_auto_encryptionrQ   r&   r#   )rR   Z	encrypterrS   rS   rT   bulk_ctx_classy   s   
z_Bulk.bulk_ctx_classdocumentr0   c                 C  s:   t d| t|tsd|v st |d< | jt|f dS )z*Add an insert document to the list of ops.rZ   Z_idN)r   
isinstancer   r   rD   appendr!   )rR   rZ   rS   rS   rT   
add_insert   s   

z_Bulk.add_insertselectorMapping[str, Any]update#Union[Mapping[str, Any], _Pipeline]multiupsert	collationOptional[Mapping[str, Any]]array_filters!Optional[list[Mapping[str, Any]]]hint Union[str, dict[str, Any], None]sortc	           
      C  s   t | |||d}	|dur||	d< |durd| _||	d< |dur(d| _||	d< |dur3d| _||	d< |dur>d| _||	d< |rCd	| _| jt|	f dS )
z8Create an update document and add it to the list of ops.)qurb   Nrc   Trd   ZarrayFiltersrh   rj   F)	r   rG   rH   rI   rK   rL   rD   r\   r"   )
rR   r^   r`   rb   rc   rd   rf   rh   rj   cmdrS   rS   rT   
add_update   s&   z_Bulk.add_updatereplacementc                 C  sx   t | ||d}|dur||d< |durd| _||d< |dur'd| _||d< |dur2d| _||d< | jt|f dS )z8Create a replace document and add it to the list of ops.)rk   rl   Nrc   Trd   rh   rj   )r   rG   rI   rK   rD   r\   r"   )rR   r^   ro   rc   rd   rh   rj   rm   rS   rS   rT   add_replace   s   

z_Bulk.add_replacelimitintc                 C  sX   ||d}|durd| _ ||d< |durd| _||d< |tkr"d| _| jt|f dS )z7Create a delete document and add it to the list of ops.)rk   rq   NTrd   rh   F)rG   rJ   r   rL   rD   r\   r    )rR   r^   rq   rd   rh   rm   rS   rS   rT   
add_delete   s   
z_Bulk.add_deleteIterator[Optional[_Run]]c                 c  s^    d}t | jD ]!\}\}}|du rt|}n|j|kr#|V  t|}||| q|V  dS )ziGenerate batches of operations, batched by type of
        operation, in the order **provided**.
        N)	enumeraterD   r   op_typeadd)rR   runidxrv   	operationrS   rS   rT   gen_ordered   s   


z_Bulk.gen_orderedIterator[_Run]c                 c  sZ    t tt tt tg}t| jD ]\}\}}|| || q|D ]}|jr*|V  q"dS )zbGenerate batches of operations, batched by type of
        operation, in arbitrary order.
        N)r   r!   r"   r    ru   rD   rw   )rR   
operationsry   rv   rz   rx   rS   rS   rT   gen_unordered   s   z_Bulk.gen_unorderedbwcr#   rm   MutableMapping[str, Any]
request_idmsgbytesdocslist[Mapping[str, Any]]rX   r.   dict[str, Any]c                 C  s  |||j < ttjr2tttj|jj	|t
t||j|||jj|jj|jjd |jjd |jjd |jr<|||| zS|j|||j}tj |j }ttjr|tttj|jj	||t
t||j|||jj|jj|jjd |jjd |jjd |jr|||| |||j W |S  ty }	 zctj |j }t|	tt fr|	j!}
nt"|	}
ttjrtttj#|jj	||
t
t||j|||jj|jj|jjd |jjd |jjt|	t d |jr|$||
| t|	tt fr||	j!|j  d}	~	ww )zCA proxy for SocketInfo.write_command that handles event publishing.r      messageclientId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   ZisServerSideErrorN)%fieldr   isEnabledForloggingDEBUGr   r   STARTED_topology_settings_topology_id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)rR   r   rm   r   r   r   rX   r   durationexcr   rS   rS   rT   r      s   






 


z_Bulk.write_commandmax_doc_sizec                 C  s  t tjr-tt tj|jj|t	t
||j|||jj|jj|jjd |jjd |jjd |jr7||||}z]|j||}tj |j }	|durSt|j||}
n2ddi}
t tjrtt tj|jj|	|
t	t
||j|||jj|jj|jjd |jjd |jjd |jr|||
|	 W |S W |S  ty	 } zhtj |j }	t|trt|j||j}nt|tr|j}nt |}t tjrtt tj!|jj|	|t	t
||j|||jj|jj|jjd |jjd |jjt|td |jr|jdusJ |"|||	  d}~ww )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   r[   r   r   r   r$   r   r   )rR   r   rm   r   r   r   r   rX   resultr   r   r   r   rS   rS   rT   r   I  s   





 




z_Bulk.unack_write4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rD   c           
   	   C  sh   | j r|||\}}}|jj|j|tdd|j|d |S |||\}}	}| ||||	d|| |S )Nr   )w)write_concernr   rX   )rQ   batch_commandr   r   r   r,   r   r   )
rR   r   rm   rD   rX   _batched_cmdto_sendr   r   rS   rS   rT   _execute_batch_unack  s   z_Bulk._execute_batch_unack.tuple[dict[str, Any], list[Mapping[str, Any]]]c                 C  sj   | j r|||\}}}|jj|j||j|j|d}||fS |||\}	}
}| |||	|
||}||fS )N)rA   r   rX   )rQ   r   r   r   r   r   r   r   )rR   r   rm   rD   rX   r   r   r   r   r   r   rS   rS   rT   _execute_batch  s   z_Bulk._execute_batch	generatorIterator[Any]r   r,   r   Optional[ClientSession]r   r/   op_id	retryablefull_resultfinal_write_concernOptional[WriteConcern]c	              
   C  sx  | j jj}	| j jj}
|
j}| jst|| _d | _| j}||
| d}|r:| j	s6t|d | _| jd u r6d}t
|j }| |	||||||j| j j}|jt|jk r%|rct|j|j dkrc|pb|}|| j jd| ji}| jrt| j|d< t|| | jd ur| j|d< | jd ur|jttfv r| j|d< |r|r| js|  d| _|||tj| ||||
 || | |
| t!|j|jd }|j"r| #||||
\}}|$di }|$d	d
t%v rt&'|}t(|||j| t)| t(|||j| d| _	d| _| jrd|v rnn| *||||
}| jt|7  _|jt|jk sS| jr0|d r0d S | j | _}|s&d S d S )NFTr   r6   r:   ZbypassDocumentValidationr<   ZwriteConcernErrorcoder   writeErrors)+r4   rW   r   rX   _event_listenersrO   r   rP   Zvalidate_sessionrM   r   rv   rY   rA   
idx_offsetlenrD   r6   r:   r   Zapply_write_concernrF   r<   r    r"   rN   Z_start_retryable_writeZ	_apply_tor(   ZPRIMARYZsend_cluster_timeadd_server_apiZapply_timeoutr   acknowledgedr   getr   copydeepcopyr   r   r   )rR   r   r   r   r   r   r   r   r   r   rX   	listenersrx   Zlast_runcmd_namer   rm   rD   r   r   ZwcefullrS   rS   rT   _execute_command  s   











7z_Bulk._execute_commandrz   strc              	     sp   g g dddddg d t  d fdd}jjj}|jj|||d} d s2 d r6t   S )zExecute using write commands.r   r   writeConcernErrorsZ	nInsertedZ	nUpsertedZnMatchedZ	nModifiedZnRemovedZupsertedr   r   r   r/   r   r7   r>   r?   c              	     s    | ||  d S N)r   )r   r   r   r   r   r   rR   r   rS   rT   retryable_bulkN  s   z-_Bulk.execute_command.<locals>.retryable_bulk)ZbulkZoperation_idr   r   N)r   r   r   r/   r   r7   r>   r?   )r'   r4   rW   rX   Z_retryable_writerL   r   )rR   r   r   r   rz   r   rX   r   rS   r   rT   execute_command9  s.   


	z_Bulk.execute_commandc              
   C  s   | j jj}| j jj}|j}t }| jst|| _| j}|rwt|j	 }| 
|||||d|j	| j j}	|jt|jk rk|| j jdddddii}
||
 t|j|jd}| |	|
||}| jt|7  _|jt|jk s9t|d | _}|sdS dS )zCExecute write commands with OP_MSG and w=0 writeConcern, unordered.Nr6   FZwriteConcernr   r   )r4   rW   r   rX   r   r'   rO   r   r   rv   rY   rA   r   r   rD   r   r   r   )rR   r   r   r   rX   r   r   rx   r   r   rm   rD   r   rS   rS   rT   execute_op_msg_no_resultsi  s>   




z_Bulk.execute_op_msg_no_resultsc              
   C  sV   g g dddddg d}t  }t }z| ||d||d|| W dS  ty*   Y dS w )zAExecute write commands with OP_MSG and w=0 WriteConcern, ordered.r   r   NF)r,   r'   r   r   )rR   r   r   r   r   Zinitial_write_concernr   rS   rS   rT   execute_command_no_results  s2   

z _Bulk.execute_command_no_resultsc                 C  s   | j rtd| jrtd|o|j }|r"| jr"|jdk r"td|r0| jr0|jdk r0td|r>| jr>|jdk r>td| jrEt	d	| j
rO| |||S | ||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)rG   r   rH   r   rJ   Zmax_wire_versionrI   rK   rF   r   r6   r   r   )rR   r   r   r   ZunackrS   rS   rT   execute_no_results  s0   z_Bulk.execute_no_resultsr   c                 C  s   | j std| jrtdd| _|p| jj}t||}| jr$|  }n|  }| jj	j
}|jsQ|||}| ||| 	 W d   dS 1 sJw   Y  dS | ||||S )zExecute operations.zNo operations to executez*Bulk operations can only be executed once.TN)rD   r   rE   r4   r   r*   r6   r{   r~   rW   rX   r   Z_conn_for_writesr   r   )rR   r   r   rz   r   rX   
connectionrS   rS   rT   execute  s"   


$z_Bulk.execute)NN)r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   )r>   rV   )rZ   r0   r>   r?   )NNNN)r^   r_   r`   ra   rb   r7   rc   r9   rd   re   rf   rg   rh   ri   rj   re   r>   r?   )NNN)r^   r_   ro   r_   rc   r9   rd   re   rh   ri   rj   re   r>   r?   )
r^   r_   rq   rr   rd   re   rh   ri   r>   r?   )r>   rt   )r>   r|   )r   r#   rm   r   r   rr   r   r   r   r   rX   r.   r>   r   )r   r#   rm   r   r   rr   r   r   r   rr   r   r   rX   r.   r>   re   )
r   r   rm   r   rD   r   rX   r.   r>   r   )
r   r   rm   r   rD   r   rX   r.   r>   r   r   )r   r   r   r,   r   r   r   r/   r   rr   r   r7   r   r   r   r   r>   r?   )
r   r   r   r,   r   r   rz   r   r>   r   )r   r/   r   r   r>   r?   )r   r/   r   r   r   r,   r>   r?   )r   r,   r   r   rz   r   r>   r   )__name__
__module____qualname____doc__rU   propertyrY   r]   rn   rp   rs   r{   r~   r+   r   r   r   r   r   r   r   r   r   r   rS   rS   rS   rT   r3   S   sD    #
	&


U
X

g
0
%
$#r3   )Lr   
__future__r   r   r   r   collections.abcr   	itertoolsr   typingr   r   r   r   r	   r
   r   Zbson.objectidr   Zbson.raw_bsonr   Zpymongor   r   Zpymongo.bulk_sharedr   r   r   r   r   Zpymongo.commonr   r   r   Zpymongo.errorsr   r   r   r   Zpymongo.helpers_sharedr   Zpymongo.loggerr   r   r   Zpymongo.messager    r!   r"   r#   r$   r%   r&   r'   Zpymongo.read_preferencesr(   Z"pymongo.synchronous.client_sessionr)   r*   Zpymongo.synchronous.helpersr+   Zpymongo.write_concernr,   Zpymongo.synchronous.collectionr-   Z pymongo.synchronous.mongo_clientr.   Zpymongo.synchronous.poolr/   Zpymongo.typingsr0   r1   r2   Z_IS_SYNCr3   rS   rS   rS   rT   <module>   s8   $
(
