
    M}bgR                       U d Z ddlm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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mZmZ ddlm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 e	rddl2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8m9Z9 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"          ZAd1d+ZBd2d.ZC G d/ d0          ZDdS )3z<The bulk write operations interface.

.. versionadded:: 2.7
    )annotationsN)MutableMapping)islice)TYPE_CHECKINGAnyIteratorMappingNoReturnOptionalTypeUnion)ObjectId)RawBSONDocument)_csotcommon)ClientSession_validate_session_write_concern)validate_is_document_typevalidate_ok_for_replacevalidate_ok_for_update)BulkWriteErrorConfigurationErrorInvalidOperationOperationFailure)_RETRYABLE_ERROR_CODES_get_wce_doc)_DELETE_INSERT_UPDATE_BulkWriteContext_EncryptedBulkWriteContext_randint)ReadPreference)WriteConcern)
Collection)
Connection)_DocumentOut_DocumentType	_Pipelineint_DELETE_ALL   _DELETE_ONE   
_BAD_VALUE   _UNKNOWN_ERROR@   _WRITE_CONCERN_ERROR)insertupdatedeleteztuple[str, str, str]	_COMMANDSc                  *    e Zd ZdZddZddZddZdS )_Runz'Represents a batch of write operations.op_typer*   returnNonec                >    || _         g | _        g | _        d| _        dS )zInitialize a new Run object.r   N)r:   	index_mapops
idx_offset)selfr:   s     _/home/e360mart.nyusoft.in/public_html/e360mart_env/lib/python3.11/site-packages/pymongo/bulk.py__init__z_Run.__init__Q   s"    #$&      idxc                    | j         |         S )zGet the original index of an operation in this run.

        :param idx: The Run index that maps to the original index.
        )r>   )rA   rE   s     rB   indexz
