o
    C©Ìh5s  ã                   @  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 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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)Ú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                   @  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 )}Ú
_AsyncBulkz'The private guts of the bulk write API.NÚ
collectionúAsyncCollection[_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 _AsyncBulk instance.Úreplace)Úunicode_decode_error_handlerÚdocument_class)Úcodec_optionsNr<   FT)Úwith_optionsrC   Ú_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<   © rV   úh/var/www/html/Moveonwheels/move_on_wheels/venv/lib/python3.10/site-packages/pymongo/asynchronous/bulk.pyÚ__init__V   s0   	ÿÿ

z_AsyncBulk.__init__úType[_BulkWriteContext]c                 C  s*   | j jjj}|r|jsd| _tS d| _tS )NTF)r4   ÚdatabaseÚclientÚ
_encrypterÚ_bypass_auto_encryptionrT   r)   r&   )rU   Ú	encrypterrV   rV   rW   Úbulk_ctx_classy   s   
z_AsyncBulk.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.r`   Ú_idN)r   Ú
isinstancer   r   rG   Úappendr$   )rU   r`   rV   rV   rW   Ú
add_insertƒ   s   

z_AsyncBulk.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Úuri   Nrj   Trk   ÚarrayFiltersro   rq   F)	r   rJ   rK   rL   rN   rO   rG   rc   r%   )
rU   re   rg   ri   rj   rk   rm   ro   rq   ÚcmdrV   rV   rW   Ú
add_update‹   s&   z_AsyncBulk.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.)rr   rs   Nrj   Trk   ro   rq   )r   rJ   rL   rN   rG   rc   r%   )rU   re   rw   rj   rk   ro   rq   ru   rV   rV   rW   Úadd_replace¬   s   

z_AsyncBulk.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.)rr   ry   NTrk   ro   F)rJ   rM   r   rO   rG   rc   r#   )rU   re   ry   rk   ro   ru   rV   rV   rW   Ú
add_deleteÅ   s   
z_AsyncBulk.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)Ú	enumeraterG   r   Úop_typeÚadd)rU   ÚrunÚidxr~   Ú	operationrV   rV   rW   Úgen_orderedÙ   s   €


z_AsyncBulk.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#   r}   rG   r   )rU   Ú
operationsr�   r~   r‚   r€   rV   rV   rW   Úgen_unorderedç   s   €€þz_AsyncBulk.gen_unorderedÚbwcr&   ru   úMutableMapping[str, Any]Ú
request_idÚmsgÚbytesÚdocsúlist[Mapping[str, Any]]r[   úAsyncMongoClient[Any]údict[str, Any]c                 Ã  s  �|||j < t tj¡r3tttj|jj	|t
t|ƒƒ|j|||jj|jj|jjd |jjd |jjd� |jr=| |||¡ zY|j |||j¡I dH }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¡I dH  W |S  t�y
 }	 zgtj ¡ |j }t|	tt fƒr²|	j!}
nt"|	ƒ}
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t|	t ƒd� |jrò| $||
|¡ t|	tt fƒ�r| |	j!|j¡I dH  ‚ 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Ú	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"   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Ú	Exceptionrb   r   r   Údetailsr'   ÚFAILEDÚ_fail)rU   r‡   ru   r‰   rŠ   rŒ   r[   r    ÚdurationÚexcr¢   rV   rV   rW   rµ   ó   s”   €



ó


ò á


