Search in sources :

Example 1 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoNotificationTopicDao method listTopics.

@Override
public List<NotificationTopic> listTopics(String appId, boolean includeDeleted) {
    checkNotNull(appId);
    // Consistent reads is set to true, because this is the table's primary key, and having reliable tests is more
    // important than saving a small amount of DDB capacity.
    DynamoNotificationTopic hashKey = new DynamoNotificationTopic();
    hashKey.setAppId(appId);
    DynamoDBQueryExpression<DynamoNotificationTopic> query = new DynamoDBQueryExpression<DynamoNotificationTopic>();
    query.withConsistentRead(true);
    query.withHashKeyValues(hashKey);
    if (!includeDeleted) {
        query.withQueryFilterEntry("deleted", new Condition().withComparisonOperator(ComparisonOperator.NE).withAttributeValueList(new AttributeValue().withN("1")));
    }
    QueryResultPage<DynamoNotificationTopic> resultPage = mapper.queryPage(DynamoNotificationTopic.class, query);
    List<DynamoNotificationTopic> topicList = resultPage.getResults();
    // Load criteria.
    topicList.forEach(this::loadCriteria);
    return ImmutableList.copyOf(topicList);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)

Example 2 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoOAuthAccessGrantDao method getAccessGrants.

@Override
public ForwardCursorPagedResourceList<OAuthAccessGrant> getAccessGrants(String appId, String vendorId, String offsetKey, int pageSize) {
    checkNotNull(appId);
    checkArgument(isNotBlank(vendorId));
    int pageSizeWithIndicatorRecord = pageSize + 1;
    DynamoOAuthAccessGrant grantKey = new DynamoOAuthAccessGrant();
    grantKey.setKey(getGrantKey(appId, vendorId));
    DynamoDBQueryExpression<DynamoOAuthAccessGrant> query = new DynamoDBQueryExpression<DynamoOAuthAccessGrant>().withHashKeyValues(grantKey).withLimit(pageSizeWithIndicatorRecord);
    if (offsetKey != null) {
        Condition healthCodeCondition = new Condition().withComparisonOperator(GE).withAttributeValueList(new AttributeValue().withS(offsetKey));
        query.withRangeKeyCondition("healthCode", healthCodeCondition);
    }
    QueryResultPage<DynamoOAuthAccessGrant> page = mapper.queryPage(DynamoOAuthAccessGrant.class, query);
    int pageLimit = Math.min(page.getResults().size(), pageSizeWithIndicatorRecord);
    List<OAuthAccessGrant> list = Lists.newArrayListWithCapacity(pageLimit);
    for (int i = 0; i < pageLimit; i++) {
        list.add(page.getResults().get(i));
    }
    String nextPageOffsetKey = null;
    if (list.size() == pageSizeWithIndicatorRecord) {
        nextPageOffsetKey = Iterables.getLast(list).getHealthCode();
    }
    int limit = Math.min(list.size(), pageSize);
    return new ForwardCursorPagedResourceList<OAuthAccessGrant>(list.subList(0, limit), nextPageOffsetKey).withRequestParam(PAGE_SIZE, pageSize).withRequestParam(OFFSET_KEY, offsetKey);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) OAuthAccessGrant(org.sagebionetworks.bridge.models.oauth.OAuthAccessGrant)

Example 3 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoHealthDataDao method getRecordsByHealthCodeCreatedOn.

/**
 * {@inheritDoc}
 */
@Override
public List<HealthDataRecord> getRecordsByHealthCodeCreatedOn(String healthCode, long createdOnStart, long createdOnEnd) {
    // Hash key.
    DynamoHealthDataRecord queryRecord = new DynamoHealthDataRecord();
    queryRecord.setHealthCode(healthCode);
    // Range key.
    Condition rangeKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN).withAttributeValueList(new AttributeValue().withN(String.valueOf(createdOnStart)), new AttributeValue().withN(String.valueOf(createdOnEnd)));
    // Construct query.
    DynamoDBQueryExpression<DynamoHealthDataRecord> expression = new DynamoDBQueryExpression<DynamoHealthDataRecord>().withConsistentRead(false).withHashKeyValues(queryRecord).withRangeKeyCondition("createdOn", rangeKeyCondition).withLimit(BridgeConstants.DUPE_RECORDS_MAX_COUNT);
    // Execute query.
    QueryResultPage<DynamoHealthDataRecord> resultPage = mapper.queryPage(DynamoHealthDataRecord.class, expression);
    List<DynamoHealthDataRecord> recordList = resultPage.getResults();
    return ImmutableList.copyOf(recordList);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)

