U
    ~fh-                     @  s   U 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 ddlmZmZmZ ernddlmZ dZded	< d
Zded< dZded< dZded< dZded< dZded< G dd dZddddddddZdddd d!Zd"S )#zKConstants, types, and classes shared across Bulk Write API implementations.    )annotations)TYPE_CHECKINGAnyMappingMutableMappingNoReturn)BulkWriteErrorOperationFailure)_get_wce_doc)_DELETE_INSERT_UPDATE)_DocumentOutint_DELETE_ALL   _DELETE_ONE   
_BAD_VALUE   _UNKNOWN_ERROR@   _WRITE_CONCERN_ERROR)insertupdatedeleteztuple[str, str, str]	_COMMANDSc                   @  sB   e Zd ZdZdddddZddddd	Zdd
ddddZdS )_Runz'Represents a batch of write operations.r   None)op_typereturnc                 C  s   || _ g | _g | _d| _dS )zInitialize a new Run object.r   N)r   	index_mapopsZ
idx_offset)selfr    r$   7/tmp/pip-unpacked-wheel-36gvocj8/pymongo/bulk_shared.py__init__/   s    z_Run.__init__)idxr    c                 C  s
   | j | S )zGet the original index of an operation in this run.

        :param idx: The Run index that maps to the original index.
        )r!   )r#   r'   r$   r$   r%   index6   s    z
_Run.indexr   )original_index	operationr    c                 C  s   | j | | j| dS )zAdd an operation to this Run instance.

        :param original_index: The original index of this operation
            within a larger bulk operation.
        :param operation: The operation document.
        N)r!   appendr"   )r#   r)   r*   r$   r$   r%   add=   s    z_Run.addN)__name__
__module____qualname____doc__r&   r(   r,   r$   r$   r$   r%   r   ,   s   r   zMutableMapping[str, Any]zMapping[str, Any]r   )runfull_resultoffsetresultr    c                 C  sT  | dd}| jtkr(|d  |7  < n| jtkrD|d  |7  < n| jtkr| d}|rt|}|D ]}| |d | |d< qh|d | |d  |7  < |d  || 7  < n|d  |7  < |d	  |d	 7  < | d
}|r4|D ]B}| }	|d | }
| |
|	d< | j	|
 |	d< |d
 
|	 qt|}|rP|d 
| dS )z7Merge a write command result into the full bulk result.nr   Z	nInsertedZnRemovedupsertedr(   Z	nUpsertedZnMatchedZ	nModifiedwriteErrorsopZwriteConcernErrorsN)getr   r   r   r   lenr(   extendcopyr"   r+   r
   )r1   r2   r3   r4   Zaffectedr6   Z
n_upserteddocZwrite_errorsreplacementr'   Zwcer$   r$   r%   _merge_commandH   s6    




r?   r   r   )r2   r    c                 C  sf   | d rZ| d j dd d | d d }|d }|d }|dkrZ|d	rZd
}t||| t| dS )z5Raise a BulkWriteError from the full bulk api result.r7   c                 S  s   | d S )Nr(   r$   )errorr$   r$   r%   <lambda>x       z)_raise_bulk_write_error.<locals>.<lambda>)keyr   codeerrmsg   zTransaction numberszrThis MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.N)sort
startswithr	   r   )r2   errrD   msgrE   r$   r$   r%   _raise_bulk_write_errort   s    rK   N)r0   
__future__r   typingr   r   r   r   r   Zpymongo.errorsr   r	   Zpymongo.helpers_sharedr
   Zpymongo.messager   r   r   Zpymongo.typingsr   r   __annotations__r   r   r   r   r   r   r?   rK   r$   r$   r$   r%   <module>   s    ,