
    f3fi#                     *   d Z ddlmZmZ ddlmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ  ed      Z ed	      Z G d
 dee
eef         Zeeef   Z G d deeef   e
e         Z G d dee	         Z G d dee         Z G d de      Zy)z**Store** implements the key-value stores and storage helpers.

Module provides implementations of various key-value stores that conform
to a simple key-value interface.

The primary goal of these storages is to support implementation of caching.
    )ABCabstractmethod)AsyncIteratorIteratorSequence)AnyGenericTypeVar)override)LangChainException)run_in_executorKVc                   D   e Zd ZdZedee   deedz     fd       Z	dee   deedz     fdZ
edeeeef      ddfd       Zdeeeef      ddfd	Zedee   ddfd
       Zdee   ddfdZedddedz  dee   ee   z  fd       Zdddedz  dee   ee   z  fdZy)	BaseStoreaw  Abstract interface for a key-value store.

    This is an interface that's meant to abstract away the details of
    different key-value stores. It provides a simple interface for
    getting, setting, and deleting key-value pairs.

    The basic methods are `mget`, `mset`, and `mdelete` for getting,
    setting, and deleting multiple key-value pairs at once. The `yield_keys`
    method is used to iterate over keys that match a given prefix.

    The async versions of these methods are also provided, which are
    meant to be used in async contexts. The async methods are named with
    an `a` prefix, e.g., `amget`, `amset`, `amdelete`, and `ayield_keys`.

    By default, the `amget`, `amset`, `amdelete`, and `ayield_keys` methods
    are implemented using the synchronous methods. If the store can natively
    support async  operations, it should override these methods.

    By design the methods only accept batches of keys and values, and not
    single keys or values. This is done to force user code to work with batches
    which will usually be more efficient by saving on round trips to the store.

    Examples:
        ```python
        from langchain.storage import BaseStore


        class MyInMemoryStore(BaseStore[str, int]):
            def __init__(self) -> None:
                self.store: dict[str, int] = {}

            def mget(self, keys: Sequence[str]) -> list[int | None]:
                return [self.store.get(key) for key in keys]

            def mset(self, key_value_pairs: Sequence[tuple[str, int]]) -> None:
                for key, value in key_value_pairs:
                    self.store[key] = value

            def mdelete(self, keys: Sequence[str]) -> None:
                for key in keys:
                    if key in self.store:
                        del self.store[key]

            def yield_keys(self, prefix: str | None = None) -> Iterator[str]:
                if prefix is None:
                    yield from self.store.keys()
                else:
                    for key in self.store.keys():
                        if key.startswith(prefix):
                            yield key
        ```
    keysreturnNc                      y)a  Get the values associated with the given keys.

        Args:
            keys: A sequence of keys.

        Returns:
            A sequence of optional values associated with the keys.
            If a key is not found, the corresponding value will be `None`.
        N selfr   s     S/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/stores.pymgetzBaseStore.mgetP           c                 L   K   t        d| j                  |       d{   S 7 w)a  Async get the values associated with the given keys.

        Args:
            keys: A sequence of keys.

        Returns:
            A sequence of optional values associated with the keys.
            If a key is not found, the corresponding value will be `None`.
        N)r   r   r   s     r   amgetzBaseStore.amget\   s!      %T499d;;;;   $"$key_value_pairsc                      y)zvSet the values for the given keys.

        Args:
            key_value_pairs: A sequence of key-value pairs.
        Nr   r   r   s     r   msetzBaseStore.mseth   r   r   c                 L   K   t        d| j                  |       d{   S 7 w)z|Async set the values for the given keys.

        Args:
            key_value_pairs: A sequence of key-value pairs.
        N)r   r"   r!   s     r   amsetzBaseStore.amsetp   s!      %T499oFFFFr   c                      y)zzDelete the given keys and their associated values.

        Args:
            keys: A sequence of keys to delete.
        Nr   r   s     r   mdeletezBaseStore.mdeletex   r   r   c                 L   K   t        d| j                  |       d{   S 7 w)zAsync delete the given keys and their associated values.

        Args:
            keys: A sequence of keys to delete.
        N)r   r&   r   s     r   amdeletezBaseStore.amdelete   s!      %T4<<>>>>r   prefixr*   c                     y)aY  Get an iterator over keys that match the given prefix.

        Args:
            prefix: The prefix to match.

        Yields:
            An iterator over keys that match the given prefix.
            This method is allowed to return an iterator over either K or str
            depending on what makes more sense for the given store.
        Nr   )r   r*   s     r   
yield_keyszBaseStore.yield_keys   r   r   c                  K   t        d| j                  |       d{   }t               	 t        dfd|       d{   }|u ry| #7 27 w)aR  Async get an iterator over keys that match the given prefix.

        Args:
            prefix: The prefix to match.

        Yields:
            The keys that match the given prefix.
            This method is allowed to return an iterator over either K or str
            depending on what makes more sense for the given store.
        Nr)   c                     t        |       S N)next)itdones    r   <lambda>z'BaseStore.ayield_keys.<locals>.<lambda>   s    $r4. r   )r   r,   object)r   r*   iteratoritemr2   s       @r   ayield_keyszBaseStore.ayield_keys   s[      )tvNNx(/H(SSDt|J	  O Ts!   AA"AAAA)__name__
