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);
}
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);
}
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);
}
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));
}
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());
}
Aggregations