
    FCfR                       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"      ZA	 	 	 	 	 	 	 	 	 	 d'd#ZBd(d$ZC G d% d&      ZDy))z<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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     L/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pymongo/bulk.py__init__z_Run.__init__Q   s    #$&      c                     | 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?   idxs     r@   indexz
_Run.indexX   s    
 ~~c""rB   c                p    | j                   j                  |       | j                  j                  |       y)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?   original_index	operations      r@   addz_Run.add_   s&     	n-	"rB   N)r;   r*   returnNone)rD   r*   rK   r*   )rH   r*   rI   r   rK   rL   )__name__
__module____qualname____doc__rA   rE   rJ    rB   r@   r9   r9   N   s    1!##rB   r9   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upsertedrE   	nUpsertednMatched	nModifiedwriteErrorsopwriteConcernErrorsN)getr;   r   r   r   lenrE   extendcopyr=   rG   r   )runfull_resultoffsetresultaffectedrV   
n_upserteddocwrite_errorsreplacementrD   wces               r@   _merge_commandrk   j   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 rB   c                    | d   rM| d   j                  d        | d   d   }|d   }|d   }|dk(  r |j                  d      rd	}t        |||       t        |       )
z5Raise a BulkWriteError from the full bulk api result.rZ   c                    | d   S )NrE   rQ   )errors    r@   <lambda>z)_raise_bulk_write_error.<locals>.<lambda>   s
    %. rB   )keyr   codeerrmsg   zTransaction numberszrThis MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.)sort
startswithr   r   )rb   errrq   msgrr   s        r@   _raise_bulk_write_errorrx      s}     =!M"'',H'I-(+6{(m2:#..)>?- 
 #64==

%%rB   c                  p   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_optionsr   _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         r@   rA   z_Bulk.__init__   s     %11$22;;-6t <  2 

 88,,UDHH=&-8:8#"' % % ',$rB   c                    | j                   j                  j                  j                  }|r|j                  st
        S t        S N)r   databaseclient
_encrypter_bypass_auto_encryptionr!   r    )r?   	encrypters     r@   bulk_ctx_classz_Bulk.bulk_ctx_class   s4    OO,,33>>	Y>>--$$rB   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=   rG   r   )r?   r   s     r@   
add_insertz_Bulk.add_insert   s=    !*h78_5(9J&jHUO(+,rB   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=   rG   r   )	r?   selectorr5   r   r   r   array_filtersr   cmds	            r@   
add_updatez_Bulk.add_update   s     	v&"8_sFmgu-=&?QR
  "&D(C$&*D#"/C$(D!CK %D#'rB   c                    t        |       ||d|d}|d| _        ||d<   |d| _        ||d<   | j                  j	                  t
        |f       y)z8Create a replace document and add it to the list of ops.F)r   r   r   r   NTr   r   )r   r   r   r=   rG   r   )r?   r   ri   r   r   r   r   s          r@   add_replacez_Bulk.add_replace   s]     	 ,;&Q "&D(C$(D!CK#'rB   c                    ||d}|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=   rG   r   )r?   r   r   r   r   r   s         r@   
add_deletez_Bulk.add_delete  sa     u- "&D(C$(D!CKK %D#'rB   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=   r9   r;   rJ   )r?   ra   rD   r;   rI   s        r@   gen_orderedz_Bulk.gen_ordered   sl      )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)r9   r   r   r   r   r=   rJ   )r?   
operationsrD   r;   rI   ra   s         r@   gen_unorderedz_Bulk.gen_unordered.  st      7mT']DMB
)2488)< 	4%C%'9w##C3	4  	Cww		s   A3A=6A=c	           
        | j                   j                  j                  }	| j                   j                  j                  }
|
j                  }| j
                  st        |      | _        d | _        | j
                  }|j                  |
|       d}|r7| j                  st        |d       | _        | j                  d}t        |j                     }| j                  |	||||||j                  | j                   j                        }|j                  t        |j                         k  rv|r+t        |j                         |j                  z
  dk(  r|xs |}|| j                   j                  d| j"                  i}| j$                  r| j$                  |d<   t'        j(                  ||       | j*                  rd|d<   | j,                  '|j                  t.        t0        fv r| j,                  |d<   |rG|r#| j2                  s|j5                          d| _        |j7                  ||t8        j:                  |       |j=                  |||
       |j?                  |       |jA                  |
