DATACASS-85 - COMPLETED

Added strict mode on verifier to relax the enforcement of equals() and
hashcode().
This commit is contained in:
David T Webb
2014-02-13 13:00:26 -05:00
parent d4849e5d27
commit 56aec13e01
5 changed files with 92 additions and 7 deletions

View File

@@ -51,7 +51,8 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
protected CassandraMappingContext mappingContext;
protected final SpelExpressionParser spelParser;
protected final StandardEvaluationContext spelContext;
protected CassandraPersistentEntityMetadataVerifier verifier = new DefaultCassandraPersistentEntityMetadataVerifier();
protected static final CassandraPersistentEntityMetadataVerifier DEFAULT_VERIFIER = new DefaultCassandraPersistentEntityMetadataVerifier();
protected CassandraPersistentEntityMetadataVerifier verifier = DEFAULT_VERIFIER;
public BasicCassandraPersistentEntity(TypeInformation<T> typeInformation) {
this(typeInformation, null);
@@ -64,6 +65,18 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
* @param typeInformation
*/
public BasicCassandraPersistentEntity(TypeInformation<T> typeInformation, CassandraMappingContext mappingContext) {
this(typeInformation, mappingContext, DEFAULT_VERIFIER);
}
/**
* Creates a new {@link BasicCassandraPersistentEntity} with the given {@link TypeInformation}. Will default the table
* name to the entity's simple type name.
*
* @param typeInformation
*/
public BasicCassandraPersistentEntity(TypeInformation<T> typeInformation, CassandraMappingContext mappingContext,
CassandraPersistentEntityMetadataVerifier verifier) {
super(typeInformation, CassandraPersistentPropertyComparator.IT);
@@ -71,6 +84,8 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
this.spelContext = new StandardEvaluationContext();
this.mappingContext = mappingContext;
setVerifier(verifier);
determineTableName();
}
@@ -156,6 +171,22 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
@Override
public void verify() throws MappingException {
super.verify();
verifier.verify(this);
if (verifier != null) {
verifier.verify(this);
}
}
/**
* @return Returns the verifier.
*/
public CassandraPersistentEntityMetadataVerifier getVerifier() {
return verifier;
}
/**
* @param verifier The verifier to set.
*/
public void setVerifier(CassandraPersistentEntityMetadataVerifier verifier) {
this.verifier = verifier;
}
}

View File

@@ -22,6 +22,11 @@ public class CachingCassandraPersistentEntity<T> extends BasicCassandraPersisten
super(typeInformation, mappingContext);
}
public CachingCassandraPersistentEntity(TypeInformation<T> typeInformation, CassandraMappingContext mappingContext,
CassandraPersistentEntityMetadataVerifier verifier) {
super(typeInformation, mappingContext, verifier);
}
@Override
public String getTableName() {
if (tableName == null) {

View File

@@ -71,4 +71,9 @@ public interface CassandraMappingContext extends
* given type.
*/
boolean contains(Class<?> type);
/**
* Sets a verifier other than the {@link DefaultCassandraPersistentEntityMetadataVerifier}
*/
void setVerifier(CassandraPersistentEntityMetadataVerifier verifier);
}

View File

@@ -56,6 +56,7 @@ public class DefaultCassandraMappingContext extends
protected ApplicationContext context;
protected Mapping mapping = new Mapping();
protected ClassLoader beanClassLoader;
protected CassandraPersistentEntityMetadataVerifier verifier = new DefaultCassandraPersistentEntityMetadataVerifier();
// useful caches
protected Map<String, Set<CassandraPersistentEntity<?>>> entitySetsByTableName = new HashMap<String, Set<CassandraPersistentEntity<?>>>();
@@ -117,7 +118,7 @@ public class DefaultCassandraMappingContext extends
@Override
protected <T> CassandraPersistentEntity<T> createPersistentEntity(TypeInformation<T> typeInformation) {
CassandraPersistentEntity<T> entity = new CachingCassandraPersistentEntity<T>(typeInformation, this);
CassandraPersistentEntity<T> entity = new CachingCassandraPersistentEntity<T>(typeInformation, this, verifier);
if (context != null) {
entity.setApplicationContext(context);
@@ -262,4 +263,18 @@ public class DefaultCassandraMappingContext extends
public boolean contains(Class<?> type) {
return entitiesByType.containsKey(type);
}
/**
* @return Returns the verifier.
*/
public CassandraPersistentEntityMetadataVerifier getVerifier() {
return verifier;
}
/**
* @param verifier The verifier to set.
*/
public void setVerifier(CassandraPersistentEntityMetadataVerifier verifier) {
this.verifier = verifier;
}
}

View File

@@ -20,6 +20,8 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.mapping.PropertyHandler;
@@ -34,6 +36,10 @@ import org.springframework.data.mapping.model.MappingException;
*/
public class DefaultCassandraPersistentEntityMetadataVerifier implements CassandraPersistentEntityMetadataVerifier {
private static final Logger log = LoggerFactory.getLogger(DefaultCassandraPersistentEntityMetadataVerifier.class);
protected boolean strict = false;
@Override
public void verify(CassandraPersistentEntity<?> entity) throws MappingException {
@@ -157,8 +163,12 @@ public class DefaultCassandraPersistentEntityMetadataVerifier implements Cassand
throw new NoSuchMethodException();
}
} catch (NoSuchMethodException e) {
exceptions.add(new MappingException(
"@PrimaryKeyClass must override 'boolean equals(Object)' method and use all @PrimaryKeyColumn fields", e));
String message = "@PrimaryKeyClass must override 'boolean equals(Object)' method and use all @PrimaryKeyColumn fields";
if (strict) {
exceptions.add(new MappingException(message, e));
} else {
log.warn(message);
}
}
/*
@@ -170,8 +180,12 @@ public class DefaultCassandraPersistentEntityMetadataVerifier implements Cassand
throw new NoSuchMethodException();
}
} catch (NoSuchMethodException e) {
exceptions.add(new MappingException(
"@PrimaryKeyClass must override 'int hashCode()' method and use all @PrimaryKeyColumn fields", e));
String message = "@PrimaryKeyClass must override 'int hashCode()' method and use all @PrimaryKeyColumn fields";
if (strict) {
exceptions.add(new MappingException(message, e));
} else {
log.warn(message);
}
}
}
@@ -209,7 +223,22 @@ public class DefaultCassandraPersistentEntityMetadataVerifier implements Cassand
* Determine whether or not to throw Exception based on errors found
*/
if (exceptions.getCount() > 0) {
log.error("Exceptions while verifying PersistentEntity", exceptions);
throw exceptions;
}
}
/**
* @return Returns the strict.
*/
public boolean isStrict() {
return strict;
}
/**
* @param strict The strict to set.
*/
public void setStrict(boolean strict) {
this.strict = strict;
}
}