
    Zi                         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          Z G d d	e          Z G d
 de          Z G d de          ZdS )    N)ABCabstractmethod)AnySet)override)	Annotatedc            
           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         Y/root/projects/butler/venv/lib64/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%   r8   r   r   r   s      r   r   zPool.__init__1       r   r   r   c                     dS )z"Return a connection from the pool.Nr8   )r   r   r   s      r   r   zPool.connect5   	     	r   c                     dS )z"Close all connections in the pool.Nr8   r'   s    r   r-   z
Pool.close:   r>   r   connc                     dS )z Return a connection to the pool.Nr8   r   r@   s     r   return_to_poolzPool.return_to_pool?   r>   r   r/   )r0   r1   r2   r3   r   r5   r6   r   r   r
   r   r-   rC   r8   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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
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%   )	setrF   	threadinglocalrH   RLockrG   r   rI   r;   s      r   r   zLockPool.__init__Q   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        	                    t          j        |                     |S Nr@   )rG   acquirehasattrrH   r@   r
   r   rI   rF   addweakrefrefr   r   r   new_connections       r   r   zLockPool.connectX   s    
4#V,, 	"1A1F1R#(('dmT\48  <B N %3D!!!'+n"="=>>>!!r   r@   Nc                 \    	 | j                                          d S # t          $ r Y d S w xY wr%   )rG   releaseRuntimeErrorrB   s     r   rC   zLockPool.return_to_poole   sC    	J      	 	 	DD	s    
++c                    | j         D ](} |             |                                             )| j                                          t          j                    | _        	 | j                                         d S # t          $ r Y d S w xY wr%   )	rF   r.   clearrL   rM   rH   rG   rY   rZ   rB   s     r   r-   zLockPool.closel   s    % 	& 	&Dtvv!##%%%!!!$?,,	J      	 	 	DD	s   #A> >
BBFr/   )r0   r1   r2   r3   r   r   rT   ReferenceTyper
   r4   rL   rN   rM   r5   r6   r   r   r   r   rC   r-   r8   r   r   rE   rE   E   s         
 i 5z ABCCCC?   MMMMMM  T     
"S 
"C 
"J 
" 
" 
" X
" : $    X 	 	 	 X	 	 	r   rE   c                       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
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.
    rF   rG   rH   r   _is_uri_Fr   r   c                     t                      | _        t          j                    | _        t          j                    | _        || _        || _        d S r%   )	rK   rF   rL   rM   rH   LockrG   r   rI   r;   s      r   r   zPerThreadPool.__init__   s?    EE$?,,^%%
r   r   r   r    c                 F   t          | j        d          r| j        j        | j        j        S t          | | j        | j        g|R i |}|| j        _        | j        5  | j                            t          j
        |                     d d d            n# 1 swxY w Y   |S rP   )rR   rH   r@   r
   r   rI   rG   rF   rS   rT   rU   rV   s       r   r   zPerThreadPool.connect   s   4#V,, 		"1A1F1R#(('dmT\48  <B N %3D! C C!%%gk.&A&ABBBC C C C C C C C C C C C C C C!!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%   )rG   rF   r.   r\   rL   rM   rH   rB   s     r   r-   zPerThreadPool.close   s    Z 	1 	1) * *466%DFF'')))##%%%(00D	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   A"A77A;>A;r@   c                     d S r%   r8   rB   s     r   rC   zPerThreadPool.return_to_pool   r<   r   r]   r/   )r0   r1   r2   r3   r   r   rT   r^   r
   r4   rL   rc   rM   r5   r6   r   r   r   r   r-   rC   r8   r   r   r`   r`   y   s          i 5z ABCCCC>   MMMNNN  T     
"S 
"C 
"J 
" 
" 
" X
" 1 1 1 X1 : $    X  r   r`   )r   rT   abcr   r   typingr   r   rL   	overridesr   typing_extensionsr   r
   r   rE   r`   r8   r   r   <module>rk      s)     # # # # # # # #                   ' ' ' ' ' '! ! ! ! ! ! ! !H    3   .1 1 1 1 1t 1 1 1h* * * * *D * * * * *r   