use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension in project aws-sdk-java-v2 by aws.
the class CreateTableOperation method generateRequest.
@Override
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) {
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) {
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()).build();
}
use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension in project aws-sdk-java-v2 by aws.
the class TransactWriteItemsEnhancedRequestTest method builder_passRequestToken_shouldWork.
@Test
public void builder_passRequestToken_shouldWork() {
String token = UUID.randomUUID().toString();
TransactWriteItemsEnhancedRequest enhancedRequest = TransactWriteItemsEnhancedRequest.builder().clientRequestToken(token).build();
DynamoDbEnhancedClientExtension extension = ExtensionResolver.resolveExtensions(ExtensionResolver.defaultExtensions());
TransactWriteItemsRequest request = TransactWriteItemsOperation.create(enhancedRequest).generateRequest(extension);
assertEquals(token, request.clientRequestToken());
}
use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension in project aws-sdk-java-v2 by aws.
the class ExtensionResolverTest method resolveExtensions_multiple_beforeWrite_correctCallingOrder.
@Test
public void resolveExtensions_multiple_beforeWrite_correctCallingOrder() {
DynamoDbEnhancedClientExtension extension = ExtensionResolver.resolveExtensions(Arrays.asList(mockExtension1, mockExtension2));
extension.beforeWrite(mock(DynamoDbExtensionContext.BeforeWrite.class));
InOrder inOrder = Mockito.inOrder(mockExtension1, mockExtension2);
inOrder.verify(mockExtension1).beforeWrite(any(DynamoDbExtensionContext.BeforeWrite.class));
inOrder.verify(mockExtension2).beforeWrite(any(DynamoDbExtensionContext.BeforeWrite.class));
inOrder.verifyNoMoreInteractions();
}
use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension in project aws-sdk-java-v2 by aws.
the class ExtensionResolverTest method resolveExtensions_multiple_afterRead_correctCallingOrder.
@Test
public void resolveExtensions_multiple_afterRead_correctCallingOrder() {
DynamoDbEnhancedClientExtension extension = ExtensionResolver.resolveExtensions(Arrays.asList(mockExtension1, mockExtension2));
extension.afterRead(mock(DynamoDbExtensionContext.AfterRead.class));
InOrder inOrder = Mockito.inOrder(mockExtension1, mockExtension2);
inOrder.verify(mockExtension2).afterRead(any(DynamoDbExtensionContext.AfterRead.class));
inOrder.verify(mockExtension1).afterRead(any(DynamoDbExtensionContext.AfterRead.class));
inOrder.verifyNoMoreInteractions();
}
use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension in project aws-sdk-java-v2 by aws.
the class UpdateItemOperation method generateRequest.
@Override
public UpdateItemRequest generateRequest(TableSchema<T> tableSchema, OperationContext operationContext, DynamoDbEnhancedClientExtension extension) {
if (!TableMetadata.primaryIndexName().equals(operationContext.indexName())) {
throw new IllegalArgumentException("UpdateItem cannot be executed against a secondary index.");
}
T item = request.map(UpdateItemEnhancedRequest::item, TransactUpdateItemEnhancedRequest::item);
Boolean ignoreNulls = request.map(r -> Optional.ofNullable(r.ignoreNulls()), r -> Optional.ofNullable(r.ignoreNulls())).orElse(null);
Map<String, AttributeValue> itemMap = tableSchema.itemToMap(item, Boolean.TRUE.equals(ignoreNulls));
TableMetadata tableMetadata = tableSchema.tableMetadata();
WriteModification transformation = extension != null ? extension.beforeWrite(DefaultDynamoDbExtensionContext.builder().items(itemMap).operationContext(operationContext).tableMetadata(tableMetadata).tableSchema(tableSchema).operationName(operationName()).build()) : null;
if (transformation != null && transformation.transformedItem() != null) {
itemMap = transformation.transformedItem();
}
Collection<String> primaryKeys = tableSchema.tableMetadata().primaryKeys();
Map<String, AttributeValue> keyAttributes = filterMap(itemMap, entry -> primaryKeys.contains(entry.getKey()));
Map<String, AttributeValue> nonKeyAttributes = filterMap(itemMap, entry -> !primaryKeys.contains(entry.getKey()));
Expression updateExpression = generateUpdateExpressionIfExist(tableMetadata, transformation, nonKeyAttributes);
Expression conditionExpression = generateConditionExpressionIfExist(transformation, request);
Map<String, String> expressionNames = coalesceExpressionNames(updateExpression, conditionExpression);
Map<String, AttributeValue> expressionValues = coalesceExpressionValues(updateExpression, conditionExpression);
UpdateItemRequest.Builder requestBuilder = UpdateItemRequest.builder().tableName(operationContext.tableName()).key(keyAttributes).returnValues(ReturnValue.ALL_NEW);
if (request.left().isPresent()) {
addPlainUpdateItemParameters(requestBuilder, request.left().get());
}
if (updateExpression != null) {
requestBuilder.updateExpression(updateExpression.expression());
}
if (conditionExpression != null) {
requestBuilder.conditionExpression(conditionExpression.expression());
}
if (CollectionUtils.isNotEmpty(expressionNames)) {
requestBuilder = requestBuilder.expressionAttributeNames(expressionNames);
}
if (CollectionUtils.isNotEmpty(expressionValues)) {
requestBuilder = requestBuilder.expressionAttributeValues(expressionValues);
}
return requestBuilder.build();
}
Aggregations