com.amazonaws.services.dynamodbv2.document.spec.QuerySpec Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.document.spec.QuerySpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentAPIQuery.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void findRepliesUsingAFilterExpression(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec().withProjectionExpression("Message, ReplyDateTime, PostedBy")
            .withKeyConditionExpression("Id = :v_id").withFilterExpression("PostedBy = :v_postedby")
            .withValueMap(new ValueMap().withString(":v_id", replyId).withString(":v_postedby", "User B"));

        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesUsingAFilterExpression results:");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    }
 
Example #2
Source File: QuerySpecBuilderTest.java    From Cheddar with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuild_withAttributeQuery() {
    // Given
    final String attributeName = randomString(10);
    final Condition mockCondition = randomCondition(1);
    final String expectedValue = mockCondition.getValues().iterator().next();
    final AttributeQuery mockAttributeQuery = mock(AttributeQuery.class);

    when(mockAttributeQuery.getAttributeName()).thenReturn(attributeName);
    when(mockAttributeQuery.getCondition()).thenReturn(mockCondition);

    // When
    final QuerySpec querySpec = QuerySpecBuilder.build(mockAttributeQuery, StubItem.class);

    // Then
    assertEquals(attributeName, querySpec.getHashKey().getName());
    assertEquals(expectedValue, querySpec.getHashKey().getValue());
    assertNull(querySpec.getRangeKeyCondition());
}
 
Example #3
Source File: QuerySpecBuilder.java    From Cheddar with Apache License 2.0 6 votes vote down vote up
private static <T extends Item> void addRangeKeyConditionToQuerySpec(final QuerySpec querySpec,
        final CompoundAttributeQuery compoundAttributeQuery, final Class<T> itemClass) {
    final String supportingConditionStringValue = compoundAttributeQuery.getSupportingCondition().getValues().iterator()
            .next();
    final Operators comparisonOperator = compoundAttributeQuery.getSupportingCondition().getComparisonOperator();
    final Class<?> supportingAttributeType = compoundAttributeQuery.getSupportingAttributeType(itemClass);

    try {
        final Object supportingConditionValue = supportingAttributeType.getConstructor(String.class).newInstance(supportingConditionStringValue);

        final RangeKeyCondition rangeKeyCondition = RangeKeyConditionBuilder
                .build(compoundAttributeQuery.getSupportingAttributeName(), supportingConditionValue, comparisonOperator);
        querySpec.withRangeKeyCondition(rangeKeyCondition);
    } catch (final Exception e) {
        throw new PersistenceResourceFailureException(
                String.format("Could not add range key condition for query: %s on item %s.", compoundAttributeQuery,
                        itemClass.getSimpleName()),
                e);
    }
}
 
Example #4
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldQueryTable() {
    // Given
    final ItemId itemId = new ItemId(randomId());

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final ItemCollection<QueryOutcome> outcome = mock(ItemCollection.class);
    when(mockTable.query(any(QuerySpec.class))).thenReturn(outcome);
    // when
    dynamoDocumentStoreTemplate.fetch(new AttributeQuery("id", new Condition(Operators.EQUALS, itemId.value())),
            StubItem.class);
    // then
    verify(mockTable).query(any(QuerySpec.class));
}
 
Example #5
Source File: DynamoDBCertRecordStoreConnection.java    From athenz with Apache License 2.0 6 votes vote down vote up
private List<Item> getUnrefreshedCertRecordsByDate(String provider, Index index, long yesterday, String unrefreshedCertDate) {
    QuerySpec spec = new QuerySpec()
            .withKeyConditionExpression("currentDate = :v_current_date")
            .withFilterExpression("provider = :v_provider AND (attribute_not_exists(lastNotifiedTime) OR lastNotifiedTime < :v_last_notified)")
            .withValueMap(new ValueMap()
                    .withString(":v_current_date", unrefreshedCertDate)
                    .withNumber(":v_last_notified", yesterday)
                    .withString(":v_provider", provider));

    ItemCollection<QueryOutcome> outcome = index.query(spec);
    List<Item> items = new ArrayList<>();
    for (Item item : outcome) {
        items.add(item);
    }
    return items;
}
 
Example #6
Source File: DocumentAPIQuery.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void findRepliesPostedWithinTimePeriod(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        long startDateMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L);
        long endDateMilli = (new Date()).getTime() - (5L * 24L * 60L * 60L * 1000L);
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String startDate = df.format(startDateMilli);
        String endDate = df.format(endDateMilli);

        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec().withProjectionExpression("Message, ReplyDateTime, PostedBy")
            .withKeyConditionExpression("Id = :v_id and ReplyDateTime between :v_start_dt and :v_end_dt")
            .withValueMap(new ValueMap().withString(":v_id", replyId).withString(":v_start_dt", startDate)
                .withString(":v_end_dt", endDate));

        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesPostedWithinTimePeriod results:");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    }
 
Example #7
Source File: DocumentAPIQuery.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void findRepliesInLast15DaysWithConfig(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        long twoWeeksAgoMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L);
        Date twoWeeksAgo = new Date();
        twoWeeksAgo.setTime(twoWeeksAgoMilli);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String twoWeeksAgoStr = df.format(twoWeeksAgo);

        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec().withProjectionExpression("Message, ReplyDateTime, PostedBy")
            .withKeyConditionExpression("Id = :v_id and ReplyDateTime <= :v_reply_dt_tm")
            .withValueMap(new ValueMap().withString(":v_id", replyId).withString(":v_reply_dt_tm", twoWeeksAgoStr));

        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesInLast15DaysWithConfig results:");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }

    }
 
