
    3fi-                        d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	 d dl
mZmZ d dlmZmZ ddlmZ ddlmZ dZd	Z e	d
      ZdededefdZ	 ddedededeee      fdZ	 d deee      dededdfdZ	 	 d!dedededee   deddfdZdedee   fdZ	 ddedededeee      fdZ dedee   fdZ!	 	 d!dedededee   deddfdZ"	 ddedededeee      fdZ#dedee   fdZ$	 	 d!dedededee   deddfdZ%y)"    N)AnyAsyncIteratorIteratorOptionalSequenceTypeVar)
RowMappingtext)ProgrammingErrorSQLAlchemyError   )PGEngine)PGVectorStorelangchain_pg_collectionlangchain_pg_embeddingTenginecollection_namereturnc                   K   dt          d}| j                  j                         4 d{   }|j                  t	        |      d|i       d{   }|j                         }|j                         }ddd      d{    t        d| d      |j                  S 7 w7 S7 %# 1 d{  7  sw Y   5xY ww)a/  
    Get the collection uuid for a collection present in PGVector tables.

    Args:
        engine (PGEngine): The PG engine corresponding to the Database.
        collection_name (str): The name of the collection to get the uuid for.
    Returns:
        The uuid corresponding to the collection.
    zSELECT name, uuid FROM z WHERE name = :collection_nameNr   
parametersCollection, z not found.)	COLLECTIONS_TABLE_poolconnectexecuter
   mappingsfetchone
ValueErroruuid)r   r   queryconnresult
result_mapresult_fetchs          h/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_postgres/utils/pgvector_migrator.py__aget_collection_uuidr(      s      &&7%88VWE||##% - -||K%6$H $ 
 
 __&
!**,- - <'8DEE-
- - - -sV   )B>B#B>"B)B%$B)6B>B'"B>%B)'B>)B;/B20B;7B>
batch_sizec                P  K   	 t        j                  t        | |            }dt         d}| j                  j                         4 d{   }| d{   }|j                  t        |      d|i       d{   }	 |j                  |      }|sn|D 	cg c]  }	|	j                   c}	 3ddd      d{    y7 y7 q7 Mc c}	w 7 # 1 d{  7  sw Y   yxY w# t        $ r t        d| d      t        $ r}
t        d	| d
|
 di|
      |
d}
~
ww xY ww)  
    Extract all data belonging to a PGVector collection.

    Args:
        engine (PGEngine): The PG engine corresponding to the Database.
        collection_name (str): The name of the collection to get the data for.
        batch_size (int): The batch size for collection extraction.
            Default: 1000. Optional.

    Yields:
        The data present in the collection.
    zSELECT * FROM z WHERE collection_id = :idNidr   )sizer    does not exist.z(Failed to extract data from collection 'z': )	statementparamsorig)asynciocreate_taskr(   EMBEDDINGS_TABLEr   r   r   r
   	fetchmany_mappingr    r   r   )r   r   r)   	uuid_taskr"   r#   r!   result_proxyrowsrowes              r'   __aextract_pgvector_collectionr<   +   s;    "''(>v(WX	 !1 22LM<<'') 	5 	5T"?D!%d5ktTl!SSL#--:->/34s||44	 	5 	5 	5"S
 5	5 	5 	5 	5  K<'88HIJJ @@QQTUVTWX$<
 		s   D&AC' CC' CC%C;C	<CC-C4C' ?C C' D&C' C	CCC' C$CC$ C' #D&$C' ' D#DD##D&data_batchesvector_storemax_concurrencyc                 ~  K   t               }| 2 3 d {   }|j                  t        j                  |j	                  |D cg c]  }|j
                   c}|D cg c]!  }t        j                  |j                        # c}|D cg c]  }|j                   c}|D cg c]  }|j                   c}                   t        |      |k\  st        j                  |t        j                         d {   \  }}7 c c}w c c}w c c}w c c}w 7 6 |rt        j                  |       d {  7   y y w)N)texts