|       tC        |j                   |j                  d       }|jD                  r|jG                  |||
      \  }}|jI                  di       }|jI                  d	d
      tJ        v r8tM        jN                  |      }tQ        |||j                  |       tS        |       tQ        |||j                  |       d| _	        d| _        | j"                  rd|v rnU|jU                  |||
      }|xj                  t        |      z  c_        |j                  t        |j                         k  rv| j"                  r|d   ry | j                  x| _        }|r6y y )NFTr,   r   r   bypassDocumentValidationr   writeConcernErrorrq   r   rZ   )+r   r   namer   _event_listenersr   nextr   validate_sessionr   r7   r;   r   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   r`   deepcopyrk   rx   execute_unack)r?   	generatorwrite_concernsessionconnop_id	retryablerb   final_write_concerndb_namer   	listenersra   last_runcmd_namebwcr   r=   rd   to_sendrj   fulls                         r@   _execute_commandz_Bulk._execute_command:  s,    //**//))00++	#ID DM 	fg.== $Y 5==(#H -H%%--	C ..3sww</ SWW!>1 D$7$H=M!5!5y$,,O<<%)\\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 rB   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   rZ   r\   rT   rW   rX   rY   rU   rV   c           	     4    j                  | ||       y r   )r   )r   r   r   rb   r   r   r?   r   s      r@   retryable_bulkz-_Bulk.execute_command.<locals>.retryable_bulk  s(     !!rB   )bulkoperation_idrZ   r\   )r   Optional[ClientSession]r   r&   r   boolrK   rL   )r"   r   r   r   _retryable_writer   rx   )	r?   r   r   r   rI   r   r   rb   r   s	   ```    @@r@   execute_commandz_Bulk.execute_command  s     "$	
 
	,	4>	KO		 	 ))00 	  	
 }%5I)J#K0rB   c           
     T   | j                   j                  j                  }| j                   j                  j                  }|j                  }t               }| j                  st        |      | _        | j                  }|r't        |j                     }| j                  |||||d|j                  | j                   j                        }	|j                  t        |j                        k  r|| j                   j                  dddddii}
|j                  |
       t!        |j                  |j                  d      }|	j#                  |
||      }|xj                  t        |      z  c_        |j                  t        |j                        k  rt        |d      x| _        }|r&yy)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>   r^   r=   r   r   r   )r?   r   r   r   r   r   r   ra   r   r   r   r=   r   s                r@   execute_op_msg_no_resultsz_Bulk.execute_op_msg_no_results  s\   //**//))00++	
#ID -H%%--	C ..3sww</doo22u"S!H
 ##C(SWWcnnd;++Cf=#g,. ..3sww</ &*)T%::Ds1 rB   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   rb   initial_write_concernr   s          r@   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.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   )r?   r   r   r   unacks        r@   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>>rB   c                
   | j                   st        d      | j                  rt        d      d| _        |xs | j                  j                  }t        ||      }| j                  r| j                         }n| j                         }| j                  j                  j                  }|j                  s0|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   rI   r   r   
connections          r@   r   z_Bulk.execute9  s     xx"#=>>=="#OPP%F)F)F1'=I<<((*I**,I))00))(()< 
''
I}M  ''	='9UU	 s   C99D)NN)r   zCollection[_DocumentType]r   r   r   r   r   zOptional[str]r   zOptional[Any]rK   rL   )rK   zType[_BulkWriteContext])r   r'   rK   rL   )FFNNN)r   Mapping[str, Any]r5   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, dict[str, Any], None]rK   rL   )FNN)r   r   ri   r   r   r   r   r   r   r   rK   rL   )
r   r   r   r*   r   r   r   r   rK   rL   )rK   zIterator[Optional[_Run]])rK   zIterator[_Run]r   )r   Iterator[Any]r   r$   r   r   r   r&   r   r*   r   r   rb   MutableMapping[str, Any]r   zOptional[WriteConcern]rK   rL   )
r   r   r   r$   r   r   rI   strrK   zdict[str, Any])r   r&   r   r   rK   rL   )r   r&   r   r   r   r$   rK   rL   )r   r$   r   r   rI   r   rK   r   )rM   rN   rO   rP   rA   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   rQ   rB   r@   rz   rz      s   1 "&!-  %)	
   
B % %- 15;?15(#( 4( 	(
 ( /( 9( /( 
(B 1515(#( '( 	(
 /( /( 
(. 2615(#( ( /	(
 /( 
((
* 7;e3 e3 $e3 )	e3
 e3 e3 e3 .e3 4e3 
e3N. . $. )	.
 . 
.`#;J"" !" $	"
 
"H?? !? $	?
 
?>V#V )V 	V
 
VrB   rz   )
ra   r9   rb   r   rc   r*   rd   r   rK   rL   )rb   r'   rK   r
   )ErP   
__future__r   r`   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   rk   rx   rz   rQ   rB   r@   <module>r
     s   #  * 	 	 	 # ) ! Q 
  A  4 .-'FFS S  
C   c "@	 @# #8)6	)6))6 )6 	)6
 
)6X&$kV kVrB   