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