
    nhq                       d Z ddlmZ ddlZddl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 ddlmZ ddlmZ dd	lmZmZ dd
lmZmZmZ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) ddl*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z? erddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZGmHZHmIZI dZJ G d d      ZKy)z<The bulk write operations interface.

.. versionadded:: 2.7
    )annotationsN)MutableMapping)islice)TYPE_CHECKINGAnyIteratorMappingOptionalTypeUnion)ObjectId)RawBSONDocument)_csotcommon)	_COMMANDS_DELETE_ALL_merge_command_raise_bulk_write_error_Run)validate_is_document_typevalidate_ok_for_replacevalidate_ok_for_update)ConfigurationErrorInvalidOperationNotPrimaryErrorOperationFailure)_RETRYABLE_ERROR_CODES)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_DELETE_INSERT_UPDATE_BulkWriteContext_convert_exception_convert_write_result_EncryptedBulkWriteContext_randint)ReadPreference)ClientSession_validate_session_write_concern)_handle_reauth)WriteConcern)
Collection)MongoClient)
Connection)_DocumentOut_DocumentType	_PipelineTc                     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e	 	 	 	 	 	 	 	 	 	 	 	 	 	 d d       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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| _        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orderedopsexecutedbypass_doc_valuses_collationuses_array_filtersuses_hint_updateuses_hint_delete	uses_sortis_retryableretryingstarted_retryable_writecurrent_runnext_runis_encrypted)selfr?   rA   bypass_document_validationr@   r;   s         ~/var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/synchronous/bulk.py__init__z_Bulk.__init__V   s     %11$22;;-6t <  2 

 88,,UDHH=&-8:8#"' % % ',$!    c                    | j                   j                  j                  j                  }|r|j                  sd| _        t        S d| _        t        S )NTF)r?   databaseclient
_encrypter_bypass_auto_encryptionrO   r'   r$   )rP   	encrypters     rR   bulk_ctx_classz_Bulk.bulk_ctx_classy   sD    OO,,33>>	Y>> $D-- %D$$rT   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   rB   appendr"   )rP   r]   s     rR   
add_insertz_Bulk.add_insert   s=    !*h78_5(9J&jHUO(+,rT   c	                    t        |       |||d}	|||	d<   |d| _        ||	d<   |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multiNupsertT	collationarrayFiltershintsortF)	r   rE   rF   rG   rI   rJ   rB   r`   r#   )
rP   selectorupdatere   rf   rg   array_filtersri   rj   cmds
             rR   
add_updatez_Bulk.add_update   s     	v&$,6EJ"CM "&D(C$&*D#"/C$(D!CK!DNCK %D#'rT   c                    t        |       ||d}|||d<   |d| _        ||d<   |d| _        ||d<   |d| _        ||d<   | j                  j                  t        |f       y)z8Create a replace document and add it to the list of ops.)rc   rd   Nrf   Trg   ri   rj   )r   rE   rG   rI   rB   r`   r#   )rP   rk   replacementrf   rg   ri   rj   rn   s           rR   add_replacez_Bulk.add_replace   s~     	 ,$,;?"CM "&D(C$(D!CK!DNCK#'rT   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.)rc   limitNTrg   ri   F)rE   rH   r   rJ   rB   r`   r!   )rP   rk   rt   rg   ri   rn   s         rR   
add_deletez_Bulk.add_delete   sa     %-u= "&D(C$(D!CKK %D#'rT   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)	enumeraterB   r   op_typeadd)rP   runidxrx   	operations        rR   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)r   r"   r#   r!   rw   rB   ry   )rP   
operationsr{   rx   r|   rz   s         rR   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                   <   t        j                  t        j                        rt        t        t        j                  |j                  j                  |t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                   d   |j                  j                   d   |j                  j"                         |j$                  r|j'                  |||       	 |j                  j)                  |||j*                        }t,        j,                  j/                         |j0                  z
  }t        j                  t        j                        rt        t        t        j2                  |j                  j                  ||t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                   d   |j                  j                   d   |j                  j"                         |j$                  r|j5                  |||       |j7                  ||j8                         |S # t:        $ r}	t,        j,                  j/                         |j0                  z
  }t=        |	t>        t@        f      r|	jB                  }
