
    %Gg                        d dl Z d dl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y)    N)IterableOptional)to_dict)
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          z/home/kushmeetdev/apache_webroot/langgraph_flaskproject/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      ?N=/:s   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 U]KIM;TU & U]KIM;TU    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      
;JO66
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T:z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   sR    "224@@?OPZ__PP 5<L8M M)** Qs   Ac                    | j                  |      }|j                  }|j                  }|j                  |      r|j	                  |       |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        |j(                        |j*                  |j,                         t/        |||       y )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_collectioncreate_collectionr6   vectorssparse_vectorsrL   rM   rN   rO   r   HnswConfigDiffr   rP   OptimizersConfigDiffoptimizer_configWalConfigDiffrR   rS   rT   _recreate_payload_schema)r   r   r	   src_collection_info
src_configsrc_payload_schemas         r   r+   r+   j   s-   
 (66G$++J,;;$$_5%%o6!!!((00(//>>&&33%,,??!+!2!2!K!K"))99))LGJ4J4J,KL 55]
@[@[8\]''I'*2G2G*HI&::%88 "  [/;MNr0   rU   c                     |j                         D ];  \  }}| j                  |||j                  |j                  n|j                         = y )N)
field_namefield_schema)itemscreate_payload_indexr6   	data_type)r   r	   rU   rd   
field_infos        r   r_   r_      sW    
 #1"6"6"8 

J((!1;1B1B1J--PZPaPa 	) 	

r0   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)offsetrl   rm   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"   )rv   )r   typingr   r   qdrant_client._pydantic_compatr   qdrant_client.client_baser   qdrant_client.httpr   strPointStructintfloatr   rE   boolr/   r#   r&   r)   r+   dictPayloadIndexInfor_   r,   r<   r0   r   <module>r      s    % 2 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8





 f5556

 
	

" 	ZZZ Z 	Z
 
Zr0   