
    ui                         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	  G d d          Z
 G d de          Z G d	 d
e          Z G d de          ZdS )    N)ABCabstractmethod)AnySet)overridec            
           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Z
ddedej        fdZddZddZdej        fdZ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                 v    || _         || _        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   s         O/var/www/icac/venv/lib/python3.11/site-packages/chromadb/db/impl/sqlite_pool.py__init__zConnection.__init__   s`     
_
IM
 
!U
 
QW
 

 &*
"""    .sqlreturnc                 t    |du r| j                             |          S | j                             ||          S )N.)r   execute)r   r   
parameterss      r   r!   zConnection.execute   s;    :%%c***z!!#z222r   Nc                 8    | j                                          d S N)r   commitr   s    r   r%   zConnection.commit   s    
r   c                 8    | j                                          d S r$   )r   rollbackr&   s    r   r(   zConnection.rollback!   s    
r   c                 4    | j                                         S r$   )r   cursorr&   s    r   r*   zConnection.cursor$   s    z  """r   c                 8    | j                                          dS )z(Actually closes the connection to the dbN)r   closer&   s    r   close_actualzConnection.close_actual'   s    
r   ).r   N)__name__
__module____qualname____doc____annotations__strr   r	   boolr   r   Cursorr!   r%   r(   r*   r-    r   r   r	   r	      s         LLMMMMMM**%(*26*?B*NQ* * * *3 33 37> 3 3 3 3
      # # # # #     r   r	   c                       e Zd ZdZedededdfd            Zededede	fd	            Z
edd
            Zede	ddfd            ZdS )r
   zCAbstract base class for a pool of connections to a sqlite database.r   r   r   Nc                     d S r$   r7   r   r   r   s      r   r   zPool.__init__/       r   r   r   c                     dS )z"Return a connection from the pool.Nr7   )r   r   r   s      r   r   zPool.connect3   	     	r   c                     dS )z"Close all connections in the pool.Nr7   r&   s    r   r,   z
Pool.close8   r=   r   connc                     dS )z Return a connection to the pool.Nr7   r   r?   s     r   return_to_poolzPool.return_to_pool=   r=   r   r.   )r/   r0   r1   r2   r   r4   r5   r   r   r	   r   r,   rB   r7   r   r   r
   r
   ,   s        MM T d    ^ S C J    ^    ^ : $    ^  r   r
   c                       e Zd ZU dZee         ed<   ej        ed<   ej	        ed<   e
ed<   eed<   dde
d	efd
Zedededefd            Zededdfd            Ze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                     t                      | _        t          j                    | _        t          j                    | _        || _        || _        d S r$   )	setrE   	threadinglocalrG   RLockrF   r   rH   r:   s      r   r   zLockPool.__init__O   s?    EE$?,,_&&
r   r   r   r   c                    | j                                          t          | j        d          r| j        j        | j        j        S t          | | j        | j        g|R i |}|| j        _        | j        	                    |           |S Nr?   )
rF   acquirehasattrrG   r?   r	   r   rH   rE   addr   r   r   new_connections       r   r   zLockPool.connectV   s    
4#V,, 	"1A1F1R#(('dmT\48  <B N %3D!!!.111!!r   r?   Nc                 \    	 | j                                          d S # t          $ r Y d S w xY wr$   )rF   releaseRuntimeErrorrA   s     r   rB   zLockPool.return_to_poolc   sC    	J      	 	 	DD	s    
++c                     | j         D ]}|                                 | j                                          t          j                    | _        	 | j                                         d S # t          $ r Y d S w xY wr$   )	rE   r-   clearrK   rL   rG   rF   rV   rW   rA   s     r   r,   zLockPool.closej   s    % 	  	 D!!!$?,,	J      	 	 	DD	s   A, ,
A:9A:Fr.   )r/   r0   r1   r2   r   r	   r3   rK   rM   rL   r4   r5   r   r   r   r   rB   r,   r7   r   r   rD   rD   C   s
         
 j/!!!?   MMMMMM  T     
"S 
"C 
"J 
" 
" 
" X
" : $    X    X  r   rD   c                       e Zd ZU dZee         ed<   ej        ed<   ej	        ed<   e
ed<   eed<   dde
d	efd
Zedededefd            Zedd            Zededdf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.
    rE   rF   rG   r   _is_uri_Fr   r   c                     t                      | _        t          j                    | _        t          j                    | _        || _        || _        d S r$   )	rJ   rE   rK   rL   rG   LockrF   r   rH   r:   s      r   r   zPerThreadPool.__init__   s?    EE$?,,^%%
r   r   r   r   c                 "   t          | j        d          r| j        j        | j        j        S t          | | j        | j        g|R i |}|| j        _        | j        5  | j                            |           d d d            n# 1 swxY w Y   |S rO   )	rQ   rG   r?   r	   r   rH   rF   rE   rR   rS   s       r   r   zPerThreadPool.connect   s    4#V,, 		"1A1F1R#(('dmT\48  <B N %3D! 6 6!%%n5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6!!s   BBBNc                     | j         5  | j        D ]}|                                 | j                                         t	          j                    | _        d d d            d S # 1 swxY w Y   d S r$   )rF   rE   r-   rY   rK   rL   rG   rA   s     r   r,   zPerThreadPool.close   s    Z 	1 	1) $ $!!######%%%(00D		1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   AA%%A),A)r?   c                     d S r$   r7   rA   s     r   rB   zPerThreadPool.return_to_pool   r;   r   rZ   r.   )r/   r0   r1   r2   r   r	   r3   rK   r_   rL   r4   r5   r   r   r   r   r,   rB   r7   r   r   r\   r\   v   s
          j/!!!>   MMMNNN  T     
"S 
"C 
"J 
" 
" 
" X
" 1 1 1 X1 : $    X  r   r\   )r   abcr   r   typingr   r   rK   	overridesr   r	   r
   rD   r\   r7   r   r   <module>rf      s    # # # # # # # #                  ! ! ! ! ! ! ! !H    3   .0 0 0 0 0t 0 0 0f) ) ) ) )D ) ) ) ) )r   