
    =hP                       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 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l0m1Z1 ddl2m3Z3 e	rddl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z; dZ<de=d<   dZ>de=d<   dZ?de=d<   dZ@de=d<   dZAde=d<   dZBd e=d!<    G d" d#      ZC	 	 	 	 	 	 	 	 	 	 d(d$ZDd)d%ZE G d& d'      ZFy)*z<The bulk write operations interface.

.. versionadded:: 2.7
    )annotationsN)MutableMapping)islice)TYPE_CHECKINGAnyIteratorMappingNoReturnOptionalTypeUnion)ObjectId)RawBSONDocument)SON)_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y)	_Runz'Represents a batch of write operations.c                <    || _         g | _        g | _        d| _        y)zInitialize a new Run object.r   N)op_type	index_mapops
idx_offset)selfr<   s     \/var/www/html/phonemate/phone_mate_backend/venv/lib/python3.12/site-packages/pymongo/bulk.py__init__z_Run.__init__R   s    #$&      c                     | j                   |   S )zGet the original index of an operation in this run.

        :Parameters:
          - `idx`: The Run index that maps to the original index.
        )r=   )r@   idxs     rA   indexz
_Run.indexY   s     ~~c""rC   c                p    | j                   j                  |       | j                  j                  |       y)zAdd an operation to this Run instance.

        :Parameters:
          - `original_index`: The original index of this operation
            within a larger bulk operation.
          - `operation`: The operation document.
        N)r=   appendr>   )r@   original_index	operations      rA   addz_Run.adda   s&     	n-	"rC   N)r<   r+   returnNone)rE   r+   rL   r+   )rI   r+   rJ   r   rL   rM   )__name__
__module____qualname____doc__rB   rF   rK    rC   rA   r:   r:   O   s    1!#	#rC   r:   c                
   |j                  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|j                  d      }|r^t        |      }|D ]  }| j                  |d   |z         |d<    |d   j                  |       |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<   |j                  d
      }|rY|D ]T  }|j                         }	|d   |z   }
| j                  |
      |	d<   | j                  |
   |	d<   |d
   j                  |	       V t        |      }|r|d   j                  |       yy)z7Merge a write command result into the full bulk result.nr   	nInsertednRemovedupsertedrF   	nUpsertednMatched	nModifiedwriteErrorsopwriteConcernErrorsN)getr<   r   r   r    lenrF   extendcopyr>   rH   r   )runfull_resultoffsetresultaffectedrW   
n_upserteddocwrite_errorsreplacementrE   wces               rA   _merge_commandrl   m   s    zz#q!H
{{gK H, 		J8+		::j)XJ"yyW)>?G  
#**84$
2$
#x*'<<#
#x/#K F;$77 ::m,LC((*Kg,'C#&99S>K  #K&--k:   v
C
()005 rC   c                N    | d   r| d   j                  d        t        |       )z5Raise a BulkWriteError from the full bulk api result.r[   c                    | d   S )NrF   rR   )errors    rA   <lambda>z)_raise_bulk_write_error.<locals>.<lambda>   s    %.rC   )key)sortr   )rc   s    rA   _raise_bulk_write_errorrs      s,    =!M"'',H'I

%%rC   c                  \   e Zd ZdZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZedd       ZddZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ		 	 d	 	 	 	 	 	 	 	 	 ddZ
dd	Zdd
Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 	 	 d dZ	 	 	 	 	 	 	 	 d dZd!dZy)"_Bulkz'The private guts of the bulk write API.Nc                   |j                  |j                  j                  dt                    | _        || _        | j
                   t        j                  d| j
                         || _        || _	        g | _
        d| _        || _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        y)zInitialize a _Bulk instance.replace)unicode_decode_error_handlerdocument_class)codec_optionsNletFT)with_optionsrz   _replacedict
collectionr{   r   r   commentorderedr>   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)r@   r   r   bypass_document_validationr   r{   s         rA   rB   z_Bulk.__init__   s     %11$22;;-6t <  2 

 88,,UDHH=&-8:8#"' % % ',$rC   c                    | j                   j                  j                  j                  }|r|j                  st
        S t        S N)r   databaseclient
