Search in sources :

Example 1 with TransactUpdateItemEnhancedRequest

use of software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest in project aws-sdk-java-v2 by aws.

the class UpdateItemOperation method generateConditionExpressionIfExist.

/**
 * Retrieves the ConditionExpression from extensions if existing, and retrieves the ConditionExpression from the request
 * if existing. If both exist, they are merged.
 */
private Expression generateConditionExpressionIfExist(WriteModification transformation, Either<UpdateItemEnhancedRequest<T>, TransactUpdateItemEnhancedRequest<T>> request) {
    Expression conditionExpression = null;
    if (transformation != null && transformation.additionalConditionalExpression() != null) {
        conditionExpression = transformation.additionalConditionalExpression();
    }
    Expression operationConditionExpression = request.map(r -> Optional.ofNullable(r.conditionExpression()), r -> Optional.ofNullable(r.conditionExpression())).orElse(null);
    if (operationConditionExpression != null) {
        conditionExpression = operationConditionExpression.and(conditionExpression);
    }
    return conditionExpression;
}
Also used : TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) CompletableFuture(java.util.concurrent.CompletableFuture) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) Function(java.util.function.Function) Update(software.amazon.awssdk.services.dynamodb.model.Update) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) CollectionUtils(software.amazon.awssdk.utils.CollectionUtils) Map(java.util.Map) CollectionUtils.filterMap(software.amazon.awssdk.utils.CollectionUtils.filterMap) UpdateExpressionConverter(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionConverter) UpdateExpressionUtils.operationExpression(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) EnhancedClientUtils.readAndTransformSingleItem(software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.readAndTransformSingleItem) Collection(java.util.Collection) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) Either(software.amazon.awssdk.utils.Either) UpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest) DefaultDynamoDbExtensionContext(software.amazon.awssdk.enhanced.dynamodb.internal.extensions.DefaultDynamoDbExtensionContext) List(java.util.List) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) UpdateItemEnhancedResponse(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedResponse) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Optional(java.util.Optional) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) UpdateItemResponse(software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) UpdateExpressionUtils.operationExpression(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression)

Example 2 with TransactUpdateItemEnhancedRequest

use of software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest in project aws-sdk-java-v2 by aws.

the class TransactWriteItemsTest method mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail.

@Test
public void mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail() {
    mappedTable1.putItem(r -> r.item(RECORDS_1.get(0)));
    mappedTable1.putItem(r -> r.item(RECORDS_1.get(1)));
    mappedTable2.putItem(r -> r.item(RECORDS_2.get(0)));
    Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("99"))).expressionNames(singletonMap("#attribute", "attribute")).build();
    Key key0 = Key.builder().partitionValue(0).build();
    Key key1 = Key.builder().partitionValue(1).build();
    ReturnValuesOnConditionCheckFailure returnValues = ReturnValuesOnConditionCheckFailure.ALL_OLD;
    TransactPutItemEnhancedRequest<Record2> putItemRequest = TransactPutItemEnhancedRequest.builder(Record2.class).conditionExpression(conditionExpression).item(RECORDS_2.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
    TransactUpdateItemEnhancedRequest<Record1> updateItemRequest = TransactUpdateItemEnhancedRequest.builder(Record1.class).conditionExpression(conditionExpression).item(RECORDS_1.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
    TransactDeleteItemEnhancedRequest deleteItemRequest = TransactDeleteItemEnhancedRequest.builder().key(key1).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build();
    TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addPutItem(mappedTable2, putItemRequest).addUpdateItem(mappedTable1, updateItemRequest).addConditionCheck(mappedTable1, ConditionCheck.builder().key(key0).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build()).addDeleteItem(mappedTable1, deleteItemRequest).build();
    try {
        enhancedClient.transactWriteItems(transactWriteItemsEnhancedRequest);
        fail("Expected TransactionCanceledException to be thrown");
    } catch (TransactionCanceledException e) {
        List<CancellationReason> cancellationReasons = e.cancellationReasons();
        assertThat(cancellationReasons.size(), is(4));
        cancellationReasons.forEach(r -> assertThat(r.item().isEmpty(), is(false)));
    }
}
Also used : IntStream(java.util.stream.IntStream) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) TransactDeleteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactDeleteItemEnhancedRequest) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) TransactWriteItemsEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest) Matchers.nullValue(org.hamcrest.Matchers.nullValue) After(org.junit.After) Collections.singletonMap(java.util.Collections.singletonMap) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) Before(org.junit.Before) ReturnValuesOnConditionCheckFailure(software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) Key(software.amazon.awssdk.enhanced.dynamodb.Key) CancellationReason(software.amazon.awssdk.services.dynamodb.model.CancellationReason) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) Test(org.junit.Test) TransactPutItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactPutItemEnhancedRequest) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) StaticAttributeTags.primaryPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey) List(java.util.List) ConditionCheck(software.amazon.awssdk.enhanced.dynamodb.model.ConditionCheck) Matchers.is(org.hamcrest.Matchers.is) StaticTableSchema(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema) AttributeValues.stringValue(software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue) TransactDeleteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactDeleteItemEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) List(java.util.List) ReturnValuesOnConditionCheckFailure(software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Key(software.amazon.awssdk.enhanced.dynamodb.Key) StaticAttributeTags.primaryPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey) TransactWriteItemsEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest) Test(org.junit.Test)

Example 3 with TransactUpdateItemEnhancedRequest

use of software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest 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();
}
Also used : TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) CompletableFuture(java.util.concurrent.CompletableFuture) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) Function(java.util.function.Function) Update(software.amazon.awssdk.services.dynamodb.model.Update) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) CollectionUtils(software.amazon.awssdk.utils.CollectionUtils) Map(java.util.Map) CollectionUtils.filterMap(software.amazon.awssdk.utils.CollectionUtils.filterMap) UpdateExpressionConverter(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionConverter) UpdateExpressionUtils.operationExpression(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) EnhancedClientUtils.readAndTransformSingleItem(software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.readAndTransformSingleItem) Collection(java.util.Collection) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) Either(software.amazon.awssdk.utils.Either) UpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest) DefaultDynamoDbExtensionContext(software.amazon.awssdk.enhanced.dynamodb.internal.extensions.DefaultDynamoDbExtensionContext) List(java.util.List) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) UpdateItemEnhancedResponse(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedResponse) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Optional(java.util.Optional) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) UpdateItemResponse(software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) UpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) UpdateExpressionUtils.operationExpression(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression)

Aggregations

List (java.util.List)3 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)3 TableSchema (software.amazon.awssdk.enhanced.dynamodb.TableSchema)3 TransactUpdateItemEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest)3 Collection (java.util.Collection)2 Map (java.util.Map)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Function (java.util.function.Function)2 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)2 DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)2 OperationContext (software.amazon.awssdk.enhanced.dynamodb.OperationContext)2 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)2 WriteModification (software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification)2 EnhancedClientUtils.readAndTransformSingleItem (software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.readAndTransformSingleItem)2 DefaultDynamoDbExtensionContext (software.amazon.awssdk.enhanced.dynamodb.internal.extensions.DefaultDynamoDbExtensionContext)2 UpdateExpressionConverter (software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionConverter)2 UpdateExpressionUtils.operationExpression (software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression)2 UpdateItemEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest)2 Collections.singletonMap (java.util.Collections.singletonMap)1