Skip to content

Commit 68f00c5

Browse files
authored
Merge pull request #130 from derjust/Issue_99
Followup: Handle new `Sort.unsorted()` sorting strategy
2 parents 7060dc9 + 014fa26 commit 68f00c5

15 files changed

+268
-71
lines changed

src/main/java/org/socialsignin/spring/data/dynamodb/mapping/event/AbstractDynamoDBEventListener.java

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ else if (domainClass.isAssignableFrom(source.getClass())) {
108108
}
109109
}
110110

111+
@SuppressWarnings("unchecked")
111112
private void publishEachElement(List<?> list, Consumer<E> publishMethod) {
112113
list.stream()
113114
.filter(o -> domainClass.isAssignableFrom(o.getClass()))

src/main/java/org/socialsignin/spring/data/dynamodb/query/ScanExpressionCountQuery.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ScanExpressionCountQuery(DynamoDBOperations dynamoDBOperations, Class<T>
3737
@Override
3838
public Long getSingleResult() {
3939
assertScanCountEnabled(isScanCountEnabled());
40-
return new Long(dynamoDBOperations.count(domainClass,scanExpression));
40+
return Long.valueOf(dynamoDBOperations.count(domainClass,scanExpression));
4141
}
4242

4343
public void assertScanCountEnabled(boolean scanCountEnabled)

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

+34-47
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.socialsignin.spring.data.dynamodb.marshaller.Instant2IsoDynamoDBMarshaller;
3030
import org.socialsignin.spring.data.dynamodb.query.Query;
3131
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
32+
import org.socialsignin.spring.data.dynamodb.utils.SortHandler;
3233
import org.springframework.data.domain.Sort;
3334
import org.springframework.data.domain.Sort.Direction;
3435
import org.springframework.data.domain.Sort.Order;
@@ -51,7 +52,7 @@
5152
/**
5253
* @author Michael Lavelle
5354
*/
54-
public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQueryCriteria<T, ID> {
55+
public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQueryCriteria<T, ID>, SortHandler {
5556

5657
protected Class<T> clazz;
5758
private DynamoDBEntityInformation<T, ID> entityInformation;
@@ -65,19 +66,10 @@ public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQu
6566
protected Object hashKeyAttributeValue;
6667
protected Object hashKeyPropertyValue;
6768
protected String globalSecondaryIndexName;
68-
protected Sort sort;
69+
protected Sort sort = Sort.unsorted();
6970

7071
public abstract boolean isApplicableForLoad();
7172

72-
/**
73-
* @throws UnsupportedOperationException if a {@link #sort} is initalized (non-null &amp;&amp; not {@link Sort#unsorted()}
74-
*/
75-
protected void ensureNoSort() throws UnsupportedOperationException {
76-
if (sort != null && sort != Sort.unsorted()) {
77-
throw new UnsupportedOperationException("Sort not supported for scan expressions");
78-
}
79-
}
80-
8173
protected QueryRequest buildQueryRequest(String tableName, String theIndexName, String hashKeyAttributeName,
8274
String rangeKeyAttributeName, String rangeKeyPropertyName, List<Condition> hashKeyConditions,
8375
List<Condition> rangeKeyConditions) {
@@ -119,14 +111,12 @@ protected QueryRequest buildQueryRequest(String tableName, String theIndexName,
119111
}
120112
}
121113

122-
if (sort != null) {
123-
for (Order order : sort) {
124-
final String sortProperty = order.getProperty();
125-
if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
126-
allowedSortProperties.add(sortProperty);
127-
}
128-
}
129-
}
114+
for (Order order : sort) {
115+
final String sortProperty = order.getProperty();
116+
if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
117+
allowedSortProperties.add(sortProperty);
118+
}
119+
}
130120

131121
queryRequest.setKeyConditions(keyConditions);
132122
queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
@@ -140,20 +130,19 @@ protected void applySortIfSpecified(DynamoDBQueryExpression<T> queryExpression,
140130
throw new UnsupportedOperationException("Can only sort by at most a single range or index range key");
141131

142132
}
143-
if (sort != null) {
144-
boolean sortAlreadySet = false;
145-
for (Order order : sort) {
146-
if (permittedPropertyNames.contains(order.getProperty())) {
147-
if (sortAlreadySet) {
148-
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
149133

150-
}
151-
queryExpression.setScanIndexForward(order.getDirection().equals(Direction.ASC));
152-
sortAlreadySet = true;
153-
} else {
154-
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
155-
+ " for the criteria specified");
134+
boolean sortAlreadySet = false;
135+
for (Order order : sort) {
136+
if (permittedPropertyNames.contains(order.getProperty())) {
137+
if (sortAlreadySet) {
138+
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
139+
156140
}
141+
queryExpression.setScanIndexForward(order.getDirection().equals(Direction.ASC));
142+
sortAlreadySet = true;
143+
} else {
144+
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
145+
+ " for the criteria specified");
157146
}
158147
}
159148
}
@@ -163,25 +152,23 @@ protected void applySortIfSpecified(QueryRequest queryRequest, List<String> perm
163152
throw new UnsupportedOperationException("Can only sort by at most a single global hash and range key");
164153
}
165154

166-
if (sort != null) {
167-
boolean sortAlreadySet = false;
168-
for (Order order : sort) {
169-
if (permittedPropertyNames.contains(order.getProperty())) {
170-
if (sortAlreadySet) {
171-
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
155+
boolean sortAlreadySet = false;
156+
for (Order order : sort) {
157+
if (permittedPropertyNames.contains(order.getProperty())) {
158+
if (sortAlreadySet) {
159+
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
172160

173-
}
174-
if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) {
175-
throw new UnsupportedOperationException(
176-
"Sorting for global index queries with criteria on both hash and range not possible");
161+
}
162+
if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) {
163+
throw new UnsupportedOperationException(
164+
"Sorting for global index queries with criteria on both hash and range not possible");
177165

178-
}
179-
queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC));
180-
sortAlreadySet = true;
181-
} else {
182-
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
183-
+ " for the criteria specified");
184166
}
167+
queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC));
168+
sortAlreadySet = true;
169+
} else {
170+
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
171+
+ " for the criteria specified");
185172
}
186173
}
187174
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public boolean isApplicableForQuery() {
330330

331331
public DynamoDBScanExpression buildScanExpression() {
332332

333-
ensureNoSort();
333+
ensureNoSort(sort);
334334

335335
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
336336
if (isHashKeySpecified()) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public boolean isApplicableForLoad() {
9090

9191
public DynamoDBScanExpression buildScanExpression() {
9292

93-
ensureNoSort();
93+
ensureNoSort(sort);
9494

9595
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
9696
if (isHashKeySpecified()) {

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.lang.annotation.Annotation;
3434
import java.lang.reflect.Field;
35+
import java.lang.reflect.InvocationTargetException;
3536
import java.lang.reflect.Method;
3637
import java.util.ArrayList;
3738
import java.util.HashMap;
@@ -301,12 +302,9 @@ public DynamoDBMarshaller<?> getMarshallerForProperty(final String propertyName)
301302

302303
if(annotation != null) {
303304
try {
304-
return annotation.marshallerClass().newInstance();
305-
} catch (InstantiationException e) {
305+
return annotation.marshallerClass().getDeclaredConstructor().newInstance();
306+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
306307
throw new RuntimeException(e);
307-
} catch (IllegalAccessException e) {
308-
throw new RuntimeException(e);
309-
310308
}
311309
}
312310

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.util.ReflectionUtils.MethodCallback;
2525

2626
import java.lang.reflect.Field;
27+
import java.lang.reflect.InvocationTargetException;
2728
import java.lang.reflect.Method;
2829
import java.util.HashSet;
2930
import java.util.Set;
@@ -109,7 +110,7 @@ public void doWith(Field field) {
109110
public T getHashKeyPropotypeEntityForHashKey(Object hashKey) {
110111

111112
try {
112-
T entity = getJavaType().newInstance();
113+
T entity = getJavaType().getDeclaredConstructor().newInstance();
113114
if (hashKeySetterMethod != null)
114115
{
115116
ReflectionUtils.invokeMethod(hashKeySetterMethod, entity, hashKey);
@@ -120,9 +121,7 @@ public T getHashKeyPropotypeEntityForHashKey(Object hashKey) {
120121
}
121122

122123
return entity;
123-
} catch (InstantiationException e) {
124-
throw new RuntimeException(e);
125-
} catch (IllegalAccessException e) {
124+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
126125
throw new RuntimeException(e);
127126
}
128127
}

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/SimpleDynamoDBCrudRepository.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.socialsignin.spring.data.dynamodb.exception.BatchWriteException;
2323
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
2424
import org.socialsignin.spring.data.dynamodb.repository.DynamoDBCrudRepository;
25+
import org.socialsignin.spring.data.dynamodb.utils.SortHandler;
2526
import org.springframework.dao.DataAccessException;
2627
import org.springframework.dao.EmptyResultDataAccessException;
2728
import org.springframework.util.Assert;
@@ -46,7 +47,8 @@
4647
* the type of the entity's identifier
4748
*/
4849
public class SimpleDynamoDBCrudRepository<T, ID>
49-
implements DynamoDBCrudRepository<T, ID> {
50+
implements DynamoDBCrudRepository<T, ID>,
51+
SortHandler {
5052

5153
protected DynamoDBEntityInformation<T, ID> entityInformation;
5254

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/SimpleDynamoDBPagingAndSortingRepository.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,13 @@ public SimpleDynamoDBPagingAndSortingRepository(DynamoDBEntityInformation<T, ID>
6464

6565
@Override
6666
public Iterable<T> findAll(Sort sort) {
67-
throw new UnsupportedOperationException("Sorting not supported for find all scan operations");
67+
return throwUnsupportedSortOperationException();
6868
}
6969

7070
@Override
7171
public Page<T> findAll(Pageable pageable) {
7272

73-
if (pageable.getSort() != null) {
74-
throw new UnsupportedOperationException("Sorting not supported for find all scan operations");
75-
}
73+
ensureNoSort(pageable);
7674

7775
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
7876
// Scan to the end of the page after the requested page
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright © 2013 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.utils;
17+
18+
import org.springframework.data.domain.Pageable;
19+
import org.springframework.data.domain.Sort;
20+
21+
/**
22+
* Some helper methods to deal with {@link Sort}.
23+
*
24+
* @author derjust
25+
*/
26+
public interface SortHandler {
27+
28+
default void ensureNoSort(Pageable pageable) {
29+
Sort sort = pageable.getSort();
30+
ensureNoSort(sort);
31+
}
32+
33+
/**
34+
* @throws UnsupportedOperationException if a {@code sort} is initialized (non-null &amp;&amp; not {@link Sort#unsorted()}
35+
*/
36+
default void ensureNoSort(Sort sort) throws UnsupportedOperationException {
37+
if (!Sort.unsorted().equals(sort)) {
38+
throwUnsupportedSortOperationException();
39+
}
40+
}
41+
42+
default <T> T throwUnsupportedSortOperationException() {
43+
throw new UnsupportedOperationException("Sorting not supported for scan expressions");
44+
}
45+
}

src/test/java/org/socialsignin/spring/data/dynamodb/config/DynamoDBAuditingRegistrarUnitTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.junit.Test;
1919
import org.junit.runner.RunWith;
2020
import org.mockito.Mock;
21-
import org.mockito.runners.MockitoJUnitRunner;
21+
import org.mockito.junit.MockitoJUnitRunner;
2222
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2323
import org.springframework.core.type.AnnotationMetadata;
2424

src/test/java/org/socialsignin/spring/data/dynamodb/core/FeedUserIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static class TestAppConfig {
4242

4343
@Test
4444
public void feed_test(){
45-
PageRequest pageRequest = new PageRequest(1, 10, new Sort(Direction.DESC, "usrNo"));
45+
PageRequest pageRequest = PageRequest.of(1, 10, new Sort(Direction.DESC, "usrNo"));
4646
feedUserRepository.findByUsrNo(2, pageRequest); //runnable
4747
feedUserRepository.findByUsrNoAndFeedOpenYn(2, true, pageRequest); //not runnable
4848
}

0 commit comments

Comments
 (0)