Search in sources :

Example 11 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition 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 12 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition in project BridgeServer2 by Sage-Bionetworks.

the class DynamoSurveyDaoTest method getSurveyAllVersionsExcludeDeletedByIdentifier.

@Test
public void getSurveyAllVersionsExcludeDeletedByIdentifier() {
    // Survey 1A
    DynamoSurvey survey1A = new DynamoSurvey();
    survey1A.setGuid(GUID);
    survey1A.setIdentifier(SURVEY_ID);
    survey1A.setCreatedOn(CREATED_ON);
    // Survey 1B
    DynamoSurvey survey1B = new DynamoSurvey();
    survey1B.setGuid(GUID);
    survey1B.setIdentifier(SURVEY_ID);
    survey1B.setCreatedOn(CREATED_ON - 2000L);
    mockSurveyMapper(survey1B, survey1A);
    mockSurveyElementMapper();
    List<Survey> results = dao.getSurveyAllVersions(TEST_APP_ID, SURVEY_IDENTIFIER, false);
    assertEquals(results, ImmutableList.of(survey1A, survey1B));
    verify(mockSurveyMapper).queryPage(eq(DynamoSurvey.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoSurvey> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getAppId(), TEST_APP_ID);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("identifier");
    assertEquals(rangeKeyCondition.getComparisonOperator(), EQ.name());
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getS(), SURVEY_ID);
    assertEquals(query.getQueryFilter().size(), 1);
    verifyIsDeletedQueryCondition(query);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) Survey(org.sagebionetworks.bridge.models.surveys.Survey) Test(org.testng.annotations.Test)

Example 13 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition in project BridgeServer2 by Sage-Bionetworks.

the class DynamoScheduledActivityDao method getActivityHistoryV3.

/**
 * {@inheritDoc}
 */
@Override
public ForwardCursorPagedResourceList<ScheduledActivity> getActivityHistoryV3(final String healthCode, final ActivityType activityType, final String referentGuid, final DateTime scheduledOnStart, final DateTime scheduledOnEnd, final String offsetKey, final int pageSize) {
    checkNotNull(healthCode);
    checkNotNull(activityType);
    checkNotNull(referentGuid);
    checkNotNull(scheduledOnStart);
    checkNotNull(scheduledOnEnd);
    int pageSizeWithIndicator = pageSize + 1;
    String start = createReferentGuidIndex(activityType, referentGuid, scheduledOnStart.toLocalDateTime().toString());
    String end = createReferentGuidIndex(activityType, referentGuid, scheduledOnEnd.toLocalDateTime().toString());
    // GSIs don't support offset keys, but we can simulate by setting lower-bound range key to the page boundary
    if (offsetKey != null) {
        String[] components = offsetKey.split(":", 2);
        start = createReferentGuidIndex(activityType, referentGuid, components[1]);
    }
    RangeKeyCondition dateCondition = new RangeKeyCondition(REFERENT_GUID).between(start, end);
    List<DynamoScheduledActivity> itemsToLoad = query(healthCode, offsetKey, pageSizeWithIndicator, dateCondition);
    // has other issues.
    if (offsetKey != null) {
        if (!containsIndicatorRecord(itemsToLoad, pageSizeWithIndicator, offsetKey)) {
            int largerPageSize = pageSizeWithIndicator + API_MAXIMUM_PAGE_SIZE;
            itemsToLoad = query(healthCode, offsetKey, largerPageSize, dateCondition);
        }
        if (indexOfIndicator(itemsToLoad, offsetKey) == -1) {
            throw new BadRequestException(INVALID_KEY_MSG + offsetKey);
        }
    }
    // Truncate from index of indicator record, to pageSizeWithIndicator number of records
    itemsToLoad = truncateListToStartAtIndicatorRecord(itemsToLoad, offsetKey);
    itemsToLoad = itemsToLoad.subList(0, Math.min(itemsToLoad.size(), pageSizeWithIndicator));
    // Load the full items
    Map<String, List<Object>> resultMap = mapper.batchLoad(itemsToLoad);
    List<ScheduledActivity> results = Lists.newArrayListWithCapacity(itemsToLoad.size());
    for (List<Object> list : resultMap.values()) {
        for (Object oneResult : list) {
            ScheduledActivity activity = (ScheduledActivity) oneResult;
            activity.setTimeZone(scheduledOnStart.getZone());
            results.add(activity);
        }
    }
    results.sort(ScheduledActivity::compareByReferentGuidThenGuid);
    // determine the offset key
    String nextPageOffsetKey = null;
    if (results.size() == pageSizeWithIndicator) {
        nextPageOffsetKey = Iterables.getLast(results).getGuid();
    }
    results = results.subList(0, Math.min(results.size(), pageSize));
    return new ForwardCursorPagedResourceList<ScheduledActivity>(results, nextPageOffsetKey).withRequestParam(ResourceList.OFFSET_KEY, offsetKey).withRequestParam(ResourceList.PAGE_SIZE, pageSize).withRequestParam(ResourceList.SCHEDULED_ON_START, scheduledOnStart).withRequestParam(ResourceList.SCHEDULED_ON_END, scheduledOnEnd);
}
Also used : ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) RangeKeyCondition(com.amazonaws.services.dynamodbv2.document.RangeKeyCondition) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) ResourceList(org.sagebionetworks.bridge.models.ResourceList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList)

