o
    'h!s                     @  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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)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   U/var/www/html/olx_land/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[   r.   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rt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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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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 } zit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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_write4Union[_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 rt&'|}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   $
(
