Search in sources :

Example 6 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project aws-sdk-android by aws-amplify.

the class DynamoDBMapper method queryPage.

/**
 * Queries an Amazon DynamoDB table and returns a single page of matching
 * results. The table to query is determined by looking at the annotations
 * on the specified class, which declares where to store the object data in
 * Amazon DynamoDB, and the query expression parameter allows the caller to
 * filter results and control how the query is executed.
 *
 * @param <T> The type of the objects being returned.
 * @param clazz The class annotated with DynamoDB annotations describing how
 *            to store the object data in AWS DynamoDB.
 * @param queryExpression Details on how to run the query, including any
 *            conditions on the key values
 * @param config The configuration to use for this query, which overrides
 *            the default provided at object construction.
 * @return a single page of matching results
 */
public <T> QueryResultPage<T> queryPage(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) {
    config = mergeConfig(config);
    final QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config);
    final QueryResult scanResult = db.query(applyUserAgent(queryRequest));
    final QueryResultPage<T> result = new QueryResultPage<T>();
    final List<AttributeTransformer.Parameters<T>> parameters = toParameters(scanResult.getItems(), clazz, queryRequest.getTableName(), config);
    result.setResults(marshallIntoObjects(parameters));
    result.setLastEvaluatedKey(scanResult.getLastEvaluatedKey());
    return result;
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest)

Example 7 with QueryResultPage

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

the class DynamoScheduledActivityDao method getActivityHistoryV2.

@Override
public ForwardCursorPagedResourceList<ScheduledActivity> getActivityHistoryV2(String healthCode, String activityGuid, DateTime scheduledOnStart, DateTime scheduledOnEnd, String offsetKey, int pageSize) {
    checkNotNull(healthCode);
    checkNotNull(scheduledOnStart);
    checkNotNull(scheduledOnEnd);
    checkNotNull(activityGuid);
    if (pageSize < API_MINIMUM_PAGE_SIZE || pageSize > API_MAXIMUM_PAGE_SIZE) {
        throw new BadRequestException(BridgeConstants.PAGE_SIZE_ERROR);
    }
    DynamoScheduledActivity hashKey = new DynamoScheduledActivity();
    hashKey.setHealthCode(healthCode);
    String start = activityGuid + ":" + scheduledOnStart.toLocalDateTime().toString();
    String end = activityGuid + ":" + scheduledOnEnd.toLocalDateTime().toString();
    // range key is between start date and end date
    Condition dateCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN).withAttributeValueList(new AttributeValue().withS(start), new AttributeValue().withS(end));
    DynamoDBQueryExpression<DynamoScheduledActivity> query = new DynamoDBQueryExpression<DynamoScheduledActivity>().withHashKeyValues(hashKey).withLimit(pageSize).withScanIndexForward(false).withRangeKeyCondition(GUID, dateCondition);
    if (offsetKey != null) {
        Map<String, AttributeValue> map = Maps.newHashMap();
        map.put(HEALTH_CODE, new AttributeValue(healthCode));
        map.put(GUID, new AttributeValue(activityGuid + ":" + offsetKey));
        query.withExclusiveStartKey(map);
    }
    QueryResultPage<DynamoScheduledActivity> queryResult = mapper.queryPage(DynamoScheduledActivity.class, query);
    List<ScheduledActivity> activities = Lists.newArrayListWithCapacity(queryResult.getResults().size());
    for (DynamoScheduledActivity act : queryResult.getResults()) {
        act.setTimeZone(scheduledOnStart.getZone());
        activities.add((ScheduledActivity) act);
    }
    String nextPageOffsetKey = null;
    if (queryResult.getLastEvaluatedKey() != null) {
        nextPageOffsetKey = queryResult.getLastEvaluatedKey().get(GUID).getS().split(":", 2)[1];
    }
    return new ForwardCursorPagedResourceList<ScheduledActivity>(activities, 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 : Condition(com.amazonaws.services.dynamodbv2.model.Condition) RangeKeyCondition(com.amazonaws.services.dynamodbv2.document.RangeKeyCondition) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity)

Example 8 with QueryResultPage

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

the class DynamoReportDataDao method getReportDataV4.

/**
 * Query for report records within a range of DateTime values, using the indicated page size and offset key.
 * The report's date field will be returned using the timezone provided in the startTime/endTime parameters,
 * but all searches are done against the values in UTC time so they remain accurate when a user switches
 * time zones.
 */
