
    Ng                        d dl Z d dlmZmZmZmZ d dl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j        f         ddfdZ	 d!de	de	d	ededdf
dZdS )"    N)DictIterableListOptional)to_dict)
QdrantBase)models         @clientcollection_namepointsmax_attemptspausereturnc           	      <   d}||k    r	 |                      ||d           d S # t          $ rT}t          d| d| d|            ||k     r't          d| d           t          j        |           |dz  }Y d }~nd }~ww xY w||k    t          d	| d
          )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          Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/qdrant_client/migrate/migrate.pyupload_with_retryr   	   s     H
l
"
"	   / !   
 F 	 	 	FFFXFFFFGGG,&&8888999
5!!!MHHHHHH	 l
"
" L\LLL
M
MMs   $ 
BA
A==BFd   source_clientdest_clientcollection_namesrecreate_on_collision
batch_sizec                     t           |          }t           fd|D                       rt          d          t          ||          }t	          |          t	          |          z
  }|r|st          d|           |D ]%}t           ||           t           |||           &|D ]%}t           ||           t           |||           &dS )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   8   K   | ]}t          |          V  d S N)_has_custom_shards).0r   r    s     r   	<genexpr>zmigrate.<locals>.<genexpr>7   s?        ?N=/::         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   migrater5   #   s3   & 2-AQRR
    Rb     ] [\\\!+/?@@J!"233c*ooE T/ TRjRRSSS2 U U]KIIIM;TTTT% U U]KIIIM;TTTTU Ur+   c                     |                      |          }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   s>    #22?CCO&-/@$GG '	(r+   c                     |                                  j        }d |D             |Ft          fd|D                       s*J dt          |          t                    z
               n}|S )Nc                     g | ]	}|j         
S  namer)   
collections     r   
<listcomp>z._select_source_collections.<locals>.<listcomp>W   s    TTT:zTTTr+   c              3       K   | ]}|v V  	d S r'   rA   )r)   r   source_collection_namess     r   r*   z-_select_source_collections.<locals>.<genexpr>Z   s:       
 
;JO66
 
 
 
 
 
r+   z)Source client does not have collections: )get_collectionscollectionsallr0   )r    r"   source_collectionsrH   s      @r   r,   r,   S   s     '6688DTTASTTT# 
 
 
 
N^
 
 
 
 
 	n 	nms;K7L7LsSjOkOk7kmm	n 	n 	n 	n 3r+   c                     |                                  j        }d |D             }|t          |          z  }t          |          S )Nc                     h | ]	}|j         
S rA   rB   rD   s     r   	<setcomp>z#_find_collisions.<locals>.<setcomp>e   s    PPPZ_PPPr+   )rI   rJ   r0   list)r!   r"   dest_collectionsdest_collection_namesexisting_dest_collectionss        r   r/   r/   c   sO    "2244@PP?OPPP 5<L8M8M M)***r+   c                 J   |                      |          }|j        }|j        }|                    |          r|                    |           |                    ||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          |j                  |j                   t-          |||           d S )N)
vectors_configsparse_vectors_configshard_numberreplication_factorwrite_consistency_factoron_disk_payloadhnsw_configoptimizers_config
wal_configquantization_configrA   )r8   r:   payload_schemacollection_existsdelete_collectioncreate_collectionr;   vectorssparse_vectorsrW   rX   rY   rZ   r	   HnswConfigDiffr   r[   OptimizersConfigDiffoptimizer_configWalConfigDiffr]   r^   _recreate_payload_schema)r    r!   r   src_collection_info
src_configsrc_payload_schemas         r   r1   r1   j   s'   
 (66GG$+J,;$$_55 7%%o666!!!(0(/>&3%,?!+!2!K")9)LLGJ4J,K,KLL 5]]
@[8\8\]]'II'*2G*H*HII&: "    [/;MNNNNNr+   r_   c                     |                                 D ]0\  }}|                     |||j        |j        n|j                   1d S )N)
field_namefield_schema)itemscreate_payload_indexr;   	data_type)r!   r   r_   rn   
field_infos        r   ri   ri      sm    
 #1"6"6"8"8 
 

J((!1;1B1J--PZPa 	) 	
 	
 	
 	

 
r+   c                 X   |                      |dd          \  }}t          |||           |0|                      |||d          \  }}t          |||           |0|                     |          j        }|                    |          j        }||k    sJ d| d|             dS )	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)offsetrv   rw   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   r2   r2      s    )//qW[/\\G[[/RYZZZZ 
!,33KzPT  4  
  
 	oV]^^^^	 
!
 #0"5"5o"F"F"L + 1 1/ B B H#'@@@@ ZMh  Z  Z  X  Z  Z 	A@@@@r+   )r
   r   )NFr   r'   )r   )r   typingr   r   r   r   qdrant_client._pydantic_compatr   qdrant_client.client_baser   qdrant_client.httpr	   strPointStructintfloatr   boolr5   r(   r,   r/   r1   PayloadIndexInfori   r2   rA   r+   r   <module>r      s    1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0 % % % % % % N NNN V'(N 	N
 N 
N N N N: -1"'%U %U%U%U tCy)%U  	%U
 %U 
%U %U %U %UPj 3 4     HL 19$s)1D	#Y    +* +S	 +dSVi + + + +OOO O 
	O O O O6





 f556

 
	

 

 

 

" 	Z ZZZ Z 	Z
 
Z Z Z Z Z Zr+   