Example 4 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoUploadSchemaDaoTest method getLatestById.

@Test
public void getLatestById() {
    // mock DDB mapper
    DynamoUploadSchema mapperOutputSchema = new DynamoUploadSchema();
    List<DynamoUploadSchema> mapperOutputSchemaList = ImmutableList.of(mapperOutputSchema);
    QueryResultPage<DynamoUploadSchema> queryResultPage = new QueryResultPage<>();
    queryResultPage.setResults(mapperOutputSchemaList);
    ArgumentCaptor<DynamoDBQueryExpression> mapperQueryCaptor = ArgumentCaptor.forClass(DynamoDBQueryExpression.class);
    when(mapper.queryPage(eq(DynamoUploadSchema.class), mapperQueryCaptor.capture())).thenReturn(queryResultPage);
    // execute
    UploadSchema daoOutputSchema = dao.getUploadSchemaLatestRevisionById(TEST_APP_ID, SCHEMA_ID);
    // validate query
    DynamoDBQueryExpression<DynamoUploadSchema> mapperQuery = mapperQueryCaptor.getValue();
    assertFalse(mapperQuery.isScanIndexForward());
    assertEquals(mapperQuery.getLimit().intValue(), 1);
    assertEquals(mapperQuery.getHashKeyValues().getAppId(), TEST_APP_ID);
    assertEquals(mapperQuery.getHashKeyValues().getSchemaId(), SCHEMA_ID);
    // Verify DAO output
    assertSame(daoOutputSchema, mapperOutputSchema);
}
Also used : DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) QueryResultPage(com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage) Test(org.testng.annotations.Test)

Example 5 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoUploadSchemaDaoTest method getLatestByIdNoResult.

@Test
public void getLatestByIdNoResult() {
    // mock DDB mapper
    List<DynamoUploadSchema> mapperOutputSchemaList = ImmutableList.of();
    QueryResultPage<DynamoUploadSchema> queryResultPage = new QueryResultPage<>();
    queryResultPage.setResults(mapperOutputSchemaList);
    when(mapper.queryPage(eq(DynamoUploadSchema.class), any())).thenReturn(queryResultPage);
    // execute and validate result is null
    UploadSchema daoOutputSchema = dao.getUploadSchemaLatestRevisionById(TEST_APP_ID, SCHEMA_ID);
    assertNull(daoOutputSchema);
// query is already validated in getLatestById(). Don't bother checking again.
}
Also used : UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) QueryResultPage(com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage) Test(org.testng.annotations.Test)

Aggregations

DynamoDBQueryExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)10 Condition (com.amazonaws.services.dynamodbv2.model.Condition)8 Test (org.testng.annotations.Test)8 QueryResultPage (com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage)7 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)7 ForwardCursorPagedResourceList (org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList)5 ScheduledActivity (org.sagebionetworks.bridge.models.schedules.ScheduledActivity)4 PaginatedQueryList (com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SmsMessage (org.sagebionetworks.bridge.models.sms.SmsMessage)2 UploadSchema (org.sagebionetworks.bridge.models.upload.UploadSchema)2 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)1 Item (com.amazonaws.services.dynamodbv2.document.Item)1 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)1 RangeKeyCondition (com.amazonaws.services.dynamodbv2.document.RangeKeyCondition)1 QueryRequest (com.amazonaws.services.dynamodbv2.model.QueryRequest)1 QueryResult (com.amazonaws.services.dynamodbv2.model.QueryResult)1 DateTime (org.joda.time.DateTime)1