Skip to content

Commit

Permalink
Release: 5.2.2 (#14)
Browse files Browse the repository at this point in the history
* Read Consistency Setting

* #8

Custom Mapper Spec

* #8

Custom Mapper CDI fix

* Fix symlink to CONTRIBUTING.md (#13)

* #8 Don't use autowired or constructor injection with FactoryBeans (#12)

* [maven-release-plugin] prepare release v5.2.2

* [maven-release-plugin] prepare for next development iteration

Co-authored-by: Christian Frommeyer <frommeyerc@googlemail.com>
  • Loading branch information
boostchicken and frommeyerc authored Feb 25, 2020
1 parent 4d6371e commit 8d241ab
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.boostchicken</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>5.2.2-SNAPSHOT</version>
<version>5.2.3-SNAPSHOT</version>
<name>Spring Data DynamoDB</name>
<inceptionYear>2018</inceptionYear>

Expand Down
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;
}
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 @@ -46,6 +46,8 @@ class DynamoDBRepositoryBean<T> extends CdiRepositoryBean<T> {

private final Bean<DynamoDBOperations> dynamoDBOperationsBean;

private final Bean<DynamoDBMapper> dynamoDBMapperBean;

/**
* Constructs a {@link DynamoDBRepositoryBean}.
*
Expand All @@ -62,7 +64,7 @@ class DynamoDBRepositoryBean<T> extends CdiRepositoryBean<T> {
*/
DynamoDBRepositoryBean(BeanManager beanManager, Bean<AmazonDynamoDB> amazonDynamoDBBean,
Bean<DynamoDBMapperConfig> dynamoDBMapperConfigBean, Bean<DynamoDBOperations> dynamoDBOperationsBean,
Set<Annotation> qualifiers, Class<T> repositoryType) {
Bean<DynamoDBMapper> dynamoDBMapperBean, Set<Annotation> qualifiers, Class<T> repositoryType) {

super(qualifiers, repositoryType, beanManager);
if (dynamoDBOperationsBean == null) {
Expand All @@ -77,6 +79,7 @@ class DynamoDBRepositoryBean<T> extends CdiRepositoryBean<T> {
this.amazonDynamoDBBean = amazonDynamoDBBean;
this.dynamoDBMapperConfigBean = dynamoDBMapperConfigBean;
this.dynamoDBOperationsBean = dynamoDBOperationsBean;
this.dynamoDBMapperBean = dynamoDBMapperBean;
}

/*
Expand All @@ -97,15 +100,20 @@ protected T create(CreationalContext<T> creationalContext, Class<T> repositoryTy
? null
: getDependencyInstance(dynamoDBMapperConfigBean, DynamoDBMapperConfig.class);

DynamoDBMapper dynamoDBMapper = dynamoDBMapperBean == null
? null
: getDependencyInstance(dynamoDBMapperBean, DynamoDBMapper.class);

DynamoDBOperations dynamoDBOperations = dynamoDBOperationsBean == null
? null
: getDependencyInstance(dynamoDBOperationsBean, DynamoDBOperations.class);

if (dynamoDBMapperConfig == null) {
dynamoDBMapperConfig = DynamoDBMapperConfig.DEFAULT;
}
DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB, dynamoDBMapperConfig);

if(dynamoDBMapper == null) {
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB, dynamoDBMapperConfig);
}
if (dynamoDBOperations == null) {
dynamoDBOperations = new DynamoDBTemplate(amazonDynamoDB, dynamoDBMapper, dynamoDBMapperConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class DynamoDBRepositoryExtension extends CdiRepositoryExtensionSupport {

private final Map<Set<Annotation>, Bean<DynamoDBMapperConfig>> dbMapperConfigs = new HashMap<Set<Annotation>, Bean<DynamoDBMapperConfig>>();

private final Map<Set<Annotation>, Bean<DynamoDBMapper>> dbMapper = new HashMap<>();
public DynamoDBRepositoryExtension() {
LOGGER.info("Activating CDI extension for Spring Data DynamoDB repositories.");
}
Expand Down Expand Up @@ -89,6 +91,14 @@ <X> void processBean(@Observes ProcessBean<X> processBean) {
dbMapperConfigs.put(qualifiers, (Bean<DynamoDBMapperConfig>) bean);
}
}
if (type instanceof Class<?> && DynamoDBMapper.class.isAssignableFrom((Class<?>) type)) {
Set<Annotation> qualifiers = new HashSet<Annotation>(bean.getQualifiers());
if (bean.isAlternative() || !dbMapper.containsKey(qualifiers)) {
LOGGER.debug("Discovered '{}' with qualifiers {}.", DynamoDBMapper.class.getName(),
qualifiers);
dbMapper.put(qualifiers, (Bean<DynamoDBMapper>) bean);
}
}
}
}

Expand Down Expand Up @@ -138,16 +148,17 @@ private <T> Bean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation
// qualifiers of the repository.
Bean<DynamoDBMapperConfig> dynamoDBMapperConfigBean = dbMapperConfigs.get(qualifiers);

Bean<DynamoDBOperations> dynamoDBOperationsBean = dynamoDBOperationss.get(qualifiers);
if (amazonDynamoDBBean == null) {
throw new UnsatisfiedResolutionException(
String.format("Unable to resolve a bean for '%s' with qualifiers %s.",
AmazonDynamoDBClient.class.getName(), qualifiers));
}

Bean<DynamoDBOperations> dynamoDBOperationsBean = dynamoDBOperationss.get(qualifiers);
Bean<DynamoDBMapper> dynamoDBMapperBean = dbMapper.get(qualifiers);

// Construct and return the repository bean.
return new DynamoDBRepositoryBean<T>(beanManager, amazonDynamoDBBean, dynamoDBMapperConfigBean,
dynamoDBOperationsBean, qualifiers, repositoryType);
dynamoDBOperationsBean, dynamoDBMapperBean, qualifiers, repositoryType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,23 @@
*/
package org.socialsignin.spring.data.dynamodb.repository.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;

public class DynamoDBMapperFactory implements FactoryBean<DynamoDBMapper> {

private final AmazonDynamoDB amazonDynamoDB;
private final DynamoDBMapperConfig dynamoDBMapperConfig;

@Autowired
public DynamoDBMapperFactory(AmazonDynamoDB amazonDynamoDB, DynamoDBMapperConfig dynamoDBMapperConfig) {
this.amazonDynamoDB = amazonDynamoDB;
this.dynamoDBMapperConfig = dynamoDBMapperConfig;
}
public class DynamoDBMapperFactory implements FactoryBean<DynamoDBMapper>, BeanFactoryAware {

private BeanFactory beanFactory;

@Override
public DynamoDBMapper getObject() throws Exception {
AmazonDynamoDB amazonDynamoDB = beanFactory.getBean(AmazonDynamoDB.class);
DynamoDBMapperConfig dynamoDBMapperConfig = beanFactory.getBean(DynamoDBMapperConfig.class);
return new DynamoDBMapper(amazonDynamoDB, dynamoDBMapperConfig);
}

Expand All @@ -43,4 +40,9 @@ public Class<?> getObjectType() {
return DynamoDBMapper.class;
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void registerBeansForRoot(BeanDefinitionRegistry registry,
this.registry = registry;

this.dynamoDBMapperConfigName = getBeanNameWithModulePrefix("DynamoDBMapperConfig");
Optional dynamoDBMapperConfigRef = configurationSource.getAttribute("dynamoDBMapperConfigRef");
Optional<String> dynamoDBMapperConfigRef = configurationSource.getAttribute("dynamoDBMapperConfigRef");

if (!dynamoDBMapperConfigRef.isPresent()) {
BeanDefinitionBuilder dynamoDBMapperConfigBuiilder = BeanDefinitionBuilder
Expand All @@ -269,10 +269,13 @@ public void registerBeansForRoot(BeanDefinitionRegistry registry,
dynamoDBMapperConfigBuiilder.getBeanDefinition());
}

this.dynamoDBMapperName = getBeanNameWithModulePrefix("DynamoDBMapper");
BeanDefinitionBuilder dynamoDBMapperBuilder = BeanDefinitionBuilder
.genericBeanDefinition(DynamoDBMapperFactory.class);
registry.registerBeanDefinition(this.dynamoDBMapperName, dynamoDBMapperBuilder.getBeanDefinition());
Optional<String> dynamoDBMapperRef = configurationSource.getAttribute("dynamoDBMapperRef");
if(!dynamoDBMapperRef.isPresent()) {
this.dynamoDBMapperName = getBeanNameWithModulePrefix("DynamoDBMapper");
BeanDefinitionBuilder dynamoDBMapperBuilder = BeanDefinitionBuilder
.genericBeanDefinition(DynamoDBMapperFactory.class);
registry.registerBeanDefinition(this.dynamoDBMapperName, dynamoDBMapperBuilder.getBeanDefinition());
}
}

protected String getBeanNameWithModulePrefix(String baseBeanName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@
*/
String dynamoDBMapperConfigRef() default "";

/**
* Returns the
* {@link com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper }
* reference to be used
*
* @return The
* {@link com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper}
* bean name
*/
String dynamoDBMapperRef() default "";

/**
* Returns the {@link javax.validation.Validator } reference to be used for to
* validate DynamoDB entities
Expand Down
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
Loading

0 comments on commit 8d241ab

Please sign in to comment.