basic spring java config-based CassandraRepository now working

This commit is contained in:
Matthew Adams
2014-01-30 17:51:36 -06:00
parent df096bf124
commit 56eee40bd0
38 changed files with 302 additions and 475 deletions

View File

@@ -0,0 +1,23 @@
package org.springframework.cassandra.config;
/**
* Simple data structure to be used when setting the replication factor for a given data center.
*/
public class DataCenterReplication {
public static DataCenterReplication[] dcrs(DataCenterReplication... dcrs) {
return dcrs;
}
public static DataCenterReplication dcr(String dataCenter, long replicationFactor) {
return new DataCenterReplication(dataCenter, replicationFactor);
}
public String dataCenter;
public long replicationFactor;
public DataCenterReplication(String dataCenter, long replicationFactor) {
this.dataCenter = dataCenter;
this.replicationFactor = replicationFactor;
}
}

View File

@@ -109,8 +109,7 @@ public class KeyspaceActionSpecificationFactoryBean implements FactoryBean<Set<K
int i = 0;
for (String datacenter : networkTopologyDataCenters) {
replicationStrategyMap.put(new DefaultOption(datacenter, Long.class, true, false, false),
networkTopologyReplicationFactors.get(i));
i++;
networkTopologyReplicationFactors.get(i++));
}
}

View File

@@ -74,19 +74,6 @@ public class KeyspaceAttributes {
return builder.build();
}
/**
* Simple data structure to be used when setting the replication factor for a given data center.
*/
public static class DataCenterReplication {
public String dataCenter;
public long replicationFactor;
public DataCenterReplication(String dataCenter, long replicationFactor) {
this.dataCenter = dataCenter;
this.replicationFactor = replicationFactor;
}
}
private ReplicationStrategy replicationStrategy = DEFAULT_REPLICATION_STRATEGY;
private long replicationFactor = DEFAULT_REPLICATION_FACTOR;
private boolean durableWrites = DEFAULT_DURABLE_WRITES;

View File

