
    Ng                         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eeef                   Z e	dd           G d	 d
e                      Z G d de          ZdS )    )AnyIteratorListOptionalSequenceTuplecast)
deprecated)	BaseStore	ByteStorec                   *   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
Zded	efdZ	de
e         d	eee                  fdZde
eeef                  d	dfdZde
e         d	dfdZdddee         d	ee         fdZdS )_UpstashRedisStorezEBaseStore implementation using Upstash Redis as the underlying store.Nclienturltokenttl	namespacer   r   r   r   r   returnc                   	 ddl m} n"# t          $ r}t          d          |d}~ww xY w|r|rt          d          |r8t	          ||          s%t          dt          |          j         d          |}n |r|st          d           |||	          }|| _        t	          |t                    s"| 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.)upstash_redisr   ImportError
ValueError
isinstance	TypeErrortype__name__r   intr   r   )	selfr   r   r   r   r   r   e_clients	            e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/storage/upstash_redis.py__init__z_UpstashRedisStore.__init__
   sU   *	+++++++ 	 	 	,  	  	c 	U    	2fe,, R$v,,:ORRR   GG e  W   e5111G#s## 	OMcMMMNNN"s   	 
(#(keyc                 2    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&   	delimiters      r$   _get_prefixed_keyz$_UpstashRedisStore._get_prefixed_keyA   s0     	> 	7n6i6666
    keysc                       fd|D             }t          t          t          t                              j        j        |           S ).Get the values associated with the given keys.c                 :    g | ]}                     |          S  r*   .0r&   r!   s     r$   
<listcomp>z+_UpstashRedisStore.mget.<locals>.<listcomp>R   s'    <<<&&s++<<<r+   )r	   r   r   strr   mgetr!   r,   s   ` r$   r6   z_UpstashRedisStore.mgetO   sK     =<<<t<<<#DKd#
 
 	
r+   key_value_pairsc                     |D ]:\  }}| j                             |                     |          || j                   ;dS )Set the given key-value pairs.)exN)r   setr*   r   )r!   r8   r&   values       r$   msetz_UpstashRedisStore.msetX   sP    ) 	M 	MJCKOOD2237748OLLLL	M 	Mr+   c                 B      fd|D             }  j         j        |  dS )Delete the given keys.c                 :    g | ]}                     |          S r0   r1   r2   s     r$   r4   z._UpstashRedisStore.mdelete.<locals>.<listcomp>_   s'    ==='',,===r+   N)r   delete)r!   r,   _keyss   `  r$   mdeletez_UpstashRedisStore.mdelete]   s4    =======E""""r+   prefixrF   c             #     K   |r|                      |          }n|                      d          }| j                            d|          \  }}|D ]1}| j        r$|t	          | j                  dz   d         }|V  -|V  2|dk    r[| j                            ||          \  }}|D ]1}| j        r$|t	          | j                  dz   d         }|V  -|V  2|dk    YdS dS )Yield keys in the store.*r   )match   N)r*   r   scanr   len)r!   rF   patterncursorr,   r&   relative_keys          r$   
yield_keysz_UpstashRedisStore.yield_keysb   s2      	2,,V44GG,,S11G{'''99 	 	C~ "3t~#6#6#:#<#<="""""				kk;++F'+BBLFD  > #&s4>':':Q'>'@'@#AL&&&&&IIII kkkkkkr+   )r   
__module____qualname____doc__r   r   r5   r    r%   r*   r   r   r6   r   r>   rD   r   rQ   r0   r+   r$   r   r      s{       OO
 !#!#'5# 5# 5# 5# c]	5#
 }5# c]5# C=5# 
5# 5# 5# 5#nS S    
# 
4+> 
 
 
 
MHU38_$= M$ M M M M
#HSM #d # # # #
 59   HSM Xc]      r+   r   z0.0.1UpstashRedisByteStore)alternativec                       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   rR   rS   rT   r0   r+   r$   rX   rX   {   s           r+   rX   c                      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
Zde	e         d	e
ee                  fdZde	eeef                  d	dfdZde	e         d	dfdZdddee         d	ee         fdZdS )rU   zf
    BaseStore implementation using Upstash Redis
    as the underlying store to store raw bytes.
    Nr   r   r   r   r   r   r   c                8    t          |||||          | _        d S )Nr   )r   underlying_store)r!   r   r   r   r   r   s         r$   r%   zUpstashRedisByteStore.__init__   s,     !3s%SI!
 !
 !
r+   r,   c                 J    d | j                             |          D             S )r.   c                 @    g | ]}||                     d          nd S Nzutf-8)encode)r3   r=   s     r$   r4   z.UpstashRedisByteStore.mget.<locals>.<listcomp>   s>     
 
 
 &+%6ELL!!!D
 
 
r+   )r[   r6   r7   s     r$   r6   zUpstashRedisByteStore.mget   s4    
 
.33D99
 
 
 	
r+   r8   c                 N    | j                             d |D                        dS )r:   c                 J    g | ] \  }}|||                     d          fnd !S r^   )decode)r3   kvs      r$   r4   z.UpstashRedisByteStore.mset.<locals>.<listcomp>   sF       Aq +,-AHHW%%&&T  r+   N)r[   r>   )r!   r8   s     r$   r>   zUpstashRedisByteStore.mset   sC    "" +  	
 	
 	
 	
 	
r+   c                 :    | j                             |           dS )r@   N)r[   rD   r7   s     r$   rD   zUpstashRedisByteStore.mdelete   s    %%d+++++r+   rE   rF   c             #   L   K   | j                             |          E d{V  dS )rH   rE   N)r[   rQ   )r!   rF   s     r$   rQ   z UpstashRedisByteStore.yield_keys   s8      (3363BBBBBBBBBBBr+   )r   rR   rS   rT   r   r   r5   r    r%   r   r   bytesr6   r   r>   rD   r   rQ   r0   r+   r$   rU   rU      sa         !#!#'
 
 
 
 c]	

 }
 c]
 C=
 

 
 
 

# 
4+@ 
 
 
 

HU3:->$? 
D 
 
 
 
,HSM ,d , , , , 59 C C CHSM CXc] C C C C C Cr+   N)typingr   r   r   r   r   r   r	   langchain_core._api.deprecationr
   langchain_core.storesr   r   r5   r   rX   rU   r0   r+   r$   <module>rk      s'   G G G G G G G G G G G G G G G G G G 6 6 6 6 6 6 6 6 6 6 6 6 6 6q q q q q38, q q qh G!8999    *   :9)C )C )C )C )CI )C )C )C )C )Cr+   