o
    di                     @   sR   d dl Z d dlZd dlmZ eeZdd ZG dd deZG dd de	Z
dS )	    N)TableResourcec                 K   s   |  dt d S )Nr   )insertCustomTableResource)base_classeskwargs r   K/home/ubuntu/.local/lib/python3.10/site-packages/aioboto3/dynamodb/table.pyregister_table_methods	   s   r	   c                   @   s   e Zd ZdddZdS )r   N   r   c                 C   s   t | j| jj|||dS )N)flush_amountoverwrite_by_pkeyson_exit_loop_sleep)BatchWriternamemetaclient)selfr   r   r   r   r   r   batch_writer   s   
z CustomTableResource.batch_writer)Nr
   r   )__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @   sd   e Zd ZdZ	dddZdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )r   zn
    Modified so that it does async
    Automatically handle batch writes to DynamoDB for a single table.
    r
   Nr   c                 C   s(   || _ || _g | _|| _|| _|| _dS )a=  

        :type table_name: str
        :param table_name: The name of the table.  The class handles
            batch writes to a single table.

        :type client: ``botocore.client.Client``
        :param client: A botocore client.  Note this client
            **must** have the dynamodb customizations applied
            to it for transforming AttributeValues into the
            wire protocol.  What this means in practice is that
            you need to use a client that comes from a DynamoDB
            resource if you're going to instantiate this class
            directly, i.e
            ``boto3.resource('dynamodb').Table('foo').meta.client``.

        :type flush_amount: int
        :param flush_amount: The number of items to keep in
            a local buffer before sending a batch_write_item
            request to DynamoDB.

        :type overwrite_by_pkeys: list(string)
        :param overwrite_by_pkeys: De-duplicate request items in buffer
            if match new request item on specified primary keys. i.e
            ``["partition_key1", "sort_key2", "sort_key3"]``

        :type on_exit_loop_sleep: int
        :param on_exit_loop_sleep: When aexit is called by exiting the
            context manager, if the value is > 0 then every time flush
            is called a sleep will also be called.

        N)_table_name_client_items_buffer_flush_amount_overwrite_by_pkeys_on_exit_loop_sleep)r   
table_namer   r   r   r   r   r   r   __init__   s   #
zBatchWriter.__init__c                       |  dd|iiI d H  d S )N
PutRequestItem_add_request_and_process)r   r!   r   r   r   put_itemG      zBatchWriter.put_itemc                    r   )NDeleteRequestKeyr"   )r   r'   r   r   r   delete_itemJ   r%   zBatchWriter.delete_itemc                    s0   | j r	| | | j| |  I d H  d S N)r    _remove_dup_pkeys_request_if_anyr   append_flush_if_neededr   requestr   r   r   r#   M   s
   
z$BatchWriter._add_request_and_processc                 C   s@   |  |}| jD ]}|  ||kr| j| td| qd S )Nz4With overwrite_by_pkeys enabled, skipping request:%s)_extract_pkey_valuesr   removeloggerdebug)r   r.   pkey_values_newitemr   r   r   r*   S   s   

z,BatchWriter._remove_dup_pkeys_request_if_anyc                    s@     dr fdd| jD S   dr fdd| jD S d S )Nr    c                       g | ]
} d  d | qS )r    r!   r   .0keyr.   r   r   
<listcomp>\       z4BatchWriter._extract_pkey_values.<locals>.<listcomp>r&   c                    r5   )r&   r'   r   r6   r9   r   r   r:   a   r;   )getr   r-   r   r9   r   r/   Z   s   



z BatchWriter._extract_pkey_valuesc                    s(   t | j| jkr|  I d H  d S d S r)   )lenr   r   _flushr   r   r   r   r,   g   s   zBatchWriter._flush_if_neededc                    s   | j d | j }| j | jd  | _ | jj| j|idI d H }|d }|s'i }|| jg }| j | tdt	|t	|t	| j  d S )N)RequestItemsUnprocessedItemsz/Batch write sent %s, unprocessed: %s, buffer %s)
r   r   r   batch_write_itemr   r<   extendr1   r2   r=   )r   items_to_sendresponseunprocessed_items	item_listr   r   r   r>   k   s   zBatchWriter._flushc                    s   | S r)   r   r?   r   r   r   
__aenter__}   s   zBatchWriter.__aenter__c                    sB   | j r|  I d H  | j r| jrt| jI d H  | j sd S d S r)   )r   r>   r   asynciosleep)r   exc_type	exc_valuetbr   r   r   	__aexit__   s   zBatchWriter.__aexit__)r
   Nr   )r   r   r   __doc__r   r$   r(   r#   r*   r/   r,   r>   rH   rN   r   r   r   r   r      s    
*r   )rI   loggingboto3.dynamodb.tabler   	getLoggerr   r1   r	   r   objectr   r   r   r   r   <module>   s    

