
    nhv                       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 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 ddlmZ ddlm Z  ddl!m"Z" er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- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z: ddl;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZB ddlCmDZDmEZEmFZFmGZG ddlHmIZImJZJ ddlKmLZL dZM G d d      ZNy)zIThe client-level bulk write operations interface.

.. versionadded:: 4.9
    )annotationsN)MutableMapping)islice)TYPE_CHECKINGAnyMappingOptionalTypeUnion)ObjectId)RawBSONDocument)_csotcommon)ClientSession_validate_session_write_concern)
Collection)CommandCursor)Database)_handle_reauth)MongoClient)
Connection)_merge_command"_throw_client_bulk_write_exception)validate_is_document_typevalidate_ok_for_replacevalidate_ok_for_update)ConfigurationErrorConnectionFailureInvalidOperationNotPrimaryErrorOperationFailureWaitQueueTimeoutError)_RETRYABLE_ERROR_CODES)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_ClientBulkWriteContext_convert_client_bulk_exception_convert_exception_convert_write_result_randint)ReadPreference)ClientBulkWriteResultDeleteResultInsertOneResultUpdateResult)_DocumentOut	_Pipeline)WriteConcernTc                  
   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
e	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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y)'_ClientBulkz4The private guts of the client-level bulk write API.Nc                   || _         || _        || _        | j                   t        j                  d| j                         || _        || _        || _        || _        g | _	        g | _
        d| _        d| _        d| _        d| _        d| _        | j                   j                   j"                  | _        d| _        d| _        y)z"Initialize a _ClientBulk instance.Nletr   F)clientwrite_concernr7   r   r   orderedbypass_doc_valcommentverbose_resultsops
namespaces
idx_offset	total_opsexecuteduses_collationuses_array_filtersoptionsretry_writesis_retryableretryingstarted_retryable_write)selfr8   r9   r:   bypass_document_validationr<   r7   r=   s           /var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/synchronous/client_bulk.py__init__z_ClientBulk.__init__Y   s     *88,,UDHH=8.8:%' #"' KK//<<',$    c                    t         S N)r'   )rJ   s    rL   bulk_ctx_classz_ClientBulk.bulk_ctx_classx   s    &&rN   c                   t        d|       t        |t              sd|v st               |d<   d|d}| j                  j                  d|f       | j                  j                  |       | xj                  dz  c_        y)z*Add an insert document to the list of ops.document_id)insertrS   rV      N)r   
isinstancer   r   r>   appendr?   rA   )rJ   	namespacerS   cmds       rL   
add_insertz_ClientBulk.add_insert|   sf    !*h78_5(9J&jHUO23(y)!rN   c
                6   t        |       d|||d}
|||
d<   |d| _        ||
d<   |||
d<   |d| _        ||
d<   |	|	|
d	<   |rd
| _        | j                  j                  d|
f       | j                  j                  |       | xj                  dz  c_        y)z8Create an update document and add it to the list of ops.rU   updatefilter
updateModsmultiNupsertTarrayFiltershint	collationsortFr_   rW   )r   rD   rC   rG   r>   rY   r?   rA   )rJ   rZ   selectorr_   rb   rc   rf   array_filtersre   rg   r[   s              rL   
add_updatez_ClientBulk.add_update   s     	v& 	
 "CM$&*D#"/CCK "&D(CCK %D3(y)!rN   c                   t        |       d||dd}|||d<   |||d<   |d| _        ||d<   |||d	<   | j                  j                  d
|f       | j                  j                  |       | xj
                  dz  c_        y)z8Create a replace document and add it to the list of ops.rU   Fr^   Nrc   re   Trf   rg   replacerW   )r   rC   r>   rY   r?   rA   )	rJ   rZ   rh   replacementrc   rf   re   rg   r[   s	            rL   add_replacez_ClientBulk.add_replace   s     	 ,%	
 "CMCK "&D(CCKC()y)!rN   c                    d||d}|||d<   |d| _         ||d<   |rd| _        | j                  j                  d|f       | j                  j                  |       | xj
                  d	z  c_        y)
