Skip to content

Commit 1f6b43d

Browse files
authored
Merge pull request #239 from derjust/bootchicken-master
Added Query and Scan limit features to save money on the new ondemand…
2 parents 9c2d8a7 + 709d9a2 commit 1f6b43d

20 files changed

+114
-28
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<spring-data.version>2.1.2.RELEASE</spring-data.version>
4242

4343
<hibernate-validator.version>6.0.9.Final</hibernate-validator.version>
44-
<aws-java-sdk.version>1.11.443</aws-java-sdk.version>
44+
<aws-java-sdk.version>1.11.515</aws-java-sdk.version>
4545
<junit.version>4.12</junit.version>
4646
<mockito.version>2.23.0</mockito.version>
4747
<cdi.version>1.2</cdi.version>

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<action dev="derjust" issue="235" type="fix" date="2019-03-10">
2828
Upgrading to 5.1.0 results in the error Bean property 'dynamoDBMapperConfig' is not writable or has an invalid setter method
2929
</action>
30+
<action dev="boostschicken" issue="226" type="add" date="2019-03-10">
31+
@Query annotation to support query limiting
32+
</action>
3033
</release>
3134
<release version="5.1.0" date="2019-01-28" description="Spring Boot 2.1 and Spring Data Lovelace-SR1 support">
3235
<action dev="boostschicken" type="add" date="2018-10-28">

src/main/java/org/socialsignin/spring/data/dynamodb/core/DynamoDBTemplate.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ public List<FailedBatch> batchDelete(Iterable<?> entities) {
180180
@Override
181181
public <T> PaginatedQueryList<T> query(Class<T> clazz, QueryRequest queryRequest) {
182182
QueryResult queryResult = amazonDynamoDB.query(queryRequest);
183+
184+
// If a limit is set, deactivate lazy loading of (matching) items after the
185+
// limit
186+
// via
187+
// com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList.atEndOfResults()
188+
if (queryRequest.getLimit() != null) {
189+
queryResult.setLastEvaluatedKey(null);
190+
}
191+
183192
return new PaginatedQueryList<T>(dynamoDBMapper, clazz, amazonDynamoDB, queryRequest, queryResult,
184193
dynamoDBMapperConfig.getPaginationLoadingStrategy(), dynamoDBMapperConfig);
185194
}

src/main/java/org/socialsignin/spring/data/dynamodb/repository/Query.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import static org.socialsignin.spring.data.dynamodb.repository.QueryConstants.QUERY_LIMIT_UNLIMITED;
25+
2426
@Retention(RetentionPolicy.RUNTIME)
2527
@Target(ElementType.METHOD)
2628
@Documented
@@ -36,4 +38,13 @@
3638
* Expressions</a>
3739
*/
3840
String fields() default "";
41+
42+
/**
43+
* An integer to limit the number of elements returned.
44+
*
45+
* @see <a href=
46+
* "https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ProjectionExpressions.html">Projection
47+
* Expressions</a>
48+
*/
49+
int limit() default QUERY_LIMIT_UNLIMITED;
3950
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.socialsignin.spring.data.dynamodb.repository;
17+
18+
public final class QueryConstants {
19+
20+
private QueryConstants() {
21+
}
22+
23+
public static final int QUERY_LIMIT_UNLIMITED = Integer.MIN_VALUE;
24+
25+
}

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/AbstractDynamoDBQueryCreator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,24 @@ public abstract class AbstractDynamoDBQueryCreator<T, ID, R>
4646
protected final DynamoDBEntityInformation<T, ID> entityMetadata;
4747
protected final DynamoDBOperations dynamoDBOperations;
4848
protected final Optional<String> projection;
49+
protected final Optional<Integer> limit;
4950

5051
public AbstractDynamoDBQueryCreator(PartTree tree, DynamoDBEntityInformation<T, ID> entityMetadata,
51-
Optional<String> projection, DynamoDBOperations dynamoDBOperations) {
52+
Optional<String> projection, Optional<Integer> limitResults, DynamoDBOperations dynamoDBOperations) {
5253
super(tree);
5354
this.entityMetadata = entityMetadata;
5455
this.projection = projection;
56+
this.limit = limitResults;
5557
this.dynamoDBOperations = dynamoDBOperations;
5658
}
5759

5860
public AbstractDynamoDBQueryCreator(PartTree tree, ParameterAccessor parameterAccessor,
5961
DynamoDBEntityInformation<T, ID> entityMetadata, Optional<String> projection,
60-
DynamoDBOperations dynamoDBOperations) {
62+
Optional<Integer> limitResults, DynamoDBOperations dynamoDBOperations) {
6163
super(tree, parameterAccessor);
6264
this.entityMetadata = entityMetadata;
6365
this.projection = projection;
66+
this.limit = limitResults;
6467
this.dynamoDBOperations = dynamoDBOperations;
6568
}
6669

@@ -89,7 +92,6 @@ protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID>
8992
}
9093