ntE        |	      }
t        j                  t        j                        rt        t        t        jF                  |j                  j                  ||
t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                   d   |j                  j                   d   |j                  j"                  t=        |	t@                     |j$                  r|jI                  ||
|       t=        |	t>        t@        f      r&|j7                  |	jB                  |j8                          d}	~	ww xY w)zCA proxy for SocketInfo.write_command that handles event publishing.r      messageclientIdcommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceIdr   r   
durationMSreplyr   r   r   r   r   r   r   r   r   r   r   r   failurer   r   r   r   r   r   r   r   r   isServerSideErrorN)%fieldr   isEnabledForloggingDEBUGr    r   STARTED_topology_settings_topology_idnextiterdb_nameconnidserver_connection_idaddress
service_idpublish_startwrite_commandcodecdatetimenow
start_time	SUCCEEDED_succeed_process_responsesession	Exceptionr_   r   r   detailsr%   FAILED_fail)rP   bwcrn   
request_idmsgdocsrW   r   durationexcr   s              rR   r   z_Bulk.write_command   s    CII''6-5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;JJsJ-5	HH**:sCIIFE((,,.?H++GMM:#1;;#66CC' $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11  {{Z9$$UCKK8@ ?  	((,,.?H#1ABC(+,S1++GMM:#188#66CC'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G$ {{		*gx8#1ABC((ckkB=	s   E5J 
Q	F,QQ	c                d   t        j                  t        j                        rt	        t         t
        j                  |j                  j                  |t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                          |j"                  r|j%                  |||      }	 |j                  j'                  ||      }t(        j(                  j+                         |j,                  z
  }	|t/        |j0                  ||      }
nddi}
t        j                  t        j                        rt	        t         t
        j2                  |j                  j                  |	|
t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                          |j"                  r|j5                  ||
|	       |S # t6        $ r}t(        j(                  j+                         |j,                  z
  }	t9        |t:              r"t/        |j0                  ||j<                        }n(t9        |t>              r|j<                  }ntA        |      }t        j                  t        j                        rt	        t         t
        jB                  |j                  j                  |	|t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                   t9        |t:                     |j"                  r!|j,                  J |jE                  |||	        d}~ww xY w)zAA proxy for Connection.unack_write that handles event publishing.r   r   r   Nokr   r   )#r   r   r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   unack_writer   r   r   r&   namer   r   r   r_   r   r   r   r%   r   r   )rP   r   rn   r   r   max_doc_sizer   rW   resultr   r   r   r   s                rR   r   z_Bulk.unack_writeI  s    ''6-5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;**S*d3C8	XX))#|<F((,,.?H!-chhVD q	"//>' 5 ? ?!'!:!:!G!G#+#$(cO%([[",$.+.88;;+.88+H+H#&88#3#3A#6#&88#3#3A#6"%(("5"5  {{Z9> =  	((,,.?H#/0(=chhS[[(YC1++,S1++GMM:#188#66CC'# $T#Y!$( *'*xx{{'*xx'D'D"xx//2"xx//2!hh11&06F&G" {{~~111		*gx8;	s   E,I6 6