__module____qualname____doc__r   r   r   listr   r   r   tupler"   r$   r&   r(   strr   r,   r   r7   r   r   r   r   r      sH   3j 	! 	a$h 	 	
< 
<QX 
< HU1a4[$9 d  G8E!Q$K+@ GT G HQK D  ?8A; ?4 ? 15 
C$J 
(1+QT:U 
 
 '+t	q	M#.	.r   r   c                   @   e Zd ZdZddZedee   dee	dz     fd       Z
edee   dee	dz     fd       Zedeeee	f      ddfd	       Zedeeee	f      ddfd
       Zedee   ddfd       Zedee   ddfd       Zddedz  dee   fdZddedz  dee   fdZy)InMemoryBaseStorez=In-memory implementation of the BaseStore using a dictionary.r   Nc                     i | _         y)zInitialize an empty store.Nstore)r   s    r   __init__zInMemoryBaseStore.__init__   s	    #%
r   r   c                 ^    |D cg c]  }| j                   j                  |       c}S c c}w r/   )rC   getr   r   keys      r   r   zInMemoryBaseStore.mget   s#    /34

s#444s   "*c                 ,   K   | j                  |      S wr/   )r   r   s     r   r   zInMemoryBaseStore.amget   s     yy   r   c                 6    |D ]  \  }}|| j                   |<    y r/   rB   )r   r   rH   values       r   r"   zInMemoryBaseStore.mset   s"    ) 	$JC#DJJsO	$r   c                 ,   K   | j                  |      S wr/   )r"   r!   s     r   r$   zInMemoryBaseStore.amset   s     yy))rJ   c                 J    |D ]  }|| j                   v s| j                   |=   y r/   rB   rG   s      r   r&   zInMemoryBaseStore.mdelete   s(     	$Cdjj JJsO	$r   c                 .   K   | j                  |       y wr/   )r&   r   s     r   r(   zInMemoryBaseStore.amdelete   s     Ts   r*   c              #      K   |#| j                   j                         E d{    y| j                   D ]  }|j                  |      s|  y7 -w)zGet an iterator over keys that match the given prefix.

        Args:
            prefix: The prefix to match.

        Yields:
            The keys that match the given prefix.
        N)rC   r   
startswithr   r*   rH   s      r   r,   zInMemoryBaseStore.yield_keys   sJ      >zz(((zz >>&)I )s    AA%A	Ac                   K   || j                   D ]  }| 	 y| j                   D ]  }|j                  |      s|  yw)zAsync get an async iterator over keys that match the given prefix.

        Args:
            prefix: The prefix to match.

        Yields:
            The keys that match the given prefix.
        N)rC   rQ   rR   s      r   r7   zInMemoryBaseStore.ayield_keys   sK      >zz 	 zz >>&)Is
   :AA)r   Nr/   )r8   r9   r:   r;   rD   r   r   r>   r<   r   r   r   r=   r"   r$   r&   r(   r   r,   r   r7   r   r   r   r@   r@      sG   G& 5# 54D> 5 5  $q4x.   $HU36]$; $ $ $ *8E#q&M+B *t * * $HSM $d $ $
 8C= T  t x}  d
 mC>P r   r@   c                       e Zd ZdZy)InMemoryStorea  In-memory store for any type of data.

    Attributes:
        store: The underlying dictionary that stores the key-value pairs.

    Examples:
        ```python
        from langchain.storage import InMemoryStore

        store = InMemoryStore()
        store.mset([("key1", "value1"), ("key2", "value2")])
        store.mget(["key1", "key2"])
        # ['value1', 'value2']
        store.mdelete(["key1"])
        list(store.yield_keys())
        # ['key2']
        list(store.yield_keys(prefix="k"))
        # ['key2']
        ```
    Nr8   r9   r:   r;   r   r   r   rU   rU          r   rU   c                       e Zd ZdZy)InMemoryByteStorea  In-memory store for bytes.

    Attributes:
        store: The underlying dictionary that stores the key-value pairs.

    Examples:
        ```python
        from langchain.storage import InMemoryByteStore

        store = InMemoryByteStore()
        store.mset([("key1", b"value1"), ("key2", b"value2")])
        store.mget(["key1", "key2"])
        # [b'value1', b'value2']
        store.mdelete(["key1"])
        list(store.yield_keys())
        # ['key2']
        list(store.yield_keys(prefix="k"))
        # ['key2']
        ```
    NrV   r   r   r   rY   rY   	  rW   r   rY   c                       e Zd ZdZy)InvalidKeyExceptionz>Raised when a key is invalid; e.g., uses incorrect characters.NrV   r   r   r   r[   r[      s    Hr   r[   N)r;   abcr   r   collections.abcr   r   r   typingr   r	   r
   typing_extensionsr   langchain_core.exceptionsr   langchain_core.runnablesr   r   r   r   r>   bytes	ByteStorer@   rU   rY   r[   r   r   r   <module>rd      s    $ = =  ' 8 4CLCLNWQT] Nb c5j!	A	#q&)71: AH%c* .)%0 .I, Ir   