
    dha                          S SK JrJrJrJrJr  S SKJr  SrSr	S\4S jr
S\S\S\4S	 jrS\4S
 jrS\S\S\4S jr " S S\5      rg)    )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                  N     SSK Jn   U $ ! [         a    [        [        5      ef = f)z3Import PyMongo if available, otherwise raise error.r   MongoClient)pymongor   ImportErrorIMPORT_PYMONGO_ERRORr   s    e/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/indexes/_document_manager.py_import_pymongor      s/    0'   0.//0   
 $mongodb_urlkwargsc                 p     [        5       nU" U 40 UD6nU$ ! [         a  n[        SU S35      eSnAff = f)zPGet 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
 	

    
505c                  N     SSK Jn   U $ ! [         a    [        [        5      ef = f)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                 p     [        5       nU" U 40 UD6nU$ ! [         a  n[        SU S35      eSnAff = f)zXGet 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                     ^  \ rS rSrSrSS.S\S\S\S\S	S
4
U 4S jjjrS!S jrS!S jrS
S
S.S\	\   S\
\	\
\         S\
\   S	S
4S jjrS
S
S.S\	\   S\
\	\
\         S\
\   S	S
4S jjrS	\4S jrS	\4S jrS\	\   S	\\   4S jrS\	\   S	\\   4S jrS
S
S
S
S.S\
\   S\
\   S\
\	\      S\
\   S	\\   4
S jjrS
S
S
S
S.S\
\   S\
\   S\
\	\      S\
\   S	\\   4
S jjrS\	\   S	S
4S jrS\	\   S	S
4S jrS rU =r$ )"MongoDocumentManager:   z7A MongoDB based implementation of the document manager.documentMetadata)collection_name	namespacer   db_namer)   r	   Nc                   > [         TU ]  US9  [        U5      U l        U R                  U   U l        U R                  U   U l        [        U5      U l        U R                  U   U l        U R                  U   U l	        g)zInitialize 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.   MongoDocumentManager.__init__=   su      	9-.{;''0#||O<-k:))'2 $o >    c                     gz4Create the database schema for the document manager.N r5   s    r   create_schema"MongoDocumentManager.create_schemaU   s    r8   c                    #    g7fr:   r;   r<   s    r   acreate_schema#MongoDocumentManager.acreate_schemaY   s     s   )	group_idstime_at_leastkeysrB   rC   c                
   Uc  S/[        U5      -  n[        U5      [        U5      :w  a  [        S5      e[        X5       H?  u  pEU R                  R	                  U R
                  US.SXPR                  5       S.0SS9  MA     g)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   zipr1   find_one_and_updater*   get_time)r5   rD   rB   rC   rH   rK   s         r   updateMongoDocumentManager.update]   s     T*It9I&PQQ 1MC  44"nnS9hmmoNO 5  2r8   c                l  #    Uc  S/[        U5      -  n[        U5      [        U5      :w  a  [        S5      eU R                  5       I Sh  vN nU(       a  XC:  a  [        S5      e[        X5       H9  u  pVU R                  R                  U R                  US.SXdS.0SS9I Sh  vN   M;     g Nd N7f)	z<Asynchronously upsert documents into the MongoDB collection.NrF   z0Server time is behind the expected time_at_leastrG   rI   rJ   TrM   )rO   r   	aget_timerP   r4   rQ   r*   )r5   rD   rB   rC   update_timerH   rK   s          r   aupdateMongoDocumentManager.aupdater   s      T*It9I&PQQ NN,,[8OPP 1MC'';;"nnS9hJK <    2	 -
s%   A	B4B0AB4&B2'
B42B4c                 l    U R                   R                  S5      nUS   S   nUR                  5       nU$ )z+Get the current server time as a timestamp.hostInfosystemcurrentTime)r0   command	timestamp)r5   server_info
local_timer_   s       r   rR   MongoDocumentManager.get_time   s9    ll**:6 *=9
((*	r8   c                    #    U R                   R                  R                  S5      I Sh  vN nUS   S   nUR                  5       $  N7f)z:Asynchronously get the current server time as a timestamp.r[   Nr\   r]   )r4   databaser^   r_   )r5   	host_infora   s      r   rV   MongoDocumentManager.aget_time   sH     //88@@LL	x(7
##%% Ms   )A
AA
c                     U R                   R                  U R                  SU0S.SS05       Vs1 sH  nUS   iM
     nnU Vs/ sH  oDU;   PM	     sn$ s  snf s  snf )z8Check if the given keys exist in the MongoDB collection.$inrG   rH      )r1   findr*   )r5   rD   docexisting_keysrH   s        r   existsMongoDocumentManager.exists   sw     ++00"nneT]CeQZ
 J 	 
 155}$55
 6s   AAc                    #    U R                   R                  U R                  SU0S.SS05      nU Vs1 s Sh  vN o3S   iM   N

 Os  snf nnU Vs/ sH  oUU;   PM	     sn$ s  snf 7f)zGAsynchronously check if the given keys exist in the MongoDB collection.rh   rG   rH   ri   N)r4   rj   r*   )r5   rD   cursorrk   rl   rH   s         r   aexistsMongoDocumentManager.aexists   si     &&++..%?%
 6<<<cU<<<045}$555s<   2A)A	AA
