Search in sources :

Example 1 with TransactPutItemEnhancedRequest

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

the class PutItemOperation method generateRequest.

@Override
public PutItemRequest 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.");
    }
    TableMetadata tableMetadata = tableSchema.tableMetadata();
    // Fail fast if required primary partition key does not exist and avoid the call to DynamoDb
    tableMetadata.primaryPartitionKey();
    boolean alwaysIgnoreNulls = true;
    T item = request.map(PutItemEnhancedRequest::item, TransactPutItemEnhancedRequest::item);
    Map<String, AttributeValue> itemMap = tableSchema.itemToMap(item, alwaysIgnoreNulls);
    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();
    }
    PutItemRequest.Builder requestBuilder = PutItemRequest.builder().tableName(operationContext.tableName()).item(itemMap);
    if (request.left().isPresent()) {
        requestBuilder = addPlainPutItemParameters(requestBuilder, request.left().get());
    }
    requestBuilder = addExpressionsIfExist(requestBuilder, transformation);
    return requestBuilder.build();
}
Also used : TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) TransactPutItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactPutItemEnhancedRequest) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) PutItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest) TransactPutItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactPutItemEnhancedRequest)

Example 2 with TransactPutItemEnhancedRequest

use of software.amazon.awssdk.enhanced.dynamodb.model.TransactPutItemEnhancedRequest 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)

Aggregations

TransactPutItemEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.TransactPutItemEnhancedRequest)2 Collections.singletonMap (java.util.Collections.singletonMap)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.nullValue (org.hamcrest.Matchers.nullValue)1 After (org.junit.After)1 Assert.fail (org.junit.Assert.fail)1 Before (org.junit.Before)1 Test (org.junit.Test)1 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)1 DynamoDbTable (software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable)1 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)1 Key (software.amazon.awssdk.enhanced.dynamodb.Key)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