_encrypter_bypass_auto_encryptionr"   r!   )r@   	encrypters     rA   bulk_ctx_classz_Bulk.bulk_ctx_class   s4    OO,,33>>	Y>>--$$rC   c                    t        d|       t        |t              sd|v st               |d<   | j                  j                  t        |f       y)z*Add an insert document to the list of ops.document_idN)r   
isinstancer   r   r>   rH   r   )r@   r   s     rA   
add_insertz_Bulk.add_insert   s=    !*h78_5(9J&jHUO(+,rC   c                    t        |       t        d|fd|fd|fd|fg      }|d| _        ||d<   |d| _        ||d<   |d| _        ||d	<   |rd
| _        | j                  j                  t        |f       y)z8Create an update document and add it to the list of ops.qumultiupsertNT	collationarrayFiltershintF)	r   r~   r   r   r   r   r>   rH   r    )	r@   selectorr6   r   r   r   array_filtersr   cmds	            rA   
add_updatez_Bulk.add_update   s     	v&"8_sFmgu-=&?QR
  "&D(C$&*D#"/C$(D!CK %D#'rC   c                    t        |       t        d|fd|fdd|fg      }|d| _        ||d<   |d| _        ||d<   | j                  j                  t        |f       y)	z8Create a replace document and add it to the list of ops.r   r   )r   Fr   NTr   r   )r   r   r   r   r>   rH   r    )r@   r   rj   r   r   r   r   s          rA   add_replacez_Bulk.add_replace   sr     	 ,C?S+$68H8U[J\]^ "&D(C$(D!CK#'rC   c                    t        d|fd|fg      }|d| _        ||d<   |d| _        ||d<   |t        k(  rd| _        | j
                  j                  t        |f       y)z7Create a delete document and add it to the list of ops.r   limitNTr   r   F)r   r   r   r,   r   r>   rH   r   )r@   r   r   r   r   r   s         rA   
add_deletez_Bulk.add_delete  so     C?We$456 "&D(C$(D!CKK %D#'rC   c              #     K   d}t        | j                        D ]F  \  }\  }}|t        |      }n|j                  |k7  r| t        |      }|j	                  ||       H | yw)ziGenerate batches of operations, batched by type of
        operation, in the order **provided**.
        N)	enumerater>   r:   r<   rK   )r@   rb   rE   r<   rJ   s        rA   gen_orderedz_Bulk.gen_ordered  sg      )2488)<%C%'9{7m'	7mGGC# *= 	s   A&A(c              #     K   t        t              t        t              t        t              g}t	        | j
                        D ]  \  }\  }}||   j                  ||        |D ]  }|j
                  s|  yw)zbGenerate batches of operations, batched by type of
        operation, in arbitrary order.
        N)r:   r   r    r   r   r>   rK   )r@   
operationsrE   r<   rJ   rb   s         rA   gen_unorderedz_Bulk.gen_unordered&  sj      7mT']DMB
)2488)<%C%'9w##C3 *= Cww	 s   A3A=6A=c	           
        | j                   j                  j                  }	| j                   j                  j                  }
|
j                  }| j
                  st        |      | _        d | _        | j
                  }|j                  |
|       d}|rB| j                  st        |d       | _        | j                  d}t        |j                     }| j                  |	||||||j                  | j                   j                        }|j                  t        |j                         k  r|r+t        |j                         |j                  z
  dk(  r|xs |}t#        || j                   j                  fd| j$                  fg      }| j&                  r| j&                  |d<   t)        j*                  ||       | j,                  rd|d<   | j.                  '|j                  t0        t2        fv r| j.                  |d<   |rG|r#| j4                  s|j7                          d| _        |j9                  ||t:        j<                  |       |j?                  |||
       |jA                  |       |jC                  |
|       tE        |j                   |j                  d       }|jF                  r|jI                  |||
      \  }}|jK                  di       }|jK                  d	d
      tL        v r8tO        jP                  |      }tS        |||j                  |       tU        |       tS        |||j                  |       d| _	        d| _        | j$                  rd|v rnU|jW                  |||
      }|xj                  t        |      z  c_        |j                  t        |j                         k  r| j$                  r|d   ry | j                  x| _        }|rAy y )NFTr-   r   r   bypassDocumentValidationr{   writeConcernErrorcoder   r[   ),r   r   namer   _event_listenersr   nextr   validate_sessionr   r8   r<   r   rz   r?   r_   r>   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executer^   r   ra   deepcopyrl   rs   execute_unack)r@   	generatorwrite_concernsessionconnop_id	retryablerc   final_write_concerndb_namer   	listenersrb   last_runcmd_namebwcr   r>   re   to_sendrk   fulls                         rA   _execute_commandz_Bulk._execute_command2  s7    //**//))00++	#ID DM 	fg.== $Y 5==(#H -H%%--	C ..3sww</ SWW!>1 D$7$H=MHdoo&:&:;i=VWX<<%)\\C	N))#}=&&6:C2388'CKKGW;M,M!%CJ !)E)E6687;4%%c9n6L6LdS&&sGV<##C(""63/SWWcnnd; !--&)kk#sF&COFG !**%8"=Cwwvq)-CC  $}}[9&sD#..&I/5"3S^^VL %*DM38D0||(?!//S&AG#g,.g ..3sww</n ||M :%)]]2DsY rC   c           	         g g dddddg dt               	 	 	 	 	 	 	 	 d fd} j                  j                  j                  }|j	                   j
                  ||        d   sd   rt               S )zExecute using write commands.r   r[   r]   rU   rX   rY   rZ   rV   rW   c           	     4    j                  | ||       y r   )r   )r   r   r   rc   r   r   r@   r   s      rA   retryable_bulkz-_Bulk.execute_command.<locals>.retryable_bulk  s(     !!rC   )bulkr[   r]   )r   Optional[ClientSession]r   r'   r   boolrL   rM   )r#   r   r   r   _retryable_writer   rs   )r@   r   r   r   r   r   rc   r   s   ```   @@rA   execute_commandz_Bulk.execute_command  s     "$	
 
	,	4>	KO		 	 ))00 1 1>7QUV}%5I)J#K0rC   c           
     h   | j                   j                  j                  }| j                   j                  j                  }|j                  }t               }| j                  st        |      | _        | j                  }|r1t        |j                     }| j                  |||||d|j                  | j                   j                        }	|j                  t        |j                        k  rt        || j                   j                  fddddifg      }
