o
    3Ih                     @   s   d dl Z d dlZd dlmZmZ d dlmZmZ d dlZd dl	m
Z
 d dlmZ G dd dZG dd	 d	eZG d
d deZG dd deZdS )    N)ABCabstractmethod)AnySet)override)	Annotatedc                
   @   s   e Zd ZU dZded< eed< ejed< dddeded	e	d
e	f
ddZ
ddedejfddZdddZdddZdejfddZdddZdS )
ConnectionzBA threadpool connection that returns itself to the pool on close()Pool_pool_db_file_connpooldb_fileis_uriargskwargsc                 O   s:   || _ || _tj|g|R dd|d|| _d | j_d S )Ni  F)timeoutcheck_same_threaduri)r
   r   sqlite3connectr   isolation_level)selfr   r   r   r   r    r   \/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/chromadb/db/impl/sqlite_pool.py__init__   s   zConnection.__init__.sqlreturnc                 C   s"   |du r
| j |S | j ||S )N.)r   execute)r   r   
parametersr   r   r   r      s   zConnection.executeNc                 C      | j   d S N)r   commitr   r   r   r   r"          zConnection.commitc                 C   r    r!   )r   rollbackr#   r   r   r   r%   #   r$   zConnection.rollbackc                 C   s
   | j  S r!   )r   cursorr#   r   r   r   r&   &   s   
zConnection.cursorc                 C   s   | j   dS )z(Actually closes the connection to the dbN)r   closer#   r   r   r   close_actual)   s   zConnection.close_actual).r   N)__name__
__module____qualname____doc____annotations__strr   r   boolr   r   Cursorr   r"   r%   r&   r(   r   r   r   r   r   
   s*   
 




r   c                   @   sh   e Zd ZdZedededdfddZeded	ede	fd
dZ
edddZede	ddfddZdS )r	   zCAbstract base class for a pool of connections to a sqlite database.r   r   r   Nc                 C      d S r!   r   r   r   r   r   r   r   r   1      zPool.__init__r   r   c                 O      dS )z"Return a connection from the pool.Nr   )r   r   r   r   r   r   r   5      zPool.connectc                 C   r5   )z"Close all connections in the pool.Nr   r#   r   r   r   r'   :   r6   z
Pool.closeconnc                 C   r5   )z Return a connection to the pool.Nr   r   r7   r   r   r   return_to_pool?   r6   zPool.return_to_poolr)   )r*   r+   r,   r-   r   r/   r0   r   r   r   r   r'   r9   r   r   r   r   r	   .   s    r	   c                   @   s   e Zd ZU dZeeejef  e	d< e
je	d< e
je	d< ee	d< ee	d< dded	efd
dZedededefddZededdfddZedddZdS )LockPoolaI  A pool that has a single connection per thread but uses a lock to ensure that only one thread can use it at a time.
    This is used because sqlite does not support multithreaded access with connection timeouts when using the
    shared cache mode. We use the shared cache mode to allow multiple threads to share a database.
    _connections_lock_connectionr   _is_uriFr   r   c                 C   ,   t  | _t | _t | _|| _|| _d S r!   )	setr;   	threadinglocalr=   RLockr<   r   r>   r3   r   r   r   r   Q   
   


zLockPool.__init__r   r   r   c                 O   sf   | j   t| jdr| jjd ur| jjS t| | j| jg|R i |}|| j_| j	t
| |S Nr7   )r<   acquirehasattrr=   r7   r   r   r>   r;   addweakrefrefr   r   r   new_connectionr   r   r   r   X   s   

zLockPool.connectr7   Nc                 C   s&   z| j   W d S  ty   Y d S w r!   )r<   releaseRuntimeErrorr8   r   r   r   r9   e   s
   zLockPool.return_to_poolc                 C   sZ   | j D ]}| d ur|   q| j   t | _z| j  W d S  ty,   Y d S w r!   )	r;   r(   clearrA   rB   r=   r<   rM   rN   r8   r   r   r   r'   l   s   




zLockPool.closeFr)   )r*   r+   r,   r-   r   r   rI   ReferenceTyper   r.   rA   rC   rB   r/   r0   r   r   r   r   r9   r'   r   r   r   r   r:   E   s   
 

r:   c                   @   s   e Zd ZU dZeeejef  e	d< e
je	d< e
je	d< ee	d< ee	d< dded	efd
dZedededefddZedddZededdfddZdS )PerThreadPoolzMaintains a connection per thread. For now this does not maintain a cap on the number of connections, but it could be
    extended to do so and block on connect() if the cap is reached.
    r;   r<   r=   r   _is_uri_Fr   r   c                 C   r?   r!   )	r@   r;   rA   rB   r=   Lockr<   r   r>   r3   r   r   r   r      rD   zPerThreadPool.__init__r   r   r   c                 O   s   t | jdr| jjd ur| jjS t| | j| jg|R i |}|| j_| j | jt	
| W d    |S 1 s;w   Y  |S rE   )rG   r=   r7   r   r   r>   r<   r;   rH   rI   rJ   rK   r   r   r   r      s    

zPerThreadPool.connectNc                 C   s`   | j # | jD ]}| d ur|   q| j  t | _W d    d S 1 s)w   Y  d S r!   )r<   r;   r(   rO   rA   rB   r=   r8   r   r   r   r'      s   



"zPerThreadPool.closer7   c                 C   r2   r!   r   r8   r   r   r   r9      r4   zPerThreadPool.return_to_poolrP   r)   )r*   r+   r,   r-   r   r   rI   rQ   r   r.   rA   rT   rB   r/   r0   r   r   r   r   r'   r9   r   r   r   r   rR   y   s   
 

rR   )r   rI   abcr   r   typingr   r   rA   	overridesr   typing_extensionsr   r   r	   r:   rR   r   r   r   r   <module>   s    $4