
    'Xhg                        d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	 d dl
mZ 	 	 dde	dedeej                     d	ed
eddfdZ	 	 	 dde	de	deee      dededdfdZde	dedefdZ	 dde	deee      dee   fdZde	dee   dee   fdZde	de	deddfdZde	dedeeej2                  f   ddfdZ	 dde	de	dededdf
dZy)    N)IterableOptionalAny)to_dictmodel_fields)
QdrantBase)modelsclientcollection_namepointsmax_attemptspausereturnc           	         d}||k  r	 | j                  ||d       y t        d	| d
      # t        $ rL}t        d| d| d|        ||k  r$t        d| d       t        j                  |       |dz  }Y d }~nd }~ww xY w||k  rn)N   T)r   r   waitzException: z
, attempt /zNext attempt in z secondszFailed to upload points after z	 attempts)upload_points	Exceptionprinttimesleep)r
   r   r   r   r   attemptses          G/RAG/venv/lib/python3.12/site-packages/qdrant_client/migrate/migrate.pyupload_with_retryr   	   s     H
l
"	   / ! 
  4\N)L
MM  	Ks*XJa~FG,&(x89

5!MH	 l
"s   - 	BAA==Bsource_clientdest_clientcollection_namesrecreate_on_collision
batch_sizec                 N    t         |      }t         fd|D              rt        d      t        ||      }t	        |      t	        |      z
  }|r|st        d|       |D ]  }t         ||       t         |||        |D ]  }t         ||       t         |||        y)aE  
    Migrate collections from source client to destination client

    Args:
        source_client (QdrantBase): Source client
        dest_client (QdrantBase): Destination client
        collection_names (list[str], optional): List of collection names to migrate.
            If None - migrate all source client collections. Defaults to None.
        recreate_on_collision (bool, optional): If True - recreate collection if it exists, otherwise
            raise ValueError.
        batch_size (int, optional): Batch size for scrolling and uploading vectors. Defaults to 100.
    c              3   6   K   | ]  }t        |        y wN)_has_custom_shards).0r   r   s     r   	<genexpr>zmigrate.<locals>.<genexpr>7   s      Rb=/:Rbs   z@Migration of collections with custom shards is not supported yetz*Collections already exist in dest_client: N)_select_source_collectionsany
ValueError_find_collisionsset_recreate_collection_migrate_collection)r   r   r   r    r!   
collisionsabsent_dest_collectionsr   s   `       r   migrater1   #   s    & 2-AQR
 Rb  [\\!+/?@J!"23c*oE/Ej\RSS2]KIM;T 3 &]KIM;T &    c                     | j                  |      }t        |j                  j                  dd       t        j
                  j                  k(  S )Nsharding_method)get_collectiongetattrconfigparamsr	   ShardingMethodCUSTOM)r   r   collection_infos      r   r%   r%   K   sD    #22?CO&&--/@$G  ''	(r2   c                     | j                         j                  }|D cg c]  }|j                   c}|5t        fd|D              sJ dt	        |      t	              z
          |S }|S c c}w )Nc              3   &   K   | ]  }|v  
 y wr$    )r&   r   source_collection_namess     r   r'   z-_select_source_collections.<locals>.<genexpr>Z   s      
N^?O66N^s   z)Source client does not have collections: )get_collectionscollectionsnameallr,   )r   r   source_collections
collectionr?   s       @r   r(   r(   S   s     '668DDASTAS:zAST# 
N^
 
 	n6s;K7LsSjOk7k6lm	n 
  3 Us   A1c                     | j                         j                  }|D ch c]  }|j                   }}|t        |      z  }t	        |      S c c}w r$   )r@   rA   rB   r,   list)r   r   dest_collectionsrE   dest_collection_namesexisting_dest_collectionss         r   r+   r+   c   sU    "224@@?OP?OZ__?OP 5<L8M M)** Qs   Ac                    | j                  |      }|j                  }|j                  }|j                  |      r|j	                  |       d }|j
                  et        j                  di t        |j
                        j                         D ci c]#  \  }}|t        t        j                        v r||% c}}}|j                  ||j                  j                  |j                  j                  |j                  j                  |j                  j                   |j                  j"                  |j                  j$                  t        j&                  di t        |j(                        t        j*                  di t        |j,                        t        j.                  di t        |j0                        |j2                  |       t5        |||       y c c}}w )N)vectors_configsparse_vectors_configshard_numberreplication_factorwrite_consistency_factoron_disk_payloadhnsw_configoptimizers_config
wal_configquantization_configstrict_mode_configr>   )r5   r7   payload_schemacollection_existsdelete_collectionrV   r	   StrictModeConfigr   itemsr   create_collectionr8   vectorssparse_vectorsrN   rO   rP   rQ   HnswConfigDiffrR   OptimizersConfigDiffoptimizer_configWalConfigDiffrT   rU   _recreate_payload_schema)	r   r   r   src_collection_info
src_configsrc_payload_schemarV   kvs	            r   r-   r-   j   s   
 (66G$++J,;;$$_5%%o6<@$$0#44 
 $J$A$ABHHJJDAqV%<%<== 1J
 !!!((00(//>>&&33%,,??!+!2!2!K!K"))99))LGJ4J4J,KL 55]
@[@[8\]''I'*2G2G*HI&::- "  [/;MN+s   (GrW   c                     |j                         D ];  \  }}| j                  |||j                  |j                  n|j                         = y )N)
field_namefield_schema)r[   create_payload_indexr8   	data_type)r   r   rW   rj   
field_infos        r   rc   rc      sR    
 #1"6"6"8
J((!1;1B1B1J--PZPaPa 	) 	
 #9r2   c                 4   | j                  |dd      \  }}t        |||       |)| j                  |||d      \  }}t        |||       |)| j                  |      j                  }|j                  |      j                  }||k(  sJ d| d|        y)	aF  Migrate collection from source client to destination client

    Args:
        collection_name (str): Collection name
        source_client (QdrantBase): Source client
        dest_client (QdrantBase): Destination client
        batch_size (int, optional): Batch size for scrolling and uploading vectors. Defaults to 100.
       T)limitwith_vectors)r
   r   r   N)offsetrq   rr   zCMigration failed, vectors count are not equal: source vector count z, dest vector count )scrollr   count)r   r   r   r!   recordsnext_offsetsource_client_vectors_countdest_client_vectors_counts           r   r.   r.      s     )//qW[/\G[[/RYZ 
!,33KzPT  4  
 	oV]^	 
!
 #0"5"5o"F"L"L + 1 1/ B H H#'@@Z	LMhLii}  X  ~Y  ZZ@r2   )   g      @)NFd   r$   )r{   )r   typingr   r   r   qdrant_client._pydantic_compatr   r   qdrant_client.client_baser   qdrant_client.httpr	   strPointStructintfloatr   rG   boolr1   r%   r(   r+   r-   dictPayloadIndexInforc   r.   r>   r2   r   <module>r      s    * * @ 0 % NNN V''(N 	N
 N 
N: -1"'%U%U%U tCy)%U  	%U
 %U 
%UPj 3 4  HL19$s)1D	#Y +* +S	 +dSVi +#O#O#O #O 
	#OL





 f5556

 
	

" 	ZZZ Z 	Z
 
Zr2   