use of software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse in project Signal-Server by signalapp.
the class Keys method take.
public Optional<PreKey> take(final UUID identifier, final long deviceId) {
return TAKE_KEY_FOR_DEVICE_TIMER.record(() -> {
final AttributeValue partitionKey = getPartitionKey(identifier);
QueryRequest queryRequest = QueryRequest.builder().tableName(tableName).keyConditionExpression("#uuid = :uuid AND begins_with (#sort, :sortprefix)").expressionAttributeNames(Map.of("#uuid", KEY_ACCOUNT_UUID, "#sort", KEY_DEVICE_ID_KEY_ID)).expressionAttributeValues(Map.of(":uuid", partitionKey, ":sortprefix", getSortKeyPrefix(deviceId))).projectionExpression(KEY_DEVICE_ID_KEY_ID).consistentRead(false).build();
int contestedKeys = 0;
try {
QueryResponse response = db().query(queryRequest);
for (Map<String, AttributeValue> candidate : response.items()) {
DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder().tableName(tableName).key(Map.of(KEY_ACCOUNT_UUID, partitionKey, KEY_DEVICE_ID_KEY_ID, candidate.get(KEY_DEVICE_ID_KEY_ID))).returnValues(ReturnValue.ALL_OLD).build();
DeleteItemResponse deleteItemResponse = db().deleteItem(deleteItemRequest);
if (deleteItemResponse.hasAttributes()) {
return Optional.of(getPreKeyFromItem(deleteItemResponse.attributes()));
}
contestedKeys++;
}
return Optional.empty();
} finally {
CONTESTED_KEY_DISTRIBUTION.record(contestedKeys);
}
});
}
use of software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse in project Signal-Server by signalapp.
the class MessagesDynamoDb method deleteItemsMatchingQueryAndReturnFirstOneActuallyDeleted.
@Nonnull
private Optional<OutgoingMessageEntity> deleteItemsMatchingQueryAndReturnFirstOneActuallyDeleted(AttributeValue partitionKey, QueryRequest queryRequest) {
Optional<OutgoingMessageEntity> result = Optional.empty();
for (Map<String, AttributeValue> item : db().queryPaginator(queryRequest).items()) {
final byte[] rangeKeyValue = item.get(KEY_SORT).b().asByteArray();
DeleteItemRequest.Builder deleteItemRequest = DeleteItemRequest.builder().tableName(tableName).key(Map.of(KEY_PARTITION, partitionKey, KEY_SORT, AttributeValues.fromByteArray(rangeKeyValue)));
if (result.isEmpty()) {
deleteItemRequest.returnValues(ReturnValue.ALL_OLD);
}
final DeleteItemResponse deleteItemResponse = db().deleteItem(deleteItemRequest.build());
if (deleteItemResponse.attributes() != null && deleteItemResponse.attributes().containsKey(KEY_PARTITION)) {
result = Optional.of(convertItemToOutgoingMessageEntity(deleteItemResponse.attributes()));
}
}
return result;
}
use of software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse in project aws-sdk-java-v2 by aws.
the class DynamoServiceIntegrationTest method testServiceOperations.
/**
* Tests that we can call each service operation to create and describe
* tables, put, update and delete data, and query.
*/
@Test
public void testServiceOperations() throws Exception {
// Describe all tables
ListTablesResponse describeTablesResult = dynamo.listTables(ListTablesRequest.builder().build());
// Describe our new table
DescribeTableRequest describeTablesRequest = DescribeTableRequest.builder().tableName(tableName).build();
TableDescription tableDescription = dynamo.describeTable(describeTablesRequest).table();
assertEquals(tableName, tableDescription.tableName());
assertNotNull(tableDescription.tableStatus());
assertEquals(HASH_KEY_NAME, tableDescription.keySchema().get(0).attributeName());
assertEquals(KeyType.HASH, tableDescription.keySchema().get(0).keyType());
assertNotNull(tableDescription.provisionedThroughput().numberOfDecreasesToday());
assertEquals(READ_CAPACITY, tableDescription.provisionedThroughput().readCapacityUnits());
assertEquals(WRITE_CAPACITY, tableDescription.provisionedThroughput().writeCapacityUnits());
// Add some data
int contentLength = 1 * 1024;
Set<SdkBytes> byteBufferSet = new HashSet<SdkBytes>();
byteBufferSet.add(SdkBytes.fromByteArray(generateByteArray(contentLength)));
byteBufferSet.add(SdkBytes.fromByteArray(generateByteArray(contentLength + 1)));
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put(HASH_KEY_NAME, AttributeValue.builder().s("bar").build());
item.put("age", AttributeValue.builder().n("30").build());
item.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
item.put("foos", AttributeValue.builder().ss("bleh", "blah").build());
item.put("S", AttributeValue.builder().ss("ONE", "TWO").build());
item.put("blob", AttributeValue.builder().b(SdkBytes.fromByteArray(generateByteArray(contentLength))).build());
item.put("blobs", AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength)), SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build());
item.put("BS", AttributeValue.builder().bs(byteBufferSet).build());
PutItemRequest putItemRequest = PutItemRequest.builder().tableName(tableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build();
PutItemResponse putItemResult = dynamo.putItem(putItemRequest);
// Get our new item
GetItemResponse itemResult = dynamo.getItem(GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).consistentRead(true).build());
assertNotNull(itemResult.item().get("S").ss());
assertEquals(2, itemResult.item().get("S").ss().size());
assertTrue(itemResult.item().get("S").ss().contains("ONE"));
assertTrue(itemResult.item().get("S").ss().contains("TWO"));
assertEquals("30", itemResult.item().get("age").n());
assertNotNull(itemResult.item().get("bar").s());
assertNotNull(itemResult.item().get("blob").b());
assertTrue(itemResult.item().get("blob").b().equals(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertNotNull(itemResult.item().get("blobs").bs());
assertEquals(2, itemResult.item().get("blobs").bs().size());
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
assertNotNull(itemResult.item().get("BS").bs());
assertEquals(2, itemResult.item().get("BS").bs().size());
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
// Pause to try and deal with ProvisionedThroughputExceededExceptions
Thread.sleep(1000 * 5);
// Add some data into the table with binary hash key
ByteBuffer byteBuffer = ByteBuffer.allocate(contentLength * 2);
byteBuffer.put(generateByteArray(contentLength));
byteBuffer.flip();
item = new HashMap<String, AttributeValue>();
item.put(HASH_KEY_NAME, AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build());
// Reuse the byteBuffer
item.put("blob", AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build());
item.put("blobs", AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength)), SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build());
// Reuse the byteBufferSet
item.put("BS", AttributeValue.builder().bs(byteBufferSet).build());
putItemRequest = PutItemRequest.builder().tableName(binaryKeyTableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build();
dynamo.putItem(putItemRequest);
// Get our new item
itemResult = dynamo.getItem(GetItemRequest.builder().tableName(binaryKeyTableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build())).consistentRead(true).build());
assertNotNull(itemResult.item().get("blob").b());
assertEquals(itemResult.item().get("blob").b(), SdkBytes.fromByteArray(generateByteArray(contentLength)));
assertNotNull(itemResult.item().get("blobs").bs());
assertEquals(2, itemResult.item().get("blobs").bs().size());
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
assertNotNull(itemResult.item().get("BS").bs());
assertEquals(2, itemResult.item().get("BS").bs().size());
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
// Pause to try and deal with ProvisionedThroughputExceededExceptions
Thread.sleep(1000 * 5);
// Load some random data
System.out.println("Loading data...");
Random random = new Random();
for (int i = 0; i < 50; i++) {
item = new HashMap<String, AttributeValue>();
item.put(HASH_KEY_NAME, AttributeValue.builder().s("bar-" + System.currentTimeMillis()).build());
item.put("age", AttributeValue.builder().n(Integer.toString(random.nextInt(100) + 30)).build());
item.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
item.put("foos", AttributeValue.builder().ss("bleh", "blah").build());
dynamo.putItem(PutItemRequest.builder().tableName(tableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build());
}
// Update an item
Map<String, AttributeValueUpdate> itemUpdates = new HashMap<String, AttributeValueUpdate>();
itemUpdates.put("1", AttributeValueUpdate.builder().value(AttributeValue.builder().s("¢").build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("foos", AttributeValueUpdate.builder().value(AttributeValue.builder().ss("foo").build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("S", AttributeValueUpdate.builder().value(AttributeValue.builder().ss("THREE").build()).action(AttributeAction.ADD.toString()).build());
itemUpdates.put("age", AttributeValueUpdate.builder().value(AttributeValue.builder().n("10").build()).action(AttributeAction.ADD.toString()).build());
itemUpdates.put("blob", AttributeValueUpdate.builder().value(AttributeValue.builder().b(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("blobs", AttributeValueUpdate.builder().value(AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength))).build()).action(AttributeAction.PUT.toString()).build());
UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).attributeUpdates(itemUpdates).returnValues("ALL_NEW").build();
UpdateItemResponse updateItemResult = dynamo.updateItem(updateItemRequest);
assertEquals("¢", updateItemResult.attributes().get("1").s());
assertEquals(1, updateItemResult.attributes().get("foos").ss().size());
assertTrue(updateItemResult.attributes().get("foos").ss().contains("foo"));
assertEquals(3, updateItemResult.attributes().get("S").ss().size());
assertTrue(updateItemResult.attributes().get("S").ss().contains("ONE"));
assertTrue(updateItemResult.attributes().get("S").ss().contains("TWO"));
assertTrue(updateItemResult.attributes().get("S").ss().contains("THREE"));
assertEquals(Integer.toString(30 + 10), updateItemResult.attributes().get("age").n());
assertEquals(updateItemResult.attributes().get("blob").b(), SdkBytes.fromByteArray(generateByteArray(contentLength + 1)));
assertEquals(1, updateItemResult.attributes().get("blobs").bs().size());
assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
itemUpdates.clear();
itemUpdates.put("age", AttributeValueUpdate.builder().value(AttributeValue.builder().n("30").build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("blobs", AttributeValueUpdate.builder().value(AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build()).action(AttributeAction.ADD.toString()).build());
updateItemRequest = UpdateItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).attributeUpdates(itemUpdates).returnValues("ALL_NEW").build();
updateItemResult = dynamo.updateItem(updateItemRequest);
assertEquals("30", updateItemResult.attributes().get("age").n());
assertEquals(2, updateItemResult.attributes().get("blobs").bs().size());
assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
// Get an item that doesn't exist.
GetItemRequest itemsRequest = GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("3").build())).consistentRead(true).build();
GetItemResponse itemsResult = dynamo.getItem(itemsRequest);
assertTrue(itemsResult.item() instanceof SdkAutoConstructMap);
// Get an item that doesn't have any attributes,
itemsRequest = GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).consistentRead(true).attributesToGet("non-existent-attribute").build();
itemsResult = dynamo.getItem(itemsRequest);
assertEquals(0, itemsResult.item().size());
// Scan data
ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).attributesToGet(HASH_KEY_NAME).build();
ScanResponse scanResult = dynamo.scan(scanRequest);
assertTrue(scanResult.count() > 0);
assertTrue(scanResult.scannedCount() > 0);
// Try a more advanced Scan query and run it a few times for performance metrics
System.out.println("Testing Scan...");
for (int i = 0; i < 10; i++) {
HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("age", Condition.builder().attributeValueList(AttributeValue.builder().n("40").build()).comparisonOperator(ComparisonOperator.GT.toString()).build());
scanRequest = ScanRequest.builder().tableName(tableName).scanFilter(scanFilter).build();
scanResult = dynamo.scan(scanRequest);
}
// Batch write
HashMap<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>();
List<WriteRequest> writeRequests = new ArrayList<WriteRequest>();
HashMap<String, AttributeValue> writeAttributes = new HashMap<String, AttributeValue>();
writeAttributes.put(HASH_KEY_NAME, AttributeValue.builder().s("" + System.currentTimeMillis()).build());
writeAttributes.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
writeRequests.add(WriteRequest.builder().putRequest(PutRequest.builder().item(writeAttributes).build()).build());
writeRequests.add(WriteRequest.builder().deleteRequest(DeleteRequest.builder().key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("toDelete").build())).build()).build());
requestItems.put(tableName, writeRequests);
BatchWriteItemResponse batchWriteItem = dynamo.batchWriteItem(BatchWriteItemRequest.builder().requestItems(requestItems).build());
// assertNotNull(batchWriteItem.itemCollectionMetrics());
// assertEquals(1, batchWriteItem.itemCollectionMetrics().size());
// assertEquals(tableName, batchWriteItem.itemCollectionMetrics().entrySet().iterator().next().get);
// assertNotNull(tableName, batchWriteItem.getResponses().iterator().next().getCapacityUnits());
assertNotNull(batchWriteItem.unprocessedItems());
assertTrue(batchWriteItem.unprocessedItems().isEmpty());
// Delete some data
DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("jeep").build())).returnValues(ReturnValue.ALL_OLD.toString()).build();
DeleteItemResponse deleteItemResult = dynamo.deleteItem(deleteItemRequest);
// Delete our table
DeleteTableResponse deleteTable = dynamo.deleteTable(DeleteTableRequest.builder().tableName(tableName).build());
}
use of software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse in project aws-sdk-java-v2 by aws.
the class DeleteItemOperationTest method transformResponse_noResults_returnsNull.
@Test
public void transformResponse_noResults_returnsNull() {
FakeItem keyItem = createUniqueFakeItem();
DeleteItemOperation<FakeItem> deleteItemOperation = DeleteItemOperation.create(DeleteItemEnhancedRequest.builder().key(k -> k.partitionValue(keyItem.getId())).build());
DeleteItemResponse response = DeleteItemResponse.builder().build();
FakeItem result = deleteItemOperation.transformResponse(response, FakeItem.getTableSchema(), PRIMARY_CONTEXT, null).attributes();
assertThat(result, is(nullValue()));
}
use of software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse in project aws-sdk-java-v2 by aws.
the class DeleteItemOperationTest method getServiceCall_makesTheRightCallAndReturnsResponse.
@Test
public void getServiceCall_makesTheRightCallAndReturnsResponse() {
FakeItem keyItem = createUniqueFakeItem();
DeleteItemOperation<FakeItem> deleteItemOperation = DeleteItemOperation.create(DeleteItemEnhancedRequest.builder().key(k -> k.partitionValue(keyItem.getId())).build());
DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder().tableName(TABLE_NAME).build();
DeleteItemResponse expectedResponse = DeleteItemResponse.builder().build();
when(mockDynamoDbClient.deleteItem(any(DeleteItemRequest.class))).thenReturn(expectedResponse);
DeleteItemResponse response = deleteItemOperation.serviceCall(mockDynamoDbClient).apply(deleteItemRequest);
assertThat(response, sameInstance(expectedResponse));
verify(mockDynamoDbClient).deleteItem(deleteItemRequest);
}
Aggregations