use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class AwsItem method get.
@Override
public AttributeValue get(final String attr) throws IOException {
final String attrib = String.format(Locale.ENGLISH, attr);
AttributeValue value = this.attributes.get(attrib);
if (value == null) {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final GetItemRequest request = this.makeItemRequestFor(attrib);
final long start = System.currentTimeMillis();
final GetItemResult result = aws.getItem(request);
value = result.getItem().get(attrib);
Logger.info(this, // @checkstyle LineLength (1 line)
"#get('%s'): loaded '%[text]s' from DynamoDB, %s, in %[ms]s", attrib, value, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to get \"%s\" from \"%s\" by %s", attr, this.name, this.keys), ex);
} finally {
aws.shutdown();
}
}
if (value == null) {
throw new NoSuchElementException(String.format("attribute \"%s\" not found", attr));
}
return value;
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class AwsItem method put.
@Override
public Map<String, AttributeValue> put(final Map<String, AttributeValueUpdate> attrs) throws IOException {
final AmazonDynamoDB aws = this.credentials.aws();
final Attributes expected = this.attributes.only(this.keys);
try {
final UpdateItemRequest request = new UpdateItemRequest().withTableName(this.name).withExpected(expected.asKeys()).withKey(expected).withAttributeUpdates(attrs).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withReturnValues(ReturnValue.UPDATED_NEW);
final long start = System.currentTimeMillis();
final UpdateItemResult result = aws.updateItem(request);
Logger.info(this, "#put('%s'): updated item to DynamoDB, %s, in %[ms]s", attrs, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return result.getAttributes();
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to put %s into \"%s\" with %s", attrs, this.name, this.keys), ex);
} finally {
aws.shutdown();
}
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class AwsIterator method remove.
@Override
@SuppressWarnings("PMD.UseConcurrentHashMap")
public void remove() {
synchronized (this.dosage) {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final Dosage prev = this.dosage.get();
final List<Map<String, AttributeValue>> items = new ArrayList<Map<String, AttributeValue>>(prev.items());
final Map<String, AttributeValue> item = items.remove(this.position);
final long start = System.currentTimeMillis();
final DeleteItemResult res = aws.deleteItem(new DeleteItemRequest().withTableName(this.name).withKey(new Attributes(item).only(this.keys)).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withExpected(new Attributes(item).only(this.keys).asKeys()));
this.dosage.set(new AwsIterator.Fixed(prev, items));
--this.position;
Logger.info(this, "#remove(): item #%d removed from DynamoDB, %s, in %[ms]s", this.position, new PrintableConsumedCapacity(res.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
} finally {
aws.shutdown();
}
}
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class QueryValve method count.
@Override
public int count(final Credentials credentials, final String table, final Map<String, Condition> conditions) throws IOException {
final AmazonDynamoDB aws = credentials.aws();
try {
QueryRequest request = new QueryRequest().withTableName(table).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withKeyConditions(conditions).withConsistentRead(this.consistent).withSelect(Select.COUNT).withLimit(Integer.MAX_VALUE);
if (!this.index.isEmpty()) {
request = request.withIndexName(this.index);
}
final long start = System.currentTimeMillis();
final QueryResult rslt = aws.query(request);
final int count = rslt.getCount();
Logger.info(this, // @checkstyle LineLength (1 line)
"#total(): COUNT=%d in '%s' using %s, %s, in %[ms]s", count, request.getTableName(), request.getQueryFilter(), new PrintableConsumedCapacity(rslt.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return count;
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to count from \"%s\" by %s", table, conditions), ex);
} finally {
aws.shutdown();
}
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class AwsTableTest method savesItemToDynamo.
/**
* AwsTable can save an item.
* @throws Exception If some problem inside
*/
@Test
public void savesItemToDynamo() throws Exception {
final Credentials credentials = Mockito.mock(Credentials.class);
final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
Mockito.doReturn(aws).when(credentials).aws();
Mockito.doReturn(new PutItemResult().withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1.0d))).when(aws).putItem(Mockito.any(PutItemRequest.class));
Mockito.doReturn(new DescribeTableResult().withTable(new TableDescription().withKeySchema(new KeySchemaElement().withAttributeName(AwsTableTest.KEY)))).when(aws).describeTable(Mockito.any(DescribeTableRequest.class));
final String attr = "attribute-1";
final AttributeValue value = new AttributeValue("value-1");
final String name = "table-name";
final Table table = new AwsTable(credentials, Mockito.mock(Region.class), name);
table.put(new Attributes().with(attr, value));
Mockito.verify(aws).putItem(PutItemRequest.class.cast(MockitoHamcrest.argThat(Matchers.allOf(Matchers.hasProperty(AwsTableTest.TABLE_NAME, Matchers.equalTo(name)), Matchers.hasProperty("item", Matchers.hasEntry(Matchers.equalTo(attr), Matchers.equalTo(value)))))));
}
Aggregations