o
    3IhO                     @   s  d dl mZmZmZmZmZmZ d dlmZ d dl	m	Z	 d dl
mZmZmZmZ d dlmZmZmZmZ d dlmZ d dlmZmZmZ d dlmZmZmZmZmZ d d	l m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d d
l6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZI d dlJmKZK d dlLZLG dd deZMdS )    )ListOptionalSequenceTupleUnioncast)UUID)	overrides)CreateCollectionConfiguration+create_collection_configuration_to_json_strUpdateCollectionConfiguration+update_collection_configuration_to_json_str)DEFAULT_DATABASEDEFAULT_TENANTSystemlogger)SysDB)NotFoundErrorUniqueConstraintErrorInternalError)from_proto_collectionfrom_proto_segmentto_proto_update_metadatato_proto_segmentto_proto_segment_scope)CreateCollectionRequestCreateDatabaseRequestCreateSegmentRequestCreateTenantRequestCountCollectionsRequestCountCollectionsResponseDeleteCollectionRequestDeleteDatabaseRequestDeleteSegmentRequestGetCollectionsRequestGetCollectionsResponseGetCollectionSizeRequestGetCollectionSizeResponse GetCollectionWithSegmentsRequest!GetCollectionWithSegmentsResponseGetDatabaseRequestGetSegmentsRequestGetTenantRequestListDatabasesRequestUpdateCollectionRequestUpdateSegmentRequest)	SysDBStub) RetryOnRpcErrorClientInterceptor)OtelInterceptor)OpenTelemetryGranularitytrace_method)

CollectionCollectionAndSegmentsDatabaseMetadataOptionalArgumentSegmentSegmentScopeTenantUnspecifiedUpdateMetadata)EmptyNc                       sZ  e Zd ZU dZeed< ejed< eed< e	ed< e	ed< de
f fdd	ZedH fddZedH fddZedH fddZeefdededed
dfddZeefdeded
efddZeefdeded
dfddZeddefdee	 dee	 ded
ee fddZeded
dfdd Zeded
efd!d"Zed#ed
dfd$d%Zed&eded
dfd'd(Ze			dId&edee d)ee d*ee d
ee f
d+d,Z ee! fd&eded-e"ee#  d
dfd.d/Z$eddd0ee%fdeded1e&d2ee d-ee' d3ee	 d4e(ded5ed
e)e*e(f fd6d7Z+eee%fdeded5ed
dfd8d9Z,eddee%ddfdee dee ded5edee	 dee	 d
ee* fd:d;Z-eedfded5ee d
e	fd<d=Z.eded
e	fd>d?Z/e0d@e1j2edAed
e3fdBdCZ4ee! e! e! e! fdede"e d3e"ee	  d-e"ee#  d1e"ee5  d
dfdDdEZ6dHdFdGZ7  Z8S )J	GrpcSysDBzA gRPC implementation of the SysDB. In the distributed system, the SysDB is also
    called the 'Coordinator'. This implementation is used by Chroma frontend servers
    to call a remote SysDB (Coordinator) service._sys_db_stub_channel_coordinator_url_coordinator_port_request_timeout_secondssystemc                    s6   |j d| _|j d| _|j d| _t |S )Nchroma_coordinator_hostchroma_server_grpc_port$chroma_sysdb_request_timeout_seconds)settingsrequirerC   rD   rE   super__init__)selfrF   	__class__ \/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/chromadb/db/impl/grpc/client.pyrM   M   s   zGrpcSysDB.__init__returnNc                    sX   t j| j d| j dgd| _t t g}t j| jg|R  | _t| j| _	t
  S )N:)zgrpc.max_concurrent_streamsi  )options)grpcinsecure_channelrC   rD   rB   r2   r1   intercept_channelr0   rA   rL   start)rN   interceptorsrO   rQ   rR   rY   V   s   
zGrpcSysDB.startc                    s   | j   t  S N)rB   closerL   stoprN   rO   rQ   rR   r]   a   s   

