Search in sources :

Example 6 with RangeKeyCondition

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

the class DynamoIndexHelperTest method mockResultsOfQuery.

public void mockResultsOfQuery(RangeKeyCondition condition) {
    // mock index
    List<Item> mockItemList = ImmutableList.of(new Item().with("key", "foo key"), new Item().with("key", "bar key"), new Item().with("key", "asdf key"), new Item().with("key", "jkl; key"));
    helper = new TestDynamoIndexHelper("test key", "test value", condition, mockItemList);
    // mock mapper result
    Map<String, List<Object>> mockMapperResultMap = new HashMap<>();
    mockMapperResultMap.put("dummy key 1", ImmutableList.<Object>of(new Thing("foo key", "foo value"), new Thing("bar key", "bar value")));
    mockMapperResultMap.put("dummy key 2", ImmutableList.<Object>of(new Thing("asdf key", "asdf value"), new Thing("jkl; key", "jkl; value")));
    // mock mapper
    DynamoDBMapper mockMapper = mock(DynamoDBMapper.class);
    arg = ArgumentCaptor.forClass(List.class);
    when(mockMapper.batchLoad(arg.capture())).thenReturn(mockMapperResultMap);
    helper.setMapper(mockMapper);
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) HashMap(java.util.HashMap) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 7 with RangeKeyCondition

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

the class DynamoScheduledActivityDao method query.

private List<DynamoScheduledActivity> query(String healthCode, String offsetKey, int querySize, RangeKeyCondition dateCondition) {
    QuerySpec spec = new QuerySpec().withScanIndexForward(true).withHashKey(HEALTH_CODE, healthCode).withMaxPageSize(querySize).withRangeKeyCondition(dateCondition);
    QueryOutcome outcome = referentIndex.query(spec);
    List<DynamoScheduledActivity> itemsToLoad = Lists.newArrayList();
    for (Item item : outcome.getItems()) {
        DynamoScheduledActivity keys = new DynamoScheduledActivity();
        keys.setGuid(item.getString(GUID));
        keys.setReferentGuid(item.getString(REFERENT_GUID));
        keys.setHealthCode(item.getString(HEALTH_CODE));
        itemsToLoad.add(keys);
    }
    itemsToLoad.sort(ScheduledActivity::compareByReferentGuidThenGuid);
    return itemsToLoad;
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 8 with RangeKeyCondition

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

the class DynamoUploadDao method getUploads.

/**
 * {@inheritDoc}
 */
@Override
public ForwardCursorPagedResourceList<Upload> getUploads(String healthCode, DateTime startTime, DateTime endTime, int pageSize, String offsetKey) {
    // Set a sane upper limit on this.
    if (pageSize < 1 || pageSize > API_MAXIMUM_PAGE_SIZE) {
        throw new BadRequestException(PAGE_SIZE_ERROR);
    }
    int sizeWithIndicatorRecord = pageSize + 1;
    long offsetTimestamp = (offsetKey == null) ? 0 : Long.parseLong(offsetKey);
    long offsetStartTime = Math.max(startTime.getMillis(), offsetTimestamp);
    RangeKeyCondition condition = new RangeKeyCondition(REQUESTED_ON).between(offsetStartTime, endTime.getMillis());
    QuerySpec spec = new QuerySpec().withHashKey(HEALTH_CODE, healthCode).withMaxPageSize(sizeWithIndicatorRecord).withRangeKeyCondition(// this is not a filter, it should not require paging on our side.
    condition);
    QueryOutcome outcome = healthCodeRequestedOnIndex.query(spec);
    List<Upload> itemsToLoad = new ArrayList<>(sizeWithIndicatorRecord);
    Iterator<Item> iter = outcome.getItems().iterator();
    while (iter.hasNext() && itemsToLoad.size() < sizeWithIndicatorRecord) {
        Item item = iter.next();
        DynamoUpload2 indexKeys = BridgeObjectMapper.get().convertValue(item.asMap(), DynamoUpload2.class);
        itemsToLoad.add(indexKeys);
    }
    List<Upload> results = new ArrayList<>(sizeWithIndicatorRecord);
    Map<String, List<Object>> resultMap = mapper.batchLoad(itemsToLoad);
    for (List<Object> resultList : resultMap.values()) {
        for (Object oneResult : resultList) {
            results.add((Upload) oneResult);
        }
    }
    // Due to the return in a map, these items are not in order by requestedOn attribute, so sort them.
    results.sort(Comparator.comparing(Upload::getRequestedOn));
    String nextOffsetKey = null;
    if (results.size() > pageSize) {
        nextOffsetKey = Long.toString(Iterables.getLast(results).getRequestedOn());
    }
    int lastIndex = Math.min(pageSize, results.size());
    return new ForwardCursorPagedResourceList<>(results.subList(0, lastIndex), nextOffsetKey).withRequestParam(ResourceList.OFFSET_KEY, offsetKey).withRequestParam(ResourceList.PAGE_SIZE, pageSize).withRequestParam(ResourceList.START_TIME, startTime).withRequestParam(ResourceList.END_TIME, endTime);
}
Also used : ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) RangeKeyCondition(com.amazonaws.services.dynamodbv2.document.RangeKeyCondition) ArrayList(java.util.ArrayList) Upload(org.sagebionetworks.bridge.models.upload.Upload) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) Item(com.amazonaws.services.dynamodbv2.document.Item) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) ResourceList(org.sagebionetworks.bridge.models.ResourceList) ArrayList(java.util.ArrayList) List(java.util.List) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 9 with RangeKeyCondition

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

