|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AsyncAws\DynamoDb\Result; |
| 4 | + |
| 5 | +use AsyncAws\Core\Response; |
| 6 | +use AsyncAws\Core\Result; |
| 7 | +use AsyncAws\DynamoDb\DynamoDbClient; |
| 8 | +use AsyncAws\DynamoDb\Input\BatchGetItemInput; |
| 9 | +use AsyncAws\DynamoDb\ValueObject\AttributeValue; |
| 10 | +use AsyncAws\DynamoDb\ValueObject\Capacity; |
| 11 | +use AsyncAws\DynamoDb\ValueObject\ConsumedCapacity; |
| 12 | +use AsyncAws\DynamoDb\ValueObject\KeysAndAttributes; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements \IteratorAggregate<ConsumedCapacity> |
| 16 | + */ |
| 17 | +class BatchGetItemOutput extends Result implements \IteratorAggregate |
| 18 | +{ |
| 19 | + /** |
| 20 | + * A map of table name to a list of items. Each object in `Responses` consists of a table name, along with a map of |
| 21 | + * attribute data consisting of the data type and attribute value. |
| 22 | + */ |
| 23 | + private $Responses = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * A map of tables and their respective keys that were not processed with the current response. The `UnprocessedKeys` |
| 27 | + * value is in the same form as `RequestItems`, so the value can be provided directly to a subsequent `BatchGetItem` |
| 28 | + * operation. For more information, see `RequestItems` in the Request Parameters section. |
| 29 | + */ |
| 30 | + private $UnprocessedKeys = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * The read capacity units consumed by the entire `BatchGetItem` operation. |
| 34 | + */ |
| 35 | + private $ConsumedCapacity = []; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param bool $currentPageOnly When true, iterates over items of the current page. Otherwise also fetch items in the next pages. |
| 39 | + * |
| 40 | + * @return iterable<ConsumedCapacity> |
| 41 | + */ |
| 42 | + public function getConsumedCapacity(bool $currentPageOnly = false): iterable |
| 43 | + { |
| 44 | + if ($currentPageOnly) { |
| 45 | + $this->initialize(); |
| 46 | + yield from $this->ConsumedCapacity; |
| 47 | + |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $client = $this->awsClient; |
| 52 | + if (!$client instanceof DynamoDbClient) { |
| 53 | + throw new \InvalidArgumentException('missing client injected in paginated result'); |
| 54 | + } |
| 55 | + if (!$this->input instanceof BatchGetItemInput) { |
| 56 | + throw new \InvalidArgumentException('missing last request injected in paginated result'); |
| 57 | + } |
| 58 | + $input = clone $this->input; |
| 59 | + $page = $this; |
| 60 | + while (true) { |
| 61 | + if ($page->getUnprocessedKeys()) { |
| 62 | + $input->setRequestItems($page->getUnprocessedKeys()); |
| 63 | + |
| 64 | + $this->registerPrefetch($nextPage = $client->BatchGetItem($input)); |
| 65 | + } else { |
| 66 | + $nextPage = null; |
| 67 | + } |
| 68 | + |
| 69 | + yield from $page->getConsumedCapacity(true); |
| 70 | + |
| 71 | + if (null === $nextPage) { |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + $this->unregisterPrefetch($nextPage); |
| 76 | + $page = $nextPage; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Iterates over ConsumedCapacity. |
| 82 | + * |
| 83 | + * @return \Traversable<ConsumedCapacity> |
| 84 | + */ |
| 85 | + public function getIterator(): \Traversable |
| 86 | + { |
| 87 | + $client = $this->awsClient; |
| 88 | + if (!$client instanceof DynamoDbClient) { |
| 89 | + throw new \InvalidArgumentException('missing client injected in paginated result'); |
| 90 | + } |
| 91 | + if (!$this->input instanceof BatchGetItemInput) { |
| 92 | + throw new \InvalidArgumentException('missing last request injected in paginated result'); |
| 93 | + } |
| 94 | + $input = clone $this->input; |
| 95 | + $page = $this; |
| 96 | + while (true) { |
| 97 | + if ($page->getUnprocessedKeys()) { |
| 98 | + $input->setRequestItems($page->getUnprocessedKeys()); |
| 99 | + |
| 100 | + $this->registerPrefetch($nextPage = $client->BatchGetItem($input)); |
| 101 | + } else { |
| 102 | + $nextPage = null; |
| 103 | + } |
| 104 | + |
| 105 | + yield from $page->getConsumedCapacity(true); |
| 106 | + |
| 107 | + if (null === $nextPage) { |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + $this->unregisterPrefetch($nextPage); |
| 112 | + $page = $nextPage; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return array<string, array> |
| 118 | + */ |
| 119 | + public function getResponses(): array |
| 120 | + { |
| 121 | + $this->initialize(); |
| 122 | + |
| 123 | + return $this->Responses; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @return array<string, KeysAndAttributes> |
| 128 | + */ |
| 129 | + public function getUnprocessedKeys(): array |
| 130 | + { |
| 131 | + $this->initialize(); |
| 132 | + |
| 133 | + return $this->UnprocessedKeys; |
| 134 | + } |
| 135 | + |
| 136 | + protected function populateResult(Response $response): void |
| 137 | + { |
| 138 | + $data = $response->toArray(); |
| 139 | + $fn = []; |
| 140 | + $fn['list-ItemList'] = static function (array $json) use (&$fn): array { |
| 141 | + $items = []; |
| 142 | + foreach ($json as $item) { |
| 143 | + $a = empty($item) ? [] : $fn['map-AttributeMap']($item); |
| 144 | + if (null !== $a) { |
| 145 | + $items[] = $a; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return $items; |
| 150 | + }; |
| 151 | + |
| 152 | + /** @return array<string, \AsyncAws\DynamoDb\ValueObject\AttributeValue> */ |
| 153 | + $fn['map-AttributeMap'] = static function (array $json): array { |
| 154 | + $items = []; |
| 155 | + foreach ($json as $name => $value) { |
| 156 | + $items[(string) $name] = AttributeValue::create($value); |
| 157 | + } |
| 158 | + |
| 159 | + return $items; |
| 160 | + }; |
| 161 | + |
| 162 | + /** @return array<string, \AsyncAws\DynamoDb\ValueObject\KeysAndAttributes> */ |
| 163 | + $fn['map-BatchGetRequestMap'] = static function (array $json): array { |
| 164 | + $items = []; |
| 165 | + foreach ($json as $name => $value) { |
| 166 | + $items[(string) $name] = KeysAndAttributes::create($value); |
| 167 | + } |
| 168 | + |
| 169 | + return $items; |
| 170 | + }; |
| 171 | + $fn['list-ConsumedCapacityMultiple'] = static function (array $json) use (&$fn): array { |
| 172 | + $items = []; |
| 173 | + foreach ($json as $item) { |
| 174 | + $items[] = new ConsumedCapacity([ |
| 175 | + 'TableName' => isset($item['TableName']) ? (string) $item['TableName'] : null, |
| 176 | + 'CapacityUnits' => isset($item['CapacityUnits']) ? (float) $item['CapacityUnits'] : null, |
| 177 | + 'ReadCapacityUnits' => isset($item['ReadCapacityUnits']) ? (float) $item['ReadCapacityUnits'] : null, |
| 178 | + 'WriteCapacityUnits' => isset($item['WriteCapacityUnits']) ? (float) $item['WriteCapacityUnits'] : null, |
| 179 | + 'Table' => empty($item['Table']) ? null : new Capacity([ |
| 180 | + 'ReadCapacityUnits' => isset($item['Table']['ReadCapacityUnits']) ? (float) $item['Table']['ReadCapacityUnits'] : null, |
| 181 | + 'WriteCapacityUnits' => isset($item['Table']['WriteCapacityUnits']) ? (float) $item['Table']['WriteCapacityUnits'] : null, |
| 182 | + 'CapacityUnits' => isset($item['Table']['CapacityUnits']) ? (float) $item['Table']['CapacityUnits'] : null, |
| 183 | + ]), |
| 184 | + 'LocalSecondaryIndexes' => empty($item['LocalSecondaryIndexes']) ? [] : $fn['map-SecondaryIndexesCapacityMap']($item['LocalSecondaryIndexes']), |
| 185 | + 'GlobalSecondaryIndexes' => empty($item['GlobalSecondaryIndexes']) ? [] : $fn['map-SecondaryIndexesCapacityMap']($item['GlobalSecondaryIndexes']), |
| 186 | + ]); |
| 187 | + } |
| 188 | + |
| 189 | + return $items; |
| 190 | + }; |
| 191 | + |
| 192 | + /** @return array<string, \AsyncAws\DynamoDb\ValueObject\Capacity> */ |
| 193 | + $fn['map-SecondaryIndexesCapacityMap'] = static function (array $json): array { |
| 194 | + $items = []; |
| 195 | + foreach ($json as $name => $value) { |
| 196 | + $items[(string) $name] = Capacity::create($value); |
| 197 | + } |
| 198 | + |
| 199 | + return $items; |
| 200 | + }; |
| 201 | + $this->Responses = empty($data['Responses']) ? [] : (function (array $json) use (&$fn): array { |
| 202 | + $items = []; |
| 203 | + foreach ($json as $name => $value) { |
| 204 | + $items[(string) $name] = $fn['list-ItemList']($value); |
| 205 | + } |
| 206 | + |
| 207 | + return $items; |
| 208 | + })($data['Responses']); |
| 209 | + $this->UnprocessedKeys = empty($data['UnprocessedKeys']) ? [] : $fn['map-BatchGetRequestMap']($data['UnprocessedKeys']); |
| 210 | + $this->ConsumedCapacity = empty($data['ConsumedCapacity']) ? [] : $fn['list-ConsumedCapacityMultiple']($data['ConsumedCapacity']); |
| 211 | + } |
| 212 | +} |
0 commit comments