zGrpcSysDB.stopc                    s   | j t  t  S r[   )rA   
ResetStater?   rL   reset_stater^   rO   rQ   rR   r`   f   s   
zGrpcSysDB.reset_stateidnametenantc                 C   s   zt |j||d}| jj|| jd}W d S  tjy@ } ztd| d| d| d|  |	 tj
jkr9t t d }~ww )Nra   rb   rc   timeoutzFailed to create database name z and database id  for tenant  due to error: )r   hexrA   CreateDatabaserE   rV   RpcErrorr   infocode
StatusCodeALREADY_EXISTSr   r   )rN   ra   rb   rc   requestresponseerQ   rQ   rR   create_databasek   s   zGrpcSysDB.create_databasec                 C   s   z t ||d}| jj|| jd}tt|jjd|jj|jj	dW S  t
jyI } ztd| d| d|  | t
jjkrBt t d }~ww )Nrb   rc   re   ri   rd   zFailed to get database rg   rh   )r*   rA   GetDatabaserE   r7   r   databasera   rb   rc   rV   rk   r   rl   rm   rn   	NOT_FOUNDr   r   )rN   rb   rc   rp   rq   rr   rQ   rQ   rR   get_database|   s&   zGrpcSysDB.get_databasec                 C   sv   zt ||d}| jj|| jd W d S  tjy: } ztd| d| d|  | tj	j
kr4t td }~ww )Nrt   re   zFailed to delete database rg   rh   )r"   rA   DeleteDatabaserE   rV   rk   r   rl   rm   rn   rx   r   r   )rN   rb   rc   rp   rr   rQ   rQ   rR   delete_database   s   zGrpcSysDB.delete_databaselimitoffsetc           	   
   C   s   z+t |||d}| jj|| jd}g }|jD ]}|tt|jd|j	|j
d q|W S  tjyF } ztd| d|  t d }~ww )N)r|   r}   rc   re   ru   rd   z$Failed to list databases for tenant rh   )r-   rA   ListDatabasesrE   	databasesappendr7   r   ra   rb   rc   rV   rk   r   rl   r   )	rN   r|   r}   rc   rp   rq   resultsproto_databaserr   rQ   rQ   rR   list_databases   s,   

zGrpcSysDB.list_databasesc              
   C   sp   zt |d}| jj|| jd}W d S  tjy7 } ztd| d|  | tj	j
kr0t t d }~ww )Nrb   re   zFailed to create tenant rh   )r   rA   CreateTenantrE   rV   rk   r   rl   rm   rn   ro   r   r   rN   rb   rp   rq   rr   rQ   rQ   rR   create_tenant   s   
zGrpcSysDB.create_tenantc              
   C   sz   zt |d}| jj|| jd}t|jjdW S  tjy< } zt	
d| d|  | tjjkr5t t d }~ww )Nr   re   zFailed to get tenant rh   )r,   rA   	GetTenantrE   r<   rc   rb   rV   rk   r   rl   rm   rn   rx   r   r   r   rQ   rQ   rR   
get_tenant   s   
zGrpcSysDB.get_tenantsegmentc              
   C   sx   zt |}t|d}| jj|| jd}W d S  tjy; } ztd| d|  |	 tj
jkr4t t d }~ww )N)r   re   zFailed to create segment 	, error: )r   r   rA   CreateSegmentrE   rV   rk   r   rl   rm   rn   ro   r   r   )rN   r   proto_segmentrp   rq   rr   rQ   rQ   rR   create_segment   s   zGrpcSysDB.create_segment
collectionc                 C   s|   zt |j|jd}| jj|| jd}W d S  tjy= } ztd| d| d|  |	 tj
jkr6t t d }~ww )N)ra   r   re   z!Failed to delete segment with id  for collection rh   )r#   ri   rA   DeleteSegmentrE   rV   rk   r   rl   rm   rn   rx   r   r   )rN   r   ra   rp   rq   rr   rQ   rQ   rR   delete_segment   s"   zGrpcSysDB.delete_segmenttypescopec                 C   s   z1t |r|jnd ||rt|nd |jd}| jj|| jd}g }|jD ]}t|}	||	 q#|W S  t	j
yU }
 ztd| d| d| d| d|
 
 t d }
~
ww )N)ra   r   r   r   re   zFailed to get segment id z, type z, scope r   rh   )r+   ri   r   rA   GetSegmentsrE   segmentsr   r   rV   rk   r   rl   r   )rN   r   ra   r   r   rp   rq   r   r   r   rr   rQ   rQ   rR   get_segments   s,   
 zGrpcSysDB.get_segmentsmetadatac                 C   s   z7d }|t  krtttd f |}t|j|j|rt|nd d}|d u r,|d d|_| j	j
|| jd W d S  tjyU } ztd| d| d|  t d }~ww )N)ra   r   r   r   Tre   z!Failed to update segment with id r   r   )r=   r   r   r>   r/   ri   r   
ClearFieldreset_metadatarA   UpdateSegmentrE   rV   rk   r   rl   r   )rN   r   ra   r   write_metadatarp   rr   rQ   rQ   rR   update_segment  s0   


zGrpcSysDB.update_segmentFconfigurationr   	dimensionget_or_createrw   c
                 C   s   z/t |j|t||rt|nd ||||	dd |D d	}
| jj|
| jd}t|j}||j	fW S  t
jy^ } z"td| d| d|	 d| d	| 
 | t
jjkrWt t d }~ww )
Nc                 S      g | ]}t |qS rQ   )r   .0r   rQ   rQ   rR   
<listcomp>Q      z/GrpcSysDB.create_collection.<locals>.<listcomp>)	ra   rb   configuration_json_strr   r   r   rc   rw   r   re   zFailed to create collection id , name  for database  and tenant rh   )r   ri   r   r   rA   CreateCollectionrE   r   r   createdrV   rk   r   errorrm   rn   ro   r   r   )rN   ra   rb   r   r   r   r   r   rc   rw   rp   rq   r   rr   rQ   rQ   rR   create_collection8  s8   
 zGrpcSysDB.create_collectionc                 C   s   zt |j||d}| jj|| jd}W d S  tjyU } z4td| d| d| d|  t	tj
|}td|  dtjj  | tjjkrNt t d }~ww )	N)ra   rc   rw   re   zFailed to delete collection id r   r   rh   zError code: z, NotFoundError: )r!   ri   rA   DeleteCollectionrE   rV   rk   r   r   r   Callrm   rn   rx   r   r   )rN   ra   rc   rw   rp   rq   rr   rQ   rQ   rR   delete_collection`  s,   zGrpcSysDB.delete_collectionc                 C   s   zRd }|d urt |j||d}|d ur(|d u r|d u rtdt |||||d}|d u r8|d u r8t ||||d}| jj|| jd}g }	|jD ]	}
|	t|
 qF|	W S  t	j