the class DynamoHealthDataEx3Dao method pagingHelper.

private ForwardCursorPagedResourceList<HealthDataRecordEx3> pagingHelper(DynamoDBQueryExpression<DynamoHealthDataRecordEx3> query, long createdOnStart, long createdOnEnd, int pageSize, String offsetKey) {
    // offsetKey is a long epoch milliseconds using the createdOn index. The real createdOnStart is whichever is
    // the larger of the two.
    long indexStart = createdOnStart;
    if (offsetKey != null) {
        long offsetKeyMillis;
        try {
            offsetKeyMillis = Long.parseLong(offsetKey);
        } catch (NumberFormatException ex) {
            throw new BadRequestException("Invalid offsetKey " + offsetKey);
        }
        indexStart = Math.max(createdOnStart, offsetKeyMillis);
    }
    // Range key.
    Condition rangeKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN).withAttributeValueList(new AttributeValue().withN(String.valueOf(indexStart)), new AttributeValue().withN(String.valueOf(createdOnEnd)));
    query.withRangeKeyCondition("createdOn", rangeKeyCondition);
    // Limit is pageSize+1 so we can calculate the nextOffsetKey.
    query.setLimit(pageSize + 1);
    // Can't do consistent reads with global secondary indices.
    query.setConsistentRead(false);
    // Query. Results should be sorted by createdOn, since this is the dynamo range key.
    List<DynamoHealthDataRecordEx3> dynamoRecordList = queryHelper(query);
    // Copy the list, because of generic typing reasons.
    List<HealthDataRecordEx3> recordList = ImmutableList.copyOf(dynamoRecordList);
    // Calculate the nextOffsetKey, if present. We'll know there's a next page if more than pageSize entries is
    // returned.
    String nextOffsetKey = null;
    if (recordList.size() > pageSize) {
        nextOffsetKey = String.valueOf(recordList.get(pageSize).getCreatedOn());
        recordList = recordList.subList(0, pageSize);
    }
    return new ForwardCursorPagedResourceList<>(recordList, nextOffsetKey);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) HealthDataRecordEx3(org.sagebionetworks.bridge.models.healthdata.HealthDataRecordEx3)

Example 10 with RangeKeyCondition

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

the class DynamoIndexHelper method queryKeys.

/**
 * Queries the secondary index with the specified key name and value, and an optional range key condition. Only
 * the attributes projected onto the index will be returned. (Generally, this is only the table index keys
 * and the index keys.) This is generally used to re-query the table to get the full list of results, or to
 * batch update or batch delete rows.
 *
 * @param clazz
 *         expected result class
 * @param indexKeyName
 *         index key name to query on
 * @param indexKeyValue
 *         index key value to query on
 * @param rangeKeyCondition
 *         range condition for query on range portion of key (optional)
 * @param <T>
 *         expected result type
 * @return list of key objects returned by the query
 */
public <T> List<T> queryKeys(@Nonnull Class<? extends T> clazz, @Nonnull String indexKeyName, @Nonnull Object indexKeyValue, RangeKeyCondition rangeKeyCondition) {
    // query the index
    Iterable<Item> itemIter = queryHelper(indexKeyName, indexKeyValue, rangeKeyCondition);
    // convert items to the specified class
    List<T> recordKeyList = new ArrayList<>();
    for (Item oneItem : itemIter) {
        T oneRecord = BridgeObjectMapper.get().convertValue(oneItem.asMap(), clazz);
        recordKeyList.add(oneRecord);
    }
    return recordKeyList;
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) ArrayList(java.util.ArrayList)

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