
    Ng                          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Z
dededefd	Zdefd
ZdededefdZ G 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                  Z    	 ddl m}  n"# t          $ r t          t                    w xY w| S )z3Import PyMongo if available, otherwise raise error.r   MongoClient)pymongor   ImportErrorIMPORT_PYMONGO_ERRORr   s    i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/indexes/_document_manager.py_import_pymongor      sL    0''''''' 0 0 0.///0   	 (mongodb_urlkwargsc                     	 t                      } || fi |}n%# t          $ r}t          d| d          d}~ww xY w|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es        r   _get_pymongo_clientr      sr    
!##//// 
 
 
WSTWWW
 
 	

 M    
<7<c                  Z    	 ddl m}  n"# t          $ r t          t                    w xY w| S )z1Import Motor if available, otherwise raise error.r   AsyncIOMotorClient)motor.motor_asyncior   r   IMPORT_MOTOR_ASYNCIO_ERRORr   s    r   _import_motor_asyncior!   #   sL    6::::::: 6 6 645556r   c                     	 t                      } || fi |}n%# t          $ r}t          d| d          d}~ww xY w|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   motorr   r   s        r   _get_motor_clientr$   ,   sz    
%''{--f-- 
 
 
  
 
 	


 Mr   c                       e Zd ZdZdddededededd	f
 fd
ZddZddZd	d	ddee         de	ee	e                           de	e
         dd	fdZd	d	ddee         de	ee	e                           de	e
         dd	fdZde
fdZde
fdZdee         dee         fdZdee         dee         f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Zd	d	d	d	dde	e
         de	e
         de	ee                  de	e         dee         f
dZdee         dd	fdZdee         dd	fdZ xZS ) MongoDocumentManagerz7A MongoDB based implementation of the document manager.documentMetadata)collection_name	namespacer   db_namer(   r	   Nc                ,   t                                          |           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   sync_clientsync_dbsync_collectionr$   async_clientasync_dbasync_collection)selfr)   r   r*   r(   	__class__s        r   r-   zMongoDocumentManager.__init__=   s      	9---.{;;'0#|O<-k::)'2 $o >    c                     dS z4Create the database schema for the document manager.N r4   s    r   create_schemaz"MongoDocumentManager.create_schemaU   s    r6   c                 
   K   dS r8   r9   r:   s    r   acreate_schemaz#MongoDocumentManager.acreate_schemaY   s      r6   )	group_idstime_at_leastkeysr>   r?   c                2   |dgt          |          z  }t          |          t          |          k    rt          d          t          ||          D ]A\  }}| j                            | j        |dd||                                 did           Bd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upsert)lenr   zipr0   find_one_and_updater)   get_time)r4   r@   r>   r?   rD   rG   s         r   updatezMongoDocumentManager.update]   s     T*It99I&&PQQQ y11 	 	MC 44"nS99hdmmooNNO 5    	 	r6   c                  K   |dgt          |          z  }t          |          t          |          k    rt          d          |                                  d{V }|r||k     rt          d          t          ||          D ]5\  }}| j                            | j        |dd||did           d{V  6dS )	z<Asynchronously upsert documents into the MongoDB collection.NrB   z0Server time is behind the expected time_at_leastrC   rE   rF   TrI   )rK   r   	aget_timerL   r3   rM   r)   )r4   r@   r>   r?   update_timerD   rG   s          r   aupdatezMongoDocumentManager.aupdater   s      T*It99I&&PQQQ NN,,,,,,,, 	Q[=88OPPP y11 	 	MC';;"nS99hkJJK <          	 	r6   c                 ~    | j                             d          }|d         d         }|                                }|S )z+Get the current server time as a timestamp.hostInfosystemcurrentTime)r/   command	timestamp)r4   server_info
local_timerY   s       r   rN   zMongoDocumentManager.get_time   s=    l**:66 *=9
((**	r6   c                    K   | j         j                            d           d{V }|d         d         }|                                S )z:Asynchronously get the current server time as a timestamp.rU   NrV   rW   )r3   databaserX   rY   )r4   	host_infor[   s      r   rQ   zMongoDocumentManager.aget_time   sR      /8@@LLLLLLLL	x(7
##%%%r6   c                     d | j                             | j        d|idddi          D             fd|D             S )z8Check if the given keys exist in the MongoDB collection.c                     h | ]
}|d          S rD   r9   .0docs     r   	<setcomp>z.MongoDocumentManager.exists.<locals>.<setcomp>   s,     
 
 
 J
 
 
r6   $inrC   rD      c                     g | ]}|v S r9   r9   rc   rD   existing_keyss     r   
<listcomp>z/MongoDocumentManager.exists.<locals>.<listcomp>       555}$555r6   )r0   findr)   )r4   r@   rj   s     @r   existszMongoDocumentManager.exists   sg    
 
