/** * Get a batch of items * @param Context\BatchGet $context * @throws \Riverline\DynamoDB\Exception\AttributesException * @return \Riverline\DynamoDB\Batch\BatchCollection */ public function batchGet(Context\BatchGet $context) { if (null !== $this->logger) { $this->log('BatchGet'); } if (0 === count($context)) { $message = "BatchGet context doesn't contain any key to get"; if (null !== $this->logger) { $this->log($message, Logger::ERROR); } throw new Exception\AttributesException($message); } $parameters = $context->getForDynamoDB(); if (null !== $this->logger) { $this->log('BatchGet request paramaters : ' . print_r($parameters, true), Logger::DEBUG); } $response = $this->connector->batchGetItem($parameters); if (null !== $this->logger) { $this->log('BatchGet request response : ' . print_r($response, true), Logger::DEBUG); } // UnprocessedKeys if (count((array) $response['UnprocessedKeys'])) { $unprocessKeyContext = new Context\BatchGet(); foreach ($response['UnprocessedKeys'] as $table => $tableParameters) { foreach ($tableParameters->Keys as $key) { $unprocessKeyContext->addKey($table, current($key->HashKeyElement), current($key->RangeKeyElement)); } if (!empty($tableParameters->AttributesToGet)) { $unprocessKeyContext->setAttributesToGet($table, $tableParameters->AttributesToGet); } } } else { $unprocessKeyContext = null; } $collection = new Batch\BatchCollection($unprocessKeyContext); foreach ($response['Responses'] as $table => $responseItems) { $this->addConsumedReadUnits($table, floatval($responseItems['ConsumedCapacityUnits'])); $items = new Collection(); foreach ($responseItems['Items'] as $responseItem) { $item = new Item($table); $item->populateFromDynamoDB($responseItem); $items->add($item); } if (null !== $this->logger) { $this->log('Find ' . count($items) . ' Items on table ' . $table); } $collection->setItems($table, $items); } return $collection; }
/** * Executes the BatchGetItem operation. * * @param array $requestItems Associative array of <TableName> keys mapping to (associative-array) values. * @param string $returnConsumedCapacity Sets consumed capacity return mode. * * @return Guzzle\Service\Resource\Model * * @see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_batchGetItem */ public function batchGetItem(array $requestItems, $returnConsumedCapacity = self::CAPACITY_NONE) { $args = ['RequestItems' => $requestItems, 'ReturnConsumedCapacity' => $returnConsumedCapacity]; // Try to load from cache is a cacheService is available if ($this->cacheService !== null) { $cacheKey = $this->generateCacheKey($args); if ($this->cacheService->has($cacheKey)) { return unserialize($this->cacheService->get($cacheKey)); } } // Sends batchGetItem command to AWS $result = $this->client->batchGetItem($args); // Saves to cache if ($this->cacheService !== null) { $this->cacheService->set($cacheKey, serialize($result), $this->requestTtl); } return $result; }
/** * Retrieve items in batches of up to 100 * * @param array $key_values * * HashKey: [hash_key_value1, hash_key_value2 ..] * HashKey + RangeKey: [[hash_key_value1, range_key_value1] ...] * * @return self[] */ public function batchGetItems(array $key_values) { $keys = array(); foreach ($key_values as $key_value) { if ($this->_range_key) { $conditions = array($this->_hash_key => $key_value[0], $this->_range_key => $key_value[1]); } else { $_id = is_array($key_value) ? $key_value[0] : $key_value; $conditions = array($this->_hash_key => $_id); } $keys[] = $this->_formatAttributes($conditions); } $result = self::$_client->batchGetItem(array('RequestItems' => array($this->_table_name => array('Keys' => $keys, 'ConsistentRead' => true)))); $items = $result->getPath("Responses/{$this->_table_name}"); $class_name = get_called_class(); $formatted_result = $this->_formatResults($items); $array = array(); foreach ($formatted_result as $row) { $instance = self::factory($class_name); $instance->hydrate($row); $array[] = $instance; } return $array; }