
    -ji?                       S SK Jr  S SKJr  S SKrS SKrS SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJr  S SKJr  \(       a  S SKrS SKJr  S SKJr  O\
" S5      r\
" S5      r\
" S5      r\R:                  " \5      rSr  " S S\RB                  5      r"SS jr#SS jr$SS jr%SS jr&g)    )annotations)datetimeN)TYPE_CHECKING)logging)_LazyImport)distribution_to_json)json_to_distribution)DuplicatedStudyError)UpdateFinishedTrialError)BaseStorage)StudyDirection)FrozenTrial)
TrialState)api_pb2)api_pb2_grpcz,optuna.storages._grpc.auto_generated.api_pb2z1optuna.storages._grpc.auto_generated.api_pb2_grpcgrpcz%Y-%m-%d %H:%M:%S.%fc                     \ rS rSrSS jr      SS jr      SS jr      SS jr      SS jr      SS jr	      SS jr
      SS	 jr      S S
 jr      S!S jr      S"S jr      S#S jr      S$S jr      S%S jr      S&S jr      S'S jr      S(S jr      S)S jr      S*S jr      S+S jrSrg),OptunaStorageProxyService#   c                D    Xl         [        R                  " 5       U l        g )N)_backend	threadingLock_lock)selfstorages     X/home/james-whalen/.local/lib/python3.13/site-packages/optuna/storages/_grpc/servicer.py__init__"OptunaStorageProxyService.__init__$   s    ^^%
    c                   UR                    Vs/ s H7  nU[        R                  :X  a  [        R                  O[        R                  PM9     nnUR
                  n U R                  R                  XES9n[        R                  " WS9$ s  snf ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANYS nAff = f)N)
directions
study_namecodedetailsstudy_id)r"   r   MINIMIZEr   MAXIMIZEr#   r   create_new_studyr
   abortr   
StatusCodeALREADY_EXISTSstrCreateNewStudyReply)r   requestcontextdr"   r#   r(   es           r   CreateNewStudy(OptunaStorageProxyService.CreateNewStudy(   s     ''
' ()G,<,<'<N##.BYBYY' 	 
 ''
	O}}555cH **H==
 $ 	OMMt==s1vMN	Os   >B	B 
