o
    Zhy!                     @   sh   d dl mZmZmZmZmZ d dlmZ d dlm	Z	 G dd de	e
ef ZG dd de	e
ef ZdS )	    )IteratorListOptionalSequenceTupleDocument)	BaseStorec                   @      e Zd ZdZdddeded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ee dee fddZdS )MongoDBByteStorea  BaseStore implementation using MongoDB as the underlying store.

    Examples:
        Create a MongoDBByteStore instance and perform operations on it:

        .. code-block:: python

            # Instantiate the MongoDBByteStore with a MongoDB connection
            from langchain.storage import MongoDBByteStore

            mongo_conn_str = "mongodb://localhost:27017/"
            mongodb_store = MongoDBBytesStore(mongo_conn_str, db_name="test-db",
                                         collection_name="test-collection")

            # Set values for keys
            mongodb_store.mset([("key1", "hello"), ("key2", "workd")])

            # Get values for keys
            values = mongodb_store.mget(["key1", "key2"])
            # [bytes1, bytes1]

            # Iterate over keys
            for key in mongodb_store.yield_keys():
                print(key)

            # Delete keys
            mongodb_store.mdelete(["key1", "key2"])
    Nclient_kwargsconnection_stringdb_namecollection_namer   returnc             
   C      zddl m} W n ty } ztd|d}~ww |s td|s&td|s,td||fi |p3i | _| j| | | _dS aE  Initialize the MongoDBStore with a MongoDB connection string.

        Args:
            connection_string (str): MongoDB connection string
            db_name (str): name to use
            collection_name (str): collection name to use
            client_kwargs (dict): Keyword arguments to pass to the Mongo client
        r   )MongoClientzRThe MongoDBStore requires the pymongo library to be installed. pip install pymongoNz#connection_string must be provided.zdb_name must be provided.z!collection_name must be provided.pymongor   ImportError
ValueErrorclient
collectionselfr   r   r   r   r   e r   Z/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/storage/mongodb.py__init__%   ,   zMongoDBByteStore.__init__keysc                    4   | j dd|ii}dd |D   fdd|D S )z  Get the list of documents associated with the given keys.

        Args:
            keys (list[str]): A list of keys representing Document IDs..

        Returns:
            list[Document]: A list of Documents corresponding to the provided
                keys, where each Document is either retrieved successfully or
                represented as None if not found.
        _id$inc                 S   s   i | ]	}|d  |d qS r%   valuer   .0docr   r   r   
<dictcomp>V       z)MongoDBByteStore.mget.<locals>.<dictcomp>c                       g | ]}  |qS r   getr*   keyZresult_dictr   r   
<listcomp>W       z)MongoDBByteStore.mget.<locals>.<listcomp>r   findr   r"   resultr   r3   r   mgetJ      zMongoDBByteStore.mgetkey_value_pairsc                    8   ddl m  dd |D }| j fdd|D  dS )zSet the given key-value pairs.

        Args:
            key_value_pairs (list[tuple[str, Document]]): A list of id-document
                pairs.
        r   	UpdateOnec                 S   s   g | ]	\}}||d qS r'   r   r*   kvr   r   r   r4   b   r-   z)MongoDBByteStore.mset.<locals>.<listcomp>c                    &   g | ]} d |d  id|iddqS r%   z$setT)Zupsertr   r*   ur>   r   r   r4   d      & Nr   r?   r   Z
bulk_writer   r<   Zupdatesr   r>   r   msetY   s
   zMongoDBByteStore.msetc                 C      | j dd|ii dS zvDelete the given ids.

        Args:
            keys (list[str]): A list of keys representing Document IDs..
        r%   r&   Nr   Zdelete_manyr   r"   r   r   r   mdeleteg      zMongoDBByteStore.mdeleteprefixc                 c   b    |du r| j jdgdD ]}|d V  qdS | j jddd| iidgdD ]}|d V  q'dS zfYield keys in the store.

        Args:
            prefix (str): prefix of keys to retrieve.
        Nr%   )Z
projectionz$regex^r6   r   rR   r+   r   r   r   
yield_keyso      
zMongoDBByteStore.yield_keysN)__name__
__module____qualname____doc__strr   dictr    r   r   bytesr:   r   rK   rP   r   rW   r   r   r   r   r      s$    #
% r   c                   @   r
   )MongoDBStorea  BaseStore implementation using MongoDB as the underlying store.

    Examples:
        Create a MongoDBStore instance and perform operations on it:

        .. code-block:: python

            # Instantiate the MongoDBStore with a MongoDB connection
            from langchain.storage import MongoDBStore

            mongo_conn_str = "mongodb://localhost:27017/"
            mongodb_store = MongoDBStore(mongo_conn_str, db_name="test-db",
                                         collection_name="test-collection")

            # Set values for keys
            doc1 = Document(...)
            doc2 = Document(...)
            mongodb_store.mset([("key1", doc1), ("key2", doc2)])

            # Get values for keys
            values = mongodb_store.mget(["key1", "key2"])
            # [doc1, doc2]

            # Iterate over keys
            for key in mongodb_store.yield_keys():
                print(key)

            # Delete keys
            mongodb_store.mdelete(["key1", "key2"])
    Nr   r   r   r   r   r   c             
   C   r   r   r   r   r   r   r   r       r!   zMongoDBStore.__init__r"   c                    r#   )r$   r%   r&   c                 S   s$   i | ]}|d  t di |d qS )r%   r(   r   r   r)   r   r   r   r,      s   $ z%MongoDBStore.mget.<locals>.<dictcomp>c                    r.   r   r/   r1   r3   r   r   r4      r5   z%MongoDBStore.mget.<locals>.<listcomp>r6   r8   r   r3   r   r:      r;   zMongoDBStore.mgetr<   c                    r=   )zSet the given key-value pairs.

        Args:
            key_value_pairs (list[tuple[str, Document]]): A list of id-document
                pairs.
        Returns:
            None
        r   r>   c                 S   s   g | ]
\}}||j d qS r@   )__dict__rA   r   r   r   r4      s    z%MongoDBStore.mset.<locals>.<listcomp>c                    rD   rE   r   rF   r>   r   r   r4      rH   NrI   rJ   r   r>   r   rK      s
   	zMongoDBStore.msetc                 C   rL   rM   rN   rO   r   r   r   rP      rQ   zMongoDBStore.mdeleterR   c                 c   rS   rT   r6   rV   r   r   r   rW      rX   zMongoDBStore.yield_keysrY   )rZ   r[   r\   r]   r^   r   r_   r    r   r   r   r:   r   rK   rP   r   rW   r   r   r   r   ra      s$    %
% ra   N)typingr   r   r   r   r   Zlangchain_core.documentsr   Zlangchain_core.storesr	   r^   r`   r   ra   r   r   r   r   <module>   s
    x