Search in sources :

Example 1 with DynamoDbEnhancedClientExtension

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();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Arrays(java.util.Arrays) Collection(java.util.Collection) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) KeyType(software.amazon.awssdk.services.dynamodb.model.KeyType) Set(java.util.Set) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) CompletableFuture(java.util.concurrent.CompletableFuture) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) CreateTableEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) Optional(java.util.Optional) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) Collections(java.util.Collections) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) HashSet(java.util.HashSet)

Example 2 with DynamoDbEnhancedClientExtension

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());
}
Also used : TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) Test(org.junit.Test)

Example 3 with DynamoDbEnhancedClientExtension

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();
}
Also used : InOrder(org.mockito.InOrder) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) Test(org.junit.Test)

Example 4 with DynamoDbEnhancedClientExtension

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();
}
Also used : InOrder(org.mockito.InOrder) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) Test(org.junit.Test)

Example 5 with DynamoDbEnhancedClientExtension

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();
}
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

DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)7 Test (org.junit.Test)3 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)3 Collection (java.util.Collection)2 List (java.util.List)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Function (java.util.function.Function)2 InOrder (org.mockito.InOrder)2 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)2 DynamoDbExtensionContext (software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext)2 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)2 OperationContext (software.amazon.awssdk.enhanced.dynamodb.OperationContext)2 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)2 TableSchema (software.amazon.awssdk.enhanced.dynamodb.TableSchema)2 WriteModification (software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification)2 UpdateExpression (software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression)2 DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)2 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)2 Arrays (java.util.Arrays)1