in services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/operations/CreateTableOperation.java [62:145]
public CreateTableRequest generateRequest(TableSchema<T> tableSchema,
OperationContext operationContext,
DynamoDbEnhancedClientExtension extension) {
if (!TableMetadata.primaryIndexName().equals(operationContext.indexName())) {
throw new IllegalArgumentException("PutItem cannot be executed against a secondary index.");
}
String primaryPartitionKey = tableSchema.tableMetadata().primaryPartitionKey();
Optional<String> primarySortKey = tableSchema.tableMetadata().primarySortKey();
Set<String> dedupedIndexKeys = new HashSet<>();
dedupedIndexKeys.add(primaryPartitionKey);
primarySortKey.ifPresent(dedupedIndexKeys::add);
List<software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex> sdkGlobalSecondaryIndices = null;
List<software.amazon.awssdk.services.dynamodb.model.LocalSecondaryIndex> sdkLocalSecondaryIndices = null;
if (this.request.globalSecondaryIndices() != null && !this.request.globalSecondaryIndices().isEmpty()) {
sdkGlobalSecondaryIndices =
this.request.globalSecondaryIndices().stream().map(gsi -> {
String indexPartitionKey = tableSchema.tableMetadata().indexPartitionKey(gsi.indexName());
Optional<String> indexSortKey = tableSchema.tableMetadata().indexSortKey(gsi.indexName());
dedupedIndexKeys.add(indexPartitionKey);
indexSortKey.ifPresent(dedupedIndexKeys::add);
return software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex
.builder()
.indexName(gsi.indexName())
.keySchema(generateKeySchema(indexPartitionKey, indexSortKey.orElse(null)))
.projection(gsi.projection())
.provisionedThroughput(gsi.provisionedThroughput())
.build();
}).collect(Collectors.toList());
}
if (this.request.localSecondaryIndices() != null && !this.request.localSecondaryIndices().isEmpty()) {
sdkLocalSecondaryIndices =
this.request.localSecondaryIndices().stream().map(lsi -> {
Optional<String> indexSortKey = tableSchema.tableMetadata().indexSortKey(lsi.indexName());
indexSortKey.ifPresent(dedupedIndexKeys::add);
if (!primaryPartitionKey.equals(
tableSchema.tableMetadata().indexPartitionKey(lsi.indexName()))) {
throw new IllegalArgumentException("Attempt to create a local secondary index with a partition "
+ "key that is not the primary partition key. Index name: "
+ lsi.indexName());
}
return software.amazon.awssdk.services.dynamodb.model.LocalSecondaryIndex
.builder()
.indexName(lsi.indexName())
.keySchema(generateKeySchema(primaryPartitionKey, indexSortKey.orElse(null)))
.projection(lsi.projection())
.build();
}).collect(Collectors.toList());
}
List<AttributeDefinition> attributeDefinitions =
dedupedIndexKeys.stream()
.map(attribute ->
AttributeDefinition.builder()
.attributeName(attribute)
.attributeType(tableSchema
.tableMetadata().scalarAttributeType(attribute)
.orElseThrow(() ->
new IllegalArgumentException(
"Could not map the key attribute '" + attribute +
"' to a valid scalar type.")))
.build())
.collect(Collectors.toList());
BillingMode billingMode = this.request.provisionedThroughput() == null ?
BillingMode.PAY_PER_REQUEST :
BillingMode.PROVISIONED;
return CreateTableRequest.builder()
.tableName(operationContext.tableName())
.keySchema(generateKeySchema(primaryPartitionKey, primarySortKey.orElse(null)))
.globalSecondaryIndexes(sdkGlobalSecondaryIndices)
.localSecondaryIndexes(sdkLocalSecondaryIndices)
.attributeDefinitions(attributeDefinitions)
.billingMode(billingMode)
.provisionedThroughput(this.request.provisionedThroughput())
.streamSpecification(this.request.streamSpecification())
.build();
}