_Run.indexX   s    
 ~c""rD   original_index	operationr   c                n    | 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?   )rA   rH   rI   s      rB   addz_Run.add_   s4     	n---	"""""rD   N)r:   r*   r;   r<   )rE   r*   r;   r*   )rH   r*   rI   r   r;   r<   )__name__
__module____qualname____doc__rC   rG   rL    rD   rB   r9   r9   N   sV        11! ! ! !# # # ## # # # # #rD   r9   runfull_resultMutableMapping[str, Any]offsetresultMapping[str, Any]r;   r<   c                   |                     dd          }| j        t          k    r|dxx         |z  cc<   n| j        t          k    r|dxx         |z  cc<   n| j        t          k    r|                     d          }|rtt          |          }|D ]#}|                     |d         |z             |d<   $|d                             |           |dxx         |z  cc<   |dxx         ||z
  z  cc<   n|dxx         |z  cc<   |d	xx         |d	         z  cc<   |                     d
          }|rg|D ]d}|                                }	|d         |z   }
|                     |
          |	d<   | j	        |
         |	d<   |d
         
                    |	           et          |          }|r|d         
                    |           dS dS )z7Merge a write command result into the full bulk result.nr   	nInsertednRemovedupsertedrG   	nUpsertednMatched	nModifiedwriteErrorsopwriteConcernErrorsN)getr:   r   r   r   lenrG   extendcopyr?   rK   r   )rR   rS   rU   rV   affectedr\   
n_upserteddocwrite_errorsreplacementrE   wces               rB   _merge_commandrm   j   s)    zz#q!!H
{gK   H,    			J8+			::j)) 	0XJ @ @"yyW)>??G
#**8444$$$
2$$$
###x*'<<####
###x/###K   F;$77   ::m,,L ; 	; 	;C((**Kg,'C#&99S>>K  #K&--k::::
v

C
 6()00555556 6rD   r'   r
   c                   | d         ri| d                              d            | d         d         }|d         }|d         }|dk    r(|                    d          rd	}t          |||           t          |           )
z5Raise a BulkWriteError from the full bulk api result.r`   c                    | d         S )NrG   rQ   )errors    rB   <lambda>z)_raise_bulk_write_error.<locals>.<lambda>   s
    %. rD   )keyr   codeerrmsg   zTransaction numberszrThis MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.)sort
startswithr   r   )rS   errrs   msgrt   s        rB   _raise_bulk_write_errorrz      s     =! >M"'',H,H'III-(+6{(m2::#..)>??:- 
 #64===

%
%%rD   c                      e Zd ZdZ	 	 dCdDdZedEd            ZdFdZ	 	 	 	 	 dGdHd!Z	 	 	 dIdJd#Z		 	 dCdKd&Z
dLd(ZdMd*Z	 dNdOd9ZdPd=ZdQd>ZdRd?ZdRd@ZdSdBZdS )T_Bulkz'The private guts of the bulk write API.N
collectionCollection[_DocumentType]orderedboolbypass_document_validationcommentOptional[str]letOptional[Any]r;   r<   c                   |                     |j                            dt                              | _        || _        | j        t          j        d| j                   || _        || _	        g | _
        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_optionsr   _replacedictr}   r   r   r   r   r   r?   executedbypass_doc_valuses_collationuses_array_filtersuses_hint_updateuses_hint_deleteis_retryableretryingstarted_retryable_writecurrent_runnext_run)rA   r}   r   r   r   r   s         rB   rC   z_Bulk.__init__   s     %11$2;;-6t <   2 
 

 8,UDH===&-8:8#"' % % ',$rD   Type[_BulkWriteContext]c                \    | j         j        j        j        }|r|j        st
          S t          S N)r}   databaseclient
_encrypter_bypass_auto_encryptionr!   r    )rA   	encrypters     rB   bulk_ctx_classz_Bulk.bulk_ctx_class   s0    O,3>	 	%Y> 	%--$$rD   documentr'   c                    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   r?   rK   r   )rA   r   s     rB   
add_insertz_Bulk.add_insert   sZ    !*h7778_55 	)(9J9J&jjHUO(+,,,,,rD   FselectorrW   r5   #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]c                   t          |           t          d|fd|fd|fd|fg          }|d| _        ||d<   |d| _        ||d<   |d| _        ||d	<   |rd
| _        | j                            t          |f           dS )z8Create an update document and add it to the list of ops.qur   r   NTr   arrayFiltersr   F)	r   r   r   r   r   r   r?   rK   r   )	rA   r   r5   r   r   r   r   r   cmds	            rB   
add_updatez_Bulk.add_update   s     	v&&&"8_sFmgu-=&?QR
 
  "&D(C$&*D#"/C$(D!CK 	& %D#'''''rD   rk   c                    t          |           ||d|d}|d| _        ||d<   |d| _        ||d<   | j                            t
          |f           dS )z8Create a replace document and add it to the list of ops.F)r   r   r   r   NTr   r   )r   r   r   r?   rK   r   )rA   r   rk   r   r   r   r   s          rB   add_replacez_Bulk.add_replace   sp     	 ,,,;&QQ "&D(C$(D!CK#'''''rD   limitr*   c                    ||d}|d| _         ||d<   |d| _        ||d<   |t          k    rd| _        | j                            t          |f           dS )z7Create a delete document and add it to the list of ops.)r   r   NTr   r   F)r   r   r+   r   r?   rK   r   )rA   r   r   r   r   r   s         rB   
add_deletez_Bulk.add_delete  sq     u-- "&D(C$(D!CKK %D#'''''rD   Iterator[Optional[_Run]]c              #     K   d}t          | j                  D ]N\  }\  }}|t          |          }n|j        |k    r|V  t          |          }|                    ||           O|V  dS )ziGenerate batches of operations, batched by type of
        operation, in the order **provided**.
        N)	enumerater?   r9   r:   rL   )rA   rR   rE   r:   rI   s        rB   gen_orderedz_Bulk.gen_ordered   s       )248)<)< 	$ 	$%C%'9{7mm''			7mmGGC####					rD   Iterator[_Run]c              #    K   t          t                    t          t                    t          t                    g}t	          | j                  D ]$\  }\  }}||                             ||           %|D ]}|j        r|V  dS )zbGenerate batches of operations, batched by type of
        operation, in arbitrary order.
        N)r9   r   r   r   r   r?   rL   )rA   
operationsrE   r:   rI   rR   s         rB   gen_unorderedz_Bulk.gen_unordered.  s       7mmT']]DMMB
)248)<)< 	4 	4%C%'9w##C3333 	 	Cw 				 	rD   	generatorIterator[Any]write_concernr$   sessionOptional[ClientSession]connr&   op_id	retryablerS   rT   final_write_concernOptional[WriteConcern]c	           
        | j         j        j        }	| j         j        j        }
|
j        }| j        st          |          | _        d | _        | j        }|                    |
|           d}|r| j	        st          |d           | _        | j        d}t          |j                 }|                     |	||||||j        | j         j                  }|j        t          |j                  k     rJ|r$t          |j                  |j        z
  dk    r|p|}|| j         j        d| j        i}| j        r
| j        |d<   t'          j        ||           | j        rd|d<   | j        |j        t.          t0          fv r
| j        |d<   |rF|r"| j        s|                                 d| _        |                    ||t8          j        |           |                    |||
           |                    |           |                     |
|           tC          |j        |j        d           }|j"        r|#                    |||
          \  }}|$                    di           }|$                    d	d
          tJ          v r:tM          j'        |          }tQ          |||j        |           tS          |           tQ          |||j        |           d| _	        d| _        | j        rd|v rnSn|*                    |||
          }|xj        t          |          z  c_        |j        t          |j                  k     J| j        r
|d         rd S | j        x| _        }|d S d S )NFTr,   r   r   bypassDocumentValidationr   writeConcernErrorrs   r   r`   )+r}   r   namer   _event_listenersr   nextr   validate_sessionr   r7   r:   r   r   r@   rd   r?   r   r   r   apply_write_concernr   r   r   r   r   _start_retryable_write	_apply_tor#   PRIMARYsend_cluster_timeadd_server_apiapply_timeoutr   acknowledgedexecuterc   r   rf   deepcopyrm   rz   execute_unack)rA   r   r   r   r   r   r   rS   r   db_namer   	listenersrR   last_runcmd_namebwcr   r?   rV   to_sendrl   fulls                         rB   _execute_commandz_Bulk._execute_command:  s    /*/)0+	 	!#ID DM 	fg... L	3= $ $Y 5 5=(#H -H%%-	 	C .3sw<<//  ISW!>1 D D$7$H=M!5y$,O< 2%)\C	N)#}===& ;6:C238'CKGW;M,M,M!%CJ T ! <)E <668887;4%%c9n6LdSSS&&sGV<<<##C(((""63///SWcnd;; !- B&)kk#sF&C&COFG !**%8"==Cwwvq))-CCC  $}[99&sD#.&III/555"3S^VLLL %*DM38D0| (?(?!//S&AAG#g,,.g .3sw<<//n | M : %)]2DsY  L	3 L	3 L	3 L	3 L	3rD   rI   strdict[str, Any]c           	          g g dddddg dt                      d fd} j        j        j        }|                     j        |||            d         sd         rt                     S )zExecute using write commands.r   r`   rb   rZ   r]   r^   r_   r[   r\   r   r   r   r&   r   r   r;   r<   c           	     >                         | ||           d S r   )r   )r   r   r   rS   r   r   rA   r   s      rB   retryable_bulkz-_Bulk.execute_command.<locals>.retryable_bulk  s<     !!    rD   )bulkoperation_idr`   rb   )r   r   r   r&   r   r   r;   r<   )r"   r}   r   r   _retryable_writer   rz   )	rA   r   r   r   rI   r   r   rS   r   s	   ```    @@rB   execute_commandz_Bulk.execute_command  s     "$	
 	
 

	 	 	 	 	 	 	 	 	 	 )0 	  	
 	
 	
 }% 	15I)J 	1#K000rD   c           
        | j         j        j        }| j         j        j        }|j        }t                      }| j        st          |          | _        | j        }|rt          |j	                 }| 
                    |||||d|j	        | j         j                  }	|j        t          |j                  k     r|| j         j        dddddii}
|                    |
           t!          |j        |j        d          }|	                    |
||          }|xj        t          |          z  c_        |j        t          |j                  k     t          |d          x| _        }|	dS dS )zCExecute write commands with OP_MSG and w=0 writeConcern, unordered.Nr   FwriteConcernwr   )r}   r   r   r   r   r"   r   r   r7   r:   r   r   r@   rd   r?   r   r   r   )rA   r   r   r   r   r   r   rR   r   r   r   r?   r   s                rB   execute_op_msg_no_resultsz_Bulk.execute_op_msg_no_results  sr   /*/)0+	

 	/#ID 	; -H%%-	 	C .3sw<<//do2u"S!H
 ##C(((SWcnd;;++Cf==#g,,. .3sw<<// &*)T%:%::Ds1  	; 	; 	; 	; 	;rD   c           
         g g dddddg d}t                      }t                      }	 |                     ||d||d||           dS # t          $ r Y dS w xY w)zAExecute write commands with OP_MSG and w=0 WriteConcern, ordered.r   r   NF)r$   r"   r   r   )rA   r   r   r   rS   initial_write_concernr   s          rB   execute_command_no_resultsz _Bulk.execute_command_no_results  s     "$	
 	
 !-

	!!%	 	 	 	 	   	 	 	DD	s   A 
AAc                   | j         rt          d          | j        rt          d          |o|j         }|r!| j        r|j        dk     rt          d          |r!| j        r|j        dk     rt          d          | j        rt          d          | j	        r| 
                    |||          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.r0   zPMust be connected to MongoDB 4.2+ to use hint on unacknowledged update commands.zGCannot set bypass_document_validation with unacknowledged write concern)r   r   r   r   r   max_wire_versionr   r   r   r   r   r   )rA   r   r   r   unacks        rB   execute_no_resultsz_Bulk.execute_no_results  s     	\$%Z[[[" 	_$%]^^^@m&@"@ 	T* 	t/Dq/H/H$b    	T* 	t/Dq/H/H$b    	"Y   < 	S224MRRR--dI>>>rD   r   c                   | j         st          d          | j        rt          d          d| _        |p| j        j        }t          ||          }| j        r|                                 }n|                                 }| j        j	        j
        }|j        sI|                    ||          5 }|                     |||           	 ddd           dS # 1 swxY w Y   dS |                     ||||          S )zExecute operations.zNo operations to executez*Bulk operations can only be executed once.TN)r?   r   r   r}   r   r   r   r   r   r   r   r   _conn_for_writesr   r   )rA   r   r   rI   r   r   
connections          rB   r   z_Bulk.execute9  sQ    x 	?"#=>>>= 	Q"#OPPP%F)F1'=II< 	-((**II**,,I)0) 	V(()<< 
''
I}MMM                  ''	='9UUUs   0CCC)NN)r}   r~   r   r   r   r   r   r   r   r   r;   r<   )r;   r   )r   r'   r;   r<   )FFNNN)r   rW   r5   r   r   r   r   r   r   r   r   r   r   r   r;   r<   )FNN)r   rW   rk   rW   r   r   r   r   r   r   r;   r<   )
r   rW   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   rS   rT   r   r   r;   r<   )
r   r   r   r$   r   r   rI   r   r;   r   )r   r&   r   r   r;   r<   )r   r&   r   r   r   r$   r;   r<   )r   r$   r   r   rI   r   r;   r   )rM   rN   rO   rP   rC   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   rQ   rD   rB   r|   r|      s       11 "&!    B % % % X%- - - - 15;?15( ( ( ( (B 1515( ( ( ( (. 2615( ( ( ( ((   
 
 
 
* 7;e3 e3 e3 e3 e3N. . . .`#; #; #; #;J" " " "H? ? ? ?>V V V V V VrD   r|   )
rR   r9   rS   rT   rU   r*   rV   rW   r;   r<   )rS   r'   r;   r
   )ErP   
__future__r   rf   collections.abcr   	itertoolsr   typingr   r   r   r	   r
   r   r   r   bson.objectidr   bson.raw_bsonr   pymongor   r   pymongo.client_sessionr   r   pymongo.commonr   r   r   pymongo.errorsr   r   r   r   pymongo.helpersr   r   pymongo.messager   r   r   r    r!   r"   pymongo.read_preferencesr#   pymongo.write_concernr$   pymongo.collectionr%   pymongo.poolr&   pymongo.typingsr'   r(   r)   r+   __annotations__r-   r/   r1   r3   r7   r9   rm   rz   r|   rQ   rD   rB   <module>r     sY     # " " " " "  * * * * * *      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 # " " " " " ) ) ) ) ) ) ! ! ! ! ! ! ! ! Q Q Q Q Q Q Q Q         
            A @ @ @ @ @ @ @                4 3 3 3 3 3 . . . . . . G------''''''FFFFFFFFFF         
             "@	 @ @ @ @# # # # # # # #8)6 )6 )6 )6X& & & &$kV kV kV kV kV kV kV kV kV kVrD   