private ScanResult scanNextPageOfSegment(int currentSegment, boolean checkLastEvaluatedKey) { ScanRequest segmentScanRequest = parallelScanRequests.get(currentSegment); if (checkLastEvaluatedKey) { ScanResult lastScanResult = segmentScanResults.get(currentSegment); segmentScanRequest.setExclusiveStartKey(lastScanResult.getLastEvaluatedKey()); } else { segmentScanRequest.setExclusiveStartKey(null); } ScanResult scanResult = dynamo.scan(DynamoDBMapper.applyUserAgent(segmentScanRequest)); /** * Cache the scan result in segmentScanResults. * We should never try to get these scan results by calling get() on the cached future tasks. */ segmentScanResults.set(currentSegment, scanResult); /** * Update the state and notify any waiting thread. */ synchronized(segmentScanStates) { if (null == scanResult.getLastEvaluatedKey()) segmentScanStates.set(currentSegment, SegmentScanState.SegmentScanCompleted); else segmentScanStates.set(currentSegment, SegmentScanState.HasNextPage); segmentScanStates.notifyAll(); } return scanResult; }
@Override public <T> PaginatedQueryList<T> query(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config); QueryResult queryResult = db.query(applyUserAgent(queryRequest)); return new PaginatedQueryList<T>(this, clazz, db, queryRequest, queryResult, config.getPaginationLoadingStrategy(), config); }
@Override public <T> PaginatedScanList<T> scan(Class<T> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); ScanRequest scanRequest = createScanRequestFromExpression(clazz, scanExpression, config); ScanResult scanResult = db.scan(applyUserAgent(scanRequest)); return new PaginatedScanList<T>(this, clazz, db, scanRequest, scanResult, config.getPaginationLoadingStrategy(), config); }
@Override protected synchronized List<T> fetchNextPage() { scanRequest.setExclusiveStartKey(scanResult.getLastEvaluatedKey()); scanResult = dynamo.scan(DynamoDBMapper.applyUserAgent(scanRequest)); return mapper.marshallIntoObjects(mapper.toParameters( scanResult.getItems(), clazz, scanRequest.getTableName(), config)); }
@Override protected synchronized List<T> fetchNextPage() { queryRequest.setExclusiveStartKey(queryResult.getLastEvaluatedKey()); queryResult = dynamo.query(DynamoDBMapper.applyUserAgent(queryRequest)); return mapper.marshallIntoObjects(mapper.toParameters( queryResult.getItems(), clazz, queryRequest.getTableName(), config)); } }
@Override public <T> int count(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config); queryRequest.setSelect(Select.COUNT); // Count queries can also be truncated for large datasets int count = 0; QueryResult queryResult = null; do { queryResult = db.query(applyUserAgent(queryRequest)); count += queryResult.getCount(); queryRequest.setExclusiveStartKey(queryResult.getLastEvaluatedKey()); } while (queryResult.getLastEvaluatedKey() != null); return count; }
@Override public int count(Class<?> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); ScanRequest scanRequest = createScanRequestFromExpression(clazz, scanExpression, config); scanRequest.setSelect(Select.COUNT); // Count scans can also be truncated for large datasets int count = 0; ScanResult scanResult = null; do { scanResult = db.scan(applyUserAgent(scanRequest)); count += scanResult.getCount(); scanRequest.setExclusiveStartKey(scanResult.getLastEvaluatedKey()); } while (scanResult.getLastEvaluatedKey() != null); return count; }
@Override public <T extends Object> T load(T keyObject, DynamoDBMapperConfig config) { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) keyObject.getClass(); config = mergeConfig(config); final DynamoDBMapperTableModel<T> model = getTableModel(clazz, config); String tableName = getTableName(clazz, keyObject, config); GetItemRequest rq = new GetItemRequest() .withRequestMetricCollector(config.getRequestMetricCollector()); Map<String, AttributeValue> key = model.convertKey(keyObject); rq.setKey(key); rq.setTableName(tableName); rq.setConsistentRead(config.getConsistentReads() == ConsistentReads.CONSISTENT); GetItemResult item = db.getItem(applyUserAgent(rq)); Map<String, AttributeValue> itemAttributes = item.getItem(); if ( itemAttributes == null ) { return null; } T object = privateMarshallIntoObject( toParameters(itemAttributes, clazz, tableName, config)); return object; }
db.deleteItem(applyUserAgent(req));
/** * Save the item using a PutItem request. The handler will call this * method if * <ul> * <li> CLOBBER configuration is being used; * <li> OR the item contains auto-generated key value; * <li> OR an UpdateItem request has silently failed (200 response with * no affected attribute), which indicates the key-only-put scenario * that we used to handle by the keyOnlyPut(...) hack. * </ul> */ protected PutItemResult doPutItem() { Map<String, AttributeValue> attributeValues = convertToItem(getAttributeValueUpdates()); attributeValues = transformAttributes( toParameters(attributeValues, this.clazz, getTableName(), saveConfig)); PutItemRequest req = new PutItemRequest() .withTableName(getTableName()) .withItem(attributeValues) .withExpected(mergeExpectedAttributeValueConditions()) .withConditionalOperator(userProvidedConditionOperator) .withRequestMetricCollector(saveConfig.getRequestMetricCollector()); return db.putItem(applyUserAgent(req)); }
@Override public <T> QueryResultPage<T> queryPage(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config); QueryResult queryResult = db.query(applyUserAgent(queryRequest)); QueryResultPage<T> result = new QueryResultPage<T>(); List<AttributeTransformer.Parameters<T>> parameters = toParameters(queryResult.getItems(), clazz, queryRequest.getTableName(), config); result.setResults(marshallIntoObjects(parameters)); result.setLastEvaluatedKey(queryResult.getLastEvaluatedKey()); result.setCount(queryResult.getCount()); result.setScannedCount(queryResult.getScannedCount()); result.setConsumedCapacity(queryResult.getConsumedCapacity()); return result; }
@Override public <T> ScanResultPage<T> scanPage(Class<T> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); ScanRequest scanRequest = createScanRequestFromExpression(clazz, scanExpression, config); ScanResult scanResult = db.scan(applyUserAgent(scanRequest)); ScanResultPage<T> result = new ScanResultPage<T>(); List<AttributeTransformer.Parameters<T>> parameters = toParameters(scanResult.getItems(), clazz, scanRequest.getTableName(), config); result.setResults(marshallIntoObjects(parameters)); result.setLastEvaluatedKey(scanResult.getLastEvaluatedKey()); result.setCount(scanResult.getCount()); result.setScannedCount(scanResult.getScannedCount()); result.setConsumedCapacity(scanResult.getConsumedCapacity()); return result; }
/** * Save the item using a UpdateItem request. The handler will call this * method if * <ul> * <li>CLOBBER configuration is not being used; * <li>AND the item does not contain auto-generated key value; * </ul> * <p> * The ReturnedValues parameter for the UpdateItem request is set as * ALL_NEW, which means the service should return all of the attributes * of the new version of the item after the update. The handler will use * the returned attributes to detect silent failure on the server-side. */ protected UpdateItemResult doUpdateItem() { UpdateItemRequest req = new UpdateItemRequest() .withTableName(getTableName()) .withKey(getPrimaryKeyAttributeValues()) .withAttributeUpdates( transformAttributeUpdates( this.clazz, getTableName(), getPrimaryKeyAttributeValues(), getAttributeValueUpdates(), saveConfig)) .withExpected(mergeExpectedAttributeValueConditions()) .withConditionalOperator(userProvidedConditionOperator) .withReturnValues(ReturnValue.ALL_NEW) .withRequestMetricCollector(saveConfig.getRequestMetricCollector()); return db.updateItem(applyUserAgent(req)); }
/** * @param config never null */ private ScanRequest createScanRequestFromExpression(Class<?> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { ScanRequest scanRequest = new ScanRequest(); scanRequest.setTableName(getTableName(clazz, config)); scanRequest.setIndexName(scanExpression.getIndexName()); scanRequest.setScanFilter(scanExpression.getScanFilter()); scanRequest.setLimit(scanExpression.getLimit()); scanRequest.setExclusiveStartKey(scanExpression.getExclusiveStartKey()); scanRequest.setTotalSegments(scanExpression.getTotalSegments()); scanRequest.setSegment(scanExpression.getSegment()); scanRequest.setConditionalOperator(scanExpression.getConditionalOperator()); scanRequest.setFilterExpression(scanExpression.getFilterExpression()); scanRequest.setExpressionAttributeNames(scanExpression .getExpressionAttributeNames()); scanRequest.setExpressionAttributeValues(scanExpression .getExpressionAttributeValues()); scanRequest.setRequestMetricCollector(config.getRequestMetricCollector()); scanRequest.setSelect(scanExpression.getSelect()); scanRequest.setProjectionExpression(scanExpression.getProjectionExpression()); scanRequest.setReturnConsumedCapacity(scanExpression.getReturnConsumedCapacity()); scanRequest.setConsistentRead(scanExpression.isConsistentRead()); return applyUserAgent(scanRequest); }
protected <T> QueryRequest createQueryRequestFromExpression(Class<T> clazz, DynamoDBQueryExpression<T> xpress, DynamoDBMapperConfig config) { final DynamoDBMapperTableModel<T> model = getTableModel(clazz, config); QueryRequest req = new QueryRequest(); req.setConsistentRead(xpress.isConsistentRead()); req.setTableName(getTableName(clazz, xpress.getHashKeyValues(), config)); req.setIndexName(xpress.getIndexName()); req.setKeyConditionExpression(xpress.getKeyConditionExpression()); processKeyConditions(req, xpress, model); req.withScanIndexForward(xpress.isScanIndexForward()) .withLimit(xpress.getLimit()) .withExclusiveStartKey(xpress.getExclusiveStartKey()) .withQueryFilter(xpress.getQueryFilter()) .withConditionalOperator(xpress.getConditionalOperator()) .withSelect(xpress.getSelect()) .withProjectionExpression(xpress.getProjectionExpression()) .withFilterExpression(xpress.getFilterExpression()) .withExpressionAttributeNames(xpress.getExpressionAttributeNames()) .withExpressionAttributeValues(xpress.getExpressionAttributeValues()) .withReturnConsumedCapacity(xpress.getReturnConsumedCapacity()) .withRequestMetricCollector(config.getRequestMetricCollector()) ; return applyUserAgent(req); }
@Override public <T> PaginatedScanList<T> scan(Class<T> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); ScanRequest scanRequest = createScanRequestFromExpression(clazz, scanExpression, config); ScanResult scanResult = db.scan(applyUserAgent(scanRequest)); return new PaginatedScanList<T>(this, clazz, db, scanRequest, scanResult, config.getPaginationLoadingStrategy(), config); }
@Override public <T> PaginatedQueryList<T> query(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); QueryRequest queryRequest = createQueryRequestFromExpression(clazz, queryExpression, config); QueryResult queryResult = db.query(applyUserAgent(queryRequest)); return new PaginatedQueryList<T>(this, clazz, db, queryRequest, queryResult, config.getPaginationLoadingStrategy(), config); }
@Override protected synchronized List<T> fetchNextPage() { scanRequest.setExclusiveStartKey(scanResult.getLastEvaluatedKey()); scanResult = dynamo.scan(DynamoDBMapper.applyUserAgent(scanRequest)); return mapper.marshallIntoObjects(mapper.toParameters( scanResult.getItems(), clazz, scanRequest.getTableName(), config)); }
@Override protected synchronized List<T> fetchNextPage() { queryRequest.setExclusiveStartKey(queryResult.getLastEvaluatedKey()); queryResult = dynamo.query(DynamoDBMapper.applyUserAgent(queryRequest)); return mapper.marshallIntoObjects(mapper.toParameters( queryResult.getItems(), clazz, queryRequest.getTableName(), config)); } }
@Override public int count(Class<?> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); ScanRequest scanRequest = createScanRequestFromExpression(clazz, scanExpression, config); scanRequest.setSelect(Select.COUNT); // Count scans can also be truncated for large datasets int count = 0; ScanResult scanResult = null; do { scanResult = db.scan(applyUserAgent(scanRequest)); count += scanResult.getCount(); scanRequest.setExclusiveStartKey(scanResult.getLastEvaluatedKey()); } while (scanResult.getLastEvaluatedKey() != null); return count; }