@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 List<EnvironmentRevision> listEnvironmentRevisions( @NonNull final EnvironmentId environmentId) throws InternalServiceException { try { return dynamoDBMapper .query( EnvironmentRevisionDDBRecord.class, new DynamoDBQueryExpression<EnvironmentRevisionDDBRecord>() .withHashKeyValues( EnvironmentRevisionDDBRecord.withHashKey( environmentId.generateAccountIdClusterEnvironmentName()))) .stream() .map(environmentMapper::toEnvironmentRevision) .collect(Collectors.toList()); } catch (final AmazonServiceException e) { throw new InternalServiceException( String.format( "Could not query environment revisions for environment %s", environmentId.toString()), e); } }
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 public <T> PaginatedQueryList<T> query(Class<T> clazz, QueryRequest queryRequest) { QueryResult queryResult = amazonDynamoDB.query(queryRequest); return new PaginatedQueryList<T>(dynamoDBMapper, clazz, amazonDynamoDB, queryRequest, queryResult, dynamoDBMapperConfig.getPaginationLoadingStrategy(), dynamoDBMapperConfig); }
@Test public void testListEnvironmentsEmptyResult() throws Exception { when(environmentDDBRecords.stream()).thenReturn(Stream.empty()); doReturn(environmentDDBRecords) .when(dynamoDBMapper) .query(eq(EnvironmentDDBRecord.class), any()); final List<Environment> result = environmentRepositoryDDB.listEnvironments(cluster, null); verify(dynamoDBMapper).query(eq(EnvironmentDDBRecord.class), any()); verify(environmentMapper, never()).toEnvironment(environmentDDBRecord); assertThat(result.size()).isEqualTo(0); }
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 public <T> PaginatedQueryList<T> query(Class<T> clazz, QueryRequest queryRequest) { QueryResult queryResult = amazonDynamoDB.query(queryRequest); return new PaginatedQueryList<T>(dynamoDBMapper, clazz, amazonDynamoDB, queryRequest, queryResult, dynamoDBMapperConfig.getPaginationLoadingStrategy(), dynamoDBMapperConfig); }
@Test public void testListEnvironmentsWithoutEnvironmentNamePrefix() throws Exception { when(environmentDDBRecords.stream()).thenReturn(Stream.of(environmentDDBRecord)); doReturn(environmentDDBRecords) .when(dynamoDBMapper) .query(eq(EnvironmentDDBRecord.class), any()); when(environmentMapper.toEnvironment(environmentDDBRecord)).thenReturn(environment); final List<Environment> result = environmentRepositoryDDB.listEnvironments(cluster, null); verify(dynamoDBMapper) .query(eq(EnvironmentDDBRecord.class), ddbQueryExpressionCaptor.capture()); verify(environmentMapper).toEnvironment(environmentDDBRecord); final EnvironmentDDBRecord queriedEnvironmentDDBRecord = ddbQueryExpressionCaptor.getValue().getHashKeyValues(); assertThat(ddbQueryExpressionCaptor.getValue().getRangeKeyConditions()).isNull(); assertThat(queriedEnvironmentDDBRecord.getAccountIdCluster()) .isEqualTo(cluster.generateAccountIdCluster()); assertThat(result.size()).isEqualTo(1); assertThat(result.get(0)).isEqualTo(environment); }
@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 List<Environment> listEnvironments( @NonNull final Cluster cluster, final String environmentNamePrefix) throws InternalServiceException { try { final DynamoDBQueryExpression<EnvironmentDDBRecord> queryExpression = new DynamoDBQueryExpression<EnvironmentDDBRecord>() .withHashKeyValues( EnvironmentDDBRecord.withHashKeys(cluster.generateAccountIdCluster())); if (environmentNamePrefix != null) { queryExpression.withRangeKeyCondition( ENVIRONMENT_NAME_RANGE_KEY, new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS(environmentNamePrefix))); } return dynamoDBMapper .query(EnvironmentDDBRecord.class, queryExpression) .stream() .map(environmentMapper::toEnvironment) .collect(Collectors.toList()); } catch (final AmazonServiceException e) { throw new InternalServiceException( String.format("Could not query environments for cluster %s", cluster.toString()), e); } }
@Test public void testListEnvironmentsWithEnvironmentNamePrefix() throws Exception { final String environmentNamePrefix = "environmentNamePrefix"; when(environmentDDBRecords.stream()).thenReturn(Stream.of(environmentDDBRecord)); doReturn(environmentDDBRecords) .when(dynamoDBMapper) .query(eq(EnvironmentDDBRecord.class), any()); when(environmentMapper.toEnvironment(environmentDDBRecord)).thenReturn(environment); final List<Environment> result = environmentRepositoryDDB.listEnvironments(cluster, environmentNamePrefix); verify(dynamoDBMapper) .query(eq(EnvironmentDDBRecord.class), ddbQueryExpressionCaptor.capture()); verify(environmentMapper).toEnvironment(environmentDDBRecord); final EnvironmentDDBRecord queriedEnvironmentDDBRecord = ddbQueryExpressionCaptor.getValue().getHashKeyValues(); final Map<String, Condition> queryConditions = ddbQueryExpressionCaptor.getValue().getRangeKeyConditions(); assertThat(queriedEnvironmentDDBRecord.getAccountIdCluster()) .isEqualTo(cluster.generateAccountIdCluster()); assertThat(queryConditions).isNotEmpty().hasSize(1); assertThat(queryConditions).containsKey(ENVIRONMENT_NAME_RANGE_KEY); assertThat(queryConditions) .containsValue( new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS(environmentNamePrefix))); assertThat(result.size()).isEqualTo(1); assertThat(result.get(0)).isEqualTo(environment); }