o
    Zha                      @   s   d dl mZmZmZmZmZ d dlmZ dZdZ	defddZ
ded	edefd
dZdefddZded	edefddZG dd deZdS )    )AnyDictListOptionalSequence)RecordManagerzKCould not import MongoClient. Please install it with `pip install pymongo`.zPCould not import AsyncIOMotorClient. Please install it with `pip install motor`.returnc                  C   *   z	ddl m}  W | S  ty   ttw )z3Import PyMongo if available, otherwise raise error.r   MongoClient)pymongor   ImportErrorIMPORT_PYMONGO_ERRORr
    r   d/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/indexes/_document_manager.py_import_pymongo      r   mongodb_urlkwargsc              
   K   F   zt  }|| fi |}W |S  ty" } ztd| dd}~ww )zTGet MongoClient for sync operations from the mongodb_url,
    otherwise raise error.z@MongoClient string provided is not in proper format. Got error:  N)r   
ValueErrorr   )r   r   r   clienter   r   r   _get_pymongo_client   s   
r   c                  C   r	   )z1Import Motor if available, otherwise raise error.r   AsyncIOMotorClient)Zmotor.motor_asyncior   r   IMPORT_MOTOR_ASYNCIO_ERRORr   r   r   r   _import_motor_asyncio#   r   r   c              
   K   r   )z\Get AsyncIOMotorClient for async operations from the mongodb_url,
    otherwise raise error.zGAsyncIOMotorClient string provided is not in proper format. Got error: r   N)r   r   r   )r   r   Zmotorr   r   r   r   r   _get_motor_client,   s   r   c                       s  e Zd ZdZdddededededd	f
 fd
dZd,ddZd,ddZd	d	ddee de	ee	e   de	e
 dd	fddZd	d	ddee de	ee	e   de	e
 dd	fddZde
fddZde
fddZdee dee fddZdee dee fddZd	d	d	d	d d!e	e
 d"e	e
 de	ee  d#e	e dee f
d$d%Zd	d	d	d	d d!e	e
 d"e	e
 de	ee  d#e	e dee f
d&d'Zdee dd	fd(d)Zdee dd	fd*d+Z  ZS )-MongoDocumentManagerz7A MongoDB based implementation of the document manager.ZdocumentMetadata)collection_name	namespacer   db_namer!   r   Nc                   sV   t  j|d t|| _| j| | _| j| | _t|| _| j| | _| j| | _	dS )a-  Initialize the MongoDocumentManager.

        Args:
            namespace: The namespace associated with this document manager.
            db_name: The name of the database to use.
            collection_name: The name of the collection to use.
                Default is 'documentMetadata'.
        )r"   N)
super__init__r   Zsync_clientsync_dbsync_collectionr   Zasync_clientZasync_dbasync_collection)selfr"   r   r#   r!   	__class__r   r   r%   =   s   

