Skip to content
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

Respect the pagination size for queries #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Expand Up @@ -33,6 +33,9 @@
<action dev="derjust" issue="108" type="fix" date="2018-01-14">
Opened constructor and fixed NPE in case of missing DynamoDBConfig
</action>
<action dev="derjust" issue="89" type="add" date="2018-01-17">
Added basic support for Pageable
</action>
</release>
<release version="5.0.1" date="2018-01-06" description="Maintenance release">
<action dev="derjust" issue="68" type="fix" date="2017-12-01" >
Expand Down
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.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
Expand Down Expand Up @@ -66,6 +67,7 @@ public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQu
protected Object hashKeyPropertyValue;
protected String globalSecondaryIndexName;
protected Sort sort;
protected Pageable pageable;

public abstract boolean isApplicableForLoad();

Expand Down Expand Up @@ -132,9 +134,16 @@ protected QueryRequest buildQueryRequest(String tableName, String theIndexName,
queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
applySortIfSpecified(queryRequest, new ArrayList<String>(new HashSet<String>(allowedSortProperties)));
}
applyPageableIfSpecified(queryRequest);
return queryRequest;
}

private void applyPageableIfSpecified(QueryRequest queryRequest) {
if (pageable.isPaged()) {
queryRequest.setLimit(pageable.getPageSize());
}
}

protected void applySortIfSpecified(DynamoDBQueryExpression<T> queryExpression, List<String> permittedPropertyNames) {
if (permittedPropertyNames.size() > 1) {
throw new UnsupportedOperationException("Can only sort by at most a single range or index range key");
Expand Down Expand Up @@ -688,4 +697,10 @@ public DynamoDBQueryCriteria<T, ID> withSort(Sort sort) {
return this;
}

@Override
public DynamoDBQueryCriteria<T, ID> withPageable(Pageable pageable) {
this.pageable = pageable;
return this;
}

}
Expand Up @@ -18,31 +18,38 @@
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.socialsignin.spring.data.dynamodb.query.Query;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;

public class DynamoDBQueryCreator<T,ID> extends AbstractDynamoDBQueryCreator<T, ID,T> {

private final Pageable pageable;

public DynamoDBQueryCreator(PartTree tree,
DynamoDBEntityInformation<T, ID> entityMetadata,
DynamoDBOperations dynamoDBOperations) {
super(tree, entityMetadata, dynamoDBOperations);
pageable = Pageable.unpaged();
}

public DynamoDBQueryCreator(PartTree tree,
ParameterAccessor parameterAccessor,
DynamoDBEntityInformation<T, ID> entityMetadata,
DynamoDBOperations dynamoDBOperations) {
super(tree, parameterAccessor, entityMetadata, dynamoDBOperations);
pageable = parameterAccessor.getPageable();
}

@Override
protected Query<T> complete(DynamoDBQueryCriteria<T, ID> criteria, Sort sort) {
if (sort != null) {
criteria.withSort(sort);
}

criteria.withPageable(pageable);

return criteria.buildQuery(dynamoDBOperations);

}
Expand Down
Expand Up @@ -18,6 +18,7 @@
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.socialsignin.spring.data.dynamodb.query.Query;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
Expand All @@ -37,7 +38,9 @@ DynamoDBQueryCriteria<T, ID> withSingleValueCriteria(String propertyName, Compar
DynamoDBQueryCriteria<T, ID> withPropertyBetween(String segment, Object value1, Object value2, Class<?> type);

DynamoDBQueryCriteria<T, ID> withSort(Sort sort);


DynamoDBQueryCriteria<T, ID> withPageable(Pageable pageable);

Query<T> buildQuery(DynamoDBOperations dynamoDBOperations);

Query<Long> buildCountQuery(DynamoDBOperations dynamoDBOperations, boolean pageQuery);
Expand Down