@Override protected boolean atEndOfResults() { return scanResult.getLastEvaluatedKey() == null; }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. * * @return A string representation of this object. * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{"); if (getItems() != null) sb.append("Items: ").append(getItems()).append(","); if (getCount() != null) sb.append("Count: ").append(getCount()).append(","); if (getScannedCount() != null) sb.append("ScannedCount: ").append(getScannedCount()).append(","); if (getLastEvaluatedKey() != null) sb.append("LastEvaluatedKey: ").append(getLastEvaluatedKey()).append(","); if (getConsumedCapacity() != null) sb.append("ConsumedCapacity: ").append(getConsumedCapacity()); sb.append("}"); return sb.toString(); }
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 int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getItems() == null) ? 0 : getItems().hashCode()); hashCode = prime * hashCode + ((getCount() == null) ? 0 : getCount().hashCode()); hashCode = prime * hashCode + ((getScannedCount() == null) ? 0 : getScannedCount().hashCode()); hashCode = prime * hashCode + ((getLastEvaluatedKey() == null) ? 0 : getLastEvaluatedKey().hashCode()); hashCode = prime * hashCode + ((getConsumedCapacity() == null) ? 0 : getConsumedCapacity().hashCode()); return hashCode; }
@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; }
public ScanPage( AmazonDynamoDB client, ScanSpec spec, ScanRequest request, int index, ScanOutcome outcome) { super(Collections.unmodifiableList( toItemList(outcome.getScanResult().getItems())), outcome); this.client = client; this.spec = spec; this.request = request; this.index = index; final Integer max = spec.getMaxResultSize(); final ScanResult result = outcome.getScanResult(); final List<?> ilist = result.getItems(); final int size = ilist == null ? 0 : ilist.size(); if (max != null && (index + size) > max) { this.lastEvaluatedKey = null; } else { this.lastEvaluatedKey = result.getLastEvaluatedKey(); } }
if (other.getScannedCount() != null && other.getScannedCount().equals(this.getScannedCount()) == false) return false; if (other.getLastEvaluatedKey() == null ^ this.getLastEvaluatedKey() == null) return false; if (other.getLastEvaluatedKey() != null && other.getLastEvaluatedKey().equals(this.getLastEvaluatedKey()) == false) return false; if (other.getConsumedCapacity() == null ^ this.getConsumedCapacity() == null)
@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)); }
result.add(extractResult(items)); startKey = res.getLastEvaluatedKey();
@Override protected boolean atEndOfResults() { return scanResult.getLastEvaluatedKey() == null; }
@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; }
ScanResult scanNextPageOfSegment(int currentSegment, boolean checkLastEvaluatedKey) { final ScanRequest segmentScanRequest = parallelScanRequests.get(currentSegment); if (checkLastEvaluatedKey) { final ScanResult lastScanResult = segmentScanResults.get(currentSegment); segmentScanRequest.setExclusiveStartKey(lastScanResult.getLastEvaluatedKey()); } else { segmentScanRequest.setExclusiveStartKey(null); } final 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; }
/** * Returns a string representation of this object; useful for testing and * debugging. * * @return A string representation of this object. * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{"); if (getItems() != null) sb.append("Items: " + getItems() + ","); if (getCount() != null) sb.append("Count: " + getCount() + ","); if (getScannedCount() != null) sb.append("ScannedCount: " + getScannedCount() + ","); if (getLastEvaluatedKey() != null) sb.append("LastEvaluatedKey: " + getLastEvaluatedKey() + ","); if (getConsumedCapacity() != null) sb.append("ConsumedCapacity: " + getConsumedCapacity()); sb.append("}"); return sb.toString(); }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getItems() == null) ? 0 : getItems().hashCode()); hashCode = prime * hashCode + ((getCount() == null) ? 0 : getCount().hashCode()); hashCode = prime * hashCode + ((getScannedCount() == null) ? 0 : getScannedCount().hashCode()); hashCode = prime * hashCode + ((getLastEvaluatedKey() == null) ? 0 : getLastEvaluatedKey().hashCode()); hashCode = prime * hashCode + ((getConsumedCapacity() == null) ? 0 : getConsumedCapacity().hashCode()); return hashCode; }
/** * Evaluates the specified scan expression and returns the count of matching * items, without returning any of the actual item data. * <p> * This operation will scan your entire table, and can therefore be very * expensive. Use with caution. * * @param clazz The class mapped to a DynamoDB table. * @param scanExpression The parameters for running the scan. * @param config The configuration to use for this scan, which overrides the * default provided at object construction. * @return The count of matching items, without returning any of the actual * item data. */ public int count(Class<?> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); final 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; }
&& other.getScannedCount().equals(this.getScannedCount()) == false) return false; if (other.getLastEvaluatedKey() == null ^ this.getLastEvaluatedKey() == null) return false; if (other.getLastEvaluatedKey() != null && other.getLastEvaluatedKey().equals(this.getLastEvaluatedKey()) == false) return false; if (other.getConsumedCapacity() == null ^ this.getConsumedCapacity() == null)
@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)); } }
@Test public void scanNextPageOfSegmentWithoutExclusiveStartKey() { EasyMock.reset(mockClient); ScanRequest scan1 = new ScanRequest().withTableName(TABLE); List<ScanRequest> list = new ArrayList<ScanRequest>(); list.add(scan1); Map<String, AttributeValue> lastEvaluatedKey = new HashMap<String, AttributeValue>(); lastEvaluatedKey.put("TestPrimaryKey", new AttributeValue().withS("Test1")); ScanResult result = new ScanResult().withLastEvaluatedKey(lastEvaluatedKey); ParallelScanTask testTask = new ParallelScanTask(mockClient, list); EasyMock.expect(mockClient.scan(scan1)).andReturn(result); EasyMock.replay(mockClient); testTask.scanNextPageOfSegment(0, false); EasyMock.verify(mockClient); EasyMock.reset(mockClient); Capture<ScanRequest> captureArgument = new Capture<ScanRequest>(); ScanResult mockResult = EasyMock.createMock(ScanResult.class); EasyMock.expect(mockResult.getLastEvaluatedKey()).andReturn(null).anyTimes(); EasyMock.expect(mockClient.scan(EasyMock.capture(captureArgument))).andReturn( mockResult); EasyMock.replay(mockClient, mockResult); testTask.scanNextPageOfSegment(0, false); EasyMock.verify(mockClient); assertNull(captureArgument.getValue().getExclusiveStartKey()); }
@Test public void scanNextPageOfSegmentWithExclusiveStartKey() { EasyMock.reset(mockClient); ScanRequest scan1 = new ScanRequest().withTableName(TABLE); List<ScanRequest> list = new ArrayList<ScanRequest>(); list.add(scan1); Map<String, AttributeValue> lastEvaluatedKey = new HashMap<String, AttributeValue>(); lastEvaluatedKey.put("TestPrimaryKey", new AttributeValue().withS("Test1")); ScanResult result = new ScanResult().withLastEvaluatedKey(lastEvaluatedKey); ParallelScanTask testTask = new ParallelScanTask(mockClient, list); EasyMock.expect(mockClient.scan(scan1)).andReturn(result); EasyMock.replay(mockClient); testTask.scanNextPageOfSegment(0, false); EasyMock.verify(mockClient); EasyMock.reset(mockClient); Capture<ScanRequest> captureArgument = new Capture<ScanRequest>(); ScanResult mockResult = EasyMock.createMock(ScanResult.class); EasyMock.expect(mockResult.getLastEvaluatedKey()).andReturn(null).anyTimes(); EasyMock.expect(mockClient.scan(EasyMock.capture(captureArgument))).andReturn(mockResult); EasyMock.replay(mockClient, mockResult); testTask.scanNextPageOfSegment(0, true); EasyMock.verify(mockClient); assertEquals( captureArgument.getValue().getExclusiveStartKey().get("TestPrimaryKey").getS(), "Test1"); }
/** * Scans through an Amazon DynamoDB table and returns a single page of * matching results. The table to scan is determined by looking at the * annotations on the specified class, which declares where to store the * object data in AWS DynamoDB, and the scan expression parameter allows the * caller to filter results and control how the scan 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 Amazon DynamoDB. * @param scanExpression Details on how to run the scan, including any * filters to apply to limit results. * @param config The configuration to use for this scan, which overrides the * default provided at object construction. * @return a single page of matching results. */ public <T> ScanResultPage<T> scanPage(Class<T> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) { config = mergeConfig(config); final ScanRequest scanRequest = createScanRequestFromExpression(clazz, scanExpression, config); final ScanResult scanResult = db.scan(applyUserAgent(scanRequest)); final ScanResultPage<T> result = new ScanResultPage<T>(); final List<AttributeTransformer.Parameters<T>> parameters = toParameters(scanResult.getItems(), clazz, scanRequest.getTableName(), config); result.setResults(marshallIntoObjects(parameters)); result.setLastEvaluatedKey(scanResult.getLastEvaluatedKey()); return result; }