z7Create a delete document and add it to the list of ops.rU   )deleter`   rb   Nre   Trf   Frp   rW   )rC   rG   r>   rY   r?   rA   )rJ   rZ   rh   rb   rf   re   r[   s          rL   
add_deletez_ClientBulk.add_delete   sv     x%@CK "&D(C %D3(y)!rN   c                	   ||d<   ||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'                  |||j(                        }t*        j*                  j-                         |j.                  z
  }	t        j                  t        j                        rt	        t         t
        j0                  |j                  j                  |	|t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                          |j"                  r|j3                  |||	       | j4                  j7                  ||j8                         |S # t:        $ r}
t*        j*                  j-                         |j.                  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                  |||	       d|
i}t=        |
t@              r1| j4                  j7                  |
jB                  |j8                         n&| j4                  j7                  i |j8                         Y d	}
~
|S d	}
~
ww xY w)
zCA proxy for Connection.write_command that handles event publishing.r>   nsInfor   rW   messageclientIdcommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceIdru   rv   
durationMSreplyrx   ry   rz   r{   r|   r}   r~   r   r   ru   rv   r   failurerx   ry   rz   r{   r|   r}   r~   r   r   isServerSideErrorerrorN)%r$   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_succeedr8   _process_responsesession	ExceptionrX   r    r!   detailsr)   FAILED_fail)rJ   bwcr[   
request_idmsgop_docsns_docsr8   r   durationexcr   s               rL   r   z_ClientBulk.write_command   s?    E
H''6-5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;JJsJ99	?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KK))%=F E  !	?((,,.?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cNE#/0--ckk3;;G--b#++>E!	?s   E?J 
RGQ>>Rc                   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-                         |j.                  z
  }	|t1        |j2                  ||      }
nddi}
t        j                  t        j                        rt	        t         t
        j4                  |j                  j                  |	|
t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                          |j"                  r|j7                  ||
|	       |
S # t8        $ r}t*        j*                  j-                         |j.                  z
  }	t;        |t<              r"t1        |j2                  ||j>                        }n(t;        |t@              r|j>                  }ntC        |      }t        j                  t        j                        rt	        t         t
        jD                  |j                  j                  |	|t        t        |            |j                  |||j                  j                  |j                  j                  |j                  j                  d   |j                  j                  d   |j                  j                   t;        |t<                     |j"                  r!|j.                  J |jG                  |||	       d|i}
Y d}~|
S d}~ww xY w)	zAA proxy for Connection.unack_write that handles event publishing.r   rW   rt   Nokr   r   r   )$r$   r   r   r   r&   r%   r   r   r   r   r   r   r   r   r   r   r   r   r   unack_writemax_bson_sizer   r   r   r*   namer   r   r   rX   r!   r   r    r)   r   r   )rJ   r   r[   r   r   r   r   r8   resultr   r   r   r   s                rL   r   z_ClientBulk.unack_write?  s#    ''6-5522?? cO [[$&#&88;;#&88#@#@88++A.88++A.((-- ;;**S*gw?C9	#XX))#s/@/@AF((,,.?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cNE?	#s   	E6J 
QF-P>>Qc           	     |    |j                  |||      \  }}}}| j                  ||||||| j                         ||fS )z6Executes a batch of bulkWrite server commands (unack).)batch_commandr   r8   )	rJ   r   r[   r>   r?   r   r   to_send_ops
to_send_nss	            rL   _execute_batch_unackz _ClientBulk._execute_batch_unack  sJ     473D3DS#z3Z0
Cjc:sKT[[YJ&&rN   c           	     ~    |j                  |||      \  }}}}| j                  ||||||| j                        }	|	||fS )z4Executes a batch of bulkWrite server commands (ack).)r   r   r8   )
rJ   r   r[   r>   r?   r   r   r   r   r   s
             rL   _execute_batchz_ClientBulk._execute_batch  sQ     473D3DS#z3Z0
Cj##Cj#{JX\XcXcd{J..rN   c                   |j                  d      rt        t        | j                  d      d      }t	        ||d   |j
                  ||du| j                        }|j                  |       	 |D ]  }|d   | j                  z   }| j                  |   \  }	}
|d   s"|d	   j                  |       | j                  r y|d   sT| j                  sa|	d
k(  r|
d   d   }t        |d      }|	dv rd}	t        |dd      }|	dk(  rt        |d      }||	 d   |<    yy# t         $ r4}|j"                  r|j%                          t'        |      |d<   Y d}~yd}~ww xY w)z?Internal helper for processing the server reply command cursor.cursoradminz$cmd.bulkWrite)databaser   N)r   explicit_sessionr<   idxr   writeErrorsrV   rS   rT   T)acknowledged)r_   rl   r_   )r   in_client_bulkrp   Resultsr   )getr   r   r8   r   r   r<   _maybe_pin_connectionr@   r>   rY   r:   r=   r/   r0   r.   r   alivecloser(   )rJ   full_resultr   r   r   coll
cmd_cursordocoriginal_indexop_typeopinserted_idresr   s                 rL   _process_results_cursorz#_ClientBulk._process_results_cursor  s    ::h!$++w7%D 'x !(!4J ,,T2F% OC%(Z$//%AN"&((>":KGRt9}-44S9<<" 4yT%9%9"h.*,Z.*?K"1+D"QC"&;;&.G".sVZ"[C"h.".s"FCKNwiw$78H'O#  J  F##$$&"@"Ew	Fs,   0AD"  D" D" AD" "	E+*EEc           	     	   d}d}	| j                   j                  }
|j                  | j                   |       | j                  ||	|||
|| j                   j                        }| j
                  | j                  k  r_| j                  | j
                  z
  |j                  k  r|xs |}ddi}| j                   |d<   | j                  |d<   |xr |j                   }|s|st        j                  ||       | j                  | j                  |d<   | j                  r| j                  |d<   | j                  r| j                  |d	<   |rG|r#| j                   s|j#                          d
| _        |j%                  ||t&        j(                  |       |j+                  ||| j                          |j-                  |       |j/                  | j                   |       t1        | j2                  | j
                  d      }t1        | j4                  | j
                  d      }|j6                  rJ| j9                  ||||      \  }}}|}|j;                  d      r|d   }t=        |d      xr> t?        |j@                  tB              xr" |j@                  j;                  dd      tD        v }t?        |tF              xr t?        |tH        tJ        f       }|rR|s|rNtM        jN                  |      }tQ        | j2                  | j
                  ||       tS        || j                         n8tQ        | j2                  | j
                  ||       tS        || j                         d|d<   g |d<   |j;                  dd      tU        |      k  rd
|d<   |d   s(||d<   tQ        | j2                  | j
                  ||       y|rw|j;                  di       }|j;                  dd      tD        v rMtM        jN                  |      }tQ        | j2                  | j
                  ||       tS        || j                         | jW                  ||||       tQ        | j2                  | j
                  ||       d| _,        d| _        n| j[                  ||||      \  }}| xj
                  tU        |      z  c_        |d   s| j                  r|d   ry| j
                  | j                  k  r^yy)z<Internal helper for executing batches of bulkWrite commands.r   	bulkWriterW   
errorsOnlyr:   NbypassDocumentValidationr<   r7   Tr   r   coder   r   nErrorsanySuccessfulr   writeConcernErrorF).r8   _event_listenersvalidate_sessionrQ   codec_optionsr@   rA   max_write_batch_sizer=   r:   in_transactionr   apply_write_concernr;   r<   r7   rI   _start_retryable_write	_apply_tor,   PRIMARYsend_cluster_timeadd_server_apiapply_timeoutr   r>   r?   r   r   r   hasattrrX   r   dictr#   r   r    r"   copydeepcopyr   r   lenr   rH   r   )rJ   r9   r   r   op_id	retryabler   final_write_concernr   cmd_name	listenersr   r[   not_in_transactionr>   r?   
raw_resultr   _r   r   retryable_top_level_errorretryable_network_errorfullwces                            rL   _execute_commandz_ClientBulk._execute_command  sK    KK00	 	dkk73!!KK%%
 oo. ~~/33K3KK 3 D} "C$($8$8 8C!\\C	N!(!G1G1G-G!))#}="".262E2E./||!%Ixx!XXE
 T%A%A22437D0!!#y.2H2H$O""3=$t{{C04??D9C$GJ ))-1-@-@c3PZ-[*
K# ::g&"7OEy1 S&u}}d;S!MM--fa8<RR .
 /90/ /Z(BW0XYY , !&?CZ#}}[9&txx$O:4AUAUV&txx+vV:;H\H\]"&w(*}%::i+c+.>>37K0 d|&0F7O"488T__k6R **%8"=Cwwvq)-CC  $}}[9&txx$O:4AUAUV ,,[&$P txx+vN !&/4, "&!:!:3S*!UQOOs;//O 7#]9SI oo.rN   c                     ddg g dddddi i i dt               	 	 	 	 	 	 	 	 d
 fd} j                  j                   j                  |||        d   s
d   sd	   rt	         j
                         S )z'Execute commands with w=1 WriteConcern.FNr   )r   r   r   writeConcernErrors	nInserted	nUpsertednMatched	nModifiednDeletedinsertResultsupdateResultsdeleteResultsc                z    |j                   dk  rt        d      j                  j                  | ||       y )N   <MongoClient.bulk_write requires MongoDB server version 8.0+.)max_wire_versionr   r   r9   )r   r   r   r   r   rJ   s      rL   retryable_bulkz3_ClientBulk.execute_command.<locals>.retryable_bulk|  sK    
 $$r)&R  !!""rN   )bulkoperation_idr   r   r   )r   Optional[ClientSession]r   r   r   boolreturnNone)r+   r8   _retryable_writerG   r   r=   )rJ   r   	operationr  r   r   s   `   @@rL   execute_commandz_ClientBulk.execute_commandf  s     #"$1
 
	,		 	 		$ 	$$ 	% 	
 w;}#=MaAb.{D<P<PQrN   c           	        d}d}| j                   j                  }t               }| j                  |||||d| j                   j                        }| j
                  | j                  k  r
