
    ug                         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y)    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y)
ConnectionzBA threadpool connection that returns itself to the pool on close()Pool_pool_db_file_connpooldb_fileis_uriargskwargsc                     || _         || _        t        j                  |g|dd|d|| _        d | j                  _        y )Ni  F)timeoutcheck_same_threaduri)r   r   sqlite3connectr   isolation_level)selfr   r   r   r   r   s         Q/var/www/openai/venv/lib/python3.12/site-packages/chromadb/db/impl/sqlite_pool.py__init__zConnection.__init__   sO     
__
IM
!U
QW

 &*

"    sqlreturnc                 x    |du r| j                   j                  |      S | j                   j                  ||      S )N.)r   execute)r   r   
parameterss      r   r"   zConnection.execute   s6    ::%%c**zz!!#z22r   Nc                 8    | j                   j                          y N)r   commitr   s    r   r&   zConnection.commit    s    

r   c                 8    | j                   j                          y r%   )r   rollbackr'   s    r   r)   zConnection.rollback#   s    

r   c                 6    | j                   j                         S r%   )r   cursorr'   s    r   r+   zConnection.cursor&   s    zz  ""r   c                 8    | j                   j                          y)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MM**%(*26*?B*NQ*33 37>> 3
# #r   r
   c                   x    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y)r   zCAbstract base class for a pool of connections to a sqlite database.r   r   r    Nc                      y r%   r8   r   r   r   s      r   r   zPool.__init__1       r   r   r   c                      y)z"Return a connection from the pool.Nr8   )r   r   r   s      r   r   zPool.connect5        	r   c                      y)z"Close all connections in the pool.Nr8   r'   s    r   r-   z
Pool.close:   r>   r   connc                      y)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 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y)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_urir   r   c                     t               | _        t        j                         | _        t        j
                         | _        || _        || _        y r%   )	setrF   	threadinglocalrH   RLockrG   r   rI   r;   s      r   r   zLockPool.__init__Q   s7    E$??,__&
r   r   r   r    c                    | j                   j                          t        | j                  d      r,| j                  j                  | j                  j                  S t        | | j                  | j                  g|i |}|| j                  _        | 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,1A1A1F1F1R##((('dmmT\\48<BN %3D!!!'++n"=>!!r   r@   Nc                 X    	 | j                   j                          y # t        $ r Y y w xY wr%   )rG   releaseRuntimeErrorrB   s     r   rC   zLockPool.return_to_poole   s)    	JJ  		s    	))c                    | j                   D ]  } |        |       j                          ! | j                   j                          t        j                         | _        	 | j                  j                          y # t        $ r Y y w xY wr%   )	rF   r.   clearrL   rM   rH   rG   rY   rZ   rB   s     r   r-   zLockPool.closel   sr    %%Dv!##% & 	!$??,	JJ  		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 5 5z ABCC?? MM T  
"S 
"C 
"J 
" 
" : $   	 	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y)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_r   r   c                     t               | _        t        j                         | _        t        j
                         | _        || _        || _        y r%   )	rK   rF   rL   rM   rH   LockrG   r   rI   r;   s      r   r   zPerThreadPool.__init__   s7    E$??,^^%
r   r   r   r    c                    t        | j                  d      r,| j                  j                  | j                  j                  S t        | | j                  | j
                  g|i |}|| j                  _        | j                  5  | j                  j                  t        j                  |             d d d        |S # 1 sw Y   |S xY wrP   )rR   rH   r@   r
   r   rI   rG   rF   rS   rT   rU   rV   s       r   r   zPerThreadPool.connect   s    4##V,1A1A1F1F1R##((('dmmT\\48<BN %3D!!!%%gkk.&AB !! !!s   /B>>CNc                    | j                   5  | j                  D ]  } |        |       j                          ! | j                  j                          t	        j
                         | _        d d d        y # 1 sw Y   y xY wr%   )rG   rF   r.   r\   rL   rM   rH   rB   s     r   r-   zPerThreadPool.close   s\    ZZ))6%F'') * ##%(0D ZZs   A8A
A88Br@   c                      y 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 5 5z ABCC>> MN T  
"S 
"C 
"J 
" 
" 1 1 : $  r   r`   )r   rT   abcr   r   typingr   r   rL   	overridesr   typing_extensionsr   r
   r   rE   r`   r8   r   r   <module>rk      sJ      #    '! !H3 .1t 1h*D *r   