@@ -44,18 +44,22 @@ public AbstractDynamoDBQuery(DynamoDBOperations dynamoDBOperations, DynamoDBQuer
4444 this .dynamoDBOperations = dynamoDBOperations ;
4545 this .method = method ;
4646 }
47+
4748
4849 protected QueryExecution <T , ID > getExecution () {
49- if (method .isCollectionQuery ()) {
50+ if (method .isCollectionQuery () && ! isSingleEntityResultsRestriction () ) {
5051 return new CollectionExecution ();
5152 }
52- else if (method .isSliceQuery ()) {
53+ else if (method .isSliceQuery () && ! isSingleEntityResultsRestriction () ) {
5354 return new SlicedExecution (method .getParameters ());
54- } else if (method .isPageQuery ()) {
55+ } else if (method .isPageQuery () && ! isSingleEntityResultsRestriction () ) {
5556 return new PagedExecution (method .getParameters ());
5657 } else if (method .isModifyingQuery ()) {
5758 throw new UnsupportedOperationException ("Modifying queries not yet supported" );
58- } else {
59+ } else if (isSingleEntityResultsRestriction ()) {
60+ return new SingleEntityLimitedExecution ();
61+ }
62+ else {
5963 return new SingleEntityExecution ();
6064 }
6165 }
@@ -64,6 +68,10 @@ else if (method.isSliceQuery()) {
6468 protected abstract Query <Long > doCreateCountQuery (Object [] values ,boolean pageQuery );
6569 protected abstract boolean isCountQuery ();
6670
71+ protected abstract Integer getResultsRestrictionIfApplicable ();
72+ protected abstract boolean isSingleEntityResultsRestriction ();
73+
74+
6775 protected Query <T > doCreateQueryWithPermissions (Object values []) {
6876 Query <T > query = doCreateQuery (values );
6977 query .setScanEnabled (method .isScanEnabled ());
@@ -80,12 +88,29 @@ private interface QueryExecution<T, ID extends Serializable> {
8088 public Object execute (AbstractDynamoDBQuery <T , ID > query , Object [] values );
8189 }
8290
91+
8392 class CollectionExecution implements QueryExecution <T , ID > {
8493
94+
95+
8596 @ Override
8697 public Object execute (AbstractDynamoDBQuery <T , ID > dynamoDBQuery , Object [] values ) {
8798 Query <T > query = dynamoDBQuery .doCreateQueryWithPermissions (values );
88- return query .getResultList ();
99+ if (getResultsRestrictionIfApplicable () != null )
100+ {
101+ return restrictMaxResultsIfNecessary (query .getResultList ().iterator ());
102+ }
103+ else return query .getResultList ();
104+ }
105+
106+ private List <T > restrictMaxResultsIfNecessary (Iterator <T > iterator ) {
107+ int processed = 0 ;
108+ List <T > resultsPage = new ArrayList <T >();
109+ while (iterator .hasNext () && processed < getResultsRestrictionIfApplicable ()) {
110+ resultsPage .add (iterator .next ());
111+ processed ++;
112+ }
113+ return resultsPage ;
89114 }
90115
91116 }
@@ -112,10 +137,11 @@ private int scanThroughResults(Iterator<T> iterator, int resultsToScan) {
112137 return processed ;
113138 }
114139
115- private List <T > readPageOfResults (Iterator <T > iterator , int pageSize ) {
140+ private List <T > readPageOfResultsRestrictMaxResultsIfNecessary (Iterator <T > iterator , int pageSize ) {
116141 int processed = 0 ;
142+ int toProcess = getResultsRestrictionIfApplicable () != null ? Math .min (pageSize ,getResultsRestrictionIfApplicable ()) : pageSize ;
117143 List <T > resultsPage = new ArrayList <T >();
118- while (iterator .hasNext () && processed < pageSize ) {
144+ while (iterator .hasNext () && processed < toProcess ) {
119145 resultsPage .add (iterator .next ());
120146 processed ++;
121147 }
@@ -143,11 +169,18 @@ private Page<T> createPage(List<T> allResults, Pageable pageable,AbstractDynamoD
143169 if (processedCount < pageable .getOffset ())
144170 return new PageImpl <T >(new ArrayList <T >());
145171 }
146- List <T > results = readPageOfResults (iterator , pageable .getPageSize ());
172+ List <T > results = readPageOfResultsRestrictMaxResultsIfNecessary (iterator , pageable .getPageSize ());
173+
147174
148175 Query <Long > countQuery = dynamoDBQuery .doCreateCountQueryWithPermissions (values ,true );
176+ long count = countQuery .getSingleResult ();
177+
178+ if (getResultsRestrictionIfApplicable () != null )
179+ {
180+ count = Math .min (count ,getResultsRestrictionIfApplicable ());
181+ }
149182
150- return new PageImpl <T >(results , pageable , countQuery . getSingleResult () );
183+ return new PageImpl <T >(results , pageable , count );
151184
152185 }
153186 }
@@ -170,10 +203,12 @@ private int scanThroughResults(Iterator<T> iterator, int resultsToScan) {
170203 return processed ;
171204 }
172205
173- private List <T > readPageOfResults (Iterator <T > iterator , int pageSize ) {
206+ private List <T > readPageOfResultsRestrictMaxResultsIfNecessary (Iterator <T > iterator , int pageSize ) {
174207 int processed = 0 ;
208+ int toProcess = getResultsRestrictionIfApplicable () != null ? Math .min (pageSize ,getResultsRestrictionIfApplicable ()) : pageSize ;
209+
175210 List <T > resultsPage = new ArrayList <T >();
176- while (iterator .hasNext () && processed < pageSize ) {
211+ while (iterator .hasNext () && processed < toProcess ) {
177212 resultsPage .add (iterator .next ());
178213 processed ++;
179214 }
@@ -199,9 +234,10 @@ private Slice<T> createSlice(List<T> allResults, Pageable pageable) {
199234 if (processedCount < pageable .getOffset ())
200235 return new SliceImpl <T >(new ArrayList <T >());
201236 }
202- List <T > results = readPageOfResults (iterator , pageable .getPageSize ());
237+ List <T > results = readPageOfResultsRestrictMaxResultsIfNecessary (iterator , pageable .getPageSize ());
203238 // Scan ahead to retrieve the next page count
204239 boolean hasMoreResults = scanThroughResults (iterator , 1 ) > 0 ;
240+ if (getResultsRestrictionIfApplicable () != null && getResultsRestrictionIfApplicable ().intValue () <= results .size ()) hasMoreResults = false ;
205241 return new SliceImpl <T >(results , pageable , hasMoreResults );
206242 }
207243 }
@@ -221,6 +257,24 @@ public Object execute(AbstractDynamoDBQuery<T, ID> dynamoDBQuery, Object[] value
221257
222258 }
223259 }
260+
261+ class SingleEntityLimitedExecution implements QueryExecution <T , ID > {
262+
263+ @ Override
264+ public Object execute (AbstractDynamoDBQuery <T , ID > dynamoDBQuery , Object [] values ) {
265+ if (isCountQuery ())
266+ {
267+ return dynamoDBQuery .doCreateCountQueryWithPermissions (values ,false ).getSingleResult ();
268+ }
269+ else
270+ {
271+ List <T > resultList = dynamoDBQuery .doCreateQueryWithPermissions (values ).getResultList ();
272+ return resultList .size () == 0 ? null : resultList .get (0 );
273+
274+ }
275+
276+ }
277+ }
224278
225279 /*
226280 * (non-Javadoc)
0 commit comments