o
    {gfo                     @  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		dgdhddZediddZdjddZ					dkdld$d%Z			dmdnd'd(Z			dgdod+d,Z
dpd.d/Zdqd1d2Zedrd?d@ZdsdBdCZdtdFdGZdudIdJZ	dvdwdXdYZdxd\d]Zdyd^d_Zdzd`daZdzdbdcZd{dedfZdS )|_Bulkz'The private guts of the bulk write API.N
collectionCollection[_DocumentType]orderedboolbypass_document_validation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S )zInitialize a _Bulk instance.replace)unicode_decode_error_handlerdocument_class)codec_optionsNr;   FT)with_optionsrB   _replacedictr4   r;   r   r   r9   r6   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)selfr4   r6   r8   r9   r;    rT   u/var/www/bot.gig.net.ua/public_html/telegram/P1/HellBot/venv/lib/python3.10/site-packages/pymongo/synchronous/bulk.py__init__V   s.   	

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_encryptionrR   r&   r#   )rS   	encrypterrT   rT   rU   bulk_ctx_classx   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   rF   appendr!   )rS   r^   rT   rT   rU   
add_insert   s   

z_Bulk.add_insertF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]c           	      C  s   t | td|fd|fd|fd|fg}|durd| _||d< |dur*d| _||d< |dur5d| _||d	< |r:d
| _| jt|f dS )z8Create an update document and add it to the list of ops.qurg   rh   NTri   arrayFiltersrm   F)	r   rE   rI   rJ   rK   rM   rF   ra   r"   )	rS   rc   re   rg   rh   ri   rk   rm   cmdrT   rT   rU   
add_update   s    z_Bulk.add_updatereplacementc                 C  sV   t | ||d|d}|durd| _||d< |dur!d| _||d< | jt|f dS )z8Create a replace document and add it to the list of ops.F)ro   rp   rg   rh   NTri   rm   )r   rI   rK   rF   ra   r"   )rS   rc   rt   rh   ri   rm   rr   rT   rT   rU   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.)ro   rv   NTri   rm   F)rI   rL   r   rM   rF   ra   r    )rS   rc   rv   ri   rm   rr   rT   rT   rU   
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)	enumeraterF   r   op_typeadd)rS   runidxr{   	operationrT   rT   rU   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    rz   rF   r|   )rS   
operationsr~   r{   r   r}   rT   rT   rU   gen_unordered   s   z_Bulk.gen_unorderedbwcr#   rr   MutableMapping[str, Any]
request_idmsgbytesdocslist[Mapping[str, Any]]rY   r.   dict[str, Any]c                 C  s  |||j < ttjr2tt|jjt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|jjt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|jjt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      clientIdmessage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   _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)rS   r   rr   r   r   r   rY   r   durationexcr   rT   rT   rU   r      s   






 


z_Bulk.write_commandmax_doc_sizec                 C  s  t tjr-tt |jjt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 |jjt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 |jjt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   )rS   r   rr   r   r   r   r   rY   resultr   r   r   r   rT   rT   rU   r   >  s   





 




z_Bulk.unack_write4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rF   c           
   	   C  sh   | j r|||\}}}|jj|j|tdd|j|d |S |||\}}	}| ||||	d|| |S )Nr   )w)write_concernr   rY   )rR   batch_commandr   r   r   r,   r   r   )
rS   r   rr   rF   rY   _batched_cmdto_sendr   r   rT   rT   rU   _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)rB   r   rY   )rR   r   r   r   r   r   r   r   )rS   r   rr   rF   rY   r   r   r   r   r   r   rT   rT   rU   _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  sr  | j jj}	| j jj}
|
j}| jst|| _d | _| j}||
| d}|r7| 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rd|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r-|d r-d S | j | _}|s&d S d S )NFTr   r6   r9   bypassDocumentValidationr;   writeConcernErrorcoder   writeErrors)+r4   rX   r   rY   _event_listenersrP   r   rQ   validate_sessionrN   r   r{   r]   rB   
idx_offsetlenrF   r6   r9   r   apply_write_concernrH   r;   r    r"   rO   _start_retryable_write	_apply_tor(   PRIMARYsend_cluster_timeadd_server_apiapply_timeoutr   acknowledgedr   getr   copydeepcopyr   r   r   )rS   r   r   r   r   r   r   r   r   r   rY   	listenersr}   last_runcmd_namer   rr   rF   r   r   wcefullrT   rT   rU   _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   rS   r   rT   rU   retryable_bulkC  s   z-_Bulk.execute_command.<locals>.retryable_bulk)bulkoperation_idr   r   N)r   r   r   r/   r   r7   r=   r>   )r'   r4   rX   rY   _retryable_writerM   r   )rS   r   r   r   r   r   rY   r   rT   r   rU   execute_command.  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   rX   r   rY   r   r'   rP   r   r   r{   r]   rB   r   r   rF   r   r   r   )rS   r   r   r   rY   r   r   r}   r   r   rr   rF   r   rT   rT   rU   execute_op_msg_no_results^  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   )rS   r   r   r   r   initial_write_concernr   rT   rT   rU   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| jr7td| j	rA| 
|||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.zGCannot set bypass_document_validation with unacknowledged write concern)rI   r   rJ   r   rL   max_wire_versionrK   rH   r   r6   r  r  )rS   r   r   r   unackrT   rT   rU   execute_no_results  s(   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)rF   r   rG   r4   r   r*   r6   r   r   rX   rY   r   _conn_for_writesr  r   )rS   r   r   r   r   rY   
connectionrT   rT   rU   execute  s"   


$z_Bulk.execute)NN)r4   r5   r6   r7   r8   r7   r9   r:   r;   r<   r=   r>   )r=   rW   )r^   r0   r=   r>   )FFNNN)rc   rd   re   rf   rg   r7   rh   r7   ri   rj   rk   rl   rm   rn   r=   r>   )FNN)rc   rd   rt   rd   rh   r7   ri   rj   rm   rn   r=   r>   )
rc   rd   rv   rw   ri   rj   rm   rn   r=   r>   )r=   ry   )r=   r   )r   r#   rr   r   r   rw   r   r   r   r   rY   r.   r=   r   )r   r#   rr   r   r   rw   r   r   r   rw   r   r   rY   r.   r=   rj   )
r   r   rr   r   rF   r   rY   r.   r=   r   )
r   r   rr   r   rF   r   rY   r.   r=   r   r   )r   r   r   r,   r   r   r   r/   r   rw   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__rV   propertyr]   rb   rs   ru   rx   r   r   r+   r   r   r   r   r   r   r  r  r  r  rT   rT   rT   rU   r3   S   sF    "
	!


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   rT   rT   rT   rU   <module>   s8   $
(
