Skip to content

Commit

Permalink
Read Consistency Setting
Browse files Browse the repository at this point in the history
  • Loading branch information
boostchicken committed Jan 25, 2020
1 parent 4d6371e commit 164de98
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.socialsignin.spring.data.dynamodb.repository.QueryConstants.ConsistentReadMode;
import static org.socialsignin.spring.data.dynamodb.repository.QueryConstants.QUERY_LIMIT_UNLIMITED;

@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -47,4 +47,7 @@
* Expressions</a>
*/
int limit() default QUERY_LIMIT_UNLIMITED;


ConsistentReadMode consistentReads() default ConsistentReadMode.DEFAULT;

This comment has been minimized.

Copy link
@dragneelfps

dragneelfps Jan 29, 2020

@boostchicken instead of having an enum, can't we have a boolean as the original private Boolean consistentRead; in QueryRequest is a boolean.

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
package org.socialsignin.spring.data.dynamodb.repository;

public final class QueryConstants {
public class QueryConstants {

private QueryConstants() {
}

public static final int QUERY_LIMIT_UNLIMITED = Integer.MIN_VALUE;

public enum ConsistentReadMode {
DEFAULT,
CONSISTENT,
EVENTUAL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBIdIsHashAndRangeKeyEntityInformation;
import org.springframework.data.mapping.PropertyPath;
Expand Down Expand Up @@ -47,23 +48,26 @@ public abstract class AbstractDynamoDBQueryCreator<T, ID, R>
protected final DynamoDBOperations dynamoDBOperations;
protected final Optional<String> projection;
protected final Optional<Integer> limit;
protected final QueryConstants.ConsistentReadMode consistentReads;

public AbstractDynamoDBQueryCreator(PartTree tree, DynamoDBEntityInformation<T, ID> entityMetadata,
Optional<String> projection, Optional<Integer> limitResults, DynamoDBOperations dynamoDBOperations) {
Optional<String> projection, Optional<Integer> limitResults, QueryConstants.ConsistentReadMode consistentReads, DynamoDBOperations dynamoDBOperations) {
super(tree);
this.entityMetadata = entityMetadata;
this.projection = projection;
this.limit = limitResults;
this.consistentReads = consistentReads;
this.dynamoDBOperations = dynamoDBOperations;
}

public AbstractDynamoDBQueryCreator(PartTree tree, ParameterAccessor parameterAccessor,
DynamoDBEntityInformation<T, ID> entityMetadata, Optional<String> projection,
Optional<Integer> limitResults, DynamoDBOperations dynamoDBOperations) {
DynamoDBEntityInformation<T, ID> entityMetadata, Optional<String> projection,
Optional<Integer> limitResults, QueryConstants.ConsistentReadMode consistentReads, DynamoDBOperations dynamoDBOperations) {
super(tree, parameterAccessor);
this.entityMetadata = entityMetadata;
this.projection = projection;
this.limit = limitResults;
this.consistentReads = consistentReads;
this.dynamoDBOperations = dynamoDBOperations;
}

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.Date2IsoDynamoDBMarshaller;
import org.socialsignin.spring.data.dynamodb.marshaller.Instant2IsoDynamoDBMarshaller;
import org.socialsignin.spring.data.dynamodb.query.Query;
import org.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.socialsignin.spring.data.dynamodb.utils.SortHandler;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -73,6 +74,7 @@ public abstract class AbstractDynamoDBQueryCriteria<T, ID> implements DynamoDBQu
protected Sort sort = Sort.unsorted();
protected Optional<String> projection = Optional.empty();
protected Optional<Integer> limit = Optional.empty();
protected QueryConstants.ConsistentReadMode consistentReads = QueryConstants.ConsistentReadMode.DEFAULT;

public abstract boolean isApplicableForLoad();

Expand Down Expand Up @@ -134,6 +136,17 @@ protected QueryRequest buildQueryRequest(String tableName, String theIndexName,
}

limit.ifPresent(queryRequest::setLimit);

switch (consistentReads) {
case CONSISTENT:
queryRequest.setConsistentRead(true);
break;
case EVENTUAL:
queryRequest.setConsistentRead(false);
break;
default:
break;
}
applySortIfSpecified(queryRequest, new ArrayList<>(new HashSet<>(allowedSortProperties)));
}
return queryRequest;
Expand Down Expand Up @@ -703,4 +716,10 @@ public DynamoDBQueryCriteria<T, ID> withLimit(Optional<Integer> limit) {
this.limit = limit;
return this;
}

@Override
public DynamoDBQueryCriteria<T, ID> withConsistentReads(QueryConstants.ConsistentReadMode consistentReads) {
this.consistentReads = consistentReads;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.socialsignin.spring.data.dynamodb.query.Query;
import org.socialsignin.spring.data.dynamodb.query.StaticQuery;
import org.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.ParameterAccessor;
Expand All @@ -32,13 +33,13 @@ public class DynamoDBCountQueryCreator<T, ID> extends AbstractDynamoDBQueryCreat

public DynamoDBCountQueryCreator(PartTree tree, DynamoDBEntityInformation<T, ID> entityMetadata,
DynamoDBOperations dynamoDBOperations, boolean pageQuery) {
super(tree, entityMetadata, Optional.empty(), Optional.empty(), dynamoDBOperations);
super(tree, entityMetadata, Optional.empty(), Optional.empty(), QueryConstants.ConsistentReadMode.DEFAULT, dynamoDBOperations);
this.pageQuery = pageQuery;
}

public DynamoDBCountQueryCreator(PartTree tree, ParameterAccessor parameterAccessor,
DynamoDBEntityInformation<T, ID> entityMetadata, DynamoDBOperations dynamoDBOperations, boolean pageQuery) {
super(tree, parameterAccessor, entityMetadata, Optional.empty(), Optional.empty(), dynamoDBOperations);
super(tree, parameterAccessor, entityMetadata, Optional.empty(), Optional.empty(), QueryConstants.ConsistentReadMode.DEFAULT, dynamoDBOperations);
this.pageQuery = pageQuery;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.socialsignin.spring.data.dynamodb.query.Query;
import org.socialsignin.spring.data.dynamodb.query.StaticQuery;
import org.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.ParameterAccessor;
Expand All @@ -29,9 +30,9 @@
public class DynamoDBQueryCreator<T, ID> extends AbstractDynamoDBQueryCreator<T, ID, T> {

public DynamoDBQueryCreator(PartTree tree, ParameterAccessor parameterAccessor,
DynamoDBEntityInformation<T, ID> entityMetadata, Optional<String> projection, Optional<Integer> limit,
DynamoDBEntityInformation<T, ID> entityMetadata, Optional<String> projection, Optional<Integer> limit, QueryConstants.ConsistentReadMode consistentReads,
DynamoDBOperations dynamoDBOperations) {
super(tree, parameterAccessor, entityMetadata, projection, limit, dynamoDBOperations);
super(tree, parameterAccessor, entityMetadata, projection, limit, consistentReads, dynamoDBOperations);
}

@Override
Expand All @@ -42,6 +43,7 @@ protected Query<T> complete(@Nullable DynamoDBQueryCriteria<T, ID> criteria, Sor
criteria.withSort(sort);
criteria.withProjection(projection);
criteria.withLimit(limit);
criteria.withConsistentReads(consistentReads);
return criteria.buildQuery(dynamoDBOperations);
}
}
Expand Down
Original file line number Diff line number Diff line change
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.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.springframework.data.domain.Sort;

import java.util.Optional;
Expand Down Expand Up @@ -45,6 +46,8 @@ DynamoDBQueryCriteria<T, ID> withSingleValueCriteria(String propertyName, Compar

DynamoDBQueryCriteria<T, ID> withLimit(Optional<Integer> limit);

DynamoDBQueryCriteria<T, ID> withConsistentReads(QueryConstants.ConsistentReadMode reads);

Query<T> buildQuery(DynamoDBOperations dynamoDBOperations);

Query<Long> buildCountQuery(DynamoDBOperations dynamoDBOperations, boolean pageQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.socialsignin.spring.data.dynamodb.repository.EnableScanCount;
import org.socialsignin.spring.data.dynamodb.repository.Query;
import org.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityMetadataSupport;
import org.springframework.data.projection.ProjectionFactory;
Expand All @@ -41,6 +42,7 @@ public class DynamoDBQueryMethod<T, ID> extends QueryMethod {
private final boolean scanCountEnabledForRepository;
private final Optional<String> projectionExpression;
private final Optional<Integer> limitResults;
private QueryConstants.ConsistentReadMode consistentReadMode;

public DynamoDBQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
super(method, metadata, factory);
Expand All @@ -63,9 +65,11 @@ public DynamoDBQueryMethod(Method method, RepositoryMetadata metadata, Projectio
} else {
this.limitResults = Optional.empty();
}
this.consistentReadMode = query.consistentReads();
} else {
this.projectionExpression = Optional.empty();
this.limitResults = Optional.empty();
this.consistentReadMode = QueryConstants.ConsistentReadMode.DEFAULT;
}
}

Expand Down Expand Up @@ -111,4 +115,8 @@ public Optional<String> getProjectionExpression() {
public Optional<Integer> getLimitResults() {
return this.limitResults;
}

public QueryConstants.ConsistentReadMode getConsistentReadMode() {
return this.consistentReadMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PartTreeDynamoDBQuery(DynamoDBOperations dynamoDBOperations, DynamoDBQuer

protected DynamoDBQueryCreator<T, ID> createQueryCreator(ParametersParameterAccessor accessor) {
return new DynamoDBQueryCreator<>(tree, accessor, getQueryMethod().getEntityInformation(),
getQueryMethod().getProjectionExpression(), getQueryMethod().getLimitResults(), dynamoDBOperations);
getQueryMethod().getProjectionExpression(), getQueryMethod().getLimitResults(), getQueryMethod().getConsistentReadMode(), dynamoDBOperations);
}

protected DynamoDBCountQueryCreator<T, ID> createCountQueryCreator(ParametersParameterAccessor accessor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.socialsignin.spring.data.dynamodb.repository.Query;
import org.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.springframework.data.repository.CrudRepository;

import java.time.Instant;
Expand All @@ -31,6 +32,7 @@ public interface UserRepository extends CrudRepository<User, String> {
Iterable<User> findAll();

// CRUD method using Optional
@Query(consistentReads = QueryConstants.ConsistentReadMode.CONSISTENT)
Optional<User> findById(String id);

@EnableScan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.socialsignin.spring.data.dynamodb.domain.sample.Playlist;
import org.socialsignin.spring.data.dynamodb.domain.sample.PlaylistId;
import org.socialsignin.spring.data.dynamodb.domain.sample.User;
import org.socialsignin.spring.data.dynamodb.repository.QueryConstants;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBIdIsHashAndRangeKeyEntityInformation;
import org.springframework.data.repository.query.Parameter;
Expand Down Expand Up @@ -115,6 +116,7 @@ public void setUp() {
Mockito.when(mockPlaylistEntityMetadata.getIndexRangeKeyPropertyNames()).thenReturn(new HashSet<String>());
Mockito.when(mockDynamoDBUserQueryMethod.getEntityInformation()).thenReturn(mockUserEntityMetadata);
Mockito.when(mockDynamoDBUserQueryMethod.getParameters()).thenReturn(mockParameters);
Mockito.when(mockDynamoDBUserQueryMethod.getConsistentReadMode()).thenReturn(QueryConstants.ConsistentReadMode.DEFAULT);
Mockito.when(mockDynamoDBPlaylistQueryMethod.getEntityInformation()).thenReturn(mockPlaylistEntityMetadata);
Mockito.when(mockDynamoDBPlaylistQueryMethod.getParameters()).thenReturn(mockParameters);
Mockito.when(mockUserEntityMetadata.getHashKeyPropertyName()).thenReturn("id");
Expand Down Expand Up @@ -142,6 +144,7 @@ private <T, ID extends Serializable> void setupCommonMocksForThisRepositoryMetho
Mockito.when(mockDynamoDBQueryMethod.getEntityType()).thenReturn(clazz);
Mockito.when(mockDynamoDBQueryMethod.getName()).thenReturn(repositoryMethodName);
Mockito.when(mockDynamoDBQueryMethod.getParameters()).thenReturn(mockParameters);
Mockito.when(mockDynamoDBQueryMethod.getConsistentReadMode()).thenReturn(QueryConstants.ConsistentReadMode.DEFAULT);
Mockito.when(mockParameters.getBindableParameters()).thenReturn(mockParameters);
Mockito.when(mockParameters.getNumberOfParameters()).thenReturn(numberOfParameters);
// Mockito.when(mockDynamoDBQueryMethod.getReturnedObjectType()).thenReturn(clazz);
Expand Down

0 comments on commit 164de98

Please sign in to comment.