embeddings	metadatasids)return_when)setaddr2   ensure_futureaadd_embeddingsdocumentjsonloads	embedding	cmetadatar,   lenwaitFIRST_COMPLETED)r=   r>   r?   pending
batch_datadata_s          r'   __concurrent_batch_insertrV   Q   s    
 G(  j!!,,5?@T4==@GQRt

4>> :R:DE$t~~E-78T8	 - 		
 w<?*&||W%<%<  JAw ARE8
 ) ll7### sy   D=DDD*D=DD=&D	 D=DD=D2D=(D=9D:D=DD=D=5D86D=delete_pg_collectioninsert_batch_sizec           	      r  K   |j                         }t        j                  t        | |            }dt         d}| j
                  j                         4 d{   }| d{   }	|j                  t        |      d|	i       d{   }
|
j                         }|j                         }ddd      d{    t        j                  d| d       yt        | ||      }t        ||d	
       d{    d| }| j
                  j                         4 d{   }|j                  t        |             d{   }
|
j                         }|j                         }ddd      d{    st        d| d      |d   |d   k7  rt        d|d    d| d|d          |rdt         d}| j
                  j                         4 d{   }|j                  t        |      d	i       d{    |j!                          d{    ddd      d{    dt"         d}| j
                  j                         4 d{   }|j                  t        |      d|i       d{    |j!                          d{    ddd      d{    t%        d|        yy7 ^7 W7 47 # 1 d{  7  sw Y   xY w7 7 7 7 o# 1 d{  7  sw Y   xY w7 "7 7 7 # 1 d{  7  sw Y   xY w7 7 7 7 |# 1 d{  7  sw Y   xY ww)a  
    Migrate all data present in a PGVector collection to use separate tables for each collection.
    The new data format is compatible with the PGVectoreStore interface.

    Args:
        engine (PGEngine): The PG engine corresponding to the Database.
        collection_name (str): The collection to migrate.
        vector_store (PGVectorStore): The PGVectorStore object corresponding to the new collection table.
        delete_pg_collection (bool): An option to delete the original data upon migration.
            Default: False. Optional.
        insert_batch_size (int): Number of rows to insert at once in the table.
            Default: 1000.
    zSELECT COUNT(*) FROM z# WHERE collection_id=:collection_idNcollection_idr   r   z contains no elements.)r)   d   )r?   zTable: r.   countz/All data not yet migrated.
Original row count: z
Collection table, z row count: zDELETE FROM z WHERE name=:collection_namer   z*Successfully deleted PGVector collection, )get_table_namer2   r3   r(   r4   r   r   r   r
   r   r   warningswarnr<   rV   r    commitr   print)r   r   r>   rW   rX   destination_tabler7   r"   r#   r!   r$   r%   collection_data_lenr=   
table_sizes                  r'   __amigrate_pgvector_collectionre   j   sR    ( %335 ##$:6?$STI
 011TU 
 ||##% 4 4||DK_d<S|TT__&
(113	4 4
 "_$55KLM 2,=L $L,PS
TTT $$5#67E||##% + +||DK00__&
((*
+ + 7#4"55EFGG7#z''::##6w#?"@ A!!2 3<
7@S?TV
 	

 
/00ST<<'') 	  	 T,,tE{7N,OOO++-	  	  011MN<<'') 	  	 T,,U):O(L     ++-		  	 
 	:?:KLM 
=4T4 4 4 4 U+0+ + + + 	 O	  	  	  	 
	   		  	  	  	 s  AL7J8L7K$J;%%K
J>$K/L7:K;?L7:K;'L7"K#L7&K&K $K&)L74K#5A%L7K<L7"L K?LLLL7(L),L7LL7"L";L<L"LL"L7#L $L7;K>KL7K
KK	L7L7 K&#L7&K9,K/-K94	L7?LLL7LLLL7L"L" L7"L4(L+)L40L7c                   K   	 dt          }| j                  j                         4 d{   }|j                  t	        |             d{   }|j                         }|j                         }ddd      d{    D cg c]  }|d   	 c}S 7 i7 I7 # 1 d{  7  sw Y   +xY wc c}w # t        $ r}t        dt        |      z         |d}~ww xY ww)5Lists all collection names present in PGVector table.zSELECT name from Nnamez2Please provide the correct collection table name: )
r   r   r   r   r
   r   fetchallr   r    str)r   r"   r#   r$   r%   all_rowsr:   r;   s           r'   !__alist_pgvector_collection_namesrl      s     
#$5#67<<'') 	- 	-T<<U44F*J!**,H	- 	- (00F00		-4	- 	- 	- 	- 1 @3q6I
	s   C'B5 BB5 BB$B2B5 =B>B5 B0B5 CB5 BB5 B-!B$"B-)B5 5	C>CCCc                   K   t        | ||      }	 	 | j                  |j                                d{   }| .7 
# t        $ r Y yw xY wwr+   N)r<   _run_as_async	__anext__StopAsyncIterationr   r   r)   iteratorr$   s        r'   aextract_pgvector_collectionrt      sX     " .fozRH
	!//0B0B0DEEFL E! 		s5   A"A ?	A AA 	A
AAAc                 R   K   | j                  t        |              d{   S 7 w)rg   N)ro   rl   r   s    r'   alist_pgvector_collection_namesrw      s%      %%&G&OPPPPs   '%'c           	      \   K   | j                  t        | ||||             d{    y7 w)a  
    Migrate all data present in a PGVector collection to use separate tables for each collection.
    The new data format is compatible with the PGVectorStore interface.

    Args:
        engine (PGEngine): The PG engine corresponding to the Database.
        collection_name (str): The collection to migrate.
        vector_store (PGVectorStore): The PGVectorStore object corresponding to the new collection table.
        use_json_metadata (bool): An option to keep the PGVector metadata as json in the new table.
            Default: False. Optional.
        delete_pg_collection (bool): An option to delete the original data upon migration.
            Default: False. Optional.
        insert_batch_size (int): Number of rows to insert at once in the table.
            Default: 1000.
    N)ro   re   r   r   r>   rW   rX   s        r'   amigrate_pgvector_collectionrz      s7     , 

& 	
  s   ",*,c              #      K   t        | ||      }	 	 | j                  |j                               }| %# t        $ r Y yw xY wwrn   )r<   _run_as_syncrp   rq   rr   s        r'   extract_pgvector_collectionr}     sT     " .fozRH
	((););)=>FL  " 		s$   A#6 A	AAAAc                 6    | j                  t        |             S )rg   )r|   rl   rv   s    r'   list_pgvector_collection_namesr      s    @HII    c           	      @    | j                  t        | ||||             y)a  
    Migrate all data present in a PGVector collection to use separate tables for each collection.
    The new data format is compatible with the PGVectorStore interface.

    Args:
        engine (PGEngine): The PG engine corresponding to the Database.
        collection_name (str): The collection to migrate.
        vector_store (PGVectorStore): The PGVectorStore object corresponding to the new collection table.
        delete_pg_collection (bool): An option to delete the original data upon migration.
            Default: False. Optional.
        insert_batch_size (int): Number of rows to insert at once in the table.
            Default: 1000.
    N)r|   re   ry   s        r'   migrate_pgvector_collectionr   %  s)    ( & 	
r   )  )r[   )Fr   )&r2   rK   r^   typingr   r   r   r   r   r   
sqlalchemyr	   r
   sqlalchemy.excr   r   	v2.enginer   v2.vectorstoresr   r   r4   r   rj   r(   intr<   rV   boolre   listrl   rt   rw   rz   r}   r   r    r   r'   <module>r      so      L L ' <   +- + CL 	8 ### # 8J'(	#R $ 45$$ $ 
	$: ,1!FNFNFN  FN #4.	FN
 FN 
FNR	#Y(   8J'(	4QQ	#YQ ,1!   #4.	
  
H   hz"#	4J8 JS	 J ,1!   #4.	
  
r   