Skip to content

Followup: Handle new Sort.unsorted() sorting strategy #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ else if (domainClass.isAssignableFrom(source.getClass())) {
}
}

@SuppressWarnings("unchecked")
private void publishEachElement(List<?> list, Consumer<E> publishMethod) {
list.stream()
.filter(o -> domainClass.isAssignableFrom(o.getClass()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ScanExpressionCountQuery(DynamoDBOperations dynamoDBOperations, Class<T>
@Override
public Long getSingleResult() {
assertScanCountEnabled(isScanCountEnabled());
return new Long(dynamoDBOperations.count(domainClass,scanExpression));
return Long.valueOf(dynamoDBOperations.count(domainClass,scanExpression));
}

public void assertScanCountEnabled(boolean scanCountEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.socialsignin.spring.data.dynamodb.marshaller.Instant2IsoDynamoDBMarshaller;
import org.socialsignin.spring.data.dynamodb.query.Query;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.socialsignin.spring.data.dynamodb.utils.SortHandler;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
Expand All @@ -51,7 +52,7 @@
/**
* @author Michael Lavelle
*/
public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQueryCriteria<T, ID> {
public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQueryCriteria<T, ID>, SortHandler {

protected Class<T> clazz;
private DynamoDBEntityInformation<T, ID> entityInformation;
Expand All @@ -65,19 +66,10 @@ public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQu
protected Object hashKeyAttributeValue;
protected Object hashKeyPropertyValue;
protected String globalSecondaryIndexName;
protected Sort sort;
protected Sort sort = Sort.unsorted();

public abstract boolean isApplicableForLoad();

/**
* @throws UnsupportedOperationException if a {@link #sort} is initalized (non-null &amp;&amp; not {@link Sort#unsorted()}
*/
protected void ensureNoSort() throws UnsupportedOperationException {
if (sort != null && sort != Sort.unsorted()) {
throw new UnsupportedOperationException("Sort not supported for scan expressions");
}
}

protected QueryRequest buildQueryRequest(String tableName, String theIndexName, String hashKeyAttributeName,
String rangeKeyAttributeName, String rangeKeyPropertyName, List<Condition> hashKeyConditions,
List<Condition> rangeKeyConditions) {
Expand Down Expand Up @@ -119,14 +111,12 @@ protected QueryRequest buildQueryRequest(String tableName, String theIndexName,
}
}

if (sort != null) {
for (Order order : sort) {
final String sortProperty = order.getProperty();
if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
allowedSortProperties.add(sortProperty);
}
}
}
for (Order order : sort) {
final String sortProperty = order.getProperty();
if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
allowedSortProperties.add(sortProperty);
}
}

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

}
if (sort != null) {
boolean sortAlreadySet = false;
for (Order order : sort) {
if (permittedPropertyNames.contains(order.getProperty())) {
if (sortAlreadySet) {
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");

}
queryExpression.setScanIndexForward(order.getDirection().equals(Direction.ASC));
sortAlreadySet = true;
} else {
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
+ " for the criteria specified");
boolean sortAlreadySet = false;
for (Order order : sort) {
if (permittedPropertyNames.contains(order.getProperty())) {
if (sortAlreadySet) {
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");

}
queryExpression.setScanIndexForward(order.getDirection().equals(Direction.ASC));
sortAlreadySet = true;
} else {
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
+ " for the criteria specified");
}
}
}
Expand All @@ -163,25 +152,23 @@ protected void applySortIfSpecified(QueryRequest queryRequest, List<String> perm
throw new UnsupportedOperationException("Can only sort by at most a single global hash and range key");
}

if (sort != null) {
boolean sortAlreadySet = false;
for (Order order : sort) {
if (permittedPropertyNames.contains(order.getProperty())) {
if (sortAlreadySet) {
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
boolean sortAlreadySet = false;
for (Order order : sort) {
if (permittedPropertyNames.contains(order.getProperty())) {
if (sortAlreadySet) {
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");

}
if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) {
throw new UnsupportedOperationException(
"Sorting for global index queries with criteria on both hash and range not possible");
}
if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) {
throw new UnsupportedOperationException(
"Sorting for global index queries with criteria on both hash and range not possible");

}
queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC));
sortAlreadySet = true;
} else {
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
+ " for the criteria specified");
}
queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC));
sortAlreadySet = true;
} else {
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
+ " for the criteria specified");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public boolean isApplicableForQuery() {

public DynamoDBScanExpression buildScanExpression() {

ensureNoSort();
ensureNoSort(sort);

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
if (isHashKeySpecified()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public boolean isApplicableForLoad() {

public DynamoDBScanExpression buildScanExpression() {

ensureNoSort();
ensureNoSort(sort);

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
if (isHashKeySpecified()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -301,12 +302,9 @@ public DynamoDBMarshaller<?> getMarshallerForProperty(final String propertyName)

if(annotation != null) {
try {
return annotation.marshallerClass().newInstance();
} catch (InstantiationException e) {
return annotation.marshallerClass().getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.util.ReflectionUtils.MethodCallback;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void doWith(Field field) {
public T getHashKeyPropotypeEntityForHashKey(Object hashKey) {

try {
T entity = getJavaType().newInstance();
T entity = getJavaType().getDeclaredConstructor().newInstance();
if (hashKeySetterMethod != null)
{
ReflectionUtils.invokeMethod(hashKeySetterMethod, entity, hashKey);
Expand All @@ -120,9 +121,7 @@ public T getHashKeyPropotypeEntityForHashKey(Object hashKey) {
}

return entity;
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.socialsignin.spring.data.dynamodb.exception.BatchWriteException;
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.socialsignin.spring.data.dynamodb.repository.DynamoDBCrudRepository;
import org.socialsignin.spring.data.dynamodb.utils.SortHandler;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.util.Assert;
Expand All @@ -46,7 +47,8 @@
* the type of the entity's identifier
*/
public class SimpleDynamoDBCrudRepository<T, ID>
implements DynamoDBCrudRepository<T, ID> {
implements DynamoDBCrudRepository<T, ID>,
SortHandler {

protected DynamoDBEntityInformation<T, ID> entityInformation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ public SimpleDynamoDBPagingAndSortingRepository(DynamoDBEntityInformation<T, ID>

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

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

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

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
// Scan to the end of the page after the requested page
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.socialsignin.spring.data.dynamodb.utils;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
* Some helper methods to deal with {@link Sort}.
*
* @author derjust
*/
public interface SortHandler {

default void ensureNoSort(Pageable pageable) {
Sort sort = pageable.getSort();
ensureNoSort(sort);
}

/**
* @throws UnsupportedOperationException if a {@code sort} is initialized (non-null &amp;&amp; not {@link Sort#unsorted()}
*/
default void ensureNoSort(Sort sort) throws UnsupportedOperationException {
if (!Sort.unsorted().equals(sort)) {
throwUnsupportedSortOperationException();
}
}

default <T> T throwUnsupportedSortOperationException() {
throw new UnsupportedOperationException("Sorting not supported for scan expressions");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.type.AnnotationMetadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class TestAppConfig {

@Test
public void feed_test(){
PageRequest pageRequest = new PageRequest(1, 10, new Sort(Direction.DESC, "usrNo"));
PageRequest pageRequest = PageRequest.of(1, 10, new Sort(Direction.DESC, "usrNo"));
feedUserRepository.findByUsrNo(2, pageRequest); //runnable
feedUserRepository.findByUsrNoAndFeedOpenYn(2, true, pageRequest); //not runnable
}
Expand Down
Loading