ddi}d|d<   d|d<   | j                  | j                  |d	<   d
di|d<   | j                  r| j                  |d<   | j                  r| j                  |d<   |j                  |       t        | j                  | j
                  d      }t        | j                  | j
                  d      }	| j                  ||||	      \  }
}| xj
                  t        |
      z  c_        | j
                  | j                  k  r	yy)zDExecute commands with OP_MSG and w=0 writeConcern. Always unordered.r   r   NrW   Tr   Fr:   r   wr   writeConcernr<   r7   )r8   r   r+   rQ   r   r@   rA   r;   r<   r7   r   r   r>   r?   r   r   )rJ   r   r   r   r   r   r   r[   r>   r?   r   r   s               rL   execute_command_unackz!_ClientBulk.execute_command_unack  sP   
 KK00	
!!KK%%
 oo."C $C"C	N"".262E2E./#&(C||!%Ixx!XXE
$4??D9C$GJ "66sCjQNKOOs;//O) oo.rN   c                    | j                   rt        d      | j                  rt        d      | j                  t	        d      | j                  |      S )z3Execute all operations, returning no results (w=0).z3Collation is unsupported for unacknowledged writes.z6arrayFilters is unsupported for unacknowledged writes.zGCannot set bypass_document_validation with unacknowledged write concern)rC   r   rD   r;   r!   r  )rJ   r   s     rL   execute_no_resultsz_ClientBulk.execute_no_results  s]    
 $%Z[[""$%]^^*"Y  ))$//rN   c                   | j                   st        d      | j                  rt        d      d| _        t        || j                        }| j                  j
                  s^| j                  j                  ||      5 }|j                  dk  rt        d      | j                  |       t        ddd      cddd       S | j                  ||      }t        || j                  j
                  | j                        S # 1 sw Y   FxY w)zExecute operations.zNo operations to executez*Bulk operations can only be executed once.Tr  r  NF)r>   r   rB   r   r9   r   r8   _conn_for_writesr  r  r-   r  r=   )rJ   r   r  