ñ€âz_AsyncBulk.write_commandÚmax_doc_sizec                 Ã  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r8| |||¡}z`|j ||¡I dH }tj ¡ |j }	|durWt|j||ƒ}
n2ddi}
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–| ||
|	¡ W |S W |S  t�y } zitj ¡ |j }	t|tƒr·t|j||jƒ}nt|tƒrÀ|j}nt |ƒ}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t|tƒd� |j�r	|jdu�sJ ‚| "|||	¡ ‚ d}~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¾   rb   r   r¿   r   r'   rÀ   rÁ   )rU   r‡   ru   r‰   rŠ   rÄ   rŒ   r[   ÚresultrÂ   r    rÃ   r¢   rV   rV   rW   rÆ   I  sœ   €


ó


òà â




ñ€ãz_AsyncBulk.unack_writeú4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rG   c           
   	   Ã  sv   �| j r"| ||¡\}}}|jj|j|tdd�|j|d�I d H  |S | ||¡\}}	}|  ||||	d||¡I d H  |S )Nr   )Úw)Úwrite_concernr½   r[   )rT   Úbatch_commandr®   r”   r­   r,   r½   rÆ   )
rU   r‡   ru   rG   r[   Ú_Úbatched_cmdÚto_sendr‰   rŠ   rV   rV   rW   Ú_execute_batch_unack¡  s   €ûøz_AsyncBulk._execute_batch_unackú.tuple[dict[str, Any], list[Mapping[str, Any]]]c                 Ã  sx   �| j r"| ||¡\}}}|jj|j||j|j|d�I d H }||fS | ||¡\}	}
}|  |||	|
||¡I d H }||fS )N)rC   r½   r[   )rT   rÌ   r®   r”   r­   r¶   r½   rµ   )rU   r‡   ru   rG   r[   rÍ   rÎ   rÏ   rÈ   r‰   rŠ   rV   rV   rW   Ú_execute_batch¼  s   €ûýz_AsyncBulk._execute_batchÚ	generatorúIterator[Any]rË   r,   r½   úOptional[AsyncClientSession]r®   r/   Úop_idÚ	retryableÚfull_resultÚfinal_write_concernúOptional[WriteConcern]c	              
   Ã  s†  �| j jj}	| j jj}
|
j}| jst|ƒ| _d | _| j}| |
|¡ d}|�rA| j	s7t|d ƒ| _| jd u r7d}t
|j }|  |	||||||j| j j¡}|jt|jƒk �r,|rdt|jƒ|j dkrd|pc|}|| j jd| ji}| jru| 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|  #||||
¡I d H \}}| $di ¡}| $d	d
¡t%v r÷t& '|¡}t(|||j|ƒ t)|ƒ t(|||j|ƒ d| _	d| _| j�rd|v �rnn|  *||||
¡I d H }| jt|ƒ7  _|jt|jƒk sT| j�r7|d �r7d S | j | _}|s'd S d S )NFTr�   r6   r:   ÚbypassDocumentValidationr<   ÚwriteConcernErrorÚcoder   ÚwriteErrors)+r4   rZ   rÇ   r[   Ú_event_listenersrR   r«   rS   Úvalidate_sessionrP   r   r~   r_   rC   Ú
idx_offsetÚlenrG   r6   r:   r   Úapply_write_concernrI   r<   r#   r%   rQ   Ú_start_retryable_writeÚ	_apply_tor+   ÚPRIMARYÚsend_cluster_timeÚadd_server_apiÚapply_timeoutr   ÚacknowledgedrÒ   Úgetr   ÚcopyÚdeepcopyr   r   rÐ   )rU   rÓ   rË   r½   r®   rÖ   r×   rØ   rÙ   r­   r[   Ú	listenersr€   Úlast_runÚcmd_namer‡   ru   rG   rÈ   rÏ   ÚwceÚfullrV   rV   rW   Ú_execute_commandÒ  s„   €




ø






€Í7´z_AsyncBulk._execute_commandr‚   Ústrc              	   ƒ  sx   �g g dddddg dœ‰ t ƒ ‰d‡ ‡‡‡‡fdd„}ˆjjj}|jˆj|||ˆˆd�I dH }ˆ d s6ˆ d r:tˆ ƒ ˆ S )zExecute using write commands.r   ©rÞ   ÚwriteConcernErrorsÚ	nInsertedÚ	nUpsertedÚnMatchedÚ	nModifiedÚnRemovedÚupsertedr½   rÕ   r®   r/   r×   r7   r>   r?   c              	   “  s"   �ˆ  ˆˆ| |ˆ|ˆ ¡I d H  d S ©N)ró   )r½   r®   r×   ©rØ   rÓ   rÖ   rU   rË   rV   rW   Úretryable_bulkN  s   €ùz2_AsyncBulk.execute_command.<locals>.retryable_bulk)ÚbulkÚoperation_idNrÞ   rö   )r½   rÕ   r®   r/   r×   r7   r>   r?   )r*   r4   rZ   r[   Ú_retryable_writerO   r   )rU   rÓ   rË   r½   r‚   rÿ   r[   rÍ   rV   rþ   rW   Úexecute_command9  s0   €
ø

ú	z_AsyncBulk.execute_commandc              
   Ã  sú   �| j jj}| j jj}|j}tƒ }| jst|ƒ| _| j}|r{t|j	 }|  
|||||d|j	| j j¡}	|jt|jƒk ro|| j jdddddii}
| |
¡ t|j|jdƒ}|  |	|
||¡I dH }| jt|ƒ7  _|jt|jƒk s:t|dƒ | _}|sdS dS )zCExecute write commands with OP_MSG and w=0 writeConcern, unordered.Nr6   FÚwriteConcernrÊ   r   )r4   rZ   rÇ   r[   rß   r*   rR   r«   r   r~   r_   rC   rá   râ   rG   rè   r   rÐ   )rU   r®   rÓ   r­   r[   rî   rÖ   r€   rð   r‡   ru   rG   rÏ   rV   rV   rW   Úexecute_op_msg_no_resultsi  s@   €



øý
öèz$_AsyncBulk.execute_op_msg_no_resultsc              
   Ã  s^   �g g dddddg dœ}t ƒ }tƒ }z|  ||d||d||¡I dH  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   )rU   r®   rÓ   rË   rØ   Úinitial_write_concernrÖ   rV   rV   rW   Úexecute_command_no_results�  s4   €øø
ÿz%_AsyncBulk.execute_command_no_resultsc                 Ã  s¸   �| j rtdƒ‚| jrtdƒ‚|o|j }|r#| jr#|jdk r#tdƒ‚|r1| jr1|jdk r1tdƒ‚|r?| jr?|jdk r?tdƒ‚| jrFt	d	ƒ‚| j
rS|  |||¡I d
H S |  ||¡I d
H 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 concernN)rJ   r   rK   rê   rM   Úmax_wire_versionrL   rN   rI   r   r6   r  r  )rU   r®   rÓ   rË   ÚunackrV   rV   rW   Úexecute_no_results´  s2   €ÿÿÿÿz_AsyncBulk.execute_no_resultsr   c              	   Ã  sÚ   �| j stdƒ‚| jrtdƒ‚d| _|p| jj}t||ƒ}| jr%|  ¡ }n|  ¡ }| jj	j
}|jsb| ||¡I dH 4 I dH š}|  |||¡I dH  	 W d  ƒI dH  dS 1 I dH s[w   Y  dS |  ||||¡I dH S )zExecute operations.zNo operations to executez*Bulk operations can only be executed once.TN)rG   r   rH   r4   rË   r   r6   rƒ   r†   rZ   r[   rê   Ú_conn_for_writesr  r  )rU   rË   r½   r‚   rÓ   r[   Ú
connectionrV   rV   rW   Úexecute×  s$   €


0þz_AsyncBulk.execute)NN)r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   )r>   rY   )r`   r0   r>   r?   )NNNN)re   rf   rg   rh   ri   r7   rj   r9   rk   rl   rm   rn   ro   rp   rq   rl   r>   r?   )NNN)re   rf   rw   rf   rj   r9   rk   rl   ro   rp   rq   rl   r>   r?   )
re   rf   ry   rz   rk   rl   ro   rp   r>   r?   )r>   r|   )r>   r„   )r‡   r&   ru   rˆ   r‰   rz   rŠ   r‹   rŒ   r�   r[   rŽ   r>   r�   )r‡   r&   ru   rˆ   r‰   rz   rŠ   r‹   rÄ   rz   rŒ   r�   r[   rŽ   r>   rl   )
r‡   rÉ   ru   r�   rG   r�   r[   rŽ   r>   r�   )
r‡   rÉ   ru   r�   rG   r�   r[   rŽ   r>   rÑ   rý   )rÓ   rÔ   rË   r,   r½   rÕ   r®   r/   rÖ   rz   r×   r7   rØ   rˆ   rÙ   rÚ   r>   r?   )
rÓ   rÔ   rË   r,   r½   rÕ   r‚   rô   r>   r�   )r®   r/   rÓ   rÔ   r>   r?   )r®   r/   rÓ   rÔ   rË   r,   r>   r?   )rË   r,   r½   rÕ   r‚   rô   r>   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rX   Úpropertyr_   rd   rv   rx   r{   rƒ   r†   r   rµ   rÆ   rÐ   rÒ   ró   r  r  r  r  r  rV   rV   rV   rW   r3   S   sD    ú#
	÷&ùû


U
X
÷
g
0
'
$#r3   )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.poolr/   Úpymongo.typingsr0   r1   r2   Ú_IS_SYNCr3   rV   rV   rV   rW   Ú<module>   s8   $
(
