Skip to content

Commit

Permalink
Refactoring batch write items query
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron committed Jul 18, 2022
1 parent 3957915 commit ab7b207
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 101 deletions.
4 changes: 2 additions & 2 deletions src/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use AsyncAws\DynamoDb\DynamoDbClient;
use AsyncAws\DynamoDb\Input\PutItemInput;
use Dynamite\Exception\ValidationException;
use Dynamite\Query\BatchWrite;
use Dynamite\Query\BatchWriteItems;
use Dynamite\Schema\Records;
use Dynamite\Validator\ValidatorAwareTrait;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -70,7 +70,7 @@ final public function load(DynamoDbClient $client, LoggerInterface $logger): voi
return;
}

$query = new BatchWrite($client, $logger);
$query = new BatchWriteItems($client, $logger);
$query->putItems($this->schema->getTableName(), $this->schema->getRecords());

$logger->debug('Batch records loaded', [
Expand Down
4 changes: 2 additions & 2 deletions src/Purger/Purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use AsyncAws\DynamoDb\Input\DescribeTableInput;
use AsyncAws\DynamoDb\Input\ScanInput;
use AsyncAws\DynamoDb\ValueObject\AttributeValue;
use Dynamite\Query\BatchWrite;
use Dynamite\Query\BatchWriteItems;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

Expand Down Expand Up @@ -75,7 +75,7 @@ protected function truncateData(string $tableName): void
$keysToDelete[] = $this->getDeleteKey($primaryKey, $item);
}

$query = new BatchWrite($this->client, $this->logger);
$query = new BatchWriteItems($this->client, $this->logger);
$query->deleteItems($tableName, $keysToDelete);

$this->logger->debug('Table data truncated', [
Expand Down
97 changes: 0 additions & 97 deletions src/Query/BatchWrite.php

This file was deleted.

97 changes: 97 additions & 0 deletions src/Query/BatchWriteItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Dynamite\Query;

use AsyncAws\DynamoDb\DynamoDbClient;
use AsyncAws\DynamoDb\Input\BatchWriteItemInput;
use AsyncAws\DynamoDb\ValueObject\AttributeValue;
use AsyncAws\DynamoDb\ValueObject\DeleteRequest;
use AsyncAws\DynamoDb\ValueObject\PutRequest;
use AsyncAws\DynamoDb\ValueObject\WriteRequest;
use Psr\Log\LoggerInterface;

final class BatchWriteItems
{
/**
* @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
*/
private const BATCH_MAX_SIZE = 25;

public function __construct(
private readonly DynamoDbClient $client,
private readonly LoggerInterface $logger
) {
}

/**
* @param array<int, array<string, AttributeValue>> $items
*/
public function putItems(string $tableName, array $items): void
{
$this->batchWriteRequest(
$tableName,
$items,
static fn (array $item): WriteRequest => new WriteRequest([
'PutRequest' => new PutRequest([
'Item' => $item,
]),
]),
function (string $tableName, int $batchNumber): void {
$this->logger->debug('Data batch executed', [
'table' => $tableName,
'batch' => '#' . $batchNumber,
]);
}
);
}

/**
* @param array<int, array<string, AttributeValue>> $keys
*/
public function deleteItems(string $tableName, array $keys): void
{
$this->batchWriteRequest(
$tableName,
$keys,
static fn (array $key): WriteRequest => new WriteRequest([
'DeleteRequest' => new DeleteRequest([
'Key' => $key,
]),
]),
function (string $tableName, int $batchNumber): void {
$this->logger->debug('Data batch deleted', [
'table' => $tableName,
'batch' => '#' . $batchNumber,
]);
}
);
}

private function batchWriteRequest(
string $tableName,
array $items,
\Closure $requestCallback,
\Closure $logCallback
): void {
$offset = $batchNumber = 0;

while ($offset < \count($items)) {
$batch = array_slice($items, $offset, self::BATCH_MAX_SIZE);

$input = new BatchWriteItemInput([
'RequestItems' => [
$tableName => array_map($requestCallback, $batch),
],
]);

$this->client->batchWriteItem($input)->resolve();

$offset += self::BATCH_MAX_SIZE;
++$batchNumber;

$logCallback($tableName, $batchNumber);
}
}
}

0 comments on commit ab7b207

Please sign in to comment.