AA	AA	A)A$!A))beforeafterrB   limitrs   rt   ru   c                H   SU R                   0nU(       a  SU0US'   U(       a  SU0US'   U(       a  SU0US'   U(       a-  U R                  R                  USS05      R                  U5      OU R                  R                  USS05      nU Vs/ sH  owS   PM	     sn$ s  snf )	zJList documents in the MongoDB collection based on the provided date range.r*   $ltrL   $gtrh   rK   rH   ri   )r*   r1   rj   ru   r5   rs   rt   rB   ru   queryrp   rk   s           r   	list_keysMongoDocumentManager.list_keys   s     "-dnn =#(&/E,#(%.E,!&	 2E*    %%eeQZ8>>uE%%**55!*= 	
 '--fsE
f---s   Bc                ^  #    SU R                   0nU(       a  SU0US'   U(       a  SU0US'   U(       a  SU0US'   U(       a-  U R                  R                  USS05      R                  U5      OU R                  R                  USS05      nU Vs/ s S	h  vN owS   PM   N

 sn$ s  snf 7f)
z[
Asynchronously list documents in the MongoDB collection
based on the provided date range.
r*   rw   rL   rx   rh   rK   rH   ri   N)r*   r4   rj   ru   ry   s           r   
alist_keysMongoDocumentManager.alist_keys   s      "-dnn =#(&/E,#(%.E,!&	 2E*  !!&&uuaj9??F&&++EE1:> 	
 -333SE
333s6   BB-B(B$B"
B$B("B$$B(%B-c                 X    U R                   R                  U R                  SU0S.5        g)z-Delete documents from the MongoDB collection.rh   rG   N)r1   delete_manyr*   r5   rD   s     r   delete_keys MongoDocumentManager.delete_keys   s)    ((!^^t}	
r8   c                 t   #    U R                   R                  U R                  SU0S.5      I Sh  vN   g N7f)z<Asynchronously delete documents from the MongoDB collection.rh   rG   N)r4   r   r*   r   s     r   adelete_keys!MongoDocumentManager.adelete_keys   s6     ##//!^^t}
 	
 	
s   .868)r2   r4   r3   r/   r1   r0   )r	   N)__name__
__module____qualname____firstlineno____doc__strr.   r=   r@   r   r   floatrS   rX   rR   rV   r   boolrm   rq   intr{   r~   r   r   __static_attributes____classcell__)r6   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} 
 
 
r8   r&   N)typingr   r   r   r   r    langchain_community.indexes.baser   r   r    r   r   r   r!   r$   r&   r;   r8   r   <module>r      s~    6 6 : R  W 
 
S 
C 
C 
s 3 # # s
= s
r8   