Search in sources :

Example 1 with TransactWriteItem

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

the class UpdateItemOperationTest method generateTransactWriteItem_basicRequest.

@Test
public void generateTransactWriteItem_basicRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
    doReturn(updateItemRequest).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).updateExpression(updateExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Example 2 with TransactWriteItem

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

the class UpdateItemOperationTest method generateTransactWriteItem_conditionalRequest.

@Test
public void generateTransactWriteItem_conditionalRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    String conditionExpression = "condition-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
    doReturn(updateItemRequest).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).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Example 3 with TransactWriteItem

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

the class TransactWriteItemsEnhancedRequestTest method getTransactWriteItems.

private List<TransactWriteItem> getTransactWriteItems(FakeItem fakeItem) {
    final Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    TransactWriteItem putWriteItem = TransactWriteItem.builder().put(Put.builder().item(fakeItemMap).tableName(TABLE_NAME).build()).build();
    TransactWriteItem deleteWriteItem = TransactWriteItem.builder().delete(Delete.builder().key(fakeItemMap).tableName(TABLE_NAME).build()).build();
    return Arrays.asList(putWriteItem, deleteWriteItem);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem)

Example 4 with TransactWriteItem

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

the class UpdateItemOperationTransactTest method generateTransactWriteItem_basicRequest.

@Test
public void generateTransactWriteItem_basicRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest.Builder builder = ddbRequestBuilder(fakeItemMap);
    builder.updateExpression(updateExpression);
    builder.expressionAttributeValues(attributeValues);
    builder.expressionAttributeNames(attributeNames);
    UpdateItemRequest updateItemRequest = builder.build();
    doReturn(updateItemRequest).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).updateExpression(updateExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) 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)

Example 5 with TransactWriteItem

use of software.amazon.awssdk.services.dynamodb.model.TransactWriteItem in project scalardb by scalar-labs.

the class BatchHandler method handle.

/**
 * Execute the specified list of {@link Mutation}s in batch. All the {@link Mutation}s in the list
 * must be for the same partition.
 *
 * @param mutations a list of {@code Mutation}s to execute
 * @throws NoMutationException if at least one of conditional {@code Mutation}s failed because it
 *     didn't meet the condition
 */
public void handle(List<? extends Mutation> mutations) throws ExecutionException {
    if (mutations.size() > 25) {
        throw new IllegalArgumentException("DynamoDB cannot batch more than 25 mutations at once.");
    }
    TableMetadata tableMetadata = metadataManager.getTableMetadata(mutations.get(0));
    TransactWriteItemsRequest.Builder builder = TransactWriteItemsRequest.builder();
    List<TransactWriteItem> transactItems = new ArrayList<>();
    mutations.forEach(m -> transactItems.add(makeWriteItem(m, tableMetadata)));
    builder.transactItems(transactItems);
    try {
        client.transactWriteItems(builder.build());
    } catch (TransactionCanceledException e) {
        boolean allReasonsAreTransactionConflicts = true;
        for (CancellationReason reason : e.cancellationReasons()) {
            if (reason.code().equals("ConditionalCheckFailed")) {
                throw new NoMutationException("no mutation was applied.", e);
            }
            if (!reason.code().equals("TransactionConflict") && !reason.code().equals("None")) {
                allReasonsAreTransactionConflicts = false;
            }
        }
        if (allReasonsAreTransactionConflicts) {
            // RetriableExecutionException
            throw new RetriableExecutionException(e.getMessage(), e);
        }
        throw new ExecutionException(e.getMessage(), e);
    } catch (DynamoDbException e) {
        throw new ExecutionException(e.getMessage(), e);
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) CancellationReason(software.amazon.awssdk.services.dynamodb.model.CancellationReason) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) ArrayList(java.util.ArrayList) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException)

Aggregations

TransactWriteItem (software.amazon.awssdk.services.dynamodb.model.TransactWriteItem)16 Test (org.junit.Test)14 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)14 OperationContext (software.amazon.awssdk.enhanced.dynamodb.OperationContext)13 FakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem)13 FakeItem.createUniqueFakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem)12 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)6 Map (java.util.Map)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.is (org.hamcrest.Matchers.is)4 RunWith (org.junit.runner.RunWith)4 Mock (org.mockito.Mock)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)4 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)4 AttributeValues.stringValue (software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue)4 Collections (java.util.Collections)3 Matchers.nullValue (org.hamcrest.Matchers.nullValue)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 Mockito.doReturn (org.mockito.Mockito.doReturn)3