123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace Aws\DynamoDb;
- use Aws\CommandInterface;
- use Aws\CommandPool;
- use Aws\Exception\AwsException;
- use Aws\ResultInterface;
- class WriteRequestBatch
- {
-
- private $client;
-
- private $config;
-
- private $queue;
-
- public function __construct(DynamoDbClient $client, array $config = [])
- {
-
- $config += [
- 'table' => null,
- 'batch_size' => 25,
- 'pool_size' => 1,
- 'autoflush' => true,
- 'before' => null,
- 'error' => null
- ];
-
- if ($config['batch_size'] > 25 || $config['batch_size'] < 2) {
- throw new \InvalidArgumentException('"batch_size" must be between 2 and 25.');
- }
-
- if ($config['before'] && !is_callable($config['before'])) {
- throw new \InvalidArgumentException('"before" must be callable.');
- }
- if ($config['error'] && !is_callable($config['error'])) {
- throw new \InvalidArgumentException('"error" must be callable.');
- }
-
- if ($config['autoflush']) {
- $config['threshold'] = $config['batch_size'] * $config['pool_size'];
- }
- $this->client = $client;
- $this->config = $config;
- $this->queue = [];
- }
-
- public function put(array $item, $table = null)
- {
- $this->queue[] = [
- 'table' => $this->determineTable($table),
- 'data' => ['PutRequest' => ['Item' => $item]],
- ];
- $this->autoFlush();
- return $this;
- }
-
- public function delete(array $key, $table = null)
- {
- $this->queue[] = [
- 'table' => $this->determineTable($table),
- 'data' => ['DeleteRequest' => ['Key' => $key]],
- ];
- $this->autoFlush();
- return $this;
- }
-
- public function flush($untilEmpty = true)
- {
-
- $keepFlushing = true;
- while ($this->queue && $keepFlushing) {
- $commands = $this->prepareCommands();
- $pool = new CommandPool($this->client, $commands, [
- 'before' => $this->config['before'],
- 'concurrency' => $this->config['pool_size'],
- 'fulfilled' => function (ResultInterface $result) {
-
- if ($result->hasKey('UnprocessedItems')) {
- $this->retryUnprocessed($result['UnprocessedItems']);
- }
- },
- 'rejected' => function ($reason) {
- if ($reason instanceof AwsException) {
- $code = $reason->getAwsErrorCode();
- if ($code === 'ProvisionedThroughputExceededException') {
- $this->retryUnprocessed($reason->getCommand()['RequestItems']);
- } elseif (is_callable($this->config['error'])) {
- $this->config['error']($reason);
- }
- }
- }
- ]);
- $pool->promise()->wait();
- $keepFlushing = (bool) $untilEmpty;
- }
- return $this;
- }
-
- private function prepareCommands()
- {
-
- $batches = array_chunk($this->queue, $this->config['batch_size']);
- $this->queue = [];
-
- $commands = [];
- foreach ($batches as $batch) {
- $requests = [];
- foreach ($batch as $item) {
- if (!isset($requests[$item['table']])) {
- $requests[$item['table']] = [];
- }
- $requests[$item['table']][] = $item['data'];
- }
- $commands[] = $this->client->getCommand(
- 'BatchWriteItem',
- ['RequestItems' => $requests]
- );
- }
- return $commands;
- }
-
- private function retryUnprocessed(array $unprocessed)
- {
- foreach ($unprocessed as $table => $requests) {
- foreach ($requests as $request) {
- $this->queue[] = [
- 'table' => $table,
- 'data' => $request,
- ];
- }
- }
- }
-
- private function autoFlush()
- {
- if ($this->config['autoflush']
- && count($this->queue) >= $this->config['threshold']
- ) {
-
- $this->flush(false);
- }
- }
-
- private function determineTable($table)
- {
- $table = $table ?: $this->config['table'];
- if (!$table) {
- throw new \RuntimeException('There was no table specified.');
- }
- return $table;
- }
- }
|