|j!                  |
       t#        |j                  |j                  d      }|	j%                  |
||      }|xj                  t        |      z  c_        |j                  t        |j                        k  rt        |d      x| _        }|r0yy)zCExecute write commands with OP_MSG and w=0 writeConcern, unordered.N)r   FwriteConcernwr   )r   r   r   r   r   r#   r   r   r8   r<   r   rz   r?   r_   r>   r   r   r   r   )r@   r   r   r   r   r   r   rb   r   r   r   r>   r   s                rA   execute_op_msg_no_resultsz_Bulk.execute_op_msg_no_results  sh   //**//))00++	
#ID -H%%--	C ..3sww</!4??#7#78*'#q2 ##C(SWWcnnd;++Cf=#g,. ..3sww</ &*)T%::Ds5 rC   c           
         g g dddddg d}t               }t               }	 | j                  ||d||d||       y# t        $ r Y yw xY w)zAExecute write commands with OP_MSG and w=0 WriteConcern, ordered.r   r   NF)r%   r#   r   r   )r@   r   r   r   rc   initial_write_concernr   s          rA   execute_command_no_resultsz _Bulk.execute_command_no_results  ss     "$	
 !-
	!!%	   		s   : 	AAc                   | j                   rt        d      | j                  rt        d      |xr |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| j                  |||      S | j                  ||      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.r1   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   )r@   r   r   r   unacks        rA   execute_no_resultsz_Bulk.execute_no_results  s     $%Z[[""$%]^^@m&@&@"@T**t/D/Dq/H$b  T**t/D/Dq/H$b  "Y  <<224MRR--dI>>rC   c                   | j                   st        d      | j                  rt        d      d| _        |xs | j                  j                  }t        ||      }| j                  r| j                         }n| j                         }| j                  j                  j                  }|j                  s/|j                  |      5 }| j                  |||       	 ddd       y| j                  |||      S # 1 sw Y   yxY w)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   )r@   r   r   r   r   
connections         rA   r   z_Bulk.execute+  s    xx"#=>>=="#OPP%F)F)F1'=I<<((*I**,I))00))((1Z''
I}M 21 ''	='JJ	 21s   C77D )NN)r   zCollection[_DocumentType]r   r   r   r   r   zOptional[str]r{   zOptional[Any]rL   rM   )rL   zType[_BulkWriteContext])r   r(   rL   rM   )FFNNN)r   Mapping[str, Any]r6   z#Union[Mapping[str, Any], _Pipeline]r   r   r   r   r   Optional[Mapping[str, Any]]r   z!Optional[list[Mapping[str, Any]]]r   Union[str, SON[str, Any], None]rL   rM   )FNN)r   r   rj   r   r   r   r   r   r   r   rL   rM   )
r   r   r   r+   r   r   r   r   rL   rM   )rL   zIterator[Optional[_Run]])rL   zIterator[_Run]r   )r   Iterator[Any]r   r%   r   r   r   r'   r   r+   r   r   rc   MutableMapping[str, Any]r   zOptional[WriteConcern]rL   rM   )r   r   r   r%   r   r   rL   zdict[str, Any])r   r'   r   r   rL   rM   )r   r'   r   r   r   r%   rL   rM   )r   r%   r   r   rL   r   )rN   rO   rP   rQ   rB   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   rR   rC   rA   ru   ru      sX   1 "&!-  %)	
   
B % %- 15;?04(#( 4( 	(
 ( /( 9( .( 
(B 1504(#( '( 	(
 /( .( 
(. 2604(#( ( /	(
 .( 
((
* 7;e3 e3 $e3 )	e3
 e3 e3 e3 .e3 4e3 
e3N& & $& )	&
 
&P%;N"" !" $	"
 
"H?? !? $	?
 
?>KrC   ru   )
rb   r:   rc   r   rd   r+   re   r   rL   rM   )rc   r(   rL   r
   )GrQ   
__future__r   ra   collections.abcr   	itertoolsr   typingr   r   r   r	   r
   r   r   r   bson.objectidr   bson.raw_bsonr   bson.sonr   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.   r0   r2   r4   r8   r:   rl   rs   ru   rR   rC   rA   <module>r     s   #  * 	 	 	 # )  ! Q 
  A  4 .-'FFS S  
C   c "@	 @# #<)6	)6))6 )6 	)6
 
)6X&`K `KrC   