
    sOi                        d dl Z d dl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
   dz  dededdfdZdede
defdZ	 ddedee
   dz  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	j.                  f   ddfdZ	 ddedede
deddf
dZy)    N)Iterable)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          d/volume1/homes/robertsu/coba/app/.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   migrater/   #   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  ''	(r0   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>   r?   r@   r*   list)r   r   dest_collectionsrC   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<   )r3   r5   payload_schemacollection_existsdelete_collectionrT   r   StrictModeConfigr   itemsr   create_collectionr6   vectorssparse_vectorsrL   rM   rN   rO   HnswConfigDiffrP   OptimizersConfigDiffoptimizer_configWalConfigDiffrR   rS   _recreate_payload_schema)	r   r   r	   src_collection_info
src_configsrc_payload_schemarT   kvs	            r   r+   r+   j   s   
 (66G$++J,;;$$_5%%o69=$$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rU   c                     |j                         D ];  \  }}| j                  |||j                  |j                  n|j                         = y )N)
field_namefield_schema)rY   create_payload_indexr6   	data_type)r   r	   rU   rh   
field_infos        r   ra   ra      sR    
 #1"6"6"8
J((!1;1B1B1J--PZPaPa 	) 	
 #9r0   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)offsetro   rp   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@r0   )   g      @)NFd   r"   )ry   )r   typingr   qdrant_client._pydantic_compatr   r   qdrant_client.client_baser   qdrant_client.httpr   strPointStructintfloatr   rE   boolr/   r#   r&   r)   r+   dictPayloadIndexInfora   r,   r<   r0   r   <module>r      s     @ 0 % NNN V''(N 	N
 N 
N: *."'%U%U%U 3i$&%U  	%U
 %U 
%UPj 3 4  EI15cT1A	#Y +* +S	 +dSVi +#O#O#O #O 
	#OL





 f5556

 
	

" 	ZZZ Z 	Z
 
Zr0   