
    JKi*                        S r SSKJr  SSKrSSKrSSKrSSKJrJr  SSK	J
r
JrJr  SSKJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  \" S\S9rSS	 jr " S
 S\5      r SS jr!      SS jr"g)z7Utilities for batching operations in a background task.    )annotationsN)CallableIterable)AnyLiteralTypeVar)NOT_PROVIDED	BaseStoreGetOpItemListNamespacesOpMatchConditionNamespacePathNotProvidedOpPutOpResult
SearchItemSearchOp_ensure_refresh_ensure_ttl_validate_namespaceF)boundc                J   ^  [         R                  " T 5      SU 4S jj5       nU$ )Nc                $  > TR                   n [        R                  " 5       nX@R                  L aC  U(       a	  SU SU S3OSn[        R                  " SU R
                  R                    SU S35      e T" U /UQ70 UD6$ ! [         a     Nf = f)NzSpecifically, replace `store.z(...)` with `await store.az(...)zBFor example, replace `store.get(...)` with `await store.aget(...)`zSynchronous calls to z detected in the main event loop. This can lead to deadlocks or performance issues. Please use the asynchronous interface for main thread operations.  )__name__asyncioget_running_loop_loopInvalidStateError	__class__RuntimeError)storeargskwargsmethod_namecurrent_loopreplacement_strfuncs         \/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langgraph/store/base/batch.pywrapper_check_loop.<locals>.wrapper"   s    ==	"335L{{* # 4K=@Z[fZgglm]  
 //+EOO,D,D+E FY ''q*  + E+D+F++  		s   A&B 
BB)r%   AsyncBatchedBaseStorer&   r   r'   r   returnr   )	functoolswraps)r+   r-   s   ` r,   _check_loopr3   !   s%    __T, ,( N    c                    ^  \ rS rSrSrSrSU 4S jjrSS jrSS jrSS.       SS	 jjr	SSS
SSS.             SS jjr
 S\S.           SS jjjr      S S jrSSSSSS.           S!S jjr\S"S j5       r\SS.       SS jj5       r\SSS
SSS.             SS jj5       r\ S\S.           SS jjj5       r\      S S j5       r\SSSSSS.           S!S jj5       rSrU =r$ )#r/   :   z2Efficiently batch operations in a background task.)r!   _aqueue_taskc                   > [         TU ]  5         [        R                  " 5       U l        [        R
                  " 5       U l        S U l        U R                  5         g N)	super__init__r   r    r!   Queuer7   r8   _ensure_task)selfr#   s    r,   r<   AsyncBatchedBaseStore.__init__?   s>    --/
AH*.
r4   c                ~     U R                   (       a  U R                   R                  5         g g ! [         a     g f = fr:   )r8   cancelr$   r?   s    r,   __del__AsyncBatchedBaseStore.__del__F   s5    	zz

!!#  		s   +/ 
<<c                    U R                   b  U R                   R                  5       (       aI  U R                  R                  [	        U R
                  [        R                  " U 5      5      5      U l         gg)z1Ensure the background processing loop is running.N)r8   doner!   create_task_runr7   weakrefrefrC   s    r,   r>   "AsyncBatchedBaseStore._ensure_taskM   sK    ::!2!2//T\\7;;tCT0UVDJ "3r4   Nrefresh_ttlc                  #    U R                  5         U R                  R                  5       nU R                  R	                  U[        UU[        U R                  U5      S945        UI S h  vN $  N7fNrM   )r>   r!   create_futurer7   
put_nowaitr   r   
ttl_config)r?   	namespacekeyrN   futs        r,   agetAsyncBatchedBaseStore.agetR   sg      	jj&&( / M		
 yys   A*A3,A1-A3
   r   queryfilterlimitoffsetrN   c                 #    U R                  5         U R                  R                  5       nU R                  R	                  U[        UUUUU[        U R                  U5      S945        UI S h  vN $  N7frP   )r>   r!   rQ   r7   rR   r   r   rS   )r?   namespace_prefixr[   r\   r]   r^   rN   rV   s           r,   asearchAsyncBatchedBaseStore.asearchg   sp      	jj&&($ / M
	
 yys   A-A6/A40A6ttlc                  #    U R                  5         [        U5        U R                  R                  5       nU R                  R                  U[        XX4[        U R                  U5      S945        UI S h  vN $  N7f)Nrc   )	r>   r   r!   rQ   r7   rR   r   r   rS   )r?   rT   rU   valueindexrd   rV   s          r,   aputAsyncBatchedBaseStore.aput   sn      	I&jj&&(Ek$//SV6W	
 yys   A5A>7A<8A>c                   #    U R                  5         U R                  R                  5       nU R                  R	                  U[        XS 5      45        UI S h  vN $  N7fr:   )r>   r!   rQ   r7   rR   r   )r?   rT   rU   rV   s       r,   adeleteAsyncBatchedBaseStore.adelete   sM     
 	jj&&(eID&A BCyys   AA AA d   prefixsuffix	max_depthr]   r^   c               \  #    U R                  5         U R                  R                  5       n/ nU(       a  UR                  [	        SUS95        U(       a  UR                  [	        SUS95        [        [        U5      UUUS9nU R                  R                  Xh45        UI S h  vN $  N7f)Nro   )
match_typepathrp   )match_conditionsrq   r]   r^   )	r>   r!   rQ   appendr   r   tupler7   rR   )	r?   ro   rp   rq   r]   r^   rV   ru   ops	            r,   alist_namespaces&AsyncBatchedBaseStore.alist_namespaces   s      	jj&&(##NhV$TU##NhV$TU"#34	
 		*yys   B#B,%B*&B,c                ~    [         R                  " U R                  U5      U R                  5      R	                  5       $ r:   )r   run_coroutine_threadsafeabatchr!   result)r?   opss     r,   batchAsyncBatchedBaseStore.batch   s+    //C0@$**MTTVVr4   c               |    [         R                  " U R                  XUS9U R                  5      R	                  5       $ )N)rU   rN   )r   r|   rW   r!   r~   )r?   rT   rU   rN   s       r,   getAsyncBatchedBaseStore.get   s4     //IIikIBDJJ

&(	r4   c                  [         R                  " U R                  UUUUUUS9U R                  5      R	                  5       $ )NrZ   )r   r|   ra   r!   r~   )r?   r`   r[   r\   r]   r^   rN   s          r,   searchAsyncBatchedBaseStore.search   sL     //LL '   JJ

 &(
	r4   c                   [        U5        [        R                  " U R                  UUUU[	        U R
                  U5      S9U R                  5      R                  5         g )N)rU   rf   rg   rd   )r   r   r|   rh   r   rS   r!   r~   )r?   rT   rU   rf   rg   rd   s         r,   putAsyncBatchedBaseStore.put   sW     	I&((II5   JJ		
 &(r4   c                |    [         R                  " U R                  XS9U R                  5      R	                  5         g )N)rU   )r   r|   rk   r!   r~   )r?   rT   rU   s      r,   deleteAsyncBatchedBaseStore.delete   s-     	((LLL,djj	

&(r4   c          
         [         R                  " U R                  UUUUUS9U R                  5      R	                  5       $ )Nrn   )r   r|   ry   r!   r~   )r?   ro   rp   rq   r]   r^   s         r,   list_namespaces%AsyncBatchedBaseStore.list_namespaces  sK     //!!# "  JJ	
 &(		r4   )r7   r!   r8   )r0   None)rT   tuple[str, ...]rU   strrN   bool | Noner0   zItem | None)r`   r   r[   z
str | Noner\   zdict[str, Any] | Noner]   intr^   r   rN   r   r0   zlist[SearchItem]r:   )rT   r   rU   r   rf   zdict[str, Any]rg   z!Literal[False] | list[str] | Nonerd   zfloat | None | NotProvidedr0   r   )rT   r   rU   r   r0   r   )ro   NamespacePath | Nonerp   r   rq   z
int | Noner]   r   r^   r   r0   zlist[tuple[str, ...]])r   zIterable[Op]r0   zlist[Result])r   
__module____qualname____firstlineno____doc__	__slots__r<   rD   r>   rW   ra   r	   rh   rk   ry   r3   r   r   r   r   r   r   __static_attributes____classcell__)r#   s   @r,   r/   r/   :   s   <-IW $(" 
 ! 
4 !(,#')
  &   ! 
B 48 +7"  	
 1 ( 
,"  
	 (,'+ $ % %	
    
4 W W  $(	"	 	
 !	 
	 	  !(,#')
  &   ! 
 .  48 +7"  	
 1 ( 
 * "  
	   (,'+ $ % %	
    
 r4   r/   c                   [        U 5      S::  a  S[        U 5      4$ / n/ n0 nU  H  n[        U[        [        [
        45      (       a#   UR                  UR                  U5      5        MF  [        U[        5      (       ar  UR                  UR                  4nXS;   a  X5   nXAU'   UR                  U5        M  [        U5      X5'   UR                  [        U5      5        UR                  U5        M  UR                  [        U5      5        UR                  U5        M     X!4$ ! [         a0    UR                  [        U5      5        UR                  U5         GM8  f = f)zDedupe operations while preserving order for results.

Args:
    values: List of operations to dedupe

Returns:
    Tuple of (listen indices, deduped operations)
    where listen indices map deduped operation results back to original positions
   N)lenlist
isinstancer   r   r   rv   rg   
ValueErrorr   rT   rU   )valuesdeduppedlistenputsrx   putkeyixs          r,   _dedupe_opsr     s'    6{aT&\!!HF35Db5(,<=>>$hnnR01 E""llBFF+F~\!b!"8}c(m,# MM#h-(OOB+ . '  $c(m,##$s   	 D$$5EEc                ~  #    U R                  5       I S h  vN =n(       Ga  U" 5       =n(       a   U/n U R                  5       =n(       a*  UR                  U5        U R                  5       =n(       a  M*  U Vs/ s H  o"S   PM	     nnU Vs/ s H  o"S   PM	     nn [        U5      u  pxUR                  U5      I S h  vN n	Ub  U V
s/ s H  oU
   PM	     n	n
[        XYSS9 H-  u  pUR                  5       (       a  M  UR                  U5        M/     AOg U R                  5       I S h  vN =n(       a  GM  g g  GN)! [        R                   a     Nf = fs  snf s  snf  Ns  sn
f ! [         a;  nU H+  nUR                  5       (       a  M  UR                  U5        M-      S nANS nAff = f! Af = f N7f)Nr   r   F)strict)r   
get_nowaitrv   r   
QueueEmptyr   r}   ziprG   
set_result	Exceptionset_exception)aqueuer%   itemsitemsfutsr   r   r   resultsr   rV   r~   es                 r,   rI   rI   F  s     

$
$$
$<1<"("3"3"55$5T* #)"3"3"55$5
 -22EDQE2.34edq'e41'26':$F$%HHX$66G)9?"@22;"@ (+4'G"xxzzNN62 (H G 

$
$$
$
$$ ))  34 7"@ ! 1#"xxzz--a0  $1 C %s   F=D?F=F5 ?E 7F5 ;E	F5 E!F5  !E- E&E- E($E- E- F=.F;/F==F=EF5 EF5 &E- (E- -
F27F-F-(F5 -F22F5 5F88F=)r+   r   r0   r   )r   zlist[Op]r0   z!tuple[list[int] | None, list[Op]])r   z(asyncio.Queue[tuple[asyncio.Future, Op]]r%   z weakref.ReferenceType[BaseStore]r0   r   )#r   
__future__r   r   r1   rJ   collections.abcr   r   typingr   r   r   langgraph.store.baser	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r/   r   rI    r4   r,   <module>r      s}    = "    . ( (    & Cx 2^I ^B(V'4'+' 
'r4   