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 #62

Closed
wants to merge 1 commit into from
Closed
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 @@ -30,6 +30,7 @@
import org.socialsignin.spring.data.dynamodb.mapping.DefaultDynamoDBDateMarshaller;
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 @@ -63,6 +64,7 @@ public abstract class AbstractDynamoDBQueryCriteria<T, ID extends Serializable>
protected Object hashKeyPropertyValue;
protected String globalSecondaryIndexName;
protected Sort sort;
protected Pageable pageable;

public abstract boolean isApplicableForLoad();

Expand Down Expand Up @@ -111,9 +113,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 != null) {
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 @@ -631,4 +640,10 @@ public DynamoDBQueryCriteria<T, ID> withSort(Sort sort) {
return this;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,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 Serializable> extends AbstractDynamoDBQueryCreator<T, ID,T> {

private final Pageable pageable;

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

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);
}
if (pageable != null) {
criteria.withPageable(pageable);
}

return criteria.buildQuery(dynamoDBOperations);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;

import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
Expand All @@ -41,6 +42,8 @@ public DynamoDBQueryCriteria<T, ID> withSingleValueCriteria(String propertyName,

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

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

public Query<T> buildQuery(DynamoDBOperations dynamoDBOperations);

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