a
    bg                     @   s   d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZmZ G dd deeef Ze	dddG d	d
 d
eZG dd deZdS )    )AnyIteratorListOptionalSequenceTuplecast)
deprecated)	BaseStore	ByteStorec                   @   s   e Zd ZdZddddddeee ee ee ee ddddZeeddd	Z	e
e eee  d
ddZe
eeef  ddddZe
e dd
ddZddee ee dddZdS )_UpstashRedisStorezEBaseStore implementation using Upstash Redis as the underlying store.Nclienturltokenttl	namespacer   r   r   r   r   returnc          	   
   C   s   zddl m} W n. ty> } ztd|W Y d}~n
d}~0 0 |rP|rPtd|rzt||sttdt|j d|}n|r|std|||d	}|| _t|t	s|durtd
t| d|| _
|| _dS )a  Initialize the UpstashRedisStore with HTTP API.

        Must provide either an Upstash Redis client or a url.

        Args:
            client: An Upstash Redis instance
            url: UPSTASH_REDIS_REST_URL
            token: UPSTASH_REDIS_REST_TOKEN
            ttl: time to expire keys in seconds if provided,
                 if None keys will never expire
            namespace: if provided, all keys will be prefixed with this namespace
        r   )Redisz_UpstashRedisStore requires the upstash_redis library to be installed. pip install upstash_redisNzCEither an Upstash Redis client or a url must be provided, not both.z#Expected Upstash Redis client, got .zAEither an Upstash Redis client or url and token must be provided.)r   r   zExpected int or None, got z	 instead.)Zupstash_redisr   ImportError
ValueError
isinstance	TypeErrortype__name__r   intr   r   )	selfr   r   r   r   r   r   eZ_client r    w/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/storage/upstash_redis.py__init__
   s8    
z_UpstashRedisStore.__init__)keyr   c                 C   s    d}| j r| j  | | S |S )zGet the key with the namespace prefix.

        Args:
            key (str): The original key.

        Returns:
            str: The key with the namespace prefix.
        /)r   )r   r#   	delimiterr    r    r!   _get_prefixed_keyA   s    	z$_UpstashRedisStore._get_prefixed_keykeysr   c                    s,    fdd|D }t ttt   jj| S ).Get the values associated with the given keys.c                    s   g | ]}  |qS r    r&   .0r#   r   r    r!   
<listcomp>R       z+_UpstashRedisStore.mget.<locals>.<listcomp>)r   r   r   strr   mgetr   r(   r    r-   r!   r1   O   s
    

z_UpstashRedisStore.mgetkey_value_pairsr   c                 C   s,   |D ]"\}}| j j| ||| jd qdS )Set the given key-value pairs.)exN)r   setr&   r   )r   r4   r#   valuer    r    r!   msetX   s    z_UpstashRedisStore.msetc                    s"    fdd|D } j j|  dS )Delete the given keys.c                    s   g | ]}  |qS r    r*   r+   r-   r    r!   r.   _   r/   z._UpstashRedisStore.mdelete.<locals>.<listcomp>N)r   delete)r   r(   Z_keysr    r-   r!   mdelete]   s    z_UpstashRedisStore.mdeleteprefixr>   r   c                c   s   |r|  |}n
|  d}| jjd|d\}}|D ].}| jrZ|t| jd d }|V  q2|V  q2|dkr| jj||d\}}|D ].}| jr|t| jd d }|V  q|V  qqbdS )Yield keys in the store.*r   )match   N)r&   r   scanr   len)r   r>   patterncursorr(   r#   Zrelative_keyr    r    r!   
yield_keysb   s     
z_UpstashRedisStore.yield_keys)r   
__module____qualname____doc__r   r   r0   r   r"   r&   r   r   r1   r   r9   r<   r   rH   r    r    r    r!   r      s&   7	r   z0.0.1UpstashRedisByteStore)alternativec                   @   s   e Zd ZdZdS )UpstashRedisStorez
    BaseStore implementation using Upstash Redis
    as the underlying store to store strings.

    Deprecated in favor of the more generic UpstashRedisByteStore.
    N)r   rI   rJ   rK   r    r    r    r!   rN   {   s   rN   c                   @   s   e Zd ZdZddddddeee ee ee ee ddddZe	e e
ee  ddd	Ze	eeef  dd
ddZe	e ddddZddee ee dddZdS )rL   zf
    BaseStore implementation using Upstash Redis
    as the underlying store to store raw bytes.
    Nr   r   c                C   s   t |||||d| _d S )Nr   )r   underlying_store)r   r   r   r   r   r   r    r    r!   r"      s    	
zUpstashRedisByteStore.__init__r'   c                 C   s   dd | j |D S )r)   c                 S   s"   g | ]}|d ur| dnd qS Nzutf-8)encode)r,   r8   r    r    r!   r.      s   z.UpstashRedisByteStore.mget.<locals>.<listcomp>)rO   r1   r2   r    r    r!   r1      s    
zUpstashRedisByteStore.mgetr3   c                 C   s   | j dd |D  dS )r5   c                 S   s*   g | ]"\}}|d ur"|| dfnd qS rP   )decode)r,   kvr    r    r!   r.      s   z.UpstashRedisByteStore.mset.<locals>.<listcomp>N)rO   r9   )r   r4   r    r    r!   r9      s
    zUpstashRedisByteStore.msetc                 C   s   | j | dS )r:   N)rO   r<   r2   r    r    r!   r<      s    zUpstashRedisByteStore.mdeleter=   r?   c                c   s   | j j|dE dH  dS )r@   r=   N)rO   rH   )r   r>   r    r    r!   rH      s    z UpstashRedisByteStore.yield_keys)r   rI   rJ   rK   r   r   r0   r   r"   r   r   bytesr1   r   r9   r<   r   rH   r    r    r    r!   rL      s$   	N)typingr   r   r   r   r   r   r   Zlangchain_core._api.deprecationr	   Zlangchain_core.storesr
   r   r0   r   rN   rL   r    r    r    r!   <module>   s   $t
	