
    ha                          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y)    )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                  L    	 ddl m}  | S # t        $ r t        t              w xY w)z3Import PyMongo if available, otherwise raise error.r   MongoClient)pymongor   ImportErrorIMPORT_PYMONGO_ERRORr   s    k/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/indexes/_document_manager.py_import_pymongor      s/    0'   0.//0   
 #mongodb_urlkwargsc                 n    	 t               } || fi |}|S # t        $ r}t        d| d      d}~ww xY 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es        r   _get_pymongo_clientr      sQ    
!#//
 M	  
NqcQRS
 	

    	4/4c                  L    	 ddl m}  | S # t        $ r t        t              w xY w)z1Import Motor if available, otherwise raise error.r   AsyncIOMotorClient)motor.motor_asyncior   r   IMPORT_MOTOR_ASYNCIO_ERRORr   s    r   _import_motor_asyncior!   #   s/    6:   64556r   c                 n    	 t               } || fi |}|S # t        $ r}t        d| d      d}~ww xY w)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$   ,   sU    
%'{-f- M  
A
 	

r   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                  |   | _	        y)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__=   su      	9-.{;''0#||O<-k:))'2 $o >    c                      yz4Create the database schema for the document manager.N r4   s    r   create_schemaz"MongoDocumentManager.create_schemaU   s    r6   c                    K   ywr8   r9   r:   s    r   acreate_schemaz#MongoDocumentManager.acreate_schemaY   s	     s   )	group_idstime_at_leastkeysr>   r?   c                   |dgt        |      z  }t        |      t        |      k7  rt        d      t        ||      D ]C  \  }}| j                  j	                  | j
                  |dd|| j                         did       E y)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t9I&PQQ y1 	MC  44"nnS9hdmmoNO 5 	r6   c                f  K   |dgt        |      z  }t        |      t        |      k7  rt        d      | j                          d{   }|r||k  rt        d      t        ||      D ]=  \  }}| j                  j                  | j                  |dd||did       d{    ? y7 c7 	w)	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t9I&PQQ NN,,[=8OPP y1 	MC'';;"nnS9hkJK <   		 -
s%   AB1	B-
AB1%B/&B1/B1c                 l    | j                   j                  d      }|d   d   }|j                         }|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   s9    ll**:6 *=9
((*	r6   c                    K   | j                   j                  j                  d       d{   }|d   d   }|j                         S 7 w)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   sI     //88@@LL	x(7
##%% Ms   )A
AA
c                     | j                   j                  | j                  d|idddi      D ch c]  }|d   	 }}|D cg c]  }||v  c}S c c}w c c}w )z8Check if the given keys exist in the MongoDB collection.$inrC   rD      )r0   findr)   )r4   r@   docexisting_keysrD   s        r   existszMongoDocumentManager.exists   sn     ++00"nneT]CeQZ
 J
 
 155}$55
 6s   AAc                    K   | j                   j                  | j                  d|idddi      }|2 ch c3 d{   }|d   7 
6 nc c}w }}|D cg c]  }||v  c}S c c}w w)zGAsynchronously check if the given keys exist in the MongoDB collection.r`   rC   rD   ra   N)r3   rb   r)   )r4   r@   cursorrc   rd   rD   s         r   aexistszMongoDocumentManager.aexists   sn     &&++..%?%
 6<<<cU<<<<045}$555s<   2A'A	AA
AA	AA	A'A"A')beforeafterr>   limitri   rj   rk   c                   d| j                   i}|rd|i|d<   |rd|i|d<   |rd|i|d<   |r-| j                  j                  |ddi      j                  |      n| j                  j                  |ddi      }|D cg c]  }|d   	 c}S c c}w )	zJList documents in the MongoDB collection based on the provided date range.r)   $ltrH   $gtr`   rG   rD   ra   )r)   r0   rb   rk   r4   ri   rj   r>   rk   queryrg   rc   s           r   	list_keyszMongoDocumentManager.list_keys   s     "-dnn =#(&/E,#(%.E,!&	 2E*    %%eeQZ8>>uE%%**55!*= 	
 '--sE
---s   ;B
c                6  K   d| j                   i}|rd|i|d<   |rd|i|d<   |rd|i|d<   |r-| j                  j                  |ddi      j                  |      n| j                  j                  |ddi      }|2 cg c3 d	{   }|d   7 
6 c}S c c}w w)
zs
        Asynchronously list documents in the MongoDB collection
        based on the provided date range.
        r)   rm   rH   rn   r`   rG   rD   ra   N)r)   r3   rb   rk   ro   s           r   
alist_keyszMongoDocumentManager.alist_keys   s      "-dnn =#(&/E,#(%.E,!&	 2E*  !!&&uuaj9??F&&++EE1:> 	
 -333SE
3333s6   A;B=B?BB
BBBBBc                 X    | j                   j                  | j                  d|id       y)z-Delete documents from the MongoDB collection.r`   rC   N)r0   delete_manyr)   r4   r@   s     r   delete_keysz MongoDocumentManager.delete_keys   s)    ((!^^t}	
r6   c                 t   K   | j                   j                  | j                  d|id       d{    y7 w)z<Asynchronously delete documents from the MongoDB collection.r`   rC   N)r3   ru   r)   rv   s     r   adelete_keysz!MongoDocumentManager.adelete_keys   s7     ##//!^^t}
 	
 	
s   .868)r	   N)__name__
__module____qualname____doc__strr-   r;   r=   r   r   floatrO   rS   rN   rQ   r   boolre   rh   intrq   rs   rw   ry   __classcell__)r5   s   @r   r&   r&   :   s   A  2?? 	?
 ? ? 
?0 8<)-sm HXc]34	
   
2 8<)-sm HXc]34	
   
2% & &68C= 6T$Z 66(3- 6DJ 6 #'!%-1#. . 	.
 HSM*. }. 
c.6 #'!%-1#4 4 	4
 HSM*4 }4 
c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 : R  W 
 
S 
C 
C 
s 3 # # s
= s
r6   