a
    bg                      @   s   d dl mZmZmZmZmZ d dlmZ dZdZ	edddZ
eeedd	d
ZedddZeeed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   s0   zddl m}  W n ty*   ttY n0 | S )z3Import PyMongo if available, otherwise raise error.r   MongoClient)pymongor   ImportErrorIMPORT_PYMONGO_ERRORr
    r   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/indexes/_document_manager.py_import_pymongo   s
    r   )mongodb_urlkwargsr	   c              
   K   sT   zt  }|| fi |}W n4 tyN } ztd| dW Y d}~n
d}~0 0 |S )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   s0   zddl m}  W n ty*   ttY n0 | S )z1Import Motor if available, otherwise raise error.r   AsyncIOMotorClient)Zmotor.motor_asyncior   r   IMPORT_MOTOR_ASYNCIO_ERRORr   r   r   r   _import_motor_asyncio#   s
    r   c              
   K   sT   zt  }|| fi |}W n4 tyN } ztd| dW Y d}~n
d}~0 0 |S )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eeeedd fddZddd	d
ZddddZdddee e	ee	e   e	e
 ddddZdddee e	ee	e   e	e
 ddddZe
dddZe
dddZee ee dddZee ee dddZddddde	e
 e	e
 e	ee  e	e ee dddZddddde	e
 e	e
 e	ee  e	e ee dd d!Zee ddd"d#Zee ddd$d%Z  ZS )&MongoDocumentManagerz7A MongoDB based implementation of the document manager.ZdocumentMetadata)collection_nameN)	namespacer   db_namer   r	   c                   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__r   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)keysr.   r/   r	   c                C   sn   |du rdgt | }t |t |kr.tdt||D ]0\}}| jj| j|dd||  didd q8d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'   r0   r.   r/   r3   r6   r   r   r   update]   s    
zMongoDocumentManager.updatec                   s   |du rdgt | }t |t |kr.td|  I dH }|rP||k rPtdt||D ]2\}}| jj| j|dd||diddI dH  qZdS )	z<Asynchronously upsert documents into the MongoDB collection.Nr1   z0Server time is behind the expected time_at_leastr2   r4   r5   Tr8   )r9   r   	aget_timer:   r&   r;   r    )r'   r0   r.   r/   Zupdate_timer3   r6   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_timerD   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.r@   NrA   rB   )r&   ZdatabaserC   rD   )r'   Z	host_inforE   r   r   r   r>      s    zMongoDocumentManager.aget_time)r0   r	   c                    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 r3   r   .0docr   r   r   	<setcomp>   s   z.MongoDocumentManager.exists.<locals>.<setcomp>$inr2   r3      c                    s   g | ]}| v qS r   r   rH   r3   Zexisting_keysr   r   
<listcomp>       z/MongoDocumentManager.exists.<locals>.<listcomp>)r%   findr    r'   r0   r   rN   r   exists   s    zMongoDocumentManager.existsc                    sD   | 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.rK   r2   r3   rL   c                    s   h | z3 d H W }|d q6 S Nr3   r   rG   r   r   r   rJ      rP   z/MongoDocumentManager.aexists.<locals>.<setcomp>Nc                    s   g | ]}| v qS r   r   rM   rN   r   r   rO      rP   z0MongoDocumentManager.aexists.<locals>.<listcomp>)r&   rQ   r    )r'   r0   cursorr   rN   r   aexists   s
    zMongoDocumentManager.aexists)beforeafterr.   limit)rW   rX   r.   rY   r	   c                C   sv   d| j i}|rd|i|d< |r*d|i|d< |r:d|i|d< |rV| 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    $ltr7   $gtrK   r6   r3   rL   c                 S   s   g | ]}|d  qS rF   r   rG   r   r   r   rO      rP   z2MongoDocumentManager.list_keys.<locals>.<listcomp>)r    r%   rQ   rY   r'   rW   rX   r.   rY   queryrU   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< |rV| 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    rZ   r7   r[   rK   r6   r3   rL   c                    s   g | z3 d H W }|d q6 S rT   r   rG   r   r   r   rO      rP   z3MongoDocumentManager.alist_keys.<locals>.<listcomp>N)r    r&   rQ   rY   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.rK   r2   N)r%   delete_manyr    rR   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.rK   r2   N)r&   r`   r    rR   r   r   r   adelete_keys   s    z!MongoDocumentManager.adelete_keys)__name__
__module____qualname____doc__strr#   r,   r-   r   r   floatr=   r?   r<   r>   r   boolrS   rV   intr^   r_   ra   rb   __classcell__r   r   r(   r   r   :   sn   


r   N)typingr   r   r   r   r   Z langchain_community.indexes.baser   r   r   r   rg   r   r   r   r   r   r   r   r   <module>   s   		