connectionr   s        rL   executez_ClientBulk.execute  s     xx"#=>>=="#OPP1'4;M;MN!!..--gyA AZ..3*V  ''
3,T5%@A A %%gy9$++  
 	
A As   >8C==D)TNNNF)r8   r   r9   r3   r:   r	  rK   Optional[bool]r<   zOptional[str]r7   zOptional[Any]r=   r	  r
  r  )r
  zType[_ClientBulkWriteContext])rZ   strrS   r1   r
  r  )NNNNN)rZ   r  rh   Mapping[str, Any]r_   z#Union[Mapping[str, Any], _Pipeline]rb   r	  rc   r  rf   Optional[Mapping[str, Any]]ri   z!Optional[list[Mapping[str, Any]]]re    Union[str, dict[str, Any], None]rg   r  r
  r  )NNNN)rZ   r  rh   r  rm   r  rc   r  rf   r  re   r  rg   r  r
  r  )NN)rZ   r  rh   r  rb   r	  rf   r  re   r  r
  r  )r   r'   r[   MutableMapping[str, Any]r   intr   zUnion[bytes, dict[str, Any]]r   list[Mapping[str, Any]]r   r   r8   r   r
  dict[str, Any])r   r'   r[   r  r   r  r   bytesr   r   r   r   r8   r   r
  r  )
