
    $j                         d Z ddlZddlmZ ddlmZ ddlmZ ddlZddl	m
Z
mZ  ej        e          Z G d d	e          Z G d
 de
          ZdS )u  PostgreSQL backend implementation using asyncpg.

Wraps asyncpg's pool and connection objects behind the DatabaseBackend
and DatabaseConnection interfaces.  Returns raw asyncpg.Record objects
from fetch/fetchrow — they satisfy the ResultRow protocol natively in C,
avoiding Python-level wrapping overhead (~570K __getitem__ calls per
20-query benchmark → measurable regression when wrapped).
    N)AsyncIterator)asynccontextmanager)Any   )DatabaseBackendDatabaseConnectionc                   z   e Zd ZdZdZdej        ddfdZede	d          fd            Z
ddd	ed
ededz  defdZddd	ed
eeedf                  dedz  ddfdZddd	ed
ededz  defdZddd	ed
ededz  fdZdddd	ed
edededz  def
dZdddedeeedf                  dee         dedz  ddf
dZdS )PostgresConnectionz8DatabaseConnection wrapper around an asyncpg.Connection._connconnreturnNc                     || _         d S Nr   )selfr   s     j/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/engine/db/postgresql.py__init__zPostgresConnection.__init__   s    


    c                   K   | j                                         4 d {V  | W V  d d d           d {V  d S # 1 d {V swxY w Y   d S r   )r   transactionr   s    r   r   zPostgresConnection.transaction   s      :))++ 	 	 	 	 	 	 	 	JJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ;
AAtimeoutqueryargsr   c                >   K    | j         j        |g|R d|i d {V S Nr   )r   executer   r   r   r   s       r   r   zPostgresConnection.execute#   s=      'TZ'FFFFgFFFFFFFFFr   .c                P   K   | j                             |||           d {V  d S )Nr   )r   executemany)r   r   r   r   s       r   r!   zPostgresConnection.executemany&   s;      j$$UD'$BBBBBBBBBBBr   c                >   K    | j         j        |g|R d|i d {V S r   )r   fetchr   s       r   r#   zPostgresConnection.fetch)   s?       &TZ%eDdDDDGDDDDDDDDDr   c                >   K    | j         j        |g|R d|i d {V S r   )r   fetchrowr   s       r   r%   zPostgresConnection.fetchrow/   s=      (TZ(GGGGwGGGGGGGGGr   r   columnr   r'   c                @   K    | j         j        |g|R ||d d {V S )Nr&   )r   fetchval)r   r   r'   r   r   s        r   r)   zPostgresConnection.fetchval3   s?      (TZ(VVVfgVVVVVVVVVVr   
table_namerecordscolumnsc                R   K   | j                             ||||           d{V  dS )z0Use asyncpg's native COPY for fast bulk loading.)r+   r,   r   N)r   copy_records_to_table)r   r*   r+   r,   r   s        r   r.   z(PostgresConnection.copy_records_to_table6   sA       j..z7T[el.mmmmmmmmmmmr   )__name__
__module____qualname____doc__	__slots__asyncpg
Connectionr   r   r   r   strr   floatr   listtupler!   r#   r%   intr)   r.    r   r   r
   r
      s       BBIW/ D     =1E#F     MQ G G G3 Gs GUT\ GUX G G G G ei C C Cs C$uS#X2G CUZ]aUa Cmq C C C C KO E E E ES E54< ESW E E E E NR H H HC H Hedl H H H H DE^b W W WC W WS WuW[| Wgj W W W W !%	n 	n 	n	n eCHo&		n
 c	n 	n 
	n 	n 	n 	n 	n 	nr   r
   c                       e Zd ZdZdddededz  ddfdZddZd	d
ddddddedededededede	dz  ddfdZ
ddZedee         fd            Zedee         fd            Zdej        fdZdej        fdZdS )PostgreSQLBackendzCDatabaseBackend implementation wrapping an asyncpg connection pool.N)schemadsnr>   r   c                X    ddl m} ddlm}  |            } ||||j                   dS )z&Run Alembic migrations for PostgreSQL.   )
get_config)run_migrations)r>   migration_database_urlN)configrB   
migrationsrC   rD   )r   r?   r>   rB   rC   rE   s         r   rC   z PostgreSQLBackend.run_migrationsE   sQ    ((((((000000s6&B_``````r   c                     d | _         d S r   )_poolr   s    r   r   zPostgreSQLBackend.__init__M   s    *.


r         i,     r   )min_sizemax_sizecommand_timeoutacquire_timeoutstatement_cache_sizeinit_callbackrL   rM   rN   rO   rP   rQ   c                   K   t          j        |||||||           d {V | _        t                              d| d| d| d| d	           d S )N)rL   rM   rN   rP   r   initzPostgreSQL pool created (min=z, max=z, cmd_timeout=zs, acquire_timeout=zs))r4   create_poolrH   loggerinfo)r   r?   rL   rM   rN   rO   rP   rQ   s           r   
initializezPostgreSQLBackend.initializeP   s       #.+!5#
 
 
 
 
 
 
 
 

 	SH S SH S S*S S?NS S S	
 	
 	
 	
 	
r   c                    K   | j         B| j                                          d {V  d | _         t                              d           d S d S )NzPostgreSQL pool closed)rH   closerU   rV   r   s    r   shutdownzPostgreSQLBackend.shutdowni   s\      :!*""$$$$$$$$$DJKK011111 "!r   c                   K   |                                  }|                                4 d {V }t          |          W V  d d d           d {V  d S # 1 d {V swxY w Y   d S r   )_ensure_poolacquirer
   r   poolr   s      r   r]   zPostgreSQLBackend.acquireo   s        ""<<>> 	+ 	+ 	+ 	+ 	+ 	+ 	+T$T******	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   A
A!$A!c                P  K   |                                  }|                                4 d {V }|                                4 d {V  t          |          W V  d d d           d {V  n# 1 d {V swxY w Y   d d d           d {V  d S # 1 d {V swxY w Y   d S r   )r\   r]   r   r
   r^   s      r   r   zPostgreSQLBackend.transactionu   s       ""<<>> 	/ 	/ 	/ 	/ 	/ 	/ 	/T'')) / / / / / / / /(....../ / / / / / / / / / / / / / / / / / / / / / / / / / /	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s5   BA1B1
A;	;B>A;	?B
B"Bc                 *    |                                  S r   )r\   r   s    r   get_poolzPostgreSQLBackend.get_pool|   s      """r   c                 <    | j         t          d          | j         S )Nz>PostgreSQLBackend is not initialized. Call initialize() first.)rH   RuntimeErrorr   s    r   r\   zPostgreSQLBackend._ensure_pool   s!    :_```zr   )r   N)r/   r0   r1   r2   r6   rC   r   r:   r7   r   rW   rZ   r   r   r
   r]   r   r4   Poolrb   r\   r;   r   r   r=   r=   B   s       MM?C a a a# a#* a a a a a/ / / / !$!#$%$(
 
 

 	

 
 
 
 "
 Tz
 

 
 
 
22 2 2 2 +}-?@ + + + +
 /=1C#D / / / /#', # # # #gl      r   r=   )r2   loggingcollections.abcr   
contextlibr   typingr   r4   baser   r   	getLoggerr/   rU   r
   r=   r;   r   r   <module>rl      s      ) ) ) ) ) ) * * * * * *        5 5 5 5 5 5 5 5		8	$	$)n )n )n )n )n+ )n )n )nX@ @ @ @ @ @ @ @ @ @r   