/**
  * @param                 $tableName
  * @param DynamoDbIndex   $primaryIndex
  * @param DynamoDbIndex[] $localSecondaryIndices
  * @param DynamoDbIndex[] $globalSecondaryIndices
  * @param int             $readCapacity
  * @param int             $writeCapacity
  *
  * @return bool
  * @internal param DynamoDbIndex $primaryKey
  */
 public function createTable($tableName, DynamoDbIndex $primaryIndex, array $localSecondaryIndices = [], array $globalSecondaryIndices = [], $readCapacity = 5, $writeCapacity = 5)
 {
     $attrDef = $primaryIndex->getAttributeDefinitions();
     foreach ($globalSecondaryIndices as $gsi) {
         $gsiDef = $gsi->getAttributeDefinitions();
         $attrDef = array_merge($attrDef, $gsiDef);
     }
     foreach ($localSecondaryIndices as $lsi) {
         $lsiDef = $lsi->getAttributeDefinitions();
         $attrDef = array_merge($attrDef, $lsiDef);
     }
     $attrDef = array_values($attrDef);
     $keySchema = $primaryIndex->getKeySchema();
     $gsiDef = [];
     foreach ($globalSecondaryIndices as $globalSecondaryIndex) {
         $gsiDef[] = ["IndexName" => $globalSecondaryIndex->getName(), "KeySchema" => $globalSecondaryIndex->getKeySchema(), "Projection" => $globalSecondaryIndex->getProjection(), "ProvisionedThroughput" => ["ReadCapacityUnits" => $readCapacity, "WriteCapacityUnits" => $writeCapacity]];
     }
     $lsiDef = [];
     foreach ($localSecondaryIndices as $localSecondaryIndex) {
         $lsiDef[] = ["IndexName" => $localSecondaryIndex->getName(), "KeySchema" => $localSecondaryIndex->getKeySchema(), "Projection" => $localSecondaryIndex->getProjection()];
     }
     $args = ["TableName" => $tableName, "ProvisionedThroughput" => ["ReadCapacityUnits" => $readCapacity, "WriteCapacityUnits" => $writeCapacity], "AttributeDefinitions" => $attrDef, "KeySchema" => $keySchema];
     if ($gsiDef) {
         $args["GlobalSecondaryIndexes"] = $gsiDef;
     }
     if ($lsiDef) {
         $args["LocalSecondaryIndexes"] = $lsiDef;
     }
     $result = $this->db->createTable($args);
     if (isset($result['TableDescription']) && $result['TableDescription']) {
         return true;
     } else {
         return false;
     }
 }
예제 #2
0
 public function getLocalSecondaryIndices()
 {
     $description = $this->describe();
     $lsiDefs = isset($description['LocalSecondaryIndexes']) ? $description['LocalSecondaryIndexes'] : null;
     if (!$lsiDefs) {
         return [];
     }
     $attrDefs = [];
     foreach ($description['AttributeDefinitions'] as $attributeDefinition) {
         $attrDefs[$attributeDefinition['AttributeName']] = $attributeDefinition['AttributeType'];
     }
     $lsis = [];
     foreach ($lsiDefs as $lsiDef) {
         $hashKey = null;
         $hashKeyType = null;
         $rangeKey = null;
         $rangeKeyType = null;
         foreach ($lsiDef['KeySchema'] as $keySchema) {
             switch ($keySchema['KeyType']) {
                 case "HASH":
                     $hashKey = $keySchema['AttributeName'];
                     $hashKeyType = $attrDefs[$hashKey];
                     break;
                 case "RANGE":
                     $rangeKey = $keySchema['AttributeName'];
                     $rangeKeyType = $attrDefs[$rangeKey];
                     break;
             }
         }
         $projectionType = $lsiDef['Projection']['ProjectionType'];
         $projectedAttributes = isset($lsiDef['Projection']['NonKeyAttributes']) ? $lsiDef['Projection']['NonKeyAttributes'] : [];
         $lsi = new DynamoDbIndex($hashKey, $hashKeyType, $rangeKey, $rangeKeyType, $projectionType, $projectedAttributes);
         $lsi->setName($lsiDef['IndexName']);
         $lsis[$lsi->getName()] = $lsi;
     }
     return $lsis;
 }