o
    ܀c                     @   s:   d dl Z e eZdd ZG dd dZG dd dZdS )    Nc                 K   s   |  dt d S )Nr   )insertTableResource)base_classeskwargs r   ?/usr/local/lib/python3.10/dist-packages/boto3/dynamodb/table.pyregister_table_methods   s   r   c                       s&   e Zd Z fddZdddZ  ZS )r   c                    s   t  j|i | d S N)super__init__)selfargsr   	__class__r   r   r         zTableResource.__init__Nc                 C   s   t | j| jj|dS )aY  Create a batch writer object.

        This method creates a context manager for writing
        objects to Amazon DynamoDB in batch.

        The batch writer will automatically handle buffering and sending items
        in batches.  In addition, the batch writer will also automatically
        handle any unprocessed items and resend them as needed.  All you need
        to do is call ``put_item`` for any items you want to add, and
        ``delete_item`` for any items you want to delete.

        Example usage::

            with table.batch_writer() as batch:
                for _ in range(1000000):
                    batch.put_item(Item={'HashKey': '...',
                                         'Otherstuff': '...'})
                # You can also delete_items in a batch.
                batch.delete_item(Key={'HashKey': 'SomeHashKey'})

        :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"]``

        )overwrite_by_pkeys)BatchWriternamemetaclient)r   r   r   r   r   batch_writer   s   zTableResource.batch_writerr	   )__name__
__module____qualname__r   r   __classcell__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   zAAutomatically handle batch writes to DynamoDB for a single table.   Nc                 C   s"   || _ || _g | _|| _|| _dS )aU  

        :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"]``

        N)_table_name_client_items_buffer_flush_amount_overwrite_by_pkeys)r   
table_namer   flush_amountr   r   r   r   r   B   s
   
zBatchWriter.__init__c                 C      |  dd|ii d S )N
PutRequestItem_add_request_and_process)r   r%   r   r   r   put_itemf   r   zBatchWriter.put_itemc                 C   r#   )NDeleteRequestKeyr&   )r   r*   r   r   r   delete_itemi   r   zBatchWriter.delete_itemc                 C   s(   | j r| | | j| |   d S r	   )r     _remove_dup_pkeys_request_if_anyr   append_flush_if_neededr   requestr   r   r   r'   l   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   r0   pkey_values_newitemr   r   r   r,   r   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r0   r   r   
<listcomp>~       z4BatchWriter._extract_pkey_values.<locals>.<listcomp>r)   c                    r7   )r)   r*   r   r8   r;   r   r   r<      r=   )getr    r/   r   r;   r   r1   |   s   



z BatchWriter._extract_pkey_valuesc                 C   s    t | j| jkr|   d S d S r	   )lenr   r   _flushr   r   r   r   r.      s   zBatchWriter._flush_if_neededc                 C   s|   | j d | j }| j | jd  | _ | jj| j|id}|d }|s#i }|| jg }| j | tdt	|t	| j  d S )N)RequestItemsUnprocessedItemsz$Batch write sent %s, unprocessed: %s)
r   r   r   batch_write_itemr   r>   extendr3   r4   r?   )r   items_to_sendresponseunprocessed_items	item_listr   r   r   r@      s   zBatchWriter._flushc                 C   s   | S r	   r   rA   r   r   r   	__enter__   s   zBatchWriter.__enter__c                 C   s   | j r|   | j sd S d S r	   )r   r@   )r   exc_type	exc_valuetbr   r   r   __exit__   s   zBatchWriter.__exit__)r   N)r   r   r   __doc__r   r(   r+   r'   r,   r1   r.   r@   rJ   rN   r   r   r   r   r   ?   s    
$
r   )logging	getLoggerr   r3   r   r   r   r   r   r   r   <module>   s
   
	$