Search in sources :

Example 71 with Item

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();
    }
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 72 with Item

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"));
}
Also used : Item(com.jcabi.dynamo.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.jcabi.dynamo.Table) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Attributes(com.jcabi.dynamo.Attributes) Region(com.jcabi.dynamo.Region) Test(org.junit.Test)

Example 73 with Item

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();
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) PutItemRequest(com.amazonaws.services.dynamodbv2.model.PutItemRequest) PutItemResult(com.amazonaws.services.dynamodbv2.model.PutItemResult) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) IOException(java.io.IOException)

Example 74 with Item

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();
    }
}
Also used : DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) IOException(java.io.IOException)

Example 75 with Item

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));
}
Also used : lombok.val(lombok.val) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) MetricsCallable(eu.prismacapacity.cryptoshred.core.metrics.MetricsCallable) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest) Test(org.junit.jupiter.api.Test)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)107 Item (com.amazonaws.services.dynamodbv2.document.Item)95 HashMap (java.util.HashMap)60 Table (com.amazonaws.services.dynamodbv2.document.Table)53 Test (org.testng.annotations.Test)49 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)42 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)35 AmazonServiceException (com.amazonaws.AmazonServiceException)31 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)27 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)26 ArrayList (java.util.ArrayList)25 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)24 List (java.util.List)23 Test (org.junit.Test)23 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)22 IOException (java.io.IOException)21 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)20 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)18 Date (java.util.Date)18 Map (java.util.Map)17