U
    ~fh?                     @  s   d 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 ddlmZ erXddlmZ ddd	d
ddddZddddddZdS )zRConstants, types, and classes shared across Client Bulk Write API implementations.    )annotations)TYPE_CHECKINGAnyMappingMutableMappingNoReturn)ClientBulkWriteExceptionOperationFailure)_get_wce_doc)_DocumentOutz#list[tuple[str, Mapping[str, Any]]]intzMutableMapping[str, Any]zMapping[str, Any]None)opsoffsetfull_resultresultreturnc           	      C  s   | dr|d |d< |d  | dd7  < |d  | dd7  < |d  | dd7  < |d  | dd7  < |d  | dd7  < | d}|r|D ]>}| }|d	 | }||d	< | | d
 |d< |d | qt|}|r|d | dS )z?Merge result of a single bulk write batch into the full result.errorZ	nInsertedr   ZnDeletedZnMatchedZ	nModifiedZ	nUpsertedwriteErrorsidx   opZwriteConcernErrorsN)getcopyappendr
   )	r   r   r   r   Zwrite_errorsdocreplacementZoriginal_indexZwce r   ?/tmp/pip-unpacked-wheel-36gvocj8/pymongo/_client_bulk_shared.py_merge_command   s$    

r   r   boolr   )r   verbose_resultsr   c                 C  s   | d rZ| d j dd d | d d }|d }|d }|dkrZ|d	rZd
}t||| t| d trxt| || d t| |dS )z6Raise a ClientBulkWriteException from the full result.r   c                 S  s   | d S )Nr   r   )r   r   r   r   <lambda>B       z4_throw_client_bulk_write_exception.<locals>.<lambda>)keyr   codeerrmsg   zTransaction numberszrThis MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.r   N)sort
startswithr	   
isinstanceBaseExceptionr   )r   r!   errr%   msgr&   r   r   r   "_throw_client_bulk_write_exception<   s    r.   N)__doc__
__future__r   typingr   r   r   r   r   Zpymongo.errorsr   r	   Zpymongo.helpers_sharedr
   Zpymongo.typingsr   r   r.   r   r   r   r   <module>   s    