r   r'   r[   r!  r>   #list[tuple[str, Mapping[str, Any]]]r?   	list[str]r
  z7tuple[list[Mapping[str, Any]], list[Mapping[str, Any]]])
r   r'   r[   r!  r>   r#  r?   r$  r
  zGtuple[dict[str, Any], list[Mapping[str, Any]], list[Mapping[str, Any]]])
r   r  r   r  r   r   r   r  r
  r  rP   )r9   r3   r   r  r   r   r   r  r   r	  r   r  r   zOptional[WriteConcern]r
  r  )r   r  r  r  r
  r  )r   r   r
  r  )r   r  r  r  r
  r   )__name__
__module____qualname____doc__rM   propertyrQ   r\   rj   rn   rq   r   r   r   r   r   r   r   r  r  r  r   rN   rL   r5   r5   V   s   > 59!%! %-- $- 	-
 %3- - - - 
-> ' '	" "&15;?15,0%% $% 4	%
 % % /% 9% /% *% 
%X "&1515,0 $ '	
  / / * 
H 2615 $ 	
 / / 
, Y$Y &Y 	Y
 *Y )Y )Y Y 
Y YvW$W &W 	W
 W )W )W W 
%Wr
'$
' 
' 1	
'
 
' 
A
'
/$
/ 
/ 1	
/
 
/ 
Q
/1F-1F )1F 	1F
 )1F 
1Fv 7;A#A )A 	A
 A A .A 4A 
AF3(3 3 
"	3j(0(0 
(0T00 
0"
(
 
 
	
rN   r5   )Or(  
__future__r   r   r   r   collections.abcr   	itertoolsr   typingr   r   r   r	   r
   r   bson.objectidr   bson.raw_bsonr   pymongor   r   "pymongo.synchronous.client_sessionr   r   pymongo.synchronous.collectionr   "pymongo.synchronous.command_cursorr   pymongo.synchronous.databaser   pymongo.synchronous.helpersr    pymongo.synchronous.mongo_clientr   pymongo.synchronous.poolr   pymongo._client_bulk_sharedr   r   pymongo.commonr   r   r   pymongo.errorsr   r   r   r    r!   r"   pymongo.helpers_sharedr#   pymongo.loggerr$   r%   r&   pymongo.messager'   r(   r)   r*   r+   pymongo.read_preferencesr,   pymongo.resultsr-   r.   r/   r0   pymongo.typingsr1   r2   pymongo.write_concernr3   _IS_SYNCr5   r*  rN   rL   <module>rD     s    #    *   # ) ! ] 5 < 1 6<3 
  : M M  4  4 .[

 [

rN   