Example #8
Source File: DocumentAPIQuery.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void findRepliesForAThreadSpecifyOptionalLimit(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec().withKeyConditionExpression("Id = :v_id")
            .withValueMap(new ValueMap().withString(":v_id", replyId)).withMaxPageSize(1);

        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesForAThreadSpecifyOptionalLimit results:");

        // Process each page of results
        int pageNum = 0;
        for (Page<Item, QueryOutcome> page : items.pages()) {

            System.out.println("\nPage: " + ++pageNum);

            // Process each item on the current page
            Iterator<Item> item = page.iterator();
            while (item.hasNext()) {
                System.out.println(item.next().toJSONPretty());
            }
        }
    }
 
Example #9
Source File: SampleDataTryQuery.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void findRepliesInLast15DaysWithConfig(String tableName, String forumName, String threadSubject) {

        String replyId = forumName + "#" + threadSubject;
        long twoWeeksAgoMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L);
        Date twoWeeksAgo = new Date();
        twoWeeksAgo.setTime(twoWeeksAgoMilli);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String twoWeeksAgoStr = df.format(twoWeeksAgo);

        Table table = dynamoDB.getTable(tableName);

        QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("Id = :v1 and ReplyDateTime > :v2")
            .withValueMap(new ValueMap().withString(":v1", replyId).withString(":v2", twoWeeksAgoStr))
            .withProjectionExpression("Message, ReplyDateTime, PostedBy");

        ItemCollection<QueryOutcome> items = table.query(querySpec);
        Iterator<Item> iterator = items.iterator();

        System.out.println("Query: printing results...");

        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    }
 
Example #10
Source File: DocumentAPIQuery.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
private static void findRepliesUsingAFilterExpression(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);
        
        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec()
            .withProjectionExpression("Message, ReplyDateTime, PostedBy")
            .withKeyConditionExpression("Id = :v_id")
            .withFilterExpression("PostedBy = :v_postedby")
            .withValueMap(new ValueMap()
                .withString(":v_id", replyId)
                .withString(":v_postedby", "User B"));
        
        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesUsingAFilterExpression results:");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }    
     }
 
Example #11
Source File: DynamoDBServiceImpl1.java    From Serverless-Programming-Cookbook with MIT License 5 votes vote down vote up
@Override
public final Response query(final Request request) {

    Table table = dynamoDB.getTable(request.getTableName());

    final String keyConditionExpression = request.getPartitionKey() + "=:" + request.getPartitionKey();
            //+ " and " + request.getSortKey() + "=:" + request.getSortKey();

    QuerySpec querySpec = new QuerySpec()
            .withKeyConditionExpression(keyConditionExpression);

    final Map<String, Object> valueMap = new HashMap<>();
    StringBuilder filterExpressionBuilder;
    if (request.getFilterData() != null) {
        filterExpressionBuilder = new StringBuilder();
        processFilterData(request, filterExpressionBuilder, valueMap);
        //Add to QuerySpec.
        querySpec.withFilterExpression(filterExpressionBuilder.toString());
    }

    valueMap.put(":" + request.getPartitionKey(), request.getPartitionKeyValue());
    //valueMap.put(":" + request.getSortKey(), request.getSortKeyValue());
    querySpec.withValueMap(valueMap);

    ItemCollection<QueryOutcome> items = table.query(querySpec);

    StringBuilder response = new StringBuilder();
    response.append("PK of items read with query (V1): ");
    for (Item item : items) {
        response.append(prepareKeyStr(item, request));
    }

    return new Response(response.toString(), null);
}
 