P/ F*P**P/c           	     $   | j                   rV|j                  ||      \  }}}|j                  j                  |j                  |t        d      |j                  |       |S |j                  ||      \  }}	}| j                  ||||	d||       |S )Nr   )w)write_concernr   rW   )rO   batch_commandr   r   r   r-   r   r   )
rP   r   rn   rB   rW   _batched_cmdto_sendr   r   s
             rR   _execute_batch_unackz_Bulk._execute_batch_unack  s     &)&7&7S&A#A{GHH*Q/     (+'8'8c'B$JW S#z37FKrT   c                *   | j                   rX|j                  ||      \  }}}|j                  j                  |j                  ||j
                  |j                  |      }||fS |j                  ||      \  }	}
}| j                  |||	|
||      }||fS )N)r:   r   rW   )rO   r   r   r   r   r   r   r   )rP   r   rn   rB   rW   r   r   r   r   r   r   s              rR   _execute_batchz_Bulk._execute_batch  s     &)&7&7S&A#A{GXX%%!ii & F w (+'8'8c'B$JW''S*c7FSFwrT   c	           
        | j                   j                  j                  }	| j                   j                  j                  }
|
j                  }| j
                  st        |      | _        d | _        | j
                  }|j                  |
|       d}|rC| 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 |}|| j                   j                  d| j"                  i}| j$                  r| j$                  |d<   t'        j(                  ||       | j*                  | j*                  |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nV| jU                  ||||
      }|xj                  t        |      z  c_        |j                  t        |j                         k  r| j"                  r|d   ry | j                  x| _        }|rBy y )NFTr   rA   r@   bypassDocumentValidationr;   writeConcernErrorcoder   writeErrors)+r?   rV   r   rW   _event_listenersrM   r   rN   validate_sessionrK   r   rx   r[   r:   
