use of com.amazonaws.services.dynamodbv2.document.Item in project jcabi-dynamo by jcabi.
the class QueryValve method fetch.
// @checkstyle ParameterNumber (5 lines)
@Override
public Dosage fetch(final Credentials credentials, final String table, final Map<String, Condition> conditions, final Collection<String> keys) throws IOException {
final AmazonDynamoDB aws = credentials.aws();
try {
final Collection<String> attrs = new HashSet<String>(Arrays.asList(this.attributes));
attrs.addAll(keys);
QueryRequest request = new QueryRequest().withTableName(table).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withKeyConditions(conditions).withConsistentRead(this.consistent).withScanIndexForward(this.forward).withSelect(this.select).withLimit(this.limit);
if (this.select.equals(Select.SPECIFIC_ATTRIBUTES.toString())) {
request = request.withAttributesToGet(attrs);
}
if (!this.index.isEmpty()) {
request = request.withIndexName(this.index);
}
final long start = System.currentTimeMillis();
final QueryResult result = aws.query(request);
Logger.info(this, // @checkstyle LineLength (1 line)
"#items(): loaded %d item(s) from '%s' and stopped at %s, using %s, %s, in %[ms]s", result.getCount(), table, result.getLastEvaluatedKey(), conditions, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return new QueryValve.NextDosage(credentials, request, result);
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to fetch from \"%s\" by %s and %s", table, conditions, keys), ex);
} finally {
aws.shutdown();
}
}
use of com.amazonaws.services.dynamodbv2.document.Item in project jcabi-dynamo by jcabi.
the class MkRegionTest method storesAndReadsAttributes.
/**
* MkRegion can store and read items.
* @throws Exception If some problem inside
*/
@Test
public void storesAndReadsAttributes() throws Exception {
final String name = "users";
final String key = "id";
final String attr = "description";
final String nattr = "thenumber";
final Region region = new MkRegion(new H2Data().with(name, new String[] { key }, attr, nattr));
final Table table = region.table(name);
table.put(new Attributes().with(key, "32443").with(attr, "first value to \n\t€ save").with(nattr, "150"));
final Item item = table.frame().iterator().next();
MatcherAssert.assertThat(item.has(attr), Matchers.is(true));
MatcherAssert.assertThat(item.get(attr).getS(), Matchers.containsString("\n\t\u20ac save"));
item.put(attr, new AttributeValueUpdate().withValue(new AttributeValue("this is another value")));
MatcherAssert.assertThat(item.get(attr).getS(), Matchers.containsString("another value"));
MatcherAssert.assertThat(item.get(nattr).getN(), Matchers.endsWith("50"));
}
use of com.amazonaws.services.dynamodbv2.document.Item in project jcabi-dynamo by jcabi.
the class AwsTable method put.
@Override
public Item put(final Map<String, AttributeValue> attributes) throws IOException {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final PutItemRequest request = new PutItemRequest();
request.setTableName(this.self);
request.setItem(attributes);
request.setReturnValues(ReturnValue.NONE);
request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
final PutItemResult result = aws.putItem(request);
final long start = System.currentTimeMillis();
Logger.info(this, "#put('%[text]s'): created item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return new AwsItem(this.credentials, this.frame(), this.self, new Attributes(attributes).only(this.keys()), new Array<String>(this.keys()));
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to put into \"%s\" with %s", this.self, attributes), ex);
} finally {
aws.shutdown();
}
}
use of com.amazonaws.services.dynamodbv2.document.Item in project jcabi-dynamo by jcabi.
the class AwsTable method delete.
@Override
public void delete(final Map<String, AttributeValue> attributes) throws IOException {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final DeleteItemRequest request = new DeleteItemRequest();
request.setTableName(this.self);
request.setKey(attributes);
request.setReturnValues(ReturnValue.NONE);
request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
final DeleteItemResult result = aws.deleteItem(request);
final long start = System.currentTimeMillis();
Logger.info(this, "#delete('%[text]s'): deleted item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to delete at \"%s\" by keys %s", this.self, attributes), ex);
} finally {
aws.shutdown();
}
}
use of com.amazonaws.services.dynamodbv2.document.Item in project cryptoshred by prisma-capacity.
the class DynamoDBCryptoKeyRepositoryTest method testGetOrCreateKeyForNewKey.
@SuppressWarnings("unchecked")
@Test
void testGetOrCreateKeyForNewKey() {
val uut = new DynamoDBCryptoKeyRepository(engine, dynamoDB, metrics, TABLE_NAME);
val item = Maps.of(Utils.generateKeyPropertyName(algorithm, size), new AttributeValue().withB(ByteBuffer.wrap(key.getBytes())));
val getResponseMock = mock(GetItemResult.class);
when(dynamoDB.getItem(any(GetItemRequest.class))).thenReturn(getResponseMock);
when(getResponseMock.getItem()).thenReturn(null);
val updateResponseMock = mock(UpdateItemResult.class);
when(dynamoDB.updateItem(any(UpdateItemRequest.class))).thenReturn(updateResponseMock);
when(updateResponseMock.getAttributes()).thenReturn(item);
when(engine.generateKey(algorithm, size)).thenReturn(key);
val resultKey = uut.getOrCreateKeyFor(subjectId, algorithm, size);
assertEquals(key, resultKey);
verify(dynamoDB).getItem(any(GetItemRequest.class));
verify(dynamoDB).updateItem(any(UpdateItemRequest.class));
verifyNoMoreInteractions(dynamoDB);
verify(metrics).notifyKeyLookUp();
verify(metrics).notifyKeyCreation();
verify(metrics).timedCreateKey(any(MetricsCallable.class));
verify(metrics).timedFindKey(any(MetricsCallable.class));
}
Aggregations