
    h                         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y)    )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y)_UpstashRedisStorezEBaseStore implementation using Upstash Redis as the underlying store.Nclienturltokenttl	namespacer   r   r   r   r   returnc                   	 ddl m} |r|rt        d      |r1t	        ||      s"t        dt        |      j                   d      |}n|r|st        d       |||	      }|| _        t	        |t              s|t        d
t        |       d      || _
        || _        y# t        $ r}t        d      |d}~ww xY w)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	            g/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/storage/upstash_redis.py__init__z_UpstashRedisStore.__init__
   s    *	+ cU  fe,9$v,:O:O9PPQR  Ge W  51G#s#8c9MNN"=  	, 	s   B# #	B=,B88B=keyc                 F    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   s-     	>>nn%i[66
    keysc                     |D cg c]  }| j                  |       }}t        t        t        t               | j
                  j                  |       S c c}w ).Get the values associated with the given keys.)r*   r	   r   r   strr   mget)r!   r,   r&   s      r$   r0   z_UpstashRedisStore.mgetO   sT     8<<&&s+<<#DKKd#
 	
 =s   Akey_value_pairsc                     |D ]<  \  }}| j                   j                  | j                  |      || j                         > y)Set the given key-value pairs.)exN)r   setr*   r   )r!   r1   r&   values       r$   msetz_UpstashRedisStore.msetX   s<    ) 	MJCKKOOD2237488OL	Mr+   c                 |    |D cg c]  }| j                  |       }} | j                  j                  |  yc c}w zDelete the given keys.N)r*   r   delete)r!   r,   r&   _keyss       r$   mdeletez_UpstashRedisStore.mdelete]   s9    8<='',==E" >s   9prefixr>   c             #     K   |r| j                  |      }n| j                  d      }| j                  j                  d|      \  }}|D ]2  }| j                  r |t	        | j                        dz   d }| /| 4 |dk7  r^| j                  j                  ||      \  }}|D ]2  }| j                  r |t	        | j                        dz   d }| /| 4 |dk7  r]yyw)Yield keys in the store.*r   )match   N)r*   r   scanr   len)r!   r>   patterncursorr,   r&   relative_keys          r$   
yield_keysz_UpstashRedisStore.yield_keysb   s     ,,V4G,,S1G{{'''9 	C~~"3t~~#6#:#<=""		 k;;++F'+BLFD >>#&s4>>':Q'>'@#AL&&I ks   CC#!C#)r   
__module____qualname____doc__r   r   r/   r    r%   r*   r   r   r0   r   r7   r<   r   rI    r+   r$   r   r      s    O
 !#!#'5# 5# c]	5#
 }5# c]5# C=5# 
5#nS S 
# 
4+> 
MHU38_$= M$ M
#HSM #d #
 59 HSM Xc] r+   r   z0.0.1UpstashRedisByteStore)alternativec                       e Zd ZdZy)UpstashRedisStorez
    BaseStore implementation using Upstash Redis
    as the underlying store to store strings.

    Deprecated in favor of the more generic UpstashRedisByteStore.
    N)r   rJ   rK   rL   rM   r+   r$   rQ   rQ   {   s    r+   rQ   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y)rN   zf
    BaseStore implementation using Upstash Redis
    as the underlying store to store raw bytes.
    Nr   r   r   r   r   r   r   c                .    t        |||||      | _        y )Nr   )r   underlying_store)r!   r   r   r   r   r   s         r$   r%   zUpstashRedisByteStore.__init__   s     !3s%SI!
r+   r,   c                     | j                   j                  |      D cg c]  }||j                  d      nd c}S c c}w )r.   Nutf-8)rT   r0   encode)r!   r,   r6   s      r$   r0   zUpstashRedisByteStore.mget   sH     ..33D9
 &+%6ELL!D@
 	
 
s   =r1   c           
          | j                   j                  |D cg c]  \  }}|||j                  d      fnd c}}       yc c}}w )r3   NrV   )rT   r7   decode)r!   r1   kvs       r$   r7   zUpstashRedisByteStore.mset   sN    "" ,Aq +,-AHHW%&TA	
s   !A
c                 :    | j                   j                  |       yr9   )rT   r<   )r!   r,   s     r$   r<   zUpstashRedisByteStore.mdelete   s    %%d+r+   r=   r>   c             #   X   K   | j                   j                  |      E d{    y7 w)r@   r=   N)rT   rI   )r!   r>   s     r$   rI   z UpstashRedisByteStore.yield_keys   s#     ((3363BBBs    *(*)r   rJ   rK   rL   r   r   r/   r    r%   r   r   bytesr0   r   r7   r<   r   rI   rM   r+   r$   rN   rN      s     !#!#'
 
 c]	

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