9194
switch (part.getType()) {
92-
9395
case IN :
9496
Object in = iterator.next();
9597
Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '"

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/AbstractDynamoDBQueryCriteria.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQu
7272
protected String globalSecondaryIndexName;
7373
protected Sort sort = Sort.unsorted();
7474
protected Optional<String> projection = Optional.empty();
75+
protected Optional<Integer> limit = Optional.empty();
7576

7677
public abstract boolean isApplicableForLoad();
7778

@@ -132,6 +133,7 @@ protected QueryRequest buildQueryRequest(String tableName, String theIndexName,
132133
queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
133134
}
134135

136+
limit.ifPresent(queryRequest::setLimit);
135137
applySortIfSpecified(queryRequest, new ArrayList<>(new HashSet<>(allowedSortProperties)));
136138
}
137139
return queryRequest;
@@ -695,4 +697,10 @@ public DynamoDBQueryCriteria<T, ID> withProjection(Optional<String> projection)
695697
this.projection = projection;
696698
return this;
697699
}
700+
701+
@Override
702+
public DynamoDBQueryCriteria<T, ID> withLimit(Optional<Integer> limit) {
703+
this.limit = limit;
704+
return this;
705+
}
698706
}

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/DynamoDBCountQueryCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public class DynamoDBCountQueryCreator<T, ID> extends AbstractDynamoDBQueryCreat
3232

3333
public DynamoDBCountQueryCreator(PartTree tree, DynamoDBEntityInformation<T, ID> entityMetadata,
3434
DynamoDBOperations dynamoDBOperations, boolean pageQuery) {
35-
super(tree, entityMetadata, Optional.empty(), dynamoDBOperations);
35+
super(tree, entityMetadata, Optional.empty(), Optional.empty(), dynamoDBOperations);
3636
this.pageQuery = pageQuery;
3737
}
3838

3939
public DynamoDBCountQueryCreator(PartTree tree, ParameterAccessor parameterAccessor,
4040
DynamoDBEntityInformation<T, ID> entityMetadata, DynamoDBOperations dynamoDBOperations, boolean pageQuery) {
41-
super(tree, parameterAccessor, entityMetadata, Optional.empty(), dynamoDBOperations);
41+
super(tree, parameterAccessor, entityMetadata, Optional.empty(), Optional.empty(), dynamoDBOperations);
4242
this.pageQuery = pageQuery;
4343

4444
}

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/DynamoDBEntityWithHashAndRangeKeyCriteria.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public DynamoDBQueryExpression<T> buildQueryExpression() {
190190
queryExpression.setSelect(Select.SPECIFIC_ATTRIBUTES);
191191
queryExpression.setProjectionExpression(projection.get());
192192
}
193-
193+
limit.ifPresent(queryExpression::setLimit);
194194
return queryExpression;
195195
}
196196

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/DynamoDBEntityWithHashKeyOnlyCriteria.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public DynamoDBScanExpression buildScanExpression() {
116116
scanExpression.setSelect(Select.SPECIFIC_ATTRIBUTES);
117117
scanExpression.setProjectionExpression(projection.get());
118118
}
119+
limit.ifPresent(scanExpression::setLimit);
119120
return scanExpression;
120121
}
121122

0 commit comments

Comments
 (0)