@@ -4,7 +4,6 @@ import java.util.Collections;
import java.util.List;
import org.springframework.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.cassandra.config.CompressionType;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.cassandra.core.keyspace.DropKeyspaceSpecification;
@@ -12,7 +11,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.datastax.driver.core.AuthProvider;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.SocketOptions;
import com.datastax.driver.core.policies.LoadBalancingPolicy;
@@ -26,9 +24,7 @@ import com.datastax.driver.core.policies.RetryPolicy;
* @author Matthew T. Adams
*/
@Configuration
public abstract class AbstractCassandraConfiguration {
protected abstract String getKeyspaceName();
public abstract class AbstractClusterConfiguration {
@Bean
public CassandraClusterFactoryBean cluster() throws Exception {
@@ -52,18 +48,6 @@ public abstract class AbstractCassandraConfiguration {
return bean;
}
@Bean
public CassandraSessionFactoryBean session() throws Exception {
Cluster cluster = cluster().getObject();
CassandraSessionFactoryBean bean = new CassandraSessionFactoryBean();
bean.setCluster(cluster);
bean.setKeyspaceName(getKeyspaceName());
return bean;
}
protected List<String> getStartupScripts() {
return Collections.emptyList();
}

View File

@@ -0,0 +1,31 @@
package org.springframework.cassandra.config.java;
import org.springframework.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.datastax.driver.core.Cluster;
/**
* Base class for Spring Cassandra configuration that can handle creating namespaces, execute arbitrary CQL on startup &
* shutdown, and optionally drop namespaces.
*
* @author Matthew T. Adams
*/
@Configuration
public abstract class AbstractSessionConfiguration extends AbstractClusterConfiguration {
protected abstract String getKeyspaceName();
@Bean
public CassandraSessionFactoryBean session() throws Exception {
Cluster cluster = cluster().getObject();
CassandraSessionFactoryBean bean = new CassandraSessionFactoryBean();
bean.setCluster(cluster);
bean.setKeyspaceName(getKeyspaceName());
return bean;
}
}

View File

@@ -25,6 +25,8 @@ import org.springframework.dao.UncategorizedDataAccessException;
*/
public class CassandraUncategorizedDataAccessException extends UncategorizedDataAccessException {
private static final long serialVersionUID = -155082875466458401L;
/**
* Create the Exception
*

View File

@@ -1,5 +1,9 @@
package org.springframework.cassandra.core.keyspace;
import org.springframework.cassandra.config.DataCenterReplication;
import org.springframework.cassandra.core.keyspace.KeyspaceOption.ReplicationStrategy;
import org.springframework.cassandra.core.util.MapBuilder;
public class CreateKeyspaceSpecification extends KeyspaceSpecification<CreateKeyspaceSpecification> {
private boolean ifNotExists = false;
@@ -35,6 +39,33 @@ public class CreateKeyspaceSpecification extends KeyspaceSpecification<CreateKey
return new CreateKeyspaceSpecification();
}
public CreateKeyspaceSpecification withSimpleReplication() {
return withSimpleReplication(1);
}
public CreateKeyspaceSpecification withSimpleReplication(long replicationFactor) {
return with(
KeyspaceOption.REPLICATION,
MapBuilder
.map(Option.class, Object.class)
.entry(new DefaultOption("class", String.class, true, false, true),
ReplicationStrategy.SIMPLE_STRATEGY.getValue())
.entry(new DefaultOption("replication_factor", Long.class, true, false, false), replicationFactor).build());
}
public CreateKeyspaceSpecification withNetworkReplication(DataCenterReplication... dcrs) {
MapBuilder<Option, Object> builder = MapBuilder.map(Option.class, Object.class).entry(
new DefaultOption("class", String.class, true, false, true),
ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY.getValue());
for (DataCenterReplication dcr : dcrs) {
builder.entry(new DefaultOption(dcr.dataCenter, Long.class, true, false, false), dcr.replicationFactor);
}
return with(KeyspaceOption.REPLICATION, builder.build());
}
@Override
public CreateKeyspaceSpecification name(String name) {
return (CreateKeyspaceSpecification) super.name(name);

View File

@@ -23,10 +23,11 @@ public class AbstractEmbeddedCassandraIntegrationTest {
static Logger log = LoggerFactory.getLogger(AbstractEmbeddedCassandraIntegrationTest.class);
protected static final BuildProperties PROPS = new BuildProperties();
protected static final String CASSANDRA_CONFIG = "spring-cassandra.yaml";
protected static final String CASSANDRA_HOST = "localhost";
protected static final int CASSANDRA_NATIVE_PORT = PROPS.getCassandraPort();
protected static String CASSANDRA_CONFIG = "spring-cassandra.yaml";
protected static String CASSANDRA_HOST = "localhost";
protected static BuildProperties PROPS = new BuildProperties();
protected static int CASSANDRA_NATIVE_PORT = PROPS.getCassandraPort();
/**
* The session connected to the system keyspace.

View File

@@ -1,7 +1,7 @@
package org.springframework.cassandra.test.integration.config.java;
import org.springframework.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.cassandra.config.java.AbstractSessionConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
@@ -9,7 +9,7 @@ import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Session;
@Configuration
public abstract class AbstractKeyspaceCreatingConfiguration extends AbstractCassandraConfiguration {
public abstract class AbstractKeyspaceCreatingConfiguration extends AbstractSessionConfiguration {
@Override
public CassandraSessionFactoryBean session() throws Exception {

View File

@@ -1,10 +1,10 @@
package org.springframework.cassandra.test.integration.support;
import org.springframework.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.cassandra.config.java.AbstractSessionConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
public abstract class AbstractTestJavaConfig extends AbstractCassandraConfiguration {
public abstract class AbstractTestJavaConfig extends AbstractSessionConfiguration {
public static BuildProperties PROPS = new BuildProperties();
public static final int PORT = PROPS.getCassandraPort();

View File

@@ -9,9 +9,8 @@
</encoder>
</appender>
<logger name="org.springframework.context" level="info" />
<logger name="org.springframework" level="info" />
<logger name="org.springframework.cassandra" level="debug" />
<logger name="org.springframework.data.cassandra" level="debug" />
<logger name="com.datastax" level="info" />
<root level="warn">

View File

@@ -25,16 +25,14 @@ public class CassandraDataSessionFactoryBean extends CassandraSessionFactoryBean
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.notNull(converter);
if (mapping == null) {
mapping = new Mapping();
}
admin = new CassandraAdminTemplate(session, converter);
admin = new CassandraAdminTemplate(session);
admin.setCassandraConverter(converter);
mapping = mapping == null ? new Mapping() : mapping;
processMappingOverrides();
performSchemaAction();

View File

@@ -31,15 +31,15 @@ public enum SchemaAction {
// /**
// * Validate that each required table and column exists. Fail if any required table or column does not exists.
// */
// VALIDATE("VALIDATE"),
// VALIDATE,
//
// /**
// * Alter or create each table and column as necessary, leaving unused tables and columns untouched.
// */
// UPDATE("UPDATE"),
// UPDATE,
//
// /**
// * Alter or create each table and column as necessary, removing unused tables and columns.
// */
// UPDATE_DROP_UNUNSED("UPDATE_DROP_UNUSED");
// UPDATE_DROP_UNUNSED;
}

View File

@@ -20,12 +20,15 @@ import java.util.Set;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.cassandra.config.java.AbstractClusterConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.cassandra.config.CassandraDataSessionFactoryBean;
import org.springframework.data.cassandra.config.Mapping;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraAdminOperations;
@@ -44,27 +47,55 @@ import org.springframework.util.StringUtils;
* @author Matthew T. Adams
*/
@Configuration
public abstract class AbstractSpringDataCassandraConfiguration extends AbstractCassandraConfiguration implements
public abstract class AbstractSpringDataCassandraConfiguration extends AbstractClusterConfiguration implements
BeanClassLoaderAware {
private ClassLoader beanClassLoader;
protected abstract String getKeyspaceName();
protected ClassLoader beanClassLoader;
protected Mapping mapping = new Mapping();
/**
* The {@link SchemaAction} to perform. Defaults to {@link SchemaAction#NONE}.
*/
public SchemaAction getSchemaAction() {
return SchemaAction.NONE;
}
/**
* The base package to scan for entities annotated with {@link Table} annotations. By default, returns the package
* name of {@literal this} (<code>this.getClass().getPackage().getName()</code>).
*/
protected String getMappingBasePackage() {
public String getMappingBasePackage() {
return getClass().getPackage().getName();
}
@Bean
public CassandraDataSessionFactoryBean session() throws Exception {
CassandraDataSessionFactoryBean bean = new CassandraDataSessionFactoryBean();
bean.setCluster(cluster().getObject());
bean.setConverter(converter());
bean.setSchemaAction(getSchemaAction());
bean.setKeyspaceName(getKeyspaceName());
bean.setStartupScripts(getStartupScripts());
bean.setShutdownScripts(getShutdownScripts());
bean.setEntityClassLoader(beanClassLoader);
bean.setMapping(mapping);
return bean;
}
/**
* Creates a {@link CassandraAdminTemplate}.
*
* @throws Exception
*/
@Bean
public CassandraAdminOperations adminTemplate() throws Exception {
return new CassandraAdminTemplate(session().getObject());
public CassandraAdminOperations cassandraTemplate() throws Exception {
return new CassandraAdminTemplate(session().getObject(), converter());
}
/**
@@ -109,17 +140,24 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Table.class));
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
// TODO: figure out which ClassLoader to use here
ClassLoader classLoader = getClass().getClassLoader();
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), classLoader));
Class<?> clazz = ClassUtils.forName(candidate.getBeanClassName(), beanClassLoader);
initialEntitySet.add(clazz);
}
}
processMappingOverrides(initialEntitySet);
return initialEntitySet;
}
protected void processMappingOverrides(Set<Class<?>> entityTypes) {
// TODO: search for external entity mapping info (xml/properties/yaml/etc) here & update this.mapping
// similar to JPA's or JDO's external metadata search algorithms
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;

View File

@@ -25,7 +25,7 @@ import com.datastax.driver.core.TableMetadata;
* @author David Webb
* @author Matthew T. Adams
*/
public interface CassandraAdminOperations {
public interface CassandraAdminOperations extends CassandraOperations {
/**
* Create a table with the name given and fields corresponding to the given class. If the table already exists and
@@ -38,9 +38,8 @@ public interface CassandraAdminOperations {
* @param tableName The name of the table.
* @param entityClass The class whose fields determine the columns created.
* @param optionsByName Table options, given by the string option name and the appropriate option value.
* @return Returns true if a table was created, false if not.
*/
boolean createTable(boolean ifNotExists, String tableName, Class<?> entityClass, Map<String, Object> optionsByName);
void createTable(boolean ifNotExists, String tableName, Class<?> entityClass, Map<String, Object> optionsByName);
/**
* Add columns to the given table from the given class. If parameter dropRemovedAttributColumns is true, then this

View File

@@ -7,14 +7,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cassandra.core.SessionCallback;
import org.springframework.cassandra.core.cql.generator.CreateTableCqlGenerator;
import org.springframework.cassandra.support.CassandraAccessor;
import org.springframework.cassandra.support.CassandraExceptionTranslator;
import org.springframework.cassandra.support.exception.CassandraTableExistsException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.util.CqlUtils;
import org.springframework.util.Assert;
@@ -26,77 +21,46 @@ import com.datastax.driver.core.TableMetadata;
/**
* Default implementation of {@link CassandraAdminOperations}.
*/
public class CassandraAdminTemplate extends CassandraAccessor implements CassandraAdminOperations {
public class CassandraAdminTemplate extends CassandraTemplate implements CassandraAdminOperations {
private static final Logger log = LoggerFactory.getLogger(CassandraAdminTemplate.class);
private CassandraConverter converter;
private CassandraMappingContext mappingContext;
private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator();
/**
* Constructor used for a basic template configuration
*
* @param keyspace must not be {@literal null}.
*/
public CassandraAdminTemplate(Session session) {
setSession(session);
}
public void setCassandraConverter(CassandraConverter converter) {
Assert.notNull(converter);
this.converter = converter;
setMappingContext(this.converter.getCassandraMappingContext());
}
protected void setMappingContext(CassandraMappingContext mappingContext) {
Assert.notNull(mappingContext);
this.mappingContext = mappingContext;
public CassandraAdminTemplate(Session session, CassandraConverter converter) {
super(session, converter);
}
@Override
public boolean createTable(boolean ifNotExists, final String tableName, Class<?> entityClass,
public void createTable(boolean ifNotExists, final String tableName, Class<?> entityClass,
Map<String, Object> optionsByName) {
try {
final CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
final CassandraPersistentEntity<?> entity = getCassandraMappingContext().getPersistentEntity(entityClass);
execute(new SessionCallback<Object>() {
@Override
public Object doInSession(Session s) throws DataAccessException {
execute(new SessionCallback<Object>() {
@Override
public Object doInSession(Session s) throws DataAccessException {
String cql = new CreateTableCqlGenerator(mappingContext.getCreateTableSpecificationFor(entity)).toCql();
String cql = new CreateTableCqlGenerator(getCassandraMappingContext().getCreateTableSpecificationFor(entity))
.toCql();
log.info("CREATE TABLE CQL -> " + cql);
s.execute(cql);
return null;
}
});
return true;
} catch (CassandraTableExistsException ctex) {
return !ifNotExists;
} catch (RuntimeException x) {
throw tryToConvert(x);
}
s.execute(cql);
return null;
}
});
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraAdminOperations#alterTable(java.lang.String, java.lang.Class, boolean)
*/
@Override
public void alterTable(String tableName, Class<?> entityClass, boolean dropRemovedAttributeColumns) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("not yet implemented");
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraAdminOperations#replaceTable(java.lang.String, java.lang.Class)
*/
@Override
public void replaceTable(String tableName, Class<?> entityClass, Map<String, Object> optionsByName) {
// TODO
throw new UnsupportedOperationException("not yet implemented");
}
/**
@@ -107,12 +71,11 @@ public class CassandraAdminTemplate extends CassandraAccessor implements Cassand
*/
protected void doAlterTable(Class<?> entityClass, String keyspace, String tableName) {
CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
CassandraPersistentEntity<?> entity = getCassandraMappingContext().getPersistentEntity(entityClass);
Assert.notNull(entity);
final TableMetadata tableMetadata = getTableMetadata(keyspace, tableName);
final List<String> queryList = CqlUtils.alterTable(tableName, entity, tableMetadata);
execute(new SessionCallback<Object>() {
@@ -126,26 +89,14 @@ public class CassandraAdminTemplate extends CassandraAccessor implements Cassand
}
return null;
}
});
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#dropTable(java.lang.Class)
*/
public void dropTable(Class<?> entityClass) {
final String tableName = determineTableName(entityClass);
dropTable(tableName);
dropTable(determineTableName(entityClass));
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#dropTable(java.lang.String)
*/
@Override
public void dropTable(String tableName) {
@@ -155,62 +106,31 @@ public class CassandraAdminTemplate extends CassandraAccessor implements Cassand
log.info(q);
execute(new SessionCallback<ResultSet>() {
@Override
public ResultSet doInSession(Session s) throws DataAccessException {
public ResultSet doInSession(Session s) {
return s.execute(q);
}
});
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#getTableMetadata(java.lang.Class)
*/
@Override
public TableMetadata getTableMetadata(final String keyspace, final String tableName) {
Assert.notNull(tableName);
return execute(new SessionCallback<TableMetadata>() {
@Override
public TableMetadata doInSession(Session s) throws DataAccessException {
public TableMetadata doInSession(Session s) {
return s.getCluster().getMetadata().getKeyspace(keyspace).getTable(tableName);
}
});
}
/**
* Execute a command at the Session Level
*
* @param callback
* @return
*/
protected <T> T execute(SessionCallback<T> callback) {
Assert.notNull(callback);
try {
return callback.doInSession(getSession());
} catch (RuntimeException x) {
throw tryToConvert(x);
}
}
protected RuntimeException tryToConvert(RuntimeException x) {
RuntimeException resolved = exceptionTranslator.translateExceptionIfPossible(x);
return resolved == null ? x : resolved;
}
/**
* @param entityClass
* @return
*/
@Override
public String determineTableName(Class<?> entityClass) {
if (entityClass == null) {
@@ -218,12 +138,11 @@ public class CassandraAdminTemplate extends CassandraAccessor implements Cassand
"No class parameter provided, entity table name can't be determined!");
}
CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
CassandraPersistentEntity<?> entity = getCassandraMappingContext().getPersistentEntity(entityClass);
if (entity == null) {
throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class "
+ entityClass.getName());
}
return entity.getTableName();
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.data.cassandra.core;
import java.util.List;
import org.springframework.cassandra.core.CqlOperations;
import org.springframework.cassandra.core.QueryOptions;
import org.springframework.data.cassandra.convert.CassandraConverter;
@@ -31,7 +32,7 @@ import com.datastax.driver.core.querybuilder.Select;
* @author Matthew Adams
*
*/
public interface CassandraOperations {
public interface CassandraOperations extends CqlOperations {
/**
* The table name used for the specified class by this template.

View File

@@ -30,11 +30,12 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.exception.EntityWriterException;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
import org.springframework.data.cassandra.mapping.DefaultCassandraMappingContext;
import org.springframework.data.cassandra.util.CqlUtils;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.util.Assert;
import com.datastax.driver.core.Query;
@@ -74,9 +75,8 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
* Required elements for successful Template Operations. These can be set with the Constructor, or wired in
* later.
*/
private String keyspace;
private CassandraConverter cassandraConverter;
private MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext;
private CassandraMappingContext mappingContext;
/**
* Default Constructor for wiring in the required components later
@@ -90,7 +90,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
* @param session must not be {@literal null}
*/
public CassandraTemplate(Session session) {
this(session, null, null);
this(session, new MappingCassandraConverter(new DefaultCassandraMappingContext()));
}
/**
@@ -100,46 +100,26 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
* @param converter must not be {@literal null}.
*/
public CassandraTemplate(Session session, CassandraConverter converter) {
this(session, converter, null);
}
/**
* Constructor used for a basic template configuration
*
* @param session must not be {@literal null}.
* @param converter must not be {@literal null}.
*
* @deprecated use {@link #CassandraTemplate(Session, CassandraConverter)} because session should already be connected
* to keyspace
*/
@Deprecated
public CassandraTemplate(Session session, CassandraConverter converter, String keyspace) {
setSession(session);
this.keyspace = keyspace;
this.cassandraConverter = converter;
this.mappingContext = this.cassandraConverter.getMappingContext();
this.mappingContext = cassandraConverter.getCassandraMappingContext();
}
public CassandraMappingContext getCassandraMappingContext() {
return mappingContext;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#count(com.datastax.driver.core.querybuilder.Select)
*/
@Override
public Long count(Select selectQuery) {
return doSelectCount(selectQuery);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#count(java.lang.String)
*/
@Override
public Long count(String tableName) {
Select select = QueryBuilder.select().countAll().from(tableName);
return doSelectCount(select);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List)
*/
@Override
public <T> void delete(List<T> entities) {
String tableName = getTableName(entities.get(0).getClass());
@@ -147,9 +127,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
delete(entities, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void delete(List<T> entities, QueryOptions options) {
String tableName = getTableName(entities.get(0).getClass());
@@ -157,18 +134,12 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
delete(entities, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List, java.lang.String)
*/
@Override
public <T> void delete(List<T> entities, String tableName) {
delete(entities, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void delete(List<T> entities, String tableName, QueryOptions options) {
Assert.notNull(entities);
@@ -177,9 +148,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
doBatchDelete(tableName, entities, options, false);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object)
*/
@Override
public <T> void delete(T entity) {
String tableName = getTableName(entity.getClass());
@@ -187,9 +155,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
delete(entity, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void delete(T entity, QueryOptions options) {
String tableName = getTableName(entity.getClass());
@@ -197,17 +162,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
delete(entity, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object, java.lang.String)
*/
@Override
public <T> void delete(T entity, String tableName) {
delete(entity, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void delete(T entity, String tableName, QueryOptions options) {
Assert.notNull(entity);
@@ -215,9 +174,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
doDelete(tableName, entity, options, false);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.util.List)
*/
@Override
public <T> void deleteAsynchronously(List<T> entities) {
String tableName = getTableName(entities.get(0).getClass());
@@ -225,9 +181,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
deleteAsynchronously(entities, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.util.List, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void deleteAsynchronously(List<T> entities, QueryOptions options) {
String tableName = getTableName(entities.get(0).getClass());
@@ -235,17 +188,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
deleteAsynchronously(entities, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.util.List, java.lang.String)
*/
@Override
public <T> void deleteAsynchronously(List<T> entities, String tableName) {
deleteAsynchronously(entities, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.util.List, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void deleteAsynchronously(List<T> entities, String tableName, QueryOptions options) {
Assert.notNull(entities);
@@ -254,9 +201,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
doBatchDelete(tableName, entities, options, true);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.lang.Object)
*/
@Override
public <T> void deleteAsynchronously(T entity) {
String tableName = getTableName(entity.getClass());
@@ -264,9 +208,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
deleteAsynchronously(entity, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void deleteAsynchronously(T entity, QueryOptions options) {
String tableName = getTableName(entity.getClass());
@@ -274,17 +215,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
deleteAsynchronously(entity, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.lang.Object, java.lang.String)
*/
@Override
public <T> void deleteAsynchronously(T entity, String tableName) {
deleteAsynchronously(entity, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.lang.Object, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> void deleteAsynchronously(T entity, String tableName, QueryOptions options) {
Assert.notNull(entity);
@@ -311,25 +246,16 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return entity.getTableName();
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#getConverter()
*/
@Override
public CassandraConverter getConverter() {
return cassandraConverter;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#getTableName(java.lang.Class)
*/
@Override
public String getTableName(Class<?> entityClass) {
return determineTableName(entityClass);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List)
*/
@Override
public <T> List<T> insert(List<T> entities) {
String tableName = getTableName(entities.get(0).getClass());
@@ -337,9 +263,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insert(entities, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> insert(List<T> entities, QueryOptions options) {
String tableName = getTableName(entities.get(0).getClass());
@@ -347,17 +270,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insert(entities, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> insert(List<T> entities, String tableName) {
return insert(entities, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> insert(List<T> entities, String tableName, QueryOptions options) {
Assert.notNull(entities);
@@ -366,9 +283,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return doBatchInsert(tableName, entities, options, false);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object)
*/
@Override
public <T> T insert(T entity) {
String tableName = determineTableName(entity);
@@ -376,9 +290,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insert(entity, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T insert(T entity, QueryOptions options) {
String tableName = determineTableName(entity);
@@ -386,17 +297,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insert(entity, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, java.lang.String)
*/
@Override
public <T> T insert(T entity, String tableName) {
return insert(entity, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T insert(T entity, String tableName, QueryOptions options) {
Assert.notNull(entity);
@@ -405,9 +310,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return doInsert(tableName, entity, options, false);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List)
*/
@Override
public <T> List<T> insertAsynchronously(List<T> entities) {
String tableName = getTableName(entities.get(0).getClass());
@@ -415,9 +317,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insertAsynchronously(entities, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> insertAsynchronously(List<T> entities, QueryOptions options) {
String tableName = getTableName(entities.get(0).getClass());
@@ -425,17 +324,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insertAsynchronously(entities, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> insertAsynchronously(List<T> entities, String tableName) {
return insertAsynchronously(entities, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> insertAsynchronously(List<T> entities, String tableName, QueryOptions options) {
Assert.notNull(entities);
@@ -444,9 +337,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return doBatchInsert(tableName, entities, options, true);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object)
*/
@Override
public <T> T insertAsynchronously(T entity) {
String tableName = determineTableName(entity);
@@ -454,9 +344,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insertAsynchronously(entity, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T insertAsynchronously(T entity, QueryOptions options) {
String tableName = determineTableName(entity);
@@ -464,17 +351,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return insertAsynchronously(entity, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object, java.lang.String)
*/
@Override
public <T> T insertAsynchronously(T entity, String tableName) {
return insertAsynchronously(entity, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T insertAsynchronously(T entity, String tableName, QueryOptions options) {
Assert.notNull(entity);
@@ -485,41 +366,26 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return doInsert(tableName, entity, options, true);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#select(com.datastax.driver.core.querybuilder.Select, java.lang.Class)
*/
@Override
public <T> List<T> select(Select cql, Class<T> selectClass) {
return select(cql.getQueryString(), selectClass);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#select(java.lang.String, java.lang.Class)
*/
@Override
public <T> List<T> select(String cql, Class<T> selectClass) {
return doSelect(cql, new ReadRowCallback<T>(cassandraConverter, selectClass));
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#selectOne(com.datastax.driver.core.querybuilder.Select, java.lang.Class)
*/
@Override
public <T> T selectOne(Select selectQuery, Class<T> selectClass) {
return selectOne(selectQuery.getQueryString(), selectClass);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#selectOne(java.lang.String, java.lang.Class)
*/
@Override
public <T> T selectOne(String cql, Class<T> selectClass) {
return doSelectOne(cql, new ReadRowCallback<T>(cassandraConverter, selectClass));
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List)
*/
@Override
public <T> List<T> update(List<T> entities) {
String tableName = getTableName(entities.get(0).getClass());
@@ -527,9 +393,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return update(entities, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> update(List<T> entities, QueryOptions options) {
String tableName = getTableName(entities.get(0).getClass());
@@ -537,17 +400,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return update(entities, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> update(List<T> entities, String tableName) {
return update(entities, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> update(List<T> entities, String tableName, QueryOptions options) {
Assert.notNull(entities);
@@ -556,9 +413,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return doBatchUpdate(tableName, entities, options, false);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object)
*/
@Override
public <T> T update(T entity) {
String tableName = getTableName(entity.getClass());
@@ -566,9 +420,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return update(entity, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T update(T entity, QueryOptions options) {
String tableName = getTableName(entity.getClass());
@@ -576,17 +427,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return update(entity, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object, java.lang.String)
*/
@Override
public <T> T update(T entity, String tableName) {
return update(entity, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T update(T entity, String tableName, QueryOptions options) {
Assert.notNull(entity);
@@ -594,9 +439,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return doUpdate(tableName, entity, options, false);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List)
*/
@Override
public <T> List<T> updateAsynchronously(List<T> entities) {
String tableName = getTableName(entities.get(0).getClass());
@@ -604,9 +446,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return updateAsynchronously(entities, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> updateAsynchronously(List<T> entities, QueryOptions options) {
String tableName = getTableName(entities.get(0).getClass());
@@ -614,17 +453,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return updateAsynchronously(entities, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> updateAsynchronously(List<T> entities, String tableName) {
return updateAsynchronously(entities, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> List<T> updateAsynchronously(List<T> entities, String tableName, QueryOptions options) {
Assert.notNull(entities);
@@ -633,9 +466,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return doBatchUpdate(tableName, entities, options, true);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object)
*/
@Override
public <T> T updateAsynchronously(T entity) {
String tableName = getTableName(entity.getClass());
@@ -643,9 +473,6 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return updateAsynchronously(entity, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T updateAsynchronously(T entity, QueryOptions options) {
String tableName = getTableName(entity.getClass());
@@ -653,18 +480,12 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return updateAsynchronously(entity, tableName, options);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object, java.lang.String)
*/
@Override
public <T> T updateAsynchronously(T entity, String tableName) {
return updateAsynchronously(entity, tableName, null);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object, java.lang.String, org.springframework.data.cassandra.core.QueryOptions)
*/
@Override
public <T> T updateAsynchronously(T entity, String tableName, QueryOptions options) {
Assert.notNull(entity);
@@ -793,7 +614,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
try {
final Batch b = CqlUtils.toDeleteBatchQuery(keyspace, tableName, entities, options, cassandraConverter);
final Batch b = CqlUtils.toDeleteBatchQuery(tableName, entities, options, cassandraConverter);
logger.info(b.toString());
doExecute(new SessionCallback<Object>() {
@@ -834,7 +655,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
try {
final Batch b = CqlUtils.toInsertBatchQuery(keyspace, tableName, entities, options, cassandraConverter);
final Batch b = CqlUtils.toInsertBatchQuery(tableName, entities, options, cassandraConverter);
logger.info(b.getQueryString());
return doExecute(new SessionCallback<List<T>>() {
@@ -875,7 +696,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
try {
final Batch b = CqlUtils.toUpdateBatchQuery(keyspace, tableName, entities, options, cassandraConverter);
final Batch b = CqlUtils.toUpdateBatchQuery(tableName, entities, options, cassandraConverter);
logger.info(b.toString());
return doExecute(new SessionCallback<List<T>>() {
@@ -911,7 +732,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
try {
final Query q = CqlUtils.toDeleteQuery(keyspace, tableName, objectToRemove, options, cassandraConverter);
final Query q = CqlUtils.toDeleteQuery(tableName, objectToRemove, options, cassandraConverter);
logger.info(q.toString());
doExecute(new SessionCallback<Object>() {
@@ -967,7 +788,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
try {
final Query q = CqlUtils.toInsertQuery(keyspace, tableName, entity, options, cassandraConverter);
final Query q = CqlUtils.toInsertQuery(tableName, entity, options, cassandraConverter);
logger.info(q.toString());
if (q.getConsistencyLevel() != null) {
@@ -1014,7 +835,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
try {
final Query q = CqlUtils.toUpdateQuery(keyspace, tableName, entity, options, cassandraConverter);
final Query q = CqlUtils.toUpdateQuery(tableName, entity, options, cassandraConverter);
logger.info(q.toString());
return doExecute(new SessionCallback<T>() {

View File

@@ -25,30 +25,17 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
* {@link ImportBeanDefinitionRegistrar} to setup Cassandra repositories via {@link EnableCassandraRepositories}.
*
* @author Alex Shvid
*
* @author Matthew T. Adams
*/
public class CassandraRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.config.
* RepositoryBeanDefinitionRegistrarSupport#getAnnotation()
*/
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableCassandraRepositories.class;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.config.
* RepositoryBeanDefinitionRegistrarSupport#getExtension()
*/
@Override
protected RepositoryConfigurationExtension getExtension() {
return new CassandraRepositoryConfigurationExtension();
}
}

View File

@@ -34,43 +34,26 @@ import org.w3c.dom.Element;
*/
public class CassandraRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
private static final String CASSANDRA_TEMPLATE_REF = "cql-template-ref";
private static final String CREATE_QUERY_INDEXES = "create-query-indexes";
private static final String CASSANDRA_TEMPLATE_REF = "cassandra-template-ref";
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix()
*/
@Override
protected String getModulePrefix() {
return "cassandra";
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getRepositoryFactoryClassName()
*/
@Override
public String getRepositoryFactoryClassName() {
return CassandraRepositoryFactoryBean.class.getName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.XmlRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
Element element = config.getElement();
ParsingUtils.setPropertyReference(builder, element, CASSANDRA_TEMPLATE_REF, "cassandraTemplate");
ParsingUtils.setPropertyValue(builder, element, CREATE_QUERY_INDEXES, "createIndexesForQueryMethods");
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
@@ -80,7 +63,6 @@ public class CassandraRepositoryConfigurationExtension extends RepositoryConfigu
if (StringUtils.hasText(cassandraTemplateRef)) {
builder.addPropertyReference("cassandraTemplate", cassandraTemplateRef);
}
builder.addPropertyValue("createIndexesForQueryMethods", attributes.getBoolean("createIndexesForQueryMethods"));
}
}

View File

@@ -34,9 +34,8 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
* Annotation to enable Cassandra repositories.
*
* @author Alex Shvid
*
* @author Matthew T. Adams
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@@ -114,12 +113,4 @@ public @interface EnableCassandraRepositories {
* @return
*/
String cassandraTemplateRef() default "cassandraTemplate";
/**
* Whether to automatically create indexes for query methods defined in the repository interface.
*
* @return
*/
boolean createIndexesForQueryMethods() default false;
}

View File

@@ -45,7 +45,7 @@ public class CassandraRepositoryFactoryBean<T extends Repository<S, ID>, S, ID e
*
* @param operations the operations to set
*/
public void setCassandraDataTemplate(CassandraTemplate cassandraTemplate) {
public void setCassandraTemplate(CassandraTemplate cassandraTemplate) {
this.cassandraTemplate = cassandraTemplate;
setMappingContext(cassandraTemplate.getConverter().getMappingContext());
}

View File

@@ -28,7 +28,7 @@ import com.datastax.driver.core.querybuilder.Update;
*
* @author Alex Shvid
* @author David Webb
*
* @author Matthew T. Adams
*/
public abstract class CqlUtils {
@@ -118,7 +118,6 @@ public abstract class CqlUtils {
/**
* Generates a Query Object for an insert
*
* @param keyspaceName
* @param tableName
* @param objectToSave
* @param entity
@@ -127,10 +126,10 @@ public abstract class CqlUtils {
* @return The Query object to run with session.execute();
* @throws EntityWriterException
*/
public static Query toInsertQuery(String keyspaceName, String tableName, final Object objectToSave,
QueryOptions options, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
public static Query toInsertQuery(String tableName, final Object objectToSave, QueryOptions options,
EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
final Insert q = QueryBuilder.insertInto(keyspaceName, tableName);
final Insert q = QueryBuilder.insertInto(tableName);
/*
* Write properties
@@ -156,7 +155,6 @@ public abstract class CqlUtils {
/**
* Generates a Query Object for an Update
*
* @param keyspaceName
* @param tableName
* @param objectToSave
* @param entity
@@ -165,10 +163,10 @@ public abstract class CqlUtils {
* @return The Query object to run with session.execute();
* @throws EntityWriterException
*/
public static Query toUpdateQuery(String keyspaceName, String tableName, final Object objectToSave,
QueryOptions options, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
public static Query toUpdateQuery(String tableName, final Object objectToSave, QueryOptions options,
EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
final Update q = QueryBuilder.update(keyspaceName, tableName);
final Update q = QueryBuilder.update(tableName);
/*
* Write properties
@@ -194,7 +192,6 @@ public abstract class CqlUtils {
/**
* Generates a Batch Object for multiple Updates
*
* @param keyspaceName
* @param tableName
* @param objectsToSave
* @param entity
@@ -203,9 +200,8 @@ public abstract class CqlUtils {
* @return The Query object to run with session.execute();
* @throws EntityWriterException
*/
public static <T> Batch toUpdateBatchQuery(final String keyspaceName, final String tableName,
final List<T> objectsToSave, QueryOptions options, EntityWriter<Object, Object> entityWriter)
throws EntityWriterException {
public static <T> Batch toUpdateBatchQuery(final String tableName, final List<T> objectsToSave, QueryOptions options,
EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
/*
* Return variable is a Batch statement
@@ -214,7 +210,7 @@ public abstract class CqlUtils {
for (final T objectToSave : objectsToSave) {
b.add((Statement) toUpdateQuery(keyspaceName, tableName, objectToSave, options, entityWriter));
b.add((Statement) toUpdateQuery(tableName, objectToSave, options, entityWriter));
}
@@ -230,7 +226,6 @@ public abstract class CqlUtils {
/**
* Generates a Batch Object for multiple inserts
*
* @param keyspaceName
* @param tableName
* @param objectsToSave
* @param entity
@@ -239,9 +234,8 @@ public abstract class CqlUtils {
* @return The Query object to run with session.execute();
* @throws EntityWriterException
*/
public static <T> Batch toInsertBatchQuery(final String keyspaceName, final String tableName,
final List<T> objectsToSave, QueryOptions options, EntityWriter<Object, Object> entityWriter)
throws EntityWriterException {
public static <T> Batch toInsertBatchQuery(final String tableName, final List<T> objectsToSave, QueryOptions options,
EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
/*
* Return variable is a Batch statement
@@ -250,7 +244,7 @@ public abstract class CqlUtils {
for (final T objectToSave : objectsToSave) {
b.add((Statement) toInsertQuery(keyspaceName, tableName, objectToSave, options, entityWriter));
b.add((Statement) toInsertQuery(tableName, objectToSave, options, entityWriter));
}
@@ -266,7 +260,6 @@ public abstract class CqlUtils {
/**
* Create a Delete Query Object from an annotated POJO
*
* @param keyspace
* @param tableName
* @param objectToRemove
* @param entity
@@ -274,11 +267,11 @@ public abstract class CqlUtils {
* @return
* @throws EntityWriterException
*/
public static Query toDeleteQuery(String keyspace, String tableName, final Object objectToRemove,
QueryOptions options, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
public static Query toDeleteQuery(String tableName, final Object objectToRemove, QueryOptions options,
EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
final Delete.Selection ds = QueryBuilder.delete();
final Delete q = ds.from(keyspace, tableName);
final Delete q = ds.from(tableName);
final Where w = q.where();
/*
@@ -332,7 +325,6 @@ public abstract class CqlUtils {
/**
* Create a Batch Query object for multiple deletes.
*
* @param keyspaceName
* @param tableName
* @param entities
* @param entity
@@ -341,8 +333,8 @@ public abstract class CqlUtils {
* @return
* @throws EntityWriterException
*/
public static <T> Batch toDeleteBatchQuery(String keyspaceName, String tableName, List<T> entities,
QueryOptions options, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
public static <T> Batch toDeleteBatchQuery(String tableName, List<T> entities, QueryOptions options,
EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
/*
* Return variable is a Batch statement
@@ -351,7 +343,7 @@ public abstract class CqlUtils {
for (final T objectToSave : entities) {
b.add((Statement) toDeleteQuery(keyspaceName, tableName, objectToSave, options, entityWriter));
b.add((Statement) toDeleteQuery(tableName, objectToSave, options, entityWriter));
}

View File

@@ -0,0 +1,14 @@
package org.springframework.data.cassandra.test.integration;
import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.support.SpringDataBuildProperties;
public class AbstractSpringDataEmbeddedCassandraIntegrationTest extends AbstractEmbeddedCassandraIntegrationTest {
static {
// override necessary superclass statics
SpringDataBuildProperties props = new SpringDataBuildProperties();
CASSANDRA_NATIVE_PORT = props.getCassandraPort();
}
}

View File

@@ -25,7 +25,7 @@ public class CassandraNamespaceTests {
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml");
}
@After

View File

@@ -5,8 +5,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.java.AbstractSpringDataCassandraConfiguration;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.core.CassandraAdminOperations;
import org.springframework.data.cassandra.core.CassandraAdminTemplate;
import org.springframework.data.cassandra.mapping.DefaultCassandraMappingContext;
import org.springframework.data.cassandra.test.integration.support.SpringDataBuildProperties;
@@ -40,8 +40,9 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration {
return new MappingCassandraConverter(new DefaultCassandraMappingContext());
}
@Override
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject(), converter(), KEYSPACE_NAME);
public CassandraAdminOperations cassandraTemplate() throws Exception {
return new CassandraAdminTemplate(session().getObject(), converter());
}
}

View File

@@ -49,7 +49,7 @@ public class BasicCassandraPersistentEntityIntegrationTests {
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml");
}
@Test

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.repository;
import java.util.Set;

View File

@@ -16,7 +16,6 @@
package org.springframework.data.cassandra.test.integration.repository;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.cassandra.test.integration.table.User;
/**
* Sample repository managing {@link User} entities.
@@ -25,5 +24,4 @@ import org.springframework.data.cassandra.test.integration.table.User;
*
*/
public interface UserRepository extends CassandraRepository<User, String> {
}

View File

@@ -21,17 +21,19 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.test.integration.table.User;
import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.google.common.collect.Lists;
@@ -39,29 +41,23 @@ import com.google.common.collect.Lists;
* Base class for tests for {@link UserRepository}.
*
* @author Alex Shvid
*
* @author Matthew T. Adams
*/
// @ContextConfiguration(classes = UserRepositoryIntegrationTestsConfig.class)
// @RunWith(SpringJUnit4ClassRunner.class)
public class UserRepositoryIntegrationTests {
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = UserRepositoryIntegrationTestsConfig.class)
public class UserRepositoryIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Autowired
protected UserRepository repository;
@Autowired
protected CassandraOperations dataOperations;
protected CassandraOperations template;
User tom, bob, alice, scott;
List<User> all;
// @BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
}
// @Before
@Before
public void setUp() throws InterruptedException {
repository.deleteAll();
@@ -94,10 +90,15 @@ public class UserRepositoryIntegrationTests {
scott.setPassword("444");
scott.setPlace("Boston");
all = dataOperations.insert(Arrays.asList(tom, bob, alice, scott));
all = template.insert(Arrays.asList(tom, bob, alice, scott));
}
// @Test
@After
public void after() {
repository.deleteAll();
}
@Test
public void findsUserById() throws Exception {
User user = repository.findOne(bob.getUsername());
@@ -106,7 +107,7 @@ public class UserRepositoryIntegrationTests {
}
// @Test
@Test
public void findsAll() throws Exception {
List<User> result = Lists.newArrayList(repository.findAll());
assertThat(result.size(), is(all.size()));
@@ -114,7 +115,7 @@ public class UserRepositoryIntegrationTests {
}
// @Test
@Test
public void findsAllWithGivenIds() {
Iterable<User> result = repository.findAll(Arrays.asList(bob.getUsername(), tom.getUsername()));
@@ -122,7 +123,7 @@ public class UserRepositoryIntegrationTests {
assertThat(result, not(hasItems(alice, scott)));
}
// @Test
@Test
public void deletesUserCorrectly() throws Exception {
repository.delete(tom);
@@ -133,7 +134,7 @@ public class UserRepositoryIntegrationTests {
assertThat(result, not(hasItem(tom)));
}
// @Test
@Test
public void deletesUserByIdCorrectly() {
repository.delete(tom.getUsername().toString());

View File

@@ -1,11 +1,18 @@
package org.springframework.data.cassandra.test.integration.repository;
import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.cassandra.test.integration.support.AbstractDataTestJavaConfig;
@Configuration
@EnableCassandraRepositories(basePackageClasses = UserRepository.class)
public class UserRepositoryIntegrationTestsConfig extends AbstractDataTestJavaConfig {
@Override
@@ -17,8 +24,18 @@ public class UserRepositoryIntegrationTestsConfig extends AbstractDataTestJavaCo
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
List<CreateKeyspaceSpecification> creates = new ArrayList<CreateKeyspaceSpecification>();
// TODO
creates.add(createKeyspace().name(getKeyspaceName()).withSimpleReplication());
return creates;
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.RECREATE;
}
@Override
public String getMappingBasePackage() {
return User.class.getPackage().getName();
}
}

View File

@@ -1,10 +1,10 @@
package org.springframework.data.cassandra.test.integration.support;
import org.springframework.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.config.java.AbstractSpringDataCassandraConfiguration;
public abstract class AbstractDataTestJavaConfig extends AbstractCassandraConfiguration {
public abstract class AbstractDataTestJavaConfig extends AbstractSpringDataCassandraConfiguration {
public static SpringDataBuildProperties PROPS = new SpringDataBuildProperties();
public static final SpringDataBuildProperties PROPS = new SpringDataBuildProperties();
public static final int PORT = PROPS.getCassandraPort();
@Override

View File

@@ -0,0 +1,11 @@
package org.springframework.data.cassandra.test.integration.table;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
/**
* Spring Data Cassandra marker interface for use with {@link EnableCassandraRepositories#basePackageClasses()}
*
* @author Matthew T. Adams
*/
public interface Domain {
}

View File

@@ -54,7 +54,7 @@ public class CassandraAdminTest {
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml");
/*
* Load data file to creat the test keyspace before we init the template

View File

@@ -66,7 +66,7 @@ public class CassandraDataOperationsTest {
private static Logger log = LoggerFactory.getLogger(CassandraDataOperationsTest.class);
public static final SpringDataBuildProperties PROPS = new SpringDataBuildProperties();
private final static String CASSANDRA_CONFIG = "cassandra.yaml";
private final static String CASSANDRA_CONFIG = "spring-cassandra.yaml";
private final static String KEYSPACE_NAME = "test";
private final static String CASSANDRA_HOST = "localhost";
private final static int CASSANDRA_NATIVE_PORT = PROPS.getCassandraPort();

View File

@@ -5,12 +5,12 @@
<encoder>
<!-- pattern>%d %5p %40.40c:%4L - %m%n</pattern-->
<pattern>%d %5p | %t | %-55logger{55} | %m | %n</pattern>
</encoder>
</appender>
<logger name="org.springframework.cassandra" level="info" />
<logger name="org.springframework.data.cassandra" level="info" />
<logger name="org.springframework.data.cassandra" level="info" />
<root level="warn">
<appender-ref ref="console" />

View File

@@ -43,7 +43,7 @@
cassandra-converter-ref="cassandra-converter" schema-action="CREATE">
<cassandra:mapping>
<cassandra:entity
class="org.springframework.data.cassandra.test.integration.table.User">
class="org.springframework.data.cassandra.test.integration.repository.User">
<cassandra:table name="users_x" />
</cassandra:entity>
</cassandra:mapping>