
    &`ic                         d dl 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
mZmZmZ  edddg          Z ed	d
dg          Z G d de          Z G d de          ZdS )    N)ABCMetaabstractmethod)defaultdict
namedtuple)Lock)DictListOptionalTupleStoreStatussuccessversionVersionedValuevaluec                      e Zd ZdZe	 	 	 ddedeeeef                  deee                  dee	         de
f
d            Ze	 	 dded
ededee	         dede
fd            Zededeeeeee	f         f         e	f         fd            Zededee         deeeeee	f         f         e	f         fd            Zede	fd            ZdS )Storagea  Interface for a storage backend that stores the state of nodes in the cluster.

    The storage is thread-safe.

    The storage is versioned, which means that each successful stage change to the
    storage will bump the version number. The version number can be used to
    implement optimistic concurrency control.

    Each entry in the storage table is also versioned. The version number of an entry
    is the last version number when the entry is updated.
    Ntablemutationdeletionexpected_storage_versionreturnc                      t          d          )av  Batch update the storage table. This method is atomic.

        Args:
            table: The name of the table.
            mutation: A dictionary of key-value pairs to be updated.
            deletion: A list of keys to be deleted.
            expected_storage_version: The expected storage version. The
                update will fail if the version does not match the
                current storage version.

        Returns:
            StoreStatus: A tuple of (success, version). If the update is
                successful, returns (True, new_version).
                Otherwise, returns (False, current_version).
        z$batch_update() has to be implementedNotImplementedError)selfr   r   r   r   s        ~/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/v2/instance_manager/storage.pybatch_updatezStorage.batch_update   s    . ""HIII    Fkeyr   expected_entry_versioninsert_onlyc                      t          d          )a  Update a single entry in the storage table.

        Args:
            table: The name of the table.
            key: The key of the entry.
            value: The value of the entry.
            expected_entry_version: The expected version of the entry.
                The update will fail if the version does not match the current
                version of the entry.
            insert_only: If True, the update will
                fail if the entry already exists.
        Returns:
            StoreStatus: A tuple of (success, version). If the update is
                successful, returns (True, new_version). Otherwise,
                returns (False, current_version).
        zupdate() has to be implementedr   )r   r   r   r   r    r!   s         r   updatezStorage.update1   s    2 ""BCCCr   c                      t          d          )Nzget_all() has to be implementedr   r   r   s     r   get_allzStorage.get_allL   s    !"CDDDr   keysc                      t          d          )a^  Get a list of entries from the storage table.

        Args:
            table: The name of the table.
            keys: A list of keys to be retrieved. If the list is empty,
                all entries in the table will be returned.

        Returns:
            Tuple[Dict[str, VersionedValue], int]: A tuple of
                (entries, storage_version). The entries is a dictionary of
                (key, (value, entry_version)) pairs. The entry_version is the
                version of the entry when it was last updated. The
                storage_version is the current storage version.
        zget() has to be implementedr   )r   r   r'   s      r   getzStorage.getP   s    $ ""?@@@r   c                      t          d          )ziGet the current storage version.

        Returns:
            int: The current storage version.
        z#get_version() has to be implementedr   r   s    r   get_versionzStorage.get_versiond   s     ""GHHHr   NNN)NF)__name__
__module____qualname____doc__r   strr
   r   r	   intr   r   boolr#   r   r&   r)   r,    r   r   r   r      s       
 
  .2(,26J JJ 4S>*J 49%	J
 #+3-J 
J J J ^J0  15!D DD D 	D
 !)D D 
D D D ^D4 ES EU4U38_0D+Es+J%K E E E ^E AA $S	A	tCsCx()3.	/A A A ^A& IS I I I ^I I Ir   r   )	metaclassc                   $   e Zd ZdZd Z	 	 	 ddedeeef         dee         dee	         de
f
d	Z	 	 	 ddedededee	         dee	         dede
fdZdedeeeef         e	f         fdZdedee         deeeef         e	f         fdZde	fdZdS )InMemoryStoragez\An in-memory implementation of the Storage interface. This implementation
    is not durablec                 l    d| _         t          t                    | _        t	                      | _        d S )Nr   )_versionr   dict_tablesr   _lockr+   s    r   __init__zInMemoryStorage.__init__r   s'    "4((VV


r   Nr   r   r   expected_versionr   c                     |r|ni }|r|ng } j         5  |,| j        k    r!t          d j                  cd d d            S  xj        dz  c_         fd|                                D             } j        |                             |           |D ]#} j        |                             |d            $t          d j                  cd d d            S # 1 swxY w Y   d S )NF   c                 B    i | ]\  }}|t          |j                  S r5   )r   r:   ).0r   r   r   s      r   
<dictcomp>z0InMemoryStorage.batch_update.<locals>.<dictcomp>   s;     , , ,C ^E4=99, , ,r   T)r=   r:   r   itemsr<   r#   pop)r   r   r   r   r?   key_value_pairs_with_versiondeleted_keys   `      r   r   zInMemoryStorage.batch_updatew   sx     (/88R'/88RZ 	4 	4+0@DM0Q0Q"5$-88	4 	4 	4 	4 	4 	4 	4 	4 MMQMM, , , ,"*.."2"2, , ,( L&&'CDDD' ; ;U#''T::::tT]33	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   "CB
CC"CFr   r   r    r   r!   c                 .   | j         5  |,|| j        k    r!t          d| j                  cd d d            S |r0|| j        |         v r!t          d| j                  cd d d            S | j        |                             |d          \  }}|'||k    r!t          d| j                  cd d d            S | xj        dz  c_        t          || j                  | j        |         |<   t          d| j                  cd d d            S # 1 swxY w Y   d S )NF)NrA   T)r=   r:   r   r<   r)   r   )	r   r   r   r   r    r   r!   _r   s	            r   r#   zInMemoryStorage.update   s    Z 	4 	4(4,=="5$-88	4 	4 	4 	4 	4 	4 	4 	4  9sdl5&999"5$-88	4 	4 	4 	4 	4 	4 	4 	4 e,00jAAJAw%16LPW6W6W"5$-88	4 	4 	4 	4 	4 	4 	4 	4 MMQMM'5eT]'K'KDL$tT]33	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s$   "D
%D
)A D
6AD

