use of software.amazon.awssdk.enhanced.dynamodb.OperationContext in project aws-sdk-java-v2 by aws.
the class UpdateItemOperationTest method generateTransactWriteItem_basicRequest.
@Test
public void generateTransactWriteItem_basicRequest() {
FakeItem fakeItem = createUniqueFakeItem();
Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
String updateExpression = "update-expression";
Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
assertThat(actualResult, is(expectedResult));
verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
use of software.amazon.awssdk.enhanced.dynamodb.OperationContext in project aws-sdk-java-v2 by aws.
the class UpdateItemOperationTest method generateTransactWriteItem_conditionalRequest.
@Test
public void generateTransactWriteItem_conditionalRequest() {
FakeItem fakeItem = createUniqueFakeItem();
Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
String updateExpression = "update-expression";
String conditionExpression = "condition-expression";
Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
assertThat(actualResult, is(expectedResult));
verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
use of software.amazon.awssdk.enhanced.dynamodb.OperationContext 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.OperationContext in project aws-sdk-java-v2 by aws.
the class UpdateItemOperationTransactTest method generateTransactWriteItem_basicRequest.
@Test
public void generateTransactWriteItem_basicRequest() {
FakeItem fakeItem = createUniqueFakeItem();
Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
String updateExpression = "update-expression";
Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
UpdateItemRequest.Builder builder = ddbRequestBuilder(fakeItemMap);
builder.updateExpression(updateExpression);
builder.expressionAttributeValues(attributeValues);
builder.expressionAttributeNames(attributeNames);
UpdateItemRequest updateItemRequest = builder.build();
doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
assertThat(actualResult, is(expectedResult));
verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
use of software.amazon.awssdk.enhanced.dynamodb.OperationContext 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