C1CCc                   UR                   n U R                  R                  U5        [        R                  " 5       $ ! [         a;  nUR	                  [
        R                  R                  [        U5      S9   S nANUS nAff = fNr$   )r(   r   delete_studyKeyErrorr,   r   r-   	NOT_FOUNDr/   r   DeleteStudyReplyr   r1   r2   r(   r4   s        r   DeleteStudy%OptunaStorageProxyService.DeleteStudy9   sj    
 ##	JMM&&x0 ''))  	JMMt88#a&MI	Js   > 
B1A>>Bc                \    U R                   R                  UR                  UR                  [        R
                  " UR                  5      5        [        R                  " 5       $ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANUS nAff = fr8   )r   set_study_user_attrr(   keyjsonloadsvaluer:   r,   r   r-   r;   r/   r   SetStudyUserAttributeReplyr   r1   r2   r4   s       r   SetStudyUserAttribute/OptunaStorageProxyService.SetStudyUserAttributeE   s{    
	JMM--  '++tzz'--/H
 1133  	JMMt88#a&MI	J   AA& &
B+01B&&B+c                \    U R                   R                  UR                  UR                  [        R
                  " UR                  5      5        [        R                  " 5       $ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANUS nAff = fr8   )r   set_study_system_attrr(   rB   rC   rD   rE   r:   r,   r   r-   r;   r/   r   SetStudySystemAttributeReplyrG   s       r   SetStudySystemAttribute1OptunaStorageProxyService.SetStudySystemAttributeR   s{    
	JMM//  '++tzz'--/H
 3355  	JMMt88#a&MI	JrJ   c                    U R                   R                  UR                  5      n[        R                  " WS9$ ! [         a;  nUR	                  [
        R                  R                  [        U5      S9   S nANTS nAff = f)Nr$   r'   )r   get_study_id_from_namer#   r:   r,   r   r-   r;   r/   r   GetStudyIdFromNameReplyr=   s        r   GetStudyIdFromName,OptunaStorageProxyService.GetStudyIdFromName_   sg    
	J}};;G<N<NOH ..AA  	JMMt88#a&MI	Js   %; 
B 1A;;B c                   UR                   n U R                  R                  U5      nWc   e[        R                  " US9$ ! [         a;  nUR	                  [
        R                  R                  [        U5      S9   S nANYS nAff = f)Nr$   )r#   )r(   r   get_study_name_from_idr:   r,   r   r-   r;   r/   r   GetStudyNameFromIdReply)r   r1   r2   r(   namer4   s         r   GetStudyNameFromId,OptunaStorageProxyService.GetStudyNameFromIdj   sz    
 ##	J==77AD ..$??  	JMMt88#a&MI	Js   A 
B1BBc                   UR                   n U R                  R                  U5      nWc   e[        R                  " U Vs/ s H7  nU[        R                  :X  a  [        R                  O[        R                  PM9     snS9$ ! [         a;  nUR	                  [
        R                  R                  [        U5      S9   S nANS nAff = fs  snf )Nr$   )r"   )r(   r   get_study_directionsr:   r,   r   r-   r;   r/   r   GetStudyDirectionsReplyr   r)   r*   )r   r1   r2   r(   r"   r4   r3   s          r   GetStudyDirections,OptunaStorageProxyService.GetStudyDirectionsx   s    
 ##	J;;HEJ %%%.. $#A %&)@)@$@  gFVFVV#
 	
	  	JMMt88#a&MI	J
s   B >C
C
1CC
c           
         U R                   R                  UR                  5      n[        R                  " WR                  5        VVs0 s H  u  pVU[        R                  " U5      _M     snnS9$ ! [         a;  nUR	                  [
        R                  R                  [        U5      S9   S nANS nAff = fs  snnf )Nr$   )user_attributes)r   get_study_user_attrsr(   r:   r,   r   r-   r;   r/   r   GetStudyUserAttributesReplyitemsrC   dumpsr   r1   r2   
attributesr4   rB   rE   s          r   GetStudyUserAttributes0OptunaStorageProxyService.GetStudyUserAttributes   s    
	J;;G<L<LMJ 22FPFVFVFXYFX
S$**U"33FXY
 	
  	JMMt88#a&MI	J Z   %A3 
#B;
3
B8=1B33B8c           
         U R                   R                  UR                  5      n[        R                  " WR                  5        VVs0 s H  u  pVU[        R                  " U5      _M     snnS9$ ! [         a;  nUR	                  [
        R                  R                  [        U5      S9   S nANS nAff = fs  snnf )Nr$   )system_attributes)r   get_study_system_attrsr(   r:   r,   r   r-   r;   r/   r   GetStudySystemAttributesReplyrd   rC   re   rf   s          r   GetStudySystemAttributes2OptunaStorageProxyService.GetStudySystemAttributes   s    
	J==g>N>NOJ 44HRHXHXHZ[HZ*#sDJJu$55HZ[
 	
  	JMMt88#a&MI	J \rj   c                   U R                   R                  5       n[        R                  " U VVVVs/ s GH  n[        R                  " UR
                  UR                  UR                   Vs/ s H7  nU[        R                  :X  a  [        R                  O[        R                  PM9     snUR                  R                  5        VVs0 s H  u  pgU[        R                  " U5      _M     snnUR                  R                  5        VVs0 s H  u  pgU[        R                  " U5      _M     snnS9PGM     snnnnS9$ s  snf s  snnf s  snnf s  snnnnf )N)r(   r#   r"   ra   rl   )studies)r   get_all_studiesr   GetAllStudiesReplyStudy	_study_idr#   r"   r   r)   r*   
user_attrsrd   rC   re   system_attrs)r   r1   r2   rr   studyr3   rB   rE   s           r   GetAllStudies'OptunaStorageProxyService.GetAllStudies   s.   
 --//1))  % %E "__$// "'!1!1 !1A -.1H1H,H((gN^N^^!1 
 BGAQAQAWAWAY%AY:3TZZ..AY% BGASASAYAYA['A[:3TZZ..A[' %
 	

 %'s/   ;E->E+E
#E - E#E"0EEc                Z   UR                   nS nUR                  (       d  [        UR                  5      n U R                  R                  X45      n[        R                  " WS9$ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANTS nAff = fNr$   )trial_id)r(   template_trial_is_none_from_proto_trialtemplate_trialr   create_new_trialr:   r,   r   r-   r;   r/   r   CreateNewTrialReply)r   r1   r2   r(   r   r~   r4   s          r   CreateNewTrial(OptunaStorageProxyService.CreateNewTrial   s    
 ##--.w/E/EFN	J}}55hOH **H==  	JMMt88#a&MI	Js   A% %
B*/1B%%B*c                t   UR                   nUR                  nUR                  n[        UR                  5      n U R
                  R                  X4XV5        ["        R$                  " 5       $ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANUS nAf[         a;  nUR                  [        R                  R                  [        U5      S9   S nANS nAf[         a;  nUR                  [        R                  R                   [        U5      S9   S nANS nAff = fr8   )r~   
param_nameparam_value_internalr	   distributionr   set_trial_paramr:   r,   r   r-   r;   r/   r   FAILED_PRECONDITION
ValueErrorINVALID_ARGUMENTr   SetTrialParameterReply)r   r1   r2   r~   r   r   r   r4   s           r   SetTrialParameter+OptunaStorageProxyService.SetTrialParameter   s    
 ##''
&;;+G,@,@A	QMM))(@Tc --//  	JMMt88#a&MI' 	TMMtBBCPQFMS 	QMMt??QMP	Qs/   A, ,
D761B,,D791C//D7<1D22D7c                "   UR                   nUR                  n U R                  R                  X45      n[        R                  " WS9$ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANTS nAff = fr}   )r(   trial_numberr   'get_trial_id_from_study_id_trial_numberr:   r,   r   r-   r;   r/   r   %GetTrialIdFromStudyIdTrialNumberReply)r   r1   r2   r(   r   r~   r4   s          r    GetTrialIdFromStudyIdTrialNumber:OptunaStorageProxyService.GetTrialIdFromStudyIdTrialNumber   s|    
 ##++	J}}LLH
 <<hOO  	JMMt88#a&MI	Js   A	 	
B1B		Bc                   UR                   nUR                  nUR                  (       a  [        UR                  5      OS n U R                  R                  U[        U5      U5      n[        R                   " WS9$ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANTS nAf[         a;  nUR                  [        R                  R                  [        U5      S9   S nANS nAff = f)Nr$   )trial_updated)r~   statevalueslistr   set_trial_state_values_from_proto_trial_stater:   r,   r   r-   r;   r/   r   r   r   SetTrialStateValuesReply)r   r1   r2   r~   r   r   r   r4   s           r   SetTrialStateValues-OptunaStorageProxyService.SetTrialStateValues   s    
 ##)0gnn%T	T MM@@1%8&M //mLL	  	JMMt88#a&MI' 	TMMtBBCPQFMS	Ts$   &A< <
D1B<<D	1C??Dc                   UR                   nUR                  nUR                  n U R                  R	                  X4U5        [        R                  " 5       $ ! [
         a;  nUR                  [        R                  R                  [        U5      S9   S nANUS nAf[         a;  nUR                  [        R                  R                  [        U5      S9   S nANS nAff = fr8   )r~   stepintermediate_valuer   set_trial_intermediate_valuer:   r,   r   r-   r;   r/   r   r   r   SetTrialIntermediateValueReply)r   r1   r2   r~   r   r   r4   s          r   SetTrialIntermediateValue3OptunaStorageProxyService.SetTrialIntermediateValue  s    
 ##||$77	TMM66xGYZ
 5577	  	JMMt88#a&MI' 	TMMtBBCPQFMS	Ts#   A 
C!1BC$1CCc                   UR                   nUR                  n[        R                  " UR                  5      n U R
                  R                  X4U5        [        R                   " 5       $ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANUS nAf[         a;  nUR                  [        R                  R                  [        U5      S9   S nANS nAff = fr8   )r~   rB   rC   rD   rE   r   set_trial_user_attrr:   r,   r   r-   r;   r/   r   r   r   SetTrialUserAttributeReplyr   r1   r2   r~   rB   rE   r4   s          r   SetTrialUserAttribute/OptunaStorageProxyService.SetTrialUserAttribute  s    
 ##kk

7==)	TMM--hUC
 1133	  	JMMt88#a&MI' 	TMMtBBCPQFMS	T#   A+ +
C351B++C381C..C3c                   UR                   nUR                  n[        R                  " UR                  5      n U R
                  R                  X4U5        [        R                   " 5       $ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANUS nAf[         a;  nUR                  [        R                  R                  [        U5      S9   S nANS nAff = fr8   )r~   rB   rC   rD   rE   r   set_trial_system_attrr:   r,   r   r-   r;   r/   r   r   r   SetTrialSystemAttributeReplyr   s          r   SetTrialSystemAttribute1OptunaStorageProxyService.SetTrialSystemAttribute'  s    
 ##kk

7==)	TMM//uE
 3355	  	JMMt88#a&MI' 	TMMtBBCPQFMS	Tr   c                   UR                   n U R                  R                  U5      n[        R                  " [        W5      S9$ ! [         a;  nUR	                  [
        R                  R                  [        U5      S9   S nAN]S nAff = f)Nr$   )trial)r~   r   	get_trialr:   r,   r   r-   r;   r/   r   GetTrialReply_to_proto_trial)r   r1   r2   r~   r   r4   s         r   GetTrial"OptunaStorageProxyService.GetTrial7  ss    
 ##	JMM++H5E $$?5+ABB  	JMMt88#a&MI	Js   A 
B1BBc                   UR                   n[        UR                  5      nUR                  n U R                  R                  USS9nW Vs/ s H0  nUR                  U:  d  UR                  U;   d  M%  [        U5      PM2     n	n[        R                  " U	S9$ ! [         a;  nUR                  [        R                  R                  [        U5      S9   S nANS nAff = fs  snf )NF)deepcopyr$   )trials)r(   setincluded_trial_idstrial_id_greater_thanr   get_all_trialsr:   r,   r   r-   r;   r/   	_trial_idr   r   GetTrialsReply)
r   r1   r2   r(   r   r   r   r4   tfiltered_trialss
             r   	GetTrials#OptunaStorageProxyService.GetTrialsD  s    
 ## !;!;< ' = =	J]]11(U1KF 
{{22akkEW6W OA 	 

 %%_==  	JMMt88#a&MI	J
s#   B $C"5C"
C$1CC)r   r   N)r   r   returnNone)r1   zapi_pb2.CreateNewStudyRequestr2   grpc.ServicerContextr   zapi_pb2.CreateNewStudyReply)r1   zapi_pb2.DeleteStudyRequestr2   r   r   zapi_pb2.DeleteStudyReply)r1   z$api_pb2.SetStudyUserAttributeRequestr2   r   r   z"api_pb2.SetStudyUserAttributeReply)r1   z&api_pb2.SetStudySystemAttributeRequestr2   r   r   z$api_pb2.SetStudySystemAttributeReply)r1   z!api_pb2.GetStudyIdFromNameRequestr2   r   r   zapi_pb2.GetStudyIdFromNameReply)r1   z!api_pb2.GetStudyNameFromIdRequestr2   r   r   zapi_pb2.GetStudyNameFromIdReply)r1   z!api_pb2.GetStudyDirectionsRequestr2   r   r   zapi_pb2.GetStudyDirectionsReply)r1   z%api_pb2.GetStudyUserAttributesRequestr2   r   r   z#api_pb2.GetStudyUserAttributesReply)r1   z'api_pb2.GetStudySystemAttributesRequestr2   r   r   z%api_pb2.GetStudySystemAttributesReply)r1   zapi_pb2.GetAllStudiesRequestr2   r   r   zapi_pb2.GetAllStudiesReply)r1   zapi_pb2.CreateNewTrialRequestr2   r   r   zapi_pb2.CreateNewTrialReply)r1   z api_pb2.SetTrialParameterRequestr2   r   r   zapi_pb2.SetTrialParameterReply)r1   z/api_pb2.GetTrialIdFromStudyIdTrialNumberRequestr2   r   r   z-api_pb2.GetTrialIdFromStudyIdTrialNumberReply)r1   z"api_pb2.SetTrialStateValuesRequestr2   r   r   z api_pb2.SetTrialStateValuesReply)r1   z(api_pb2.SetTrialIntermediateValueRequestr2   r   r   z&api_pb2.SetTrialIntermediateValueReply)r1   z$api_pb2.SetTrialUserAttributeRequestr2   r   r   z"api_pb2.SetTrialUserAttributeReply)r1   z&api_pb2.SetTrialSystemAttributeRequestr2   r   r   z$api_pb2.SetTrialSystemAttributeReply)r1   zapi_pb2.GetTrialRequestr2   r   r   zapi_pb2.GetTrialReply)r1   zapi_pb2.GetTrialsRequestr2   r   r   zapi_pb2.GetTrialsReply)__name__
__module____qualname____firstlineno__r   r5   r>   rH   rN   rS   rY   r^   rh   ro   rz   r   r   r   r   r   r   r   r   r   __static_attributes__ r    r   r   r   #   s   &>.> &> 
%	>"
*+
* &
* 
"	
*454 &4 
,	4676 &6 
.	6	B2	B &	B 
)		B@2@ &@ 
)	@
2
 &
 
)	
(
6
 &
 
-	

8
 &
 
/	

-
 &
 
$	
4>.> &> 
%	>$010 &0 
(	0&P@P &P 
7	P M3M &M 
*	M$898 &8 
0	8 454 &4 
,	4 676 &6 
.	6 C(C &C 
	C>)> &> 
 	>r    r   c                   U [         R                  :X  a  [        R                  $ U [         R                  :X  a  [        R                  $ U [         R                  :X  a  [        R                  $ U [         R
                  :X  a  [        R
                  $ U [         R                  :X  a  [        R                  $ [        SU  35      e)NzUnknown TrialState: )r   RUNNINGr   COMPLETEPRUNEDFAILWAITINGr   r   s    r   _to_proto_trial_stater   Y  s    
"""
###
!!!~~
||
"""
+E73
44r    c                   U [         R                  :X  a  [        R                  $ U [         R                  :X  a  [        R                  $ U [         R                  :X  a  [        R                  $ U [         R
                  :X  a  [        R
                  $ U [         R                  :X  a  [        R                  $ [        SU  35      e)NzUnknown api_pb2.TrialState: )r   r   r   r   r   r   r   r   r   s    r   r   r   g  s    !!!   """   !!!
3E7;
<<r    c                   0 nU R                   R                  5        H%  u  p#U R                  U   R                  U5      X'   M'     [        R
                  " U R                  U R                  [        U R                  5      U R                  U R                  (       a  U R                  R                  [        5      OSU R                  (       a  U R                  R                  [        5      OSU R                  R                  5        VVs0 s H  u  p$U[        U5      _M     snnUU R                   R                  5        VVs0 s H  u  p#U["        R$                  " U5      _M     snnU R&                  R                  5        VVs0 s H  u  p#U["        R$                  " U5      _M     snnU R(                  R                  5        VVs0 s H  u  pSXS_M	     snnS9$ s  snnf s  snnf s  snnf s  snnf )N )r~   numberr   r   datetime_startdatetime_completedistributionsparamsra   rl   intermediate_values)r   rd   r   to_internal_reprr   Trialr   r   r   r   r   r   strftimeDATETIME_FORMATr   r   rw   rC   re   rx   r   )r   r   rB   rE   r   r   s         r   r   r   u  s   Fll((*
))#.??F + ==||#EKK0||>C>R>RE  ))/:XZ BGAXAXE##,,_=^` &+%8%8%>%>%@
%@! %l33%@
 BGBRBRBXBXBZ[BZJCdjj//BZ[DIDVDVD\D\D^_D^jc3

5 11D^_<A<U<U<[<[<]^<][TT[<]^% 

 \_^s   
G#G##G)	G/c                   U R                   (       a%  [        R                  " U R                   [        5      OS nU R                  (       a%  [        R                  " U R                  [        5      OS nU R
                  R                  5        VVs0 s H  u  p4U[        U5      _M     nnn0 nU R                  R                  5        H  u  p4XS   R                  U5      Xc'   M     [        U R                  U R                  [        U R                  5      S U R                  (       a  U R                  OS UUUUU R                   R                  5        VVs0 s H  u  p4U["        R$                  " U5      _M     snnU R&                  R                  5        VVs0 s H  u  p4U["        R$                  " U5      _M     snnU R(                  R                  5        VVs0 s H  u  ptXt_M	     snnS9$ s  snnf s  snnf s  snnf s  snnf )N)r~   r   r   rE   r   r   r   r   r   rw   rx   r   )r   r   strptimer   r   r   rd   r	   r   to_external_reprr   r~   r   r   r   r   ra   rC   rD   rl   r   )r   r   r   rB   rE   r   r   r   s           r   r   r     s   DIDXDX%..@^b 
 "" 	%11?C  <A;N;N;T;T;V;VZS!%((;V   Fll((*
#(99%@ + ||%ekk2$||u||%+#=B=R=R=X=X=Z[=ZzsCE**=Z[?D?V?V?\?\?^_?^c4::e,,?^_<A<U<U<[<[<]^<][TT[<]^ " \_^s   G,#G2#G8G> )r   r   r   api_pb2.TrialState.ValueType)r   r   r   r   )r   r   r   api_pb2.Trial)r   r   r   r   )'
__future__r   r   rC   r   typingr   optunar   optuna._importsr   optuna.distributionsr   r	   optuna.exceptionsr
   r   optuna.storagesr   optuna.study._study_directionr   optuna.trial._frozenr   optuna.trial._stater   r   $optuna.storages._grpc.auto_generatedr   r   
get_loggerr   _loggerr   StorageServiceServicerr   r   r   r   r   r   r    r   <module>r      s    "       ' 5 5 2 6 ' 8 , * <AHIGRSLvD 

X
&(s> C C s>l	5=6r    