o
    h.                     @  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r7d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 Z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                   @  s.   e Zd ZdZdddZdd	d
ZdddZdS )_Runz'Represents a batch of write operations.op_typer   returnNonec                 C  s   || _ g | _g | _d| _dS )zInitialize a new Run object.r   N)r   	index_mapopsZ
idx_offset)selfr    r$   F/var/www/html/venv/lib/python3.10/site-packages/pymongo/bulk_shared.py__init__/   s   
z_Run.__init__idxc                 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.index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)r   r   r   r    )r'   r   r   r   )r)   r   r*   r   r   r    )__name__
__module____qualname____doc__r&   r(   r,   r$   r$   r$   r%   r   ,   s
    

r   runfull_resultMutableMapping[str, Any]offsetresultMapping[str, Any]r   r    c                 C  sT  | dd}| jtkr|d  |7  < nZ| jtkr"|d  |7  < nL| jtkrn| d}|r\t|}|D ]}| |d | |d< q4|d | |d  |7  < |d  || 7  < n|d  |7  < |d	  |d	 7  < | d
}|r|D ]!}| }	|d | }
| |
|	d< | j	|
 |	d< |d
 
|	 qwt|}|r|d 
| dS 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   r4   r5   Zaffectedr8   Z
n_upserteddocZwrite_errorsreplacementr'   Zwcer$   r$   r%   _merge_commandH   s8   




rA   r   r   c                 C  sb   | d r-| d j dd d | d d }|d }|d }|dkr-|d	r-d
}t||| t| )z5Raise a BulkWriteError from the full bulk api result.r9   c                 S  s   | d S )Nr(   r$   )errorr$   r$   r%   <lambda>x   s    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.)sort
startswithr	   r   )r2   errrE   msgrF   r$   r$   r%   _raise_bulk_write_errort   s   rL   N)
r1   r   r2   r3   r4   r   r5   r6   r   r    )r2   r   r   r   )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   rA   rL   r$   r$   r$   r%   <module>   s"   
,