Example #12
Source File: DocumentAPIQuery.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void findRepliesForAThreadSpecifyOptionalLimit(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);
        
        String replyId = forumName + "#" + threadSubject;   
               
        QuerySpec spec = new QuerySpec()
            .withKeyConditionExpression("Id = :v_id")
            .withValueMap(new ValueMap()
                .withString(":v_id", replyId))
            .withMaxPageSize(1);

        ItemCollection<QueryOutcome> items = table.query(spec);
        
        System.out.println("\nfindRepliesForAThreadSpecifyOptionalLimit results:");

        // Process each page of results
        int pageNum = 0;
        for (Page<Item, QueryOutcome> page : items.pages()) {
            
            System.out.println("\nPage: " + ++pageNum);

            // Process each item on the current page
            Iterator<Item> item = page.iterator();
            while (item.hasNext()) {
                System.out.println(item.next().toJSONPretty());
            }
        }
    }
 
Example #13
Source File: DocumentAPIQuery.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void findRepliesInLast15DaysWithConfig(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        long twoWeeksAgoMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L);
        Date twoWeeksAgo = new Date();
        twoWeeksAgo.setTime(twoWeeksAgoMilli);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String twoWeeksAgoStr = df.format(twoWeeksAgo);
        
        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec()
            .withProjectionExpression("Message, ReplyDateTime, PostedBy")
            .withKeyConditionExpression("Id = :v_id and ReplyDateTime <= :v_reply_dt_tm")
            .withValueMap(new ValueMap()
                .withString(":v_id", replyId)
                .withString(":v_reply_dt_tm", twoWeeksAgoStr));
        
        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesInLast15DaysWithConfig results:");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }

    }
 