DDc                     | j         5  t          j        | j        |                   | j        fcd d d            S # 1 swxY w Y   d S N)r=   copydeepcopyr<   r:   r%   s     r   r&   zInMemoryStorage.get_all   s    Z 	G 	GM$,u"566F	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	Gs   &;??r'   c                    |s|                      |          S | j        5  i }|D ]5}|| j                            |i           v r| j        |         |         ||<   6t	          || j                  cd d d            S # 1 swxY w Y   d S rM   )r&   r=   r<   r)   r   r:   )r   r   r'   resultr   s        r   r)   zInMemoryStorage.get   s     	'<<&&&Z 	6 	6F ; ;$,**5"5555"&,u"5c":F3Kvt}55	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   AA;;A?A?c                     | j         S rM   )r:   r+   s    r   r,   zInMemoryStorage.get_version   s
    }r   r-   )NNF)r.   r/   r0   r1   r>   r2   r   r	   r
   r3   r   r   r4   r#   r   r   r&   r)   r,   r5   r   r   r8   r8   n   s           $("*.4 44 sCx.4 s)	4
 #3-4 
4 4 4 46 1526!4 44 4 	4
 !)4 #+3-4 4 
4 4 4 40GS GU4^0C+Dc+I%J G G G G6 6DI 6%S.=P8QSV8V2W 6 6 6 6S      r   r8   )rN   abcr   r   collectionsr   r   	threadingr   typingr   r	   r
   r   r   r   r   r8   r5   r   r   <module>rW      s!    ' ' ' ' ' ' ' ' / / / / / / / /       . . . . . . . . . . . .jI(>??,w	.BCC`I `I `I `I `I `I `I `I `IFF F F F Fg F F F F Fr   