Search in sources :

Example 1 with UpdateBehavior

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

the class UpdateItemOperation method generateUpdateExpression.

private static Expression generateUpdateExpression(Map<String, AttributeValue> attributeValuesToUpdate, TableMetadata tableMetadata) {
    // Sort the updates into 'SET' or 'REMOVE' based on null value
    List<String> updateSetActions = new ArrayList<>();
    List<String> updateRemoveActions = new ArrayList<>();
    attributeValuesToUpdate.forEach((key, value) -> {
        if (!isNullAttributeValue(value)) {
            UpdateBehavior updateBehavior = UpdateBehaviorTag.resolveForAttribute(key, tableMetadata);
            updateSetActions.add(EXPRESSION_KEY_MAPPER.apply(key) + " = " + updateExpressionMapperForBehavior(updateBehavior).apply(key));
        } else {
            updateRemoveActions.add(EXPRESSION_KEY_MAPPER.apply(key));
        }
    });
    // Combine the expressions
    List<String> updateActions = new ArrayList<>();
    if (!updateSetActions.isEmpty()) {
        updateActions.add("SET " + String.join(", ", updateSetActions));
    }
    if (!updateRemoveActions.isEmpty()) {
        updateActions.add("REMOVE " + String.join(", ", updateRemoveActions));
    }
    String updateExpression = String.join(" ", updateActions);
    Map<String, AttributeValue> expressionAttributeValues = attributeValuesToUpdate.entrySet().stream().filter(entry -> !isNullAttributeValue(entry.getValue())).collect(Collectors.toMap(entry -> EXPRESSION_VALUE_KEY_MAPPER.apply(entry.getKey()), Map.Entry::getValue));
    Map<String, String> expressionAttributeNames = attributeValuesToUpdate.keySet().stream().collect(Collectors.toMap(EXPRESSION_KEY_MAPPER, key -> key));
    return Expression.builder().expression(updateExpression).expressionValues(Collections.unmodifiableMap(expressionAttributeValues)).expressionNames(expressionAttributeNames).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) 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) ArrayList(java.util.ArrayList) UpdateBehavior(software.amazon.awssdk.enhanced.dynamodb.mapper.UpdateBehavior) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)1 DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)1 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)1 OperationContext (software.amazon.awssdk.enhanced.dynamodb.OperationContext)1 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)1 TableSchema (software.amazon.awssdk.enhanced.dynamodb.TableSchema)1 WriteModification (software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification)1 EnhancedClientUtils (software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils)1 EnhancedClientUtils.isNullAttributeValue (software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.isNullAttributeValue)1 EnhancedClientUtils.readAndTransformSingleItem (software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.readAndTransformSingleItem)1 DefaultDynamoDbExtensionContext (software.amazon.awssdk.enhanced.dynamodb.internal.extensions.DefaultDynamoDbExtensionContext)1