Example #14
Source File: GettingStartedTryQuery.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void findRepliesInLast15DaysWithConfig(
    String tableName, String forumName, String threadSubject) {

    String replyId = forumName + "#" + threadSubject;
    long twoWeeksAgoMilli = (new Date()).getTime()
        - (15L * 24L * 60L * 60L * 1000L);
    Date twoWeeksAgo = new Date();
    twoWeeksAgo.setTime(twoWeeksAgoMilli);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    String twoWeeksAgoStr = df.format(twoWeeksAgo);

    Table table = dynamoDB.getTable(tableName);

    QuerySpec querySpec = new QuerySpec()
        .withKeyConditionExpression("Id = :v1 and ReplyDateTime > :v2")
        .withValueMap(new ValueMap()
            .withString(":v1", replyId)
            .withString(":v2", twoWeeksAgoStr))
        .withProjectionExpression("Message, ReplyDateTime, PostedBy");

    ItemCollection<QueryOutcome> items = table.query(querySpec);
    Iterator<Item> iterator = items.iterator();

    System.out.println("Query: printing results...");

    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
 
Example #15
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldQueryIndex_withAttributeQuery() {
    // Given
    final ItemId itemId = new ItemId(randomId());

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    itemConfiguration.registerIndexes(Arrays.asList(new IndexDefinition("stringProperty")));
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Index mockIndex = mock(Index.class);
    when(mockTable.getIndex(anyString())).thenReturn(mockIndex);

    final ItemCollection<QueryOutcome> outcome = mock(ItemCollection.class);
    when(mockIndex.query(any(QuerySpec.class))).thenReturn(outcome);

    // when
    dynamoDocumentStoreTemplate.fetch(
            new AttributeQuery("stringProperty", new Condition(Operators.EQUALS, itemId.value())), StubItem.class);

    // then
    verify(mockIndex).query(any(QuerySpec.class));
}
 
Example #16
Source File: QuerySpecBuilderTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBuild_withCompoundAttributeQuery() {
    // Given
    final String attributeName = randomString(10);
    final String supportingAttributeName = randomString(10);
    final Condition condition = randomCondition(1);
    final Condition supportingCondition = randomCondition(1);
    final CompoundAttributeQuery compoundAttributeQuery = mock(CompoundAttributeQuery.class);
    final RangeKeyCondition mockRangeKeyCondition = mock(RangeKeyCondition.class);
    final String expectedValue = condition.getValues().iterator().next();
    final String expectedSupportingValue = supportingCondition.getValues().iterator().next();
    final Operators expectedSupportingComparisonOperator = supportingCondition.getComparisonOperator();

    when(compoundAttributeQuery.getAttributeName()).thenReturn(attributeName);
    when(compoundAttributeQuery.getCondition()).thenReturn(condition);
    when(compoundAttributeQuery.getSupportingCondition()).thenReturn(supportingCondition);
    Mockito.<Class<?>> when(compoundAttributeQuery.getSupportingAttributeType(any())).thenReturn(String.class);
    when(compoundAttributeQuery.getSupportingAttributeName()).thenReturn(supportingAttributeName);
    when(RangeKeyConditionBuilder.build(supportingAttributeName, expectedSupportingValue,
            expectedSupportingComparisonOperator)).thenReturn(mockRangeKeyCondition);

    // When
    final QuerySpec querySpec = QuerySpecBuilder.build(compoundAttributeQuery,
            StubWithGlobalSecondaryIndexItem.class);

    // Then
    assertEquals(attributeName, querySpec.getHashKey().getName());
    assertEquals(expectedValue, querySpec.getHashKey().getValue());
    assertEquals(mockRangeKeyCondition, querySpec.getRangeKeyCondition());
}
 
Example #17
Source File: DocumentAPIQuery.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void findRepliesPostedWithinTimePeriod(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        long startDateMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L); 
        long endDateMilli = (new Date()).getTime() - (5L*24L*60L*60L*1000L);    
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String startDate = df.format(startDateMilli);
        String endDate = df.format(endDateMilli);

        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec()
            .withProjectionExpression("Message, ReplyDateTime, PostedBy")
            .withKeyConditionExpression("Id = :v_id and ReplyDateTime between :v_start_dt and :v_end_dt")
            .withValueMap(new ValueMap()
                .withString(":v_id", replyId)
                .withString(":v_start_dt", startDate)
                .withString(":v_end_dt", endDate));
        
        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesPostedWithinTimePeriod results:");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }    
    }
 
Example #18
Source File: QuerySpecBuilder.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
private static <T extends Item> QuerySpec buildWithHashAndRangeKey(
        final CompoundAttributeQuery compoundAttributeQuery, final Class<T> itemClass) {
    final Set<String> supportingConditionValues = compoundAttributeQuery.getSupportingCondition().getValues();
    validateSupportingConditionValues(supportingConditionValues);

    final QuerySpec querySpec = buildWithHashKey(compoundAttributeQuery);
    addRangeKeyConditionToQuerySpec(querySpec, compoundAttributeQuery, itemClass);

    return querySpec;
}
 
Example #19
Source File: QuerySpecBuilder.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public static <T extends Item> QuerySpec build(final AttributeQuery attributeQuery, final Class<T> itemClass) {
    if (CompoundAttributeQuery.class.isAssignableFrom(attributeQuery.getClass())) {
        final CompoundAttributeQuery compoundAttributeQuery = (CompoundAttributeQuery) attributeQuery;
        return buildWithHashAndRangeKey(compoundAttributeQuery, itemClass);
    }

    return buildWithHashKey(attributeQuery);
}
 
Example #20
Source File: TryDaxTests.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
void queryTest(String tableName, DynamoDB client, int pk, int sk1, int sk2, int iterations) {
    long startTime, endTime;
    System.out.println("Query test - partition key " + pk + " and sort keys between " + sk1 + " and " + sk2);
    Table table = client.getTable(tableName);

    HashMap<String, Object> valueMap = new HashMap<String, Object>();
    valueMap.put(":pkval", pk);
    valueMap.put(":skval1", sk1);
    valueMap.put(":skval2", sk2);

    QuerySpec spec = new QuerySpec()
            .withKeyConditionExpression("pk = :pkval and sk between :skval1 and :skval2")
            .withValueMap(valueMap);

    for (int i = 0; i < iterations; i++) {
        startTime = System.nanoTime();
        ItemCollection<QueryOutcome> items = table.query(spec);

        try {
            Iterator<Item> iter = items.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
        } catch (Exception e) {
            System.err.println("Unable to query table:");
            e.printStackTrace();
        }
        endTime = System.nanoTime();
        printTime(startTime, endTime, iterations);
    }
}
 
