o
    Zh                     @   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dedee dee dee dee d	dfd
dZded	efddZ	de
e d	eee  fddZde
eeef  d	dfddZde
e d	dfddZdddee d	ee fddZdS )_UpstashRedisStorezEBaseStore implementation using Upstash Redis as the underlying store.Nclienturltokenttl	namespacer   r   r   r   r   returnc          	   
   C   s   zddl m} W n ty } ztd|d}~ww |r"|r"td|r7t||s4tdt|j d|}n|r;|s?td|||d	}|| _t|t	s[|dur[t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   `/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/storage/upstash_redis.py__init__
   s<   

z_UpstashRedisStore.__init__keyc                 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keysc                    s,    fdd|D }t ttt   jj| S ).Get the values associated with the given keys.c                       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    r0   O   s
   

z_UpstashRedisStore.mgetkey_value_pairsc                 C   s,   |D ]\}}| j j| ||| jd qdS )Set the given key-value pairs.)exN)r   setr%   r   )r   r2   r"   valuer   r   r    msetX   s   z_UpstashRedisStore.msetc                    s"    fdd|D } j j|  dS )Delete the given keys.c                    r(   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prefixr<   c                c   s    |r	|  |}n|  d}| jjd|d\}}|D ]}| jr.|t| jd d }|V  q|V  q|dkr`| jj||d\}}|D ]}| jrV|t| jd d }|V  qB|V  qB|dks6dS d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   r/   r   r!   r%   r   r   r0   r   r7   r:   r   rE   r   r   r   r    r      s2    
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   rF   rG   rH   r   r   r   r    rK   {   s    rK   c                   @   s   e Zd ZdZdddddddedee dee dee dee d	dfd
dZde	e d	e
ee  fddZde	eeef  d	dfddZde	e d	dfddZdddee d	ee fddZdS )rI   zf
    BaseStore implementation using Upstash Redis
    as the underlying store to store raw bytes.
    Nr   r   r   r   r   r   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+   r6   r   r   r    r-      s    z.UpstashRedisByteStore.mget.<locals>.<listcomp>)rL   r0   r1   r   r   r    r0      s   
zUpstashRedisByteStore.mgetr2   c                 C   s   | j dd |D  dS )r3   c                 S   s*   g | ]\}}|d ur|| dfnd qS rM   )decode)r+   kvr   r   r    r-      s    z.UpstashRedisByteStore.mset.<locals>.<listcomp>N)rL   r7   )r   r2   r   r   r    r7      s
   zUpstashRedisByteStore.msetc                 C   s   | j | dS )r8   N)rL   r:   r1   r   r   r    r:      s   zUpstashRedisByteStore.mdeleter;   r<   c                c   s    | j j|dE dH  dS )r=   r;   N)rL   rE   )r   r<   r   r   r    rE      s   z UpstashRedisByteStore.yield_keys)r   rF   rG   rH   r   r   r/   r   r!   r   r   bytesr0   r   r7   r:   r   rE   r   r   r   r    rI      s0    
	$N)typingr   r   r   r   r   r   r   Zlangchain_core._api.deprecationr	   Zlangchain_core.storesr
   r   r/   r   rK   rI   r   r   r   r    <module>   s   $ 
t	