o
    g#                     @   s   d dl mZmZ d dlZd dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ dee dee fddZdee dee fddZG dd deZ dS )    )DictOptionalN)	overrides)	GetResultMetadataQueryResult)System)Executor)Scan)	CountPlanGetPlanKNNPlan)convert)QueryExecutorStub) RetryOnRpcErrorClientInterceptor)DistributedSegmentManager)OtelInterceptormetadatareturnc                 C   sD   | sdS i }|   D ]\}}|ds|||< q
t|dkr dS |S )z[Remove any chroma-specific metadata keys that the client shouldn't see from a metadata map.Nzchroma:r   )items
startswithlen)r   resultkv r   ^/var/www/visachat/venv/lib/python3.10/site-packages/chromadb/execution/executor/distributed.py_clean_metadata   s   
r   c                 C   s   | rd| v rt | d S dS )z-Retrieve the uri (if any) from a Metadata mapz
chroma:uriN)strr   r   r   r   _uri   s   r    c                       s   e Zd ZU eeef ed< eed< eed< de	f fddZ
ededefd	d
ZededefddZededefddZdedefddZ  ZS )DistributedExecutor_grpc_stub_pool_manager_request_timeout_secondssystemc                    s2   t  | t | _| t| _|jd| _d S )N$chroma_query_request_timeout_seconds)	super__init__dictr"   requirer   r#   settingsr$   )selfr%   	__class__r   r   r(   *   s   
zDistributedExecutor.__init__planr   c              
   C   sJ   |  |j}z
|t|}W n tjy } z|d }~ww t|S N)_grpc_executuor_stubscanCountr   to_proto_count_plangrpcRpcErrorfrom_proto_count_result)r,   r/   executorcount_result	rpc_errorr   r   r   count2   s   
zDistributedExecutor.countc              
   C   s   |  |j}z
|t|}W n tjy } z|d }~ww t|}dd |D }|jj	r7dd |D nd }|jj
rDdd |D nd }|jjrQdd |D nd }	|jjr^dd |D nd }
t||||	d |
|jjdS )Nc                 S      g | ]}|d  qS )idr   .0recordr   r   r   
<listcomp>D       z+DistributedExecutor.get.<locals>.<listcomp>c                 S   r<   )	embeddingr   r>   r   r   r   rA   F   rB   c                 S   r<   )documentr   r>   r   r   r   rA   K   rB   c                 S      g | ]}t |d  qS r   r    r>   r   r   r   rA   P       c                 S   rE   r   r   r>   r   r   r   rA   U   rG   )ids
embeddings	documentsurisdata	metadatasincluded)r1   r2   Getr   to_proto_get_planr5   r6   from_proto_get_result
projectionrC   rD   urir   r   rO   )r,   r/   r8   
get_resultr:   recordsrI   rJ   rK   rL   rN   r   r   r   get;   sD   
zDistributedExecutor.getc              
   C   s   |  |j}z
|t|}W n tjy } z|d }~ww t|}dd |D }|jj	r7dd |D nd }|jj
rDdd |D nd }|jjrQdd |D nd }	|jjr^dd |D nd }
|jjrkdd |D nd }t||||	d |
||jjdS )	Nc                 S      g | ]	}d d |D qS )c                 S      g | ]}|d  d qS )r@   r=   r   r>   r   r   r   rA   n   rG   6DistributedExecutor.knn.<locals>.<listcomp>.<listcomp>r   r?   rV   r   r   r   rA   n       z+DistributedExecutor.knn.<locals>.<listcomp>c                 S   rX   )c                 S   rY   )r@   rC   r   r>   r   r   r   rA   q   rG   rZ   r   r[   r   r   r   rA   p       c                 S   rX   )c                 S   rY   )r@   rD   r   r>   r   r   r   rA   y   rG   rZ   r   r[   r   r   r   rA   x   r]   c                 S   rX   )c                 S      g | ]
}t |d  d qS r@   r   rF   r>   r   r   r   rA          rZ   r   r[   r   r   r   rA      r]   c                 S   rX   )c                 S   r^   r_   rH   r>   r   r   r   rA      r`   rZ   r   r[   r   r   r   rA      r]   c                 S   rX   )c                 S   r<   )distancer   r>   r   r   r   rA      rB   rZ   r   r[   r   r   r   rA      r\   )rI   rJ   rK   rL   rM   rN   	distancesrO   )r1   r2   KNNr   to_proto_knn_planr5   r6   from_proto_knn_batch_resultrS   rC   rD   rT   r   rankr   rO   )r,   r/   r8   
knn_resultr:   resultsrI   rJ   rK   rL   rN   rb   r   r   r   knne   s^   

zDistributedExecutor.knnr2   c                 C   sX   | j |j}|| jvr't|}t t g}tj|g|R  }t	|| j|< | j| S r0   )
r#   get_endpointr@   r"   r5   insecure_channelr   r   intercept_channelr   )r,   r2   grpc_urlchannelinterceptorsr   r   r   r1      s   


z(DistributedExecutor._grpc_executuor_stub)__name__
__module____qualname__r   r   r   __annotations__r   intr   r(   r   r   r;   r   r   rW   r   r   ri   r
   r1   __classcell__r   r   r-   r   r!   %   s   
 );r!   )!typingr   r   r5   r   chromadb.api.typesr   r   r   chromadb.configr   $chromadb.execution.executor.abstractr	   &chromadb.execution.expression.operatorr
   "chromadb.execution.expression.planr   r   r   chromadb.protor   &chromadb.proto.query_executor_pb2_grpcr   chromadb.proto.utilsr   )chromadb.segment.impl.manager.distributedr   %chromadb.telemetry.opentelemetry.grpcr   r   r   r    r!   r   r   r   r   <module>   s     