@Override
public ForwardCursorPagedResourceList<ReportData> getReportDataV4(final ReportDataKey key, final DateTime startTime, final DateTime endTime, final String offsetKey, final int pageSize) {
    checkNotNull(key);
    checkNotNull(startTime);
    checkNotNull(endTime);
    int pageSizeWithIndicatorRecord = pageSize + 1;
    DynamoReportData hashKey = new DynamoReportData();
    hashKey.setKey(key.getKeyString());
    AttributeValue start = new AttributeValue().withS(startTime.withZone(DateTimeZone.UTC).toString());
    AttributeValue end = new AttributeValue().withS(endTime.withZone(DateTimeZone.UTC).toString());
    // The offsetKey should be in UTC
    if (offsetKey != null) {
        start = new AttributeValue().withS(offsetKey);
    }
    Condition dateCondition = new Condition().withComparisonOperator(BETWEEN).withAttributeValueList(start, end);
    DynamoDBQueryExpression<DynamoReportData> query = new DynamoDBQueryExpression<DynamoReportData>().withHashKeyValues(hashKey).withRangeKeyCondition("date", dateCondition).withLimit(pageSizeWithIndicatorRecord);
    QueryResultPage<DynamoReportData> page = mapper.queryPage(DynamoReportData.class, query);
    List<ReportData> list = Lists.newArrayListWithCapacity(pageSizeWithIndicatorRecord);
    for (int i = 0, len = page.getResults().size(); i < len; i++) {
        ReportData oneReport = page.getResults().get(i);
        DateTime dateTime = oneReport.getDateTime();
        if (dateTime != null) {
            oneReport.setDateTime(dateTime.withZone(startTime.getZone()));
        }
        list.add(i, oneReport);
    }
    String nextPageOffsetKey = null;
    if (list.size() == pageSizeWithIndicatorRecord) {
        nextPageOffsetKey = Iterables.getLast(list).getDate();
    }
    int limit = Math.min(list.size(), pageSize);
    return new ForwardCursorPagedResourceList<ReportData>(list.subList(0, limit), nextPageOffsetKey, true).withRequestParam(PAGE_SIZE, pageSize).withRequestParam(OFFSET_KEY, offsetKey).withRequestParam(START_TIME, startTime).withRequestParam(END_TIME, endTime);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ReportData(org.sagebionetworks.bridge.models.reports.ReportData) DateTime(org.joda.time.DateTime)

Example 9 with QueryResultPage

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

the class DynamoSmsMessageDaoTest method getMostRecentMessage.

@Test
public void getMostRecentMessage() {
    // Mock mapper.
    DynamoSmsMessage mapperOutput = new DynamoSmsMessage();
    QueryResultPage<DynamoSmsMessage> resultPage = new QueryResultPage<>();
    resultPage.setResults(ImmutableList.of(mapperOutput));
    when(mockMapper.queryPage(eq(DynamoSmsMessage.class), any())).thenReturn(resultPage);
    // Execute and validate output matches.
    SmsMessage daoOutput = dao.getMostRecentMessage(PHONE_NUMBER);
    assertSame(daoOutput, mapperOutput);
    // Verify query.
    ArgumentCaptor<DynamoDBQueryExpression> queryCaptor = ArgumentCaptor.forClass(DynamoDBQueryExpression.class);
    verify(mockMapper).queryPage(eq(DynamoSmsMessage.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoSmsMessage> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getPhoneNumber(), PHONE_NUMBER);
    assertFalse(query.isScanIndexForward());
    assertEquals(query.getLimit().intValue(), 1);
}
Also used : SmsMessage(org.sagebionetworks.bridge.models.sms.SmsMessage) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) QueryResultPage(com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage) Test(org.testng.annotations.Test)

Example 10 with QueryResultPage

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

the class DynamoSmsMessageDaoTest method getMostRecentMessage_EmptyList.

@Test
public void getMostRecentMessage_EmptyList() {
    // Mock mapper.
    QueryResultPage<DynamoSmsMessage> resultPage = new QueryResultPage<>();
    resultPage.setResults(ImmutableList.of());
    when(mockMapper.queryPage(eq(DynamoSmsMessage.class), any())).thenReturn(resultPage);
    // Execute and validate.
    SmsMessage daoOutput = dao.getMostRecentMessage(PHONE_NUMBER);
    assertNull(daoOutput);
// Query is already verified in previous test.
}
Also used : SmsMessage(org.sagebionetworks.bridge.models.sms.SmsMessage) 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