Example #21
Source File: QuerySpecBuilder.java    From Cheddar with Apache License 2.0 4 votes vote down vote up
private static QuerySpec buildWithHashKey(final AttributeQuery attributeQuery) {
    return new QuerySpec().withHashKey(attributeQuery.getAttributeName(),
            attributeQuery.getCondition().getValues().iterator().next());
}
 
Example #22
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldQueryIndex_withAttributeQueryOnHashPartOfCompoundIndex() {
    // Given
    final ItemId itemId = new ItemId(randomId());

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubWithGlobalSecondaryIndexItem.class,
            tableName);
    itemConfiguration.registerIndexes((Arrays.asList(new CompoundIndexDefinition("gsi", "gsiSupportingValue"))));
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Index mockIndex = mock(Index.class);
    when(mockTable.getIndex(anyString())).thenReturn(mockIndex);

    final ItemCollection<QueryOutcome> mockOutcome = mock(ItemCollection.class);
    when(mockIndex.query(any(QuerySpec.class))).thenReturn(mockOutcome);

    final IteratorSupport<Item, QueryOutcome> mockIterator = mock(IteratorSupport.class);
    final Item mockItem = new Item();
    mockItem.withString(randomString(), randomString());

    when(mockOutcome.iterator()).thenReturn(mockIterator);
    when(mockIterator.hasNext()).thenReturn(true, false);
    when(mockIterator.next()).thenReturn(mockItem);

    // When
    final Collection<StubWithGlobalSecondaryIndexItem> stubWithGlobalSecondaryIndexItemCollection = dynamoDocumentStoreTemplate
            .fetch(new AttributeQuery("gsi", new Condition(Operators.EQUALS, itemId.value())),
                    StubWithGlobalSecondaryIndexItem.class);

    // Then
    assertTrue(stubWithGlobalSecondaryIndexItemCollection.size() == 1);
    final ArgumentCaptor<QuerySpec> querySpecCaptor = ArgumentCaptor.forClass(QuerySpec.class);
    verify(mockIndex).query(querySpecCaptor.capture());
}
 
Example #23
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldQueryIndex_withCompoundAttributeQuery() {
    // Given
    final ItemId itemId = new ItemId(randomId());

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubWithGlobalSecondaryIndexItem.class,
            tableName);
    itemConfiguration.registerIndexes((Arrays.asList(new CompoundIndexDefinition("gsi", "gsiSupportingValue"))));
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Index mockIndex = mock(Index.class);
    when(mockTable.getIndex(anyString())).thenReturn(mockIndex);

    final ItemCollection<QueryOutcome> mockOutcome = mock(ItemCollection.class);
    when(mockIndex.query(any(QuerySpec.class))).thenReturn(mockOutcome);

    final IteratorSupport<Item, QueryOutcome> mockIterator = mock(IteratorSupport.class);
    final Item mockItem = new Item();
    mockItem.withString(randomString(), randomString());

    when(mockOutcome.iterator()).thenReturn(mockIterator);
    when(mockIterator.hasNext()).thenReturn(true, false);
    when(mockIterator.next()).thenReturn(mockItem);

    // When
    final Collection<StubWithGlobalSecondaryIndexItem> stubWithGlobalSecondaryIndexItemCollection = dynamoDocumentStoreTemplate
            .fetch(new CompoundAttributeQuery("gsi", new Condition(Operators.EQUALS, itemId.value()),
                    "gsiSupportingValue", new Condition(Operators.EQUALS, String.valueOf(randomInt(10)))),
                    StubWithGlobalSecondaryIndexItem.class);

    // Then
    assertTrue(stubWithGlobalSecondaryIndexItemCollection.size() == 1);
    final ArgumentCaptor<QuerySpec> querySpecCaptor = ArgumentCaptor.forClass(QuerySpec.class);
    verify(mockIndex).query(querySpecCaptor.capture());
    assertNotNull(querySpecCaptor.getValue().getRangeKeyCondition());
}
 