yv } ztd| d| d| d	| d
| 
 t d }~ww )N)ra   r|   r}   zmIf name is specified, tenant and database must also be specified in order to uniquely identify the collection)rb   rc   rw   r|   r}   )rc   rw   r|   r}   re   z"Failed to get collections with id r   z	, tenant z, database rh   )r$   ri   
ValueErrorrA   GetCollectionsrE   collectionsr   r   rV   rk   r   r   r   )rN   ra   rb   rc   rw   r|   r}   rp   rq   r   r   rr   rQ   rQ   rR   get_collections|  sR   

 zGrpcSysDB.get_collectionsc              
   C   s   z'|d u s	|dkrt |d}| j|}|jW S t ||d}| j|}|jW S  tjy? } ztd|  t d }~ww )N )rc   )rc   rw   z*Failed to count collections due to error: )	r   rA   CountCollectionscountrV   rk   r   r   r   )rN   rc   rw   rp   rq   rr   rQ   rQ   rR   count_collections  s(   
zGrpcSysDB.count_collectionsc              
   C   sX   zt |jd}| j|}|jW S  tjy+ } ztd| d|  t	 d }~ww )Nra   Failed to get collection z size due to error: )
r&   ri   rA   GetCollectionSizetotal_records_post_compactionrV   rk   r   r   r   )rN   ra   rp   rq   rr   rQ   rQ   rR   get_collection_size  s   zGrpcSysDB.get_collection_sizez"SysDB.get_collection_with_segmentscollection_idc              
   C   s   zt |jd}| j|}tt|jdd |jD dW S  tj	yB } z|
 tjjkr0t td| d|  t d }~ww )Nr   c                 S   r   rQ   )r   r   rQ   rQ   rR   r     r   z:GrpcSysDB.get_collection_with_segments.<locals>.<listcomp>)r   r   r   z  and its segments due to error: )r(   ri   rA   GetCollectionWithSegmentsr6   r   r   r   rV   rk   rm   rn   rx   r   r   r   r   )rN   r   rp   rq   rr   rQ   rQ   rR   get_collection_with_segments  s"   
z&GrpcSysDB.get_collection_with_segmentsc                 C   sJ  zjd }|t  krtt|}d }|t  krtttd f |}d }|t  kr-tttd f |}d }	|t  kr=tttd f |}	t|j|||rHt	|nd |	rOt
|	nd d}
|d u r_|
d d|
_| jj|
| jd}W d S  tjy } z-ttj|}td| d| d|  | tjjkrt | tjjkrt t d }~ww )N)ra   rb   r   r   r   r   Tre   zFailed to update collection id r   rh   )r=   r   strr   intr>   r   r.   ri   r   r   r   r   rA   UpdateCollectionrE   rV   rk   r   r   r   rm   rn   rx   r   ro   r   r   )rN   ra   rb   r   r   r   
write_namewrite_dimensionr   write_configurationrp   rq   rr   rQ   rQ   rR   update_collection  s\   






zGrpcSysDB.update_collectionc                 C   s   | j jt dd d S )NT)wait_for_ready)rA   r_   r?   r^   rQ   rQ   rR   reset_and_wait_for_ready*  s   z"GrpcSysDB.reset_and_wait_for_ready)rS   N)NNN)9__name__
__module____qualname____doc__r0   __annotations__rV   Channelr   r   r   rM   r	   rY   r]   r`   r   r   rs   r7   ry   r{   r   r   r   r   r<   r   r:   r   r   r;   r   r=   r9   r>   r   r   r
   r8   boolr   r5   r   r   r   r   r   r4   r3   	OPERATIONr6   r   r   r   r   __classcell__rQ   rQ   rO   rR   r@   B   sf  
 
	

 	

'2

	<r@   )Ntypingr   r   r   r   r   r   uuidr   r	   %chromadb.api.collection_configurationr
   r   r   r   chromadb.configr   r   r   r   chromadb.db.systemr   chromadb.errorsr   r   r   chromadb.proto.convertr   r   r   r   r   chromadb.proto.coordinator_pb2r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   #chromadb.proto.coordinator_pb2_grpcr0   chromadb.proto.utilsr1   %chromadb.telemetry.opentelemetry.grpcr2    chromadb.telemetry.opentelemetryr3   r4   chromadb.typesr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   google.protobuf.empty_pb2r?   rV   r@   rQ   rQ   rQ   rR   <module>   s"     \0