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)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   T/var/www/html/olx_land/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[   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   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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   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_write4Union[_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 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:   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   $
(