Example #24
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldQueryIndex_withACompoundAttributeQueryOnACompoundGSIWithAHashValueTheSameAsThePrimaryKeyDefinition() {
    // Given
    final ItemId itemId = new ItemId(randomId());

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubWithGlobalSecondaryIndexItem.class,
            tableName, new PrimaryKeyDefinition("gsi"));
    itemConfiguration.registerIndexes((Arrays.asList(new CompoundIndexDefinition("gsi", "gsiSupportingValue"))));
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Index mockIndex = mock(Index.class);
    when(mockTable.getIndex(anyString())).thenReturn(mockIndex);

    final ItemCollection<QueryOutcome> mockOutcome = mock(ItemCollection.class);
    when(mockIndex.query(any(QuerySpec.class))).thenReturn(mockOutcome);

    final IteratorSupport<Item, QueryOutcome> mockIterator = mock(IteratorSupport.class);
    final Item mockItem = new Item();
    mockItem.withString(randomString(), randomString());

    when(mockOutcome.iterator()).thenReturn(mockIterator);
    when(mockIterator.hasNext()).thenReturn(true, false);
    when(mockIterator.next()).thenReturn(mockItem);

    // When
    final Collection<StubWithGlobalSecondaryIndexItem> stubWithGlobalSecondaryIndexItemCollection = dynamoDocumentStoreTemplate
            .fetch(new CompoundAttributeQuery("gsi", new Condition(Operators.EQUALS, itemId.value()),
                    "gsiSupportingValue", new Condition(Operators.EQUALS, String.valueOf(randomInt(10)))),
                    StubWithGlobalSecondaryIndexItem.class);

    // Then
    assertTrue(stubWithGlobalSecondaryIndexItemCollection.size() == 1);
    final ArgumentCaptor<QuerySpec> querySpecCaptor = ArgumentCaptor.forClass(QuerySpec.class);
    verify(mockIndex).query(querySpecCaptor.capture());
    assertNotNull(querySpecCaptor.getValue().getRangeKeyCondition());
}
 
Example #25
Source File: MoviesQuery.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);
       
        Table table = dynamoDB.getTable("Movies");

        HashMap<String, String> nameMap = new HashMap<String, String>();
        nameMap.put("#yr", "year"); 
        
        HashMap<String, Object> valueMap = new HashMap<String, Object>();
        valueMap.put(":yyyy", 1985);
        
        QuerySpec querySpec = new QuerySpec()
            .withKeyConditionExpression("#yr = :yyyy")
            .withNameMap(new NameMap().with("#yr", "year"))
            .withValueMap(valueMap);

        ItemCollection<QueryOutcome> items = table.query(querySpec);

        Iterator<Item> iterator = items.iterator();
        Item item = null;

        System.out.println("Movies from 1985");
        while (iterator.hasNext()) {
            item = iterator.next();
            System.out.println(item.getNumber("year") + ": " + item.getString("title"));
        }
                        
        valueMap.put(":yyyy", 1992);
        valueMap.put(":letter1", "A");
        valueMap.put(":letter2", "L");
        
        querySpec
            .withProjectionExpression("#yr, title, info.genres, info.actors[0]")
            .withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2")
            .withNameMap(nameMap)
            .withValueMap(valueMap);
         
        items = table.query(querySpec);
        iterator = items.iterator();
        
        System.out.println("Movies from 1992 - titles A-L, with genres and lead actor");
        while (iterator.hasNext()) {
            item = iterator.next();
            System.out.println(item.toString());
        }
       
    }
 