idx_offsetlenrB   rA   r@   r   apply_write_concernrD   r;   r!   r#   rL   _start_retryable_write	_apply_tor)   PRIMARYsend_cluster_timeadd_server_apiapply_timeoutr   acknowledgedr   getr   copydeepcopyr   r   r   )rP   	generatorr   r   r   op_id	retryablefull_resultfinal_write_concernr   rW   	listenersrz   last_runcmd_namer   rn   rB   r   r   wcefulls                         rR   _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))#}=&&26:6I6IC2388'CKKGW;M,M!%CJ !)E)E6687;4%%c9n6L6LdS&&sGV<##C(""63/SWWcnnd; !--&*&9&9#sC&POFG !**%8"=Cwwvq)-CC  $}}[9&sD#..&I/5"3S^^VL %*DM38D0||(?"77S#vNG#g,.g ..3sww</n ||M :%)]]2DsY rT   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   writeConcernErrors	nInserted	nUpsertednMatched	nModifiednRemovedupsertedc           	     4    j                  | ||       y N)r   )r   r   r   r   r   r   rP   r   s      rR   retryable_bulkz-_Bulk.execute_command.<locals>.retryable_bulkN  s(     !!rT   )bulkoperation_idr   r   )r   Optional[ClientSession]r   r0   r   boolreturnNone)r(   r?   rV   rW   _retryable_writerJ   r   )
rP   r   r   r   r|   r   rW   r   r   r   s
   ```     @@rR   execute_commandz_Bulk.execute_command9  s     "$	
 
	,	4>	KO		 	 ))00## $ 
 }%5I)J#K0rT   c           
     V   | 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.NrA   FwriteConcernr   r   )r?   rV   r   rW   r   r(   rM   r   r   rx   r[   r:   r   r   rB   r   r   r   )rP   r   r   r   rW   r   r   rz   r   r   rn   rB   r   s                rR   execute_op_msg_no_resultsz_Bulk.execute_op_msg_no_resultsi  s^   //**//))00++	
#ID -H%%--	C ..3sww</doo22u"S!H
 ##C(SWWcnnd;33Cc6J#g,. ..3sww</ &*)T%::Ds1 rT   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   )rP   r   r   r   r   initial_write_concernr   s          rR   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      |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.   zPMust be connected to MongoDB 4.2+ to use hint on unacknowledged update commands.   zPMust be connected to MongoDB 8.0+ to use sort on unacknowledged update commands.zGCannot set bypass_document_validation with unacknowledged write concern)rE   r   rF   r   rH   max_wire_versionrG   rI   rD   r   rA   r  r   )rP   r   r   r   unacks        rR   execute_no_resultsz_Bulk.execute_no_results  s    $%Z[[""$%]^^@m&@&@"@T**t/D/Dq/H$b  T**t/D/Dq/H$b  T^^(=(=(B$b  "Y  <<224MRR--dI>>rT   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)rB   r   rC   r?   r   r+   rA   r}   r   rV   rW   r   _conn_for_writesr	  r   )rP   r   r   r|   r   rW   
connections          rR   executez_Bulk.execute  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]rA   r   rQ   Optional[bool]r@   zOptional[str]r;   zOptional[Any]r   r   )r   zType[_BulkWriteContext])r]   r1   r   r   )NNNN)rk   Mapping[str, Any]rl   z#Union[Mapping[str, Any], _Pipeline]re   r   rf   r  rg   Optional[Mapping[str, Any]]rm   z!Optional[list[Mapping[str, Any]]]ri    Union[str, dict[str, Any], None]rj   r  r   r   )NNN)rk   r  rq   r  rf   r  rg   r  ri   r  rj   r  r   r   )
rk   r  rt   intrg   r  ri   r  r   r   )r   zIterator[Optional[_Run]])r   zIterator[_Run])r   r$   rn   MutableMapping[str, Any]r   r  r   bytesr   list[Mapping[str, Any]]rW   r/   r   dict[str, Any])r   r$   rn   r  r   r  r   r  r   r  r   r  rW   r/   r   r  )
r   4Union[_BulkWriteContext, _EncryptedBulkWriteContext]rn   r  rB   r  rW   r/   r   r  )
r   r  rn   r  rB   r  rW   r/   r   z.tuple[dict[str, Any], list[Mapping[str, Any]]]r   )r   Iterator[Any]r   r-   r   r   r   r0   r   r  r   r   r   r  r   zOptional[WriteConcern]r   r   )
r   r  r   r-   r   r   r|   strr   r  )r   r0   r   r  r   r   )r   r0   r   r  r   r-   r   r   )r   r-   r   r   r|   r  r   r   )__name__
__module____qualname____doc__rS   propertyr[   ra   ro   rr   ru   r}   r   r,   r   r   r   r   r   r   r   r  r	  r   rT   rR   r5   r5   S   s   1 "&!!"-!" !" %3	!"
 !" !" 
!"F % %- 26;?15,0(#( 4( 	(
 ( /( 9( /( *( 
(L 2615,0(#( '( 	(
 /( /( *( 
(: 2615(#( ( /	(
 /( 
((
 SS &S 	S
 S &S S 
S SjVV &V 	V
 V V &V V 
%VpA  %	
  
!6A  %	
  
8> 7;e3 e3 $e3 )	e3
 e3 e3 e3 .e3 4e3 
e3N. . $. )	.
 . 
.`#;J"" !" $	"
 
"H!?!? !!? $	!?
 
!?FV#V )V 	V
 
VrT   r5   )Lr  
__future__r   r   r   r   collections.abcr   	itertoolsr   typingr   r   r   r	   r
   r   r   bson.objectidr   bson.raw_bsonr   pymongor   r   pymongo.bulk_sharedr   r   r   r   r   pymongo.commonr   r   r   pymongo.errorsr   r   r   r   pymongo.helpers_sharedr   pymongo.loggerr   r   r    pymongo.messager!   r"   r#   r$   r%   r&   r'   r(   pymongo.read_preferencesr)   "pymongo.synchronous.client_sessionr*   r+   pymongo.synchronous.helpersr,   pymongo.write_concernr-   pymongo.synchronous.collectionr.    pymongo.synchronous.mongo_clientr/   pymongo.synchronous.poolr0   pymongo.typingsr1   r2   r3   _IS_SYNCr5   r  rT   rR   <module>r6     s    #    *    # ) !  
  : M M	 	 	 4 ] 6 .9<3FF\
V \
VrT   