
    ju                         d dl Z d dlmZmZ d dlmZ d dlmZmZ erd dl	m
Z
 eee         gdf         Z G d de          Z G d d	          Z G d
 de          Z G d de          ZdS )    N)CallableSequence)TYPE_CHECKING)
MutableRowWriteRow)Tablec                       e Zd ZdS )InvalidCallbackErrorN)__name__
__module____qualname__     V/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/dataset/chunked.pyr
   r
      s        Dr   r
   c                   d    e Zd Zdddededz  ddfdZddZd	eddfd
ZddZ	de
de
de
ddfdZdS )_Chunkertabler   	chunksizecallbackNreturnc                 n    g | _         || _        || _        |rt          |          st          || _        d S N)queuer   r   callabler
   r   )selfr   r   r   s       r   __init__z_Chunker.__init__   sA     (*
!
' 	'HX.. 	'&&*2r   c                 8    | j                                          d S r   )r   clearr   s    r   flushz_Chunker.flush   s    
r   itemc                     | j                             t          |                     t          | j                   | j        k    r|                                  d S d S r   )r   appenddictlenr   r    )r   r!   s     r   
_queue_addz_Chunker._queue_add"   sL    
$t**%%%tz??dn,,JJLLLLL -,r   c                     | S r   r   r   s    r   	__enter__z_Chunker.__enter__'   s    r   exc_typeexc_valexc_tbc                 .    |                                   d S r   )r    )r   r)   r*   r+   s       r   __exit__z_Chunker.__exit__*   s    

r   r   N)r   r   )r   r   r   int	_Callbackr   r    r   r&   r(   objectr-   r   r   r   r   r      s        33 3 d"	3
 
3 3 3 3   x D    
    & & T      r   r   c            	       `     e Zd ZdZ	 	 ddddededz  ddf fd	Zd
eddf fdZd fdZ	 xZ
S )ChunkedInsertaj  Batch up insert operations
    with ChunkedInsert(my_table) as inserter:
        inserter(row)

    Rows will be inserted in groups of `chunksize` (defaulting to 1000). An
    optional callback can be provided that will be called before the insert.
    This callback takes one parameter which is the queue which is about to be
    inserted into the database
      Nr   r   r   r   r   c                 t    t                      | _        t                                          |||           d S r   )setfieldssuperr   )r   r   r   r   	__class__s       r   r   zChunkedInsert.__init__9   s2     !$	844444r   r!   c                     | j                             |                                           t                                          |           d S r   )r7   updatekeysr8   r&   r   r!   r9   s     r   insertzChunkedInsert.insertB   s?    499;;'''4     r   c                     | j         D ]$}| j        D ]}|                    |          ||<   %| j        |                     | j                    | j                            | j                    t                                                       d S r   )r   r7   getr   r   insert_manyr8   r    )r   r!   fieldr9   s      r   r    zChunkedInsert.flushF   s    J 	. 	.D . ."hhuooU.=$MM$*%%%
tz***r   r4   Nr.   )r   r   r   __doc__r/   r0   r   r   r>   r    __classcell__r9   s   @r   r3   r3   .   s          %)	5 55 5 d"	5
 
5 5 5 5 5 5!8 ! ! ! ! ! ! !         r   r3   c                   p     e Zd ZdZ	 	 ddddee         dededz  d	df
 fd
Zde	d	df fdZ
d fdZ xZS )ChunkedUpdateaf  Batch up update operations
    with ChunkedUpdate(my_table) as updater:
        updater(row)

    Rows will be updated in groups of `chunksize` (defaulting to 1000). An
    optional callback can be provided that will be called before the update.
    This callback takes one parameter which is the queue which is about to be
    updated into the database
    r4   Nr   r   r<   r   r   r   c                 \    || _         t                                          |||           d S r   )r<   r8   r   )r   r   r<   r   r   r9   s        r   r   zChunkedUpdate.__init__[   s.     $(		844444r   r!   c                 J    t                                          |           d S r   )r8   r&   r=   s     r   r;   zChunkedUpdate.updatee   s!    4     r   c                    | j         |                      | j                   | j                            t          j                   t          j        | j        t          j                  D ]2\  }}| j                            t          |          | j                   3t                                                       d S )N)key)r   r   sortr$   r<   	itertoolsgroupbyr   update_manylistr8   r    )r   _fieldsitemsr9   s      r   r    zChunkedUpdate.flushh   s    =$MM$*%%%
DI&&&'/
	JJJ 	; 	;NGUJ""4;;	::::r   rC   r.   )r   r   r   rD   r   strr/   r0   r   r   r;   r    rE   rF   s   @r   rH   rH   P   s          %)5 55 sm5 	5
 d"5 
5 5 5 5 5 5!8 ! ! ! ! ! ! !         r   rH   )rN   collections.abcr   r   typingr   dataset.utilr   r   dataset.tabler   rQ   r0   
ValueErrorr
   r   r3   rH   r   r   r   <module>rZ      s5       . . . . . . . .             - - - - - - - - $######d:&'-.		 	 	 	 	: 	 	 	       :    H   D    H     r   