/** * Returns a non-null list of the returned items; can be empty. */ public List<Item> getItems() { return InternalUtils.toItemList(result.getItems()); }
/** * 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(); }
@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; }
public QueryPage( AmazonDynamoDB client, QuerySpec spec, QueryRequest request, int index, QueryOutcome outcome) { super(Collections.unmodifiableList( toItemList(outcome.getQueryResult().getItems())), outcome); this.client = client; this.spec = spec; this.request = request; this.index = index; final Integer max = spec.getMaxResultSize(); final QueryResult result = outcome.getQueryResult(); 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(); } }
public PaginatedQueryList( DynamoDBMapper mapper, Class<T> clazz, AmazonDynamoDB dynamo, QueryRequest queryRequest, QueryResult queryResult, PaginationLoadingStrategy paginationLoadingStrategy, DynamoDBMapperConfig config ) { super(mapper, clazz, dynamo, paginationLoadingStrategy); this.queryRequest = queryRequest; this.queryResult = queryResult; this.config = config; allResults.addAll(mapper.marshallIntoObjects( mapper.toParameters( queryResult.getItems(), clazz, queryRequest.getTableName(), config))); // If the results should be eagerly loaded at once if (paginationLoadingStrategy == PaginationLoadingStrategy.EAGER_LOADING) { loadAllResults(); } }
@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> 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; }
/** * <p> * An array of item attributes that match the query criteria. Each element * in this array consists of an attribute name and the value for that * attribute. * </p> * <p> * Returns a reference to this object so that method calls can be chained * together. * * @param items <p> * An array of item attributes that match the query criteria. * Each element in this array consists of an attribute name and * the value for that attribute. * </p> * @return A reference to this updated object so that method calls can be * chained together. */ public QueryResult withItems(java.util.Map<String, AttributeValue>... items) { if (getItems() == null) { this.items = new java.util.ArrayList<java.util.Map<String, AttributeValue>>( items.length); } for (java.util.Map<String, AttributeValue> value : items) { this.items.add(value); } return this; }
@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; }
/** * 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 Map<String, List<SchemaField>> getCollections(String project) { QueryResult query = dynamoDBClient.query(new QueryRequest() .withTableName(tableConfig.getTableName()) .withKeyConditions(ImmutableMap.of("project", new Condition() .withComparisonOperator(EQ) .withAttributeValueList(new AttributeValue(project))))); Map<String, List<SchemaField>> builder = new HashMap(); for (Map<String, AttributeValue> entry : query.getItems()) { if (entry.get("id").getS().equals("|")) { continue; } builder.computeIfAbsent(entry.get("collection").getS(), (k) -> new ArrayList<>()).add(new SchemaField( entry.get("name").getS(), FieldType.valueOf(entry.get("type").getS()))); } return builder; }
@Override public List<SchemaField> getCollection(String project, String collection) { QueryResult query = dynamoDBClient.query(new QueryRequest() .withTableName(tableConfig.getTableName()) .withKeyConditions(ImmutableMap.of("project", new Condition().withComparisonOperator(EQ).withAttributeValueList(new AttributeValue(project)))) .withQueryFilter( ImmutableMap.of("collection", new Condition().withComparisonOperator(EQ) .withAttributeValueList(new AttributeValue(collection))))); return query.getItems().stream() .filter(e -> !e.get("id").getS().equals("|")) .map(e -> new SchemaField(e.get("name").getS(), FieldType.valueOf(e.get("type").getS()))) .collect(Collectors.toList()); }
List<Map<String, AttributeValue>> scan = dynamoDBClient.query(scanRequest).getItems();
queryResult.getItems(), clazz, queryRequest.getTableName(),
@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 void deleteProject(String project) { dynamoDBClient.deleteItem(new DeleteItemRequest() .withTableName(tableConfig.getTableName()) .withKey( new AbstractMap.SimpleEntry<>("project", new AttributeValue(project)), new AbstractMap.SimpleEntry<>("id", new AttributeValue("|")))); for (int i = 0; i < 100; i++) { List<Map<String, AttributeValue>> items = dynamoDBClient.query(new QueryRequest() .withTableName(tableConfig.getTableName()) .withKeyConditions(ImmutableMap.of("project", new Condition().withComparisonOperator(EQ) .withAttributeValueList(new AttributeValue(project))))).getItems(); if (items == null || items.isEmpty()) { return; } for (Map<String, AttributeValue> item : items) { dynamoDBClient.deleteItem(new DeleteItemRequest() .withTableName(tableConfig.getTableName()) .withKey( new AbstractMap.SimpleEntry<>("project", new AttributeValue(project)), new AbstractMap.SimpleEntry<>("id", new AttributeValue(item.get("id").getS())))); } } throw new RakamException("Unable to delete project", HttpResponseStatus.INTERNAL_SERVER_ERROR); }
for (final Map<String, AttributeValue> item : result.getItems()) { final Document doc = Document.fromAttributeMap(item); returnValue.add(doc);
/** * 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; }