
    7|hy!                     x    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e	e
ef         Z G d de	e
ef         Zy)	    )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	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ee   dee   fdZy)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                    	 ddl m} |st        d      |st        d      |st        d       ||fi |xs i | _        | j                  |   |   | _        y# t        $ r}t        d      |d}~ww xY w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   es          b/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/storage/mongodb.py__init__zMongoDBByteStore.__init__%        	+ !BCC899@AA#.$
"/"52$
 ++g.?#  	& 		   A 	A3"A..A3keysc                     | j                   j                  dd|ii      }|D ci c]  }|d   |d    }}|D cg c]  }|j                  |       c}S c c}w c c}w )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$invalue)r   findgetr   r"   resultdocresult_dictkeys         r   mgetzMongoDBByteStore.mgetJ   sd     %%uudm&<=;ABCs5z3w</BB045$55 C5s
   AAkey_value_pairsc           
          ddl m} |D cg c]
  \  }}||d }}}| j                  j                  |D cg c]  } |d|d   id|id       c}       yc c}}w c c}w )	zSet the given key-value pairs.

        Args:
            key_value_pairs (list[tuple[str, Document]]): A list of id-document
                pairs.
        r   	UpdateOner%   r'   r%   $setTupsertN)r   r3   r   
bulk_writer   r0   r3   kvupdatesus          r   msetzMongoDBByteStore.msetY   sb     	&6EFda1q)FF""MTUYqx(61+dCU	
 GUs
   AAc                 B    | j                   j                  dd|ii       yzvDelete the given ids.

        Args:
            keys (list[str]): A list of keys representing Document IDs..
        r%   r&   Nr   delete_manyr   r"   s     r   mdeletezMongoDBByteStore.mdeleteg        	##UUDM$:;    prefixc              #      K   |*| j                   j                  dg      D ]	  }|d     y| j                   j                  ddd| iidg      D ]	  }|d     ywzfYield keys in the store.

        Args:
            prefix (str): prefix of keys to retrieve.
        Nr%   )
projectionz$regex^r   r(   r   rG   r,   s      r   
yield_keyszMongoDBByteStore.yield_keyso         >++w+? !%j ! ++Qvh<01ug ,  ! %j !   AA!N)__name__
__module____qualname____doc__strr   dictr   r   r   bytesr/   r   r>   rD   r   rN    rF   r   r   r      s    F )-#@#@ #@ 	#@  ~#@ 
#@J6# 64+@ 6
HU3:->$? 
D 
<HSM <d <!# !(3- !rF   r   c                       e Zd ZdZdddeded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ee   dee   fdZy)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                    	 ddl m} |st        d      |st        d      |st        d       ||fi |xs i | _        | j                  |   |   | _        y# t        $ r}t        d      |d}~ww xY wr   r   r   s          r   r   zMongoDBStore.__init__   r    r!   r"   c           
          | j                   j                  dd|ii      }|D ci c]  }|d   t        di |d    }}|D cg c]  }|j                  |       c}S c c}w c c}w )r$   r%   r&   r'   rY   )r   r(   r   r)   r*   s         r   r/   zMongoDBStore.mget   sl     %%uudm&<=GMNs5z8#;c'l#;;NN045$55 O5s   AA#r0   c           
          ddl m} |D cg c]  \  }}||j                  d }}}| j                  j	                  |D cg c]  } |d|d   id|id       c}       yc c}}w c c}w )	zSet the given key-value pairs.

        Args:
            key_value_pairs (list[tuple[str, Document]]): A list of id-document
                pairs.
        Returns:
            None
        r   r2   r4   r%   r5   Tr6   N)r   r3   __dict__r   r8   r9   s          r   r>   zMongoDBStore.mset   sf     	&?NOtq!1qzz2OO""MTUYqx(61+dCU	
 PUs   A"A(c                 B    | j                   j                  dd|ii       yr@   rA   rC   s     r   rD   zMongoDBStore.mdelete   rE   rF   rG   c              #      K   |*| j                   j                  dg      D ]	  }|d     y| j                   j                  ddd| iidg      D ]	  }|d     ywrI   rL   rM   s      r   rN   zMongoDBStore.yield_keys   rO   rP   rQ   )rR   rS   rT   rU   rV   r   rW   r   r   r   r   r/   r   r>   rD   r   rN   rY   rF   r   r[   r[      s    J )-#@#@ #@ 	#@  ~#@ 
#@J6# 640B+C 6
HU3=-A$B 
t 
 <HSM <d <!# !(3- !rF   r[   N)typingr   r   r   r   r   langchain_core.documentsr   langchain_core.storesr	   rV   rX   r   r[   rY   rF   r   <module>re      s@    < < - +u!ye, u!py!9S(]+ y!rF   