Example 14 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition in project BridgeServer2 by Sage-Bionetworks.

the class DynamoSurveyDaoTest method getSurveyGuidForIdentifier.

@Test
public void getSurveyGuidForIdentifier() {
    DynamoSurvey survey = new DynamoSurvey();
    survey.setGuid(GUID);
    mockSurveyMapper(survey);
    String result = dao.getSurveyGuidForIdentifier(TEST_APP_ID, SURVEY_ID);
    assertEquals(result, GUID);
    verify(mockSurveyMapper).queryPage(eq(DynamoSurvey.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoSurvey> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getAppId(), TEST_APP_ID);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("identifier");
    assertEquals(rangeKeyCondition.getComparisonOperator(), EQ.name());
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getS(), SURVEY_ID);
    assertFalse(query.isConsistentRead());
    assertEquals(query.getLimit(), new Integer(1));
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) Test(org.testng.annotations.Test)

Example 15 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition in project BridgeServer2 by Sage-Bionetworks.

the class DynamoSurveyDaoTest method getSurveyMostRecentlyPublishedVersionIncludeElementsByIdentifier.

@Test
public void getSurveyMostRecentlyPublishedVersionIncludeElementsByIdentifier() {
    // Survey 1A
    DynamoSurvey survey1A = new DynamoSurvey();
    survey1A.setGuid(GUID);
    survey1A.setIdentifier(SURVEY_ID);
    survey1A.setCreatedOn(CREATED_ON);
    // Survey 1B
    DynamoSurvey survey1B = new DynamoSurvey();
    survey1B.setGuid(GUID);
    survey1B.setIdentifier(SURVEY_ID);
    survey1B.setCreatedOn(CREATED_ON - 2000L);
    mockSurveyMapper(survey1B, survey1A);
    mockSurveyElementMapper();
    when(mockSurveyElementMapper.queryPage(eq(DynamoSurveyElement.class), any())).thenReturn(mockElementResultsPage);
    when(mockElementResultsPage.getResults()).thenReturn(ImmutableList.of());
    Survey result = dao.getSurveyMostRecentlyPublishedVersion(TEST_APP_ID, SURVEY_IDENTIFIER, true);
    assertSame(result, survey1A);
    verify(mockSurveyMapper).queryPage(eq(DynamoSurvey.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoSurvey> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getAppId(), TEST_APP_ID);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("identifier");
    assertEquals(rangeKeyCondition.getComparisonOperator(), EQ.name());
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getS(), SURVEY_ID);
    assertEquals(query.getQueryFilter().size(), 2);
    verifyIsDeletedQueryCondition(query);
    verifyPublishedQueryCondition(query);
    verify(mockSurveyElementMapper).queryPage(eq(DynamoSurveyElement.class), any());
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) Survey(org.sagebionetworks.bridge.models.surveys.Survey) Test(org.testng.annotations.Test)

Aggregations

Condition (com.amazonaws.services.dynamodbv2.model.Condition)17 Test (org.testng.annotations.Test)14 DynamoDBQueryExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)10 Survey (org.sagebionetworks.bridge.models.surveys.Survey)5 Item (com.amazonaws.services.dynamodbv2.document.Item)4 RangeKeyCondition (com.amazonaws.services.dynamodbv2.document.RangeKeyCondition)4 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 HealthDataRecordEx3 (org.sagebionetworks.bridge.models.healthdata.HealthDataRecordEx3)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)3 ForwardCursorPagedResourceList (org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList)3 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)2 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)2 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)2 ImmutableList (com.google.common.collect.ImmutableList)2 HashMap (java.util.HashMap)2 ResourceList (org.sagebionetworks.bridge.models.ResourceList)2 ScheduledActivity (org.sagebionetworks.bridge.models.schedules.ScheduledActivity)2 QueryResultPage (com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage)1