o
    C©Ìhôq  ã                   @  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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)Ú	_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)Ú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   úg/var/www/html/Moveonwheels/move_on_wheels/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ÚclientÚ
_encrypterÚ_bypass_auto_encryptionrT   r&   r#   )rU   Ú	encrypterrV   rV   rW   Ú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.r`   Ú_idN)r   Ú
isinstancer   r   rG   Úappendr!   )rU   r`   rV   rV   rW   Ú
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Ú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_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.)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_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.)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_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)Ú	enumeraterG   r   Úop_typeÚadd)rU   ÚrunÚidxr~   Ú	operationrV   rV   rW   Ú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    r}   rG   r   )rU   Ú
operationsr�   r~   r‚   r€   rV   rV   rW   Úgen_unorderedç   s   €€þz_Bulk.gen_unorderedÚbwcr#   ru   úMutableMapping[str, Any]Ú
request_idÚmsgÚbytesÚdocsúlist[Mapping[str, Any]]r[   úMongoClient[Any]ú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¡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¡ ‚ 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�   Ú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Ú	Exceptionrb   r   r   Údetailsr$   ÚFAILEDÚ_fail)rU   r‡   ru   r‰   rŠ   rŒ   r[   r    ÚdurationÚexcr¢   rV   rV   rW   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¡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	 } zht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 )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¾   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_Bulk.unack_writeú4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rG   c           
   	   C  sh   | j r| ||¡\}}}|jj|j|tdd�|j|d� |S | ||¡\}}	}|  ||||	d||¡ |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_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)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_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 rót& '|¡}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:   Ú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_Bulk._execute_commandr‚   Ú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Þ   ÚwriteConcernErrorsÚ	nInsertedÚ	nUpsertedÚnMatchedÚ	nModifiedÚnRemovedÚupsertedr½   rÕ   r®   r/   r×   r7   r>   r?   c              	     s   ˆ  ˆˆ| |ˆ|ˆ ¡ d S ©N)ró   )r½   r®   r×   ©rØ   rÓ   rÖ   rU   rË   rV   rW   Úretryable_bulkN  s   ùz-_Bulk.execute_command.<locals>.retryable_bulk)ÚbulkÚoperation_idrÞ   rö   N)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  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   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_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   )rU   r®   rÓ   rË   rØ   Úinitial_write_concernrÖ   rV   rV   rW   Ú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)rJ   r   rK   rê   rM   Úmax_wire_versionrL   rN   rI   r   r6   r  r  )rU   r®   rÓ   rË   ÚunackrV   rV   rW   Ú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)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"   


$þz_Bulk.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.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.synchronous.client_sessionr)   r*   Úpymongo.synchronous.helpersr+   Úpymongo.write_concernr,   Úpymongo.synchronous.collectionr-   Ú pymongo.synchronous.mongo_clientr.   Úpymongo.synchronous.poolr/   Úpymongo.typingsr0   r1   r2   Ú_IS_SYNCr3   rV   rV   rV   rW   Ú<module>   s8   $
(
