
    &`i                     `    d dl Z d dlmZmZmZ  e j        e          ZdZ G d d          Z	dS )    N)CallableOptionalSetl       
 c                       e Zd ZdZdedfdeeeej	        ge
f                  de
defdZefded	ej	        fd
Zefded	ej	        fdZefdede
d	ej	        fdZde
fdZefd	ej	        fdZd Zd Zd ZdS )URICachea  Caches URIs up to a specified total size limit.

    URIs are represented by strings. Each URI has an associated size on disk.
    When a URI is added to the URICache, it is marked as "in use".
    When a URI is no longer in use, the user of this class should call
    `mark_unused` to signal that the URI is safe for deletion.

    URIs in the cache can be marked as "in use" by calling `mark_used`.

    Deletion of URIs on disk does not occur until the size limit is exceeded.
    When this happens, URIs that are not in use are deleted randomly until the
    size limit is satisfied, or there are no more URIs that are not in use.

    It is possible for the total size on disk to exceed the size limit if all
    the URIs are in use.

    NF	delete_fnmax_total_size_bytes
debug_modec                     t                      | _        t                      | _        |	d | _        n|| _        d| _        || _        || _        d S )Nc                     dS )Nr    )uriloggers     v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/uri_cache.py<lambda>z#URICache.__init__.<locals>.<lambda>'   s    !     r   )set
_used_uris_unused_uris
_delete_fn_total_size_bytesr	   _debug_mode)selfr   r	   r
   s       r   __init__zURICache.__init__   sV     %(EE&)ee33DOO'DO "#$8! &r   r   r   c                 8   || j         vr|                    d| d           n4| j                            |           | j                             |           |                    d| d           |                     |           |                                  dS )z,Mark a URI as unused and okay to be deleted.zURI z is already unused.Marked URI z unused.N)r   infor   addremove_evict_if_needed_check_validr   r   r   s      r   mark_unusedzURICache.mark_unused2   s    do%%KK7s7778888!!#&&&O""3'''/#///000f%%%r   c                    || j         v rdS || j        v r5| j                             |           | j                            |           nt	          d| d          |                    d| d           |                                  dS )z6Mark a URI as in use. URIs in use will not be deleted.NzGot request to mark URI z0 used, but this URI is not present in the cache.r   z used.)r   r   r   r   
ValueErrorr   r!   r"   s      r   	mark_usedzURICache.mark_used=   s    $/!!FD%%%O$$$$$S))))33 3 3 3   	-#---...r   
size_bytesc                 (   || j         v r| j                             |           | j                            |           | xj        |z  c_        |                     |           |                                  |                    d| d|            dS )z-Add a URI to the cache and mark it as in use.z
Added URI  with size N)r   r   r   r   r   r    r!   r   )r   r   r'   r   s       r   r   zURICache.addL   s    $###$$S)))C   *,f%%%=====>>>>>r   returnc                     | j         S N)r   r   s    r   get_total_size_byteszURICache.get_total_size_bytesX   s    %%r   c                    | j         r|                                 | j        k    rt          t	          | j                             }| j                             |           |                     ||          }| xj        |z  c_        |                    d| d| d           | j         r!|                                 | j        k    dS dS dS dS )z?Evict unused URIs (if they exist) until total size <= max size.zDeleted URI r)   .N)	r   r.   r	   nextiterr   r   r   r   )r   r   arbitrary_unused_urinum_bytes_deleteds       r   r    zURICache._evict_if_needed[   s     	))++d.GGG $(T->(?(?#@#@ $$%9::: $0Df M M""&77""KKX3XXDUXXX   	))++d.GGGGG	 	 	 	GGr   c                 Z    | j         r!| j        | j        z  t                      k    sJ dS dS )z>(Debug mode only) Check "used" and "unused" sets are disjoint.N)r   r   r   r   r-   s    r   r!   zURICache._check_validj   s?     	@?T%66#%%????	@ 	@??r   c                 &    || j         v p|| j        v S r,   )r   r   )r   r   s     r   __contains__zURICache.__contains__o   s    do%A0A)AAr   c                 *    t          | j                  S r,   )str__dict__r-   s    r   __repr__zURICache.__repr__r   s    4=!!!r   )__name__
__module____qualname____doc__ DEFAULT_MAX_URI_CACHE_SIZE_BYTESr   r   r9   loggingLoggerintboolr   default_loggerr#   r&   r   r.   r    r!   r7   r;   r   r   r   r   r   	   s        ( EI$D 	& &Hc7>%:C%?@A& "& 	& & & &, >L 	 	s 	GN 	 	 	 	 <J  S '.     GU 
? 
?s 
? 
?W^ 
? 
? 
? 
?&c & & & & 9G  w~    @ @ @
B B B" " " " "r   r   )
rA   typingr   r   r   	getLoggerr<   rE   r@   r   r   r   r   <module>rH      s     * * * * * * * * * *""8,,#1  j" j" j" j" j" j" j" j" j" j"r   