Пример #1
0
 /**
  * Return the context formated for DynamoDB
  * @return array
  */
 public function getForDynamoDB()
 {
     $parameters = array();
     foreach ($this->keysByTable as $table => $keys) {
         $parameters[$table] = array('Keys' => array());
         foreach ($keys as $key) {
             list($hash, $range) = $key;
             $formatedKey = array();
             // Convert to attribute
             $hash = new Attribute($hash);
             $formatedKey['HashKeyElement'] = $hash->getForDynamoDB();
             // Range key
             if (null !== $range) {
                 $range = new Attribute($range);
                 $formatedKey['RangeKeyElement'] = $range->getForDynamoDB();
             }
             $parameters[$table]['Keys'][] = $formatedKey;
         }
         if (isset($this->attributesToGetByTable[$table])) {
             $parameters[$table]['AttributesToGet'] = $this->attributesToGetByTable[$table];
         }
     }
     return array('RequestItems' => $parameters);
 }
Пример #2
0
 /**
  * Return the context formated for DynamoDB
  * @return array
  */
 public function getForDynamoDB()
 {
     $parameters = array();
     // Items to put
     foreach ($this->itemsToPut as $item) {
         /** @var $item \Riverline\DynamoDB\Item */
         if (!isset($parameters[$item->getTable()])) {
             $parameters[$item->getTable()] = array();
         }
         $attributes = array();
         foreach ($item as $name => $attribute) {
             /** @var $attribute \Riverline\DynamoDB\Attribute */
             if ("" !== $attribute->getValue()) {
                 // Only not empty string
                 $attributes[$name] = $attribute->getForDynamoDB();
             }
         }
         $parameters[$item->getTable()][] = array('PutRequest' => array('Item' => $attributes));
     }
     // Keys to delete
     foreach ($this->keysToDelete as $key) {
         list($table, $hash, $range) = $key;
         if (!isset($parameters[$table])) {
             $parameters[$table] = array();
         }
         $hash = new Attribute($hash);
         $key = array('HashKeyElement' => $hash->getForDynamoDB());
         if (!is_null($range)) {
             $range = new Attribute($range);
             $key['RangeKeyElement'] = $range->getForDynamoDB();
         }
         $parameters[$table][] = array('DeleteRequest' => array('Key' => $key));
     }
     return array('RequestItems' => $parameters);
 }
Пример #3
0
 /**
  * Get items via the query call
  * @param string $table The item table
  * @param mixed $hash The primary hash key
  * @param Context\Query|null $context The call context
  * @return Collection
  */
 public function query($table, $hash, Context\Query $context = null)
 {
     if (null !== $this->logger) {
         $this->log('Query on table ' . $table);
     }
     $hash = new Attribute($hash);
     $parameters = array('TableName' => $table, 'HashKeyValue' => $hash->getForDynamoDB());
     if (null !== $context) {
         $parameters += $context->getForDynamoDB();
     }
     if (null !== $this->logger) {
         $this->log('Query request paramaters : ' . print_r($parameters, true), Logger::DEBUG);
     }
     $response = $this->connector->query($parameters);
     if (null !== $this->logger) {
         $this->log('Query request response : ' . print_r($response, true), Logger::DEBUG);
     }
     $this->addConsumedReadUnits($table, floatval($response['ConsumedCapacityUnits']));
     if (isset($response['LastEvaluatedKey'])) {
         if (null === $context) {
             $nextContext = new Context\Query();
         } else {
             $nextContext = clone $context;
         }
         $nextContext->setExclusiveStartKey($response['LastEvaluatedKey']);
         if (null !== $this->logger) {
             $this->log('More Items to retrieve');
         }
     } else {
         $nextContext = null;
     }
     $items = new Collection($nextContext, $response['Count']);
     if (!empty($response['Items'])) {
         foreach ($response['Items'] as $responseItem) {
             $item = new Item($table);
             $item->populateFromDynamoDB($responseItem);
             $items->add($item);
         }
     }
     if (null !== $this->logger) {
         $this->log('Find ' . count($items) . ' Items');
     }
     return $items;
 }