zMongoDocumentManager.__init__c                 C   s   dS z4Create the database schema for the document manager.Nr   r)   r   r   r   create_schemaU   s   z"MongoDocumentManager.create_schemac                    s   dS r,   r   r-   r   r   r   acreate_schemaY   s   z#MongoDocumentManager.acreate_schema)	group_idstime_at_leastkeysr0   r1   c                C   sn   |du rdgt | }t |t |krtdt||D ]\}}| jj| j|dd||  didd qdS )z-Upsert documents into the MongoDB collection.N1Number of keys does not match number of group_idsr"   key$setgroup_id
updated_atTZupsert)lenr   zipr'   find_one_and_updater"   get_time)r)   r2   r0   r1   r5   r8   r   r   r   update]   s   
zMongoDocumentManager.updatec                   s   |du rdgt | }t |t |krtd|  I dH }|r)||k r)tdt||D ]\}}| jj| j|dd||diddI dH  q.dS )	z<Asynchronously upsert documents into the MongoDB collection.Nr3   z0Server time is behind the expected time_at_leastr4   r6   r7   Tr:   )r;   r   	aget_timer<   r(   r=   r"   )r)   r2   r0   r1   Zupdate_timer5   r8   r   r   r   aupdater   s   
zMongoDocumentManager.aupdatec                 C   s$   | j d}|d d }| }|S )z+Get the current server time as a timestamp.hostInfosystemcurrentTime)r&   command	timestamp)r)   Zserver_info
local_timerF   r   r   r   r>      s   zMongoDocumentManager.get_timec                    s*   | j jdI dH }|d d }| S )z:Asynchronously get the current server time as a timestamp.rB   NrC   rD   )r(   ZdatabaserE   rF   )r)   Z	host_inforG   r   r   r   r@      s   zMongoDocumentManager.aget_timec                    s:   dd | j | jd|idddiD   fdd|D S )	z8Check if the given keys exist in the MongoDB collection.c                 S   s   h | ]}|d  qS r5   r   .0docr   r   r   	<setcomp>   s    z.MongoDocumentManager.exists.<locals>.<setcomp>$inr4   r5      c                       g | ]}| v qS r   r   rJ   r5   Zexisting_keysr   r   
<listcomp>       z/MongoDocumentManager.exists.<locals>.<listcomp>)r'   findr"   r)   r2   r   rQ   r   exists   s   zMongoDocumentManager.existsc                    sF   | j | jd|idddi}dd |2 I dH   fdd	|D S )
zGAsynchronously check if the given keys exist in the MongoDB collection.rM   r4   r5   rN   c                    s    h | z
3 d H W }|d q6 S Nr5   r   rI   r   r   r   rL          z/MongoDocumentManager.aexists.<locals>.<setcomp>Nc                    rO   r   r   rP   rQ   r   r   rR      rS   z0MongoDocumentManager.aexists.<locals>.<listcomp>)r(   rT   r"   )r)   r2   cursorr   rQ   r   aexists   s   zMongoDocumentManager.aexists)beforeafterr0   limitr[   r\   r]   c                C   sv   d| j i}|rd|i|d< |rd|i|d< |rd|i|d< |r+| j|ddi|n| j|ddi}d	d
 |D S )zJList documents in the MongoDB collection based on the provided date range.r"   $ltr9   $gtrM   r8   r5   rN   c                 S   s   g | ]}|d  qS rH   r   rI   r   r   r   rR      rS   z2MongoDocumentManager.list_keys.<locals>.<listcomp>)r"   r'   rT   r]   r)   r[   r\   r0   r]   queryrY   r   r   r   	list_keys   s   
	zMongoDocumentManager.list_keysc                   s~   d| j i}|rd|i|d< |rd|i|d< |rd|i|d< |r,| j|ddi|n| j|ddi}d	d
 |2 I dH S )zs
        Asynchronously list documents in the MongoDB collection
        based on the provided date range.
        r"   r^   r9   r_   rM   r8   r5   rN   c                    s    g | z
3 d H W }|d q6 S rW   r   rI   r   r   r   rR      rX   z3MongoDocumentManager.alist_keys.<locals>.<listcomp>N)r"   r(   rT   r]   r`   r   r   r   
alist_keys   s   
zMongoDocumentManager.alist_keysc                 C   s   | j | jd|id dS )z-Delete documents from the MongoDB collection.rM   r4   N)r'   delete_manyr"   rU   r   r   r   delete_keys   s
   z MongoDocumentManager.delete_keysc                    s$   | j | jd|idI dH  dS )z<Asynchronously delete documents from the MongoDB collection.rM   r4   N)r(   rd   r"   rU   r   r   r   adelete_keys   s   z!MongoDocumentManager.adelete_keys)r   N)__name__
__module____qualname____doc__strr%   r.   r/   r   r   floatr?   rA   r>   r@   r   boolrV   rZ   intrb   rc   re   rf   __classcell__r   r   r*   r   r    :   s    








	r    N)typingr   r   r   r   r   Z langchain_community.indexes.baser   r   r   r   rk   r   r   r   r    r   r   r   r   <module>   s    		