Search in sources :

Example 1 with Update

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

the class UpdateItemOperation method addExpressionsIfExist.

private UpdateItemRequest.Builder addExpressionsIfExist(WriteModification transformation, Map<String, AttributeValue> filteredAttributeValues, UpdateItemRequest.Builder requestBuilder, TableMetadata tableMetadata) {
    Map<String, String> expressionNames = null;
    Map<String, AttributeValue> expressionValues = null;
    String conditionExpressionString = null;
    /* Add update expression for transformed non-key attributes if applicable */
    if (!filteredAttributeValues.isEmpty()) {
        Expression fullUpdateExpression = generateUpdateExpression(filteredAttributeValues, tableMetadata);
        expressionNames = fullUpdateExpression.expressionNames();
        expressionValues = fullUpdateExpression.expressionValues();
        requestBuilder = requestBuilder.updateExpression(fullUpdateExpression.expression());
    }
    /* Merge in conditional expression from extension WriteModification if applicable */
    if (transformation != null && transformation.additionalConditionalExpression() != null) {
        expressionNames = Expression.joinNames(expressionNames, transformation.additionalConditionalExpression().expressionNames());
        expressionValues = Expression.joinValues(expressionValues, transformation.additionalConditionalExpression().expressionValues());
        conditionExpressionString = transformation.additionalConditionalExpression().expression();
    }
    /* Merge in conditional expression from specified 'conditionExpression' if applicable */
    Expression conditionExpression = request.map(r -> Optional.ofNullable(r.conditionExpression()), r -> Optional.ofNullable(r.conditionExpression())).orElse(null);
    if (conditionExpression != null) {
        expressionNames = Expression.joinNames(expressionNames, conditionExpression.expressionNames());
        expressionValues = Expression.joinValues(expressionValues, conditionExpression.expressionValues());
        conditionExpressionString = Expression.joinExpressions(conditionExpressionString, conditionExpression.expression(), " AND ");
    }
    // Avoiding adding empty collections that the low level SDK will propagate to DynamoDb where it causes error.
    if (expressionNames != null && !expressionNames.isEmpty()) {
        requestBuilder = requestBuilder.expressionAttributeNames(expressionNames);
    }
    if (expressionValues != null && !expressionValues.isEmpty()) {
        requestBuilder = requestBuilder.expressionAttributeValues(expressionValues);
    }
    return requestBuilder.conditionExpression(conditionExpressionString);
}
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) UpdateBehaviorTag(software.amazon.awssdk.enhanced.dynamodb.internal.mapper.UpdateBehaviorTag) ArrayList(java.util.ArrayList) Map(java.util.Map) EnhancedClientUtils.isNullAttributeValue(software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.isNullAttributeValue) EnhancedClientUtils(software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) UpdateBehavior(software.amazon.awssdk.enhanced.dynamodb.mapper.UpdateBehavior) 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) Collectors(java.util.stream.Collectors) 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) Collections(java.util.Collections) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) UpdateItemResponse(software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse) EnhancedClientUtils.isNullAttributeValue(software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.isNullAttributeValue) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression)

Example 2 with Update

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

the class UpdateItemOperation method generateTransactWriteItem.

@Override
public TransactWriteItem generateTransactWriteItem(TableSchema<T> tableSchema, OperationContext operationContext, DynamoDbEnhancedClientExtension dynamoDbEnhancedClientExtension) {
    UpdateItemRequest updateItemRequest = generateRequest(tableSchema, operationContext, dynamoDbEnhancedClientExtension);
    Update.Builder builder = Update.builder().key(updateItemRequest.key()).tableName(updateItemRequest.tableName()).updateExpression(updateItemRequest.updateExpression()).conditionExpression(updateItemRequest.conditionExpression()).expressionAttributeValues(updateItemRequest.expressionAttributeValues()).expressionAttributeNames(updateItemRequest.expressionAttributeNames());
    request.right().map(TransactUpdateItemEnhancedRequest::returnValuesOnConditionCheckFailureAsString).ifPresent(builder::returnValuesOnConditionCheckFailure);
    return TransactWriteItem.builder().update(builder.build()).build();
}
Also used : UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) Update(software.amazon.awssdk.services.dynamodb.model.Update)

Example 3 with Update

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

the class UpdateItemOperationTransactTest method generateTransactWriteItem_returnValuesOnConditionCheckFailure_generatesCorrectRequest.

@Test
public void generateTransactWriteItem_returnValuesOnConditionCheckFailure_generatesCorrectRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    String returnValues = "return-values";
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(TransactUpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).returnValuesOnConditionCheckFailure(returnValues).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    doReturn(ddbRequest(fakeItemMap, b -> {
    })).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).returnValuesOnConditionCheckFailure(returnValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Mock(org.mockito.Mock) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) RunWith(org.junit.runner.RunWith) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) Mockito.spy(org.mockito.Mockito.spy) Update(software.amazon.awssdk.services.dynamodb.model.Update) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) Map(java.util.Map) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.doReturn(org.mockito.Mockito.doReturn) Test(org.junit.Test) Mockito.verify(org.mockito.Mockito.verify) UpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest) Consumer(java.util.function.Consumer) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) AttributeValues.stringValue(software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Aggregations

Update (software.amazon.awssdk.services.dynamodb.model.Update)3 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)3 Collections (java.util.Collections)2 Map (java.util.Map)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 TransactUpdateItemEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest)2 UpdateItemEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 ReturnValue (software.amazon.awssdk.services.dynamodb.model.ReturnValue)2 TransactWriteItem (software.amazon.awssdk.services.dynamodb.model.TransactWriteItem)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1