Example #26
Source File: DynamoDBCertRecordStoreConnectionTest.java    From athenz with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateUnrefreshedCertificatesNotificationTimestamp() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    Date now = new Date(1591706189000L);
    long nowL = now.getTime();
    long fiveDaysAgo = nowL - 5 * 24 * 60 * 60 * 1000;

    Map<String, AttributeValue> unNotified = generateAttributeValues(
            "home.test.service2",
            "testInstance2",
            null,
            null,
            null,
            null,
            "testHost1");

    Map<String, AttributeValue> reNotified = generateAttributeValues(
            "home.test.service3",
            "testInstance3",
            Long.toString(fiveDaysAgo),
            Long.toString(fiveDaysAgo),
            "testServer",
            null,
            "testHost1");

    Item item1 = ItemUtils.toItem(unNotified);
    Item item2 = ItemUtils.toItem(reNotified);

    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, true, false);
    when(iteratorSupport.next()).thenReturn(item1).thenReturn(item2);

    Mockito.doReturn(itemCollection).when(index).query(any(QuerySpec.class));

    AttributeValue lastNotifiedTimeAttrValue = new AttributeValue();
    lastNotifiedTimeAttrValue.setN(Long.toString(nowL));
    AttributeValue lastNotifiedServerAttrValue = new AttributeValue();
    lastNotifiedServerAttrValue.setS("localhost");
    unNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
    unNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);

    reNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
    reNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);

    Item updatedItem1 = ItemUtils.toItem(unNotified);
    Item updatedItem2 = ItemUtils.toItem(reNotified);

    UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome1.getItem()).thenReturn(updatedItem1);

    UpdateItemOutcome updateItemOutcome2 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome2.getItem()).thenReturn(updatedItem2);

    when(table.updateItem(any(UpdateItemSpec.class))).thenReturn(updateItemOutcome1).thenReturn(updateItemOutcome2);
    List<X509CertRecord> records = dbConn.updateUnrefreshedCertificatesNotificationTimestamp(
            "localhost",
            nowL,
            "provider");

    assertEquals(records.size(), 2);
    assertNull(records.get(0).getCurrentTime());
    assertEquals(records.get(0).getService(), "home.test.service2");
    assertEquals(records.get(0).getLastNotifiedTime(), now);
    assertEquals(records.get(1).getCurrentTime().getTime(), fiveDaysAgo);
    assertEquals(records.get(1).getService(), "home.test.service3");
    assertEquals(records.get(1).getLastNotifiedTime(), now);
}
 
Example #27
Source File: DocumentAPIQuery.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
private static void findRepliesForAThread(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        String replyId = forumName + "#" + threadSubject;
        
        QuerySpec spec = new QuerySpec()
            .withKeyConditionExpression("Id = :v_id")
            .withValueMap(new ValueMap()
                .withString(":v_id", replyId));
        
        ItemCollection<QueryOutcome> items = table.query(spec);
        
        System.out.println("\nfindRepliesForAThread results:");

        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
        
    }
 
Example #28
Source File: DocumentAPIGlobalSecondaryIndexExample.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void queryIndex(String indexName) {

        Table table = dynamoDB.getTable(tableName);

        System.out.println("\n***********************************************************\n");
        System.out.print("Querying index " + indexName + "...");

        Index index = table.getIndex(indexName);

        ItemCollection<QueryOutcome> items = null;

        QuerySpec querySpec = new QuerySpec();

        if (indexName == "CreateDateIndex") {
            System.out.println("Issues filed on 2013-11-01");
            querySpec.withKeyConditionExpression("CreateDate = :v_date and begins_with(IssueId, :v_issue)")
                .withValueMap(new ValueMap().withString(":v_date", "2013-11-01").withString(":v_issue", "A-"));
            items = index.query(querySpec);
        }
        else if (indexName == "TitleIndex") {
            System.out.println("Compilation errors");
            querySpec.withKeyConditionExpression("Title = :v_title and begins_with(IssueId, :v_issue)")
                .withValueMap(new ValueMap().withString(":v_title", "Compilation error").withString(":v_issue", "A-"));
            items = index.query(querySpec);
        }
        else if (indexName == "DueDateIndex") {
            System.out.println("Items that are due on 2013-11-30");
            querySpec.withKeyConditionExpression("DueDate = :v_date")
                .withValueMap(new ValueMap().withString(":v_date", "2013-11-30"));
            items = index.query(querySpec);
        }
        else {
            System.out.println("\nNo valid index name provided");
            return;
        }

        Iterator<Item> iterator = items.iterator();

        System.out.println("Query: printing results...");

        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }

    }
 
Example #29
Source File: DocumentAPIQuery.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
private static void findRepliesForAThread(String forumName, String threadSubject) {

        Table table = dynamoDB.getTable(tableName);

        String replyId = forumName + "#" + threadSubject;

        QuerySpec spec = new QuerySpec().withKeyConditionExpression("Id = :v_id")
            .withValueMap(new ValueMap().withString(":v_id", replyId));

        ItemCollection<QueryOutcome> items = table.query(spec);

        System.out.println("\nfindRepliesForAThread results:");

        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }

    }
 
