o
    "i,                     @   s   U d Z ddlZddlmZ ddlmZmZ ddlmZ e	e
ZG dd dZdaee ed< d	efd
dZddededefddZdS )z
Milvus Connection Pool Manager

Provides singleton connection management for Milvus to avoid creating
new connections per request.
    N)Optional)connections
Collection)Lockc                       s   e Zd ZU dZdZed  ed< e Z fddZ	dd Z
ddededefddZdd ZdedefddZdefddZdd Z  ZS )MilvusConnectionManagerz;Singleton Milvus connection manager with connection poolingN	_instancec                    sd   | j d u r/| j | j d u rt | | _ W d    | j S W d    | j S 1 s*w   Y  | j S )N)r   _locksuper__new__)cls	__class__ I/lsinfo/ai/hellotax_ai/base_platform/app/services/vector/milvus_client.pyr
      s   



zMilvusConnectionManager.__new__c                 C   s@   t | dsd| _d| _d| _d| _d| _d| _td dS dS )zInitialize connection managerinitializedTdefaultFNz%Milvus connection manager initialized)	hasattrr   connection_alias	connectedhostportuse_liteloggerinfoselfr   r   r   __init__   s   
z MilvusConnectionManager.__init__	localhost19530Fr   r   r   c              
   C   s   | j r| j|kr| j|kr| j|krtd dS |   z3|r.tj| j	dd t
d ntj| j	||d t
d| d|  d	| _ || _|| _|| _W dS  tyh } ztd
|  d| _  d}~ww )z
        Establish connection to Milvus server

        Args:
            host: Milvus server host
            port: Milvus server port
            use_lite: Whether to use Milvus Lite (local file storage)
        z Using existing Milvus connectionNz./data/milvus_data.db)aliasurizConnected to Milvus Lite)r   r   r   zConnected to Milvus at :TzFailed to connect to Milvus: F)r   r   r   r   r   debug
disconnectr   connectr   r   	Exceptionerror)r   r   r   r   er   r   r   r$   '   s8   	
zMilvusConnectionManager.connectc              
   C   sf   | j r1ztj| jd d| _ td W dS  ty0 } ztd|  W Y d}~dS d}~ww dS )zDisconnect from Milvus server)r   FzDisconnected from Milvusz!Error disconnecting from Milvus: N)r   r   r#   r   r   r   r%   r&   r   r'   r   r   r   r#   R   s   z"MilvusConnectionManager.disconnectcollection_namereturnc              
   C   s   | j stdz,t|}z|  W |W S  ty3 } ztd| d|  W Y d}~|W S d}~ww  tyK } ztd| d|   d}~ww )z
        Get a Milvus collection

        Args:
            collection_name: Name of the collection

        Returns:
            Collection object

        Raises:
            Exception if not connected
        z.Not connected to Milvus. Call connect() first.zCollection z load attempt: NzFailed to get collection z: )r   r%   r   loadr   r"   r&   )r   r)   
collection
load_errorr'   r   r   r   get_collection\   s"   
 z&MilvusConnectionManager.get_collectionc              
   C   sV   | j sdS zt  W dS  ty* } ztd|  d| _ W Y d}~dS d}~ww )z}
        Check if connection is healthy

        Returns:
            True if connection is healthy, False otherwise
        FTzMilvus health check failed: N)r   r   list_connectionsr%   r   r&   r(   r   r   r   health_checky   s   z$MilvusConnectionManager.health_checkc                 C   sH   |   std d| _| jr | jr"| | j| j| j dS dS dS dS )z)Reconnect to Milvus if connection is lostz$Attempting to reconnect to Milvus...FN)r0   r   r   r   r   r   r$   r   r   r   r   r   	reconnect   s   
z!MilvusConnectionManager.reconnectr   r   F)__name__
__module____qualname____doc__r   r   __annotations__r   r   r
   r   strboolr$   r#   r   r.   r0   r1   __classcell__r   r   r   r   r      s   
 +
r   _milvus_managerr*   c                   C   s   t du rt a t S )z1Get or create Milvus connection manager singletonN)r;   r   r   r   r   r   get_milvus_manager   s   r<   r   r   Fr   r   r   c                 C   s   t  }|| || dS )z
    Initialize Milvus connection at application startup

    Args:
        host: Milvus server host
        port: Milvus server port
        use_lite: Whether to use Milvus Lite
    N)r<   r$   )r   r   r   managerr   r   r   init_milvus_connection   s   	r>   r2   )r6   loggingtypingr   pymilvusr   r   	threadingr   	getLoggerr3   r   r   r;   r7   r<   r8   r9   r>   r   r   r   r   <module>   s    
 