+00"neT]CCeQZ 
 
 
 65555555r6   c                    K   | j                             | j        d|idddi          }d |2              d{V fd|D             S )zGAsynchronously check if the given keys exist in the MongoDB collection.rf   rC   rD   rg   c                 .   K   h | 3 d {V }|d         6 S NrD   r9   rb   s     r   re   z/MongoDocumentManager.aexists.<locals>.<setcomp>   s2      <<<<<<<<cU<<<<   Nc                     g | ]}|v S r9   r9   ri   s     r   rk   z0MongoDocumentManager.aexists.<locals>.<listcomp>   rl   r6   )r3   rm   r)   )r4   r@   cursorrj   s      @r   aexistszMongoDocumentManager.aexists   sw      &++.%??%
 
 =<V<<<<<<<<<55555555r6   )beforeafterr>   limitrv   rw   rx   c                    d| j         i}|rd|i|d<   |rd|i|d<   |rd|i|d<   |r0| j                            |ddi                              |          n| j                            |ddi          }d	 |D             S )
zJList documents in the MongoDB collection based on the provided date range.r)   $ltrH   $gtrf   rG   rD   rg   c                     g | ]
}|d          S ra   r9   rb   s     r   rk   z2MongoDocumentManager.list_keys.<locals>.<listcomp>   s    ---sE
---r6   )r)   r0   rm   rx   r4   rv   rw   r>   rx   queryrt   s          r   	list_keyszMongoDocumentManager.list_keys   s     "-dn = 	2#(&/E, 	1#(%.E, 	3!&	 2E* >D %%eeQZ88>>uEEE%**55!*== 	
 .-f----r6   c                  K   d| j         i}|rd|i|d<   |rd|i|d<   |rd|i|d<   |r0| j                            |ddi                              |          n| j                            |ddi          }d	 |2              d
{V S )zs
        Asynchronously list documents in the MongoDB collection
        based on the provided date range.
        r)   rz   rH   r{   rf   rG   rD   rg   c                 .   K   g | 3 d {V }|d         6 S rq   r9   rb   s     r   rk   z3MongoDocumentManager.alist_keys.<locals>.<listcomp>   s2      33333333SE
3333rr   N)r)   r3   rm   rx   r}   s          r   
alist_keyszMongoDocumentManager.alist_keys   s       "-dn = 	2#(&/E, 	1#(%.E, 	3!&	 2E* ?D!&&uuaj99??FFF&++EE1:>> 	
 43F3333333333r6   c                 N    | j                             | j        d|id           dS )z-Delete documents from the MongoDB collection.rf   rC   N)r0   delete_manyr)   r4   r@   s     r   delete_keysz MongoDocumentManager.delete_keys   s8    ((.%??	
 	
 	
 	
 	
r6   c                 ^   K   | j                             | j        d|id           d{V  dS )z<Asynchronously delete documents from the MongoDB collection.rf   rC   N)r3   r   r)   r   s     r   adelete_keysz!MongoDocumentManager.adelete_keys   sZ      #//.%??
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r6   )r	   N)__name__
__module____qualname____doc__strr-   r;   r=   r   r   floatrO   rS   rN   rQ   r   boolrn   ru   intr   r   r   r   __classcell__)r5   s   @r   r&   r&   :   s2       AA  2? ? ?? 	?
 ? ? 
? ? ? ? ? ?0       8<)-  sm HXc]34	
   
   2 8<)-  sm HXc]34	
   
   2%    & & & & &68C= 6T$Z 6 6 6 66(3- 6DJ 6 6 6 6 #'!%-1#. . . . 	.
 HSM*. }. 
c. . . .6 #'!%-1#4 4 4 4 	4
 HSM*4 }4 
c4 4 4 46
 
$ 
 
 
 

x} 
 
 
 
 
 
 
 
 
r6   r&   N)typingr   r   r   r   r    langchain_community.indexes.baser   r   r    r   r   r   r!   r$   r&   r9   r6   r   <module>r      s   6 6 6 6 6 6 6 6 6 6 6 6 6 6 : : : : : : R  W 
    
S 
C 
C 
 
 
 
s    3 # #    m
 m
 m
 m
 m
= m
 m
 m
 m
 m
r6   