Example #30
Source File: DynamoSpaceConfigClient.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
@Override
public void getSelectedSpaces(Marker marker, SpaceAuthorizationCondition authorizedCondition, SpaceSelectionCondition selectedCondition,
    Handler<AsyncResult<List<Space>>> handler) {
  logger.info(marker, "Getting selected spaces");

  if (authorizedCondition == null || selectedCondition == null) {
    throw new NullPointerException("authorizedCondition and selectedCondition are required");
  }

  final List<Space> result = new ArrayList<>();
  logger.debug(marker, "authorizedCondition: spaceIds: {}, ownerIds {}, packages: {}", authorizedCondition.spaceIds, authorizedCondition.ownerIds, authorizedCondition.packages);
  logger.debug(marker, "selectedCondition: spaceIds: {}, ownerIds {}, packages: {}, shared: {}, negateOwnerIds: {}", selectedCondition.spaceIds, selectedCondition.ownerIds, selectedCondition.packages, selectedCondition.shared, selectedCondition.negateOwnerIds);

  try {
    final Set<String> authorizedSpaces = getAuthorizedSpaces(marker, authorizedCondition);

    // get all shared spaces if the selection for shared spaces is enabled
    if (selectedCondition.shared) {
      spaces.getIndex("shared-index").query(new QuerySpec().withHashKey("shared", 1).withProjectionExpression("id")).pages()
          .forEach(p -> p.forEach(i -> {
            authorizedSpaces.add(i.getString("id"));
          }));
      logger.debug(marker, "Number of space IDs after addition of shared spaces: {}", authorizedSpaces.size());
    }

    // filter out the ones not present in the selectedCondition (null or empty represents 'do not filter')
    if (!CollectionUtils.isNullOrEmpty(selectedCondition.spaceIds)) {
      authorizedSpaces.removeIf(i -> !selectedCondition.spaceIds.contains(i));
      logger.debug(marker, "Number of space IDs after removal of the ones not selected by ID: {}", authorizedSpaces.size());
    }

    // now filter all spaceIds with the ones being selected in the selectedCondition (by checking the space's ownership) (
    if (!CollectionUtils.isNullOrEmpty(selectedCondition.ownerIds)) {
      final Set<String> ownersSpaces = new HashSet<>();
      selectedCondition.ownerIds.forEach(o ->
          spaces.getIndex("owner-index").query(new QuerySpec().withHashKey("owner", o).withProjectionExpression("id")).pages()
              .forEach(p -> p.forEach(i -> ownersSpaces.add(i.getString("id")))));

      // HINT: A ^ TRUE == !A (negateOwnerIds: keep or remove the spaces contained in the owner's spaces list)
      authorizedSpaces.removeIf(i -> !selectedCondition.negateOwnerIds ^ ownersSpaces.contains(i));
      logger.debug(marker, "Number of space IDs after removal of the ones not selected by owner: {}", authorizedSpaces.size());
    }

    // TODO selection per packages is not yet supported: selectedCondition.packages

    logger.info(marker, "Final number of space IDs to be retrieved from DynamoDB: {}", authorizedSpaces.size());
    if (!authorizedSpaces.isEmpty()) {
      int batches = (int) Math.ceil((double) authorizedSpaces.size()/100);
      for (int i=0; i<batches; i++) {
        final TableKeysAndAttributes keys = new TableKeysAndAttributes(dynamoClient.tableName);
        authorizedSpaces.stream().skip(i*100).limit(100).forEach(id -> keys.addHashOnlyPrimaryKey("id", id));

        BatchGetItemOutcome outcome = dynamoClient.db.batchGetItem(keys);
        processOutcome(outcome, result);

        while (!outcome.getUnprocessedKeys().isEmpty()) {
          outcome = dynamoClient.db.batchGetItemUnprocessed(outcome.getUnprocessedKeys());
          processOutcome(outcome, result);
        }
      }
    }

    logger.info(marker, "Number of spaces retrieved from DynamoDB: {}", result.size());
    handler.handle(Future.succeededFuture(result));
  } catch (AmazonDynamoDBException e) {
    logger.error(marker, "Failure getting authorized spaces", e);
    handler.handle(Future.failedFuture(e));
  }
}