From 8edc4ce1a9da37938dc7b2e4976887b80d384a10 Mon Sep 17 00:00:00 2001 From: David T Webb Date: Tue, 29 Apr 2014 09:45:14 -0400 Subject: [PATCH] DATACASS-126 - Added DataStax Java Driver 2.0.1 dependency and cassandra-unit 2.0.X dependency. Full support for Cassandra 2.0.X --- pom.xml | 24 +++- spring-cql/pom.xml | 55 ++++++-- .../CassandraCqlClusterFactoryBean.java | 16 +-- .../CassandraCqlSessionFactoryBean.java | 2 +- .../config/xml/CassandraCqlClusterParser.java | 11 +- .../cassandra/core/CqlOperations.java | 28 ++-- .../cassandra/core/CqlTemplate.java | 33 +++-- .../CassandraConnectionFailureException.java | 8 +- .../cassandra/config/spring-cql-1.0.xsd | 8 -- ...tractEmbeddedCassandraIntegrationTest.java | 4 +- ...AbstractKeyspaceCreatingConfiguration.java | 8 +- .../core/template/CQLOperationsTest.java | 3 +- ...NamespaceCreatingXmlConfigTest-context.xml | 8 +- .../integration/config/xml/ppncxct.properties | 3 +- .../src/test/resources/spring-cassandra.yaml | 44 ------ .../convert/MappingCassandraConverter.java | 9 +- .../cassandra/core/CassandraOperations.java | 22 ++- .../cassandra/core/CassandraTemplate.java | 131 +++++++++++------- .../support/SimpleCassandraRepository.java | 2 +- .../cassandra/config/spring-cassandra-1.0.xsd | 8 -- .../CollectionsRowValueProviderTest.java | 4 +- ...ngBasedCassandraQueryIntegrationTests.java | 12 +- .../template/CassandraDataOperationsTest.java | 4 +- .../src/test/resources/logback-test.xml | 3 +- .../src/test/resources/spring-cassandra.yaml | 43 ------ 25 files changed, 246 insertions(+), 247 deletions(-) diff --git a/pom.xml b/pom.xml index 193fb14fc..12330103b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 @@ -31,8 +32,8 @@ multi spring-data-cassandra 1.8.0.BUILD-SNAPSHOT - 1.2.0.1 - 1.0.5-dse + 2.0.2.1 + 2.0.1 2.16 0.2.5 @@ -78,13 +79,21 @@ com.datastax.cassandra - cassandra-driver-core - ${cassandra-driver-core.version} + cassandra-driver-dse + ${cassandra-driver-dse.version} slf4j-log4j12 org.slf4j + + log4j + log4j + + + guava + com.google.guava + @@ -203,7 +212,7 @@ com.datastax.cassandra - cassandra-driver-core + cassandra-driver-dse com.github.stephenc @@ -286,7 +295,8 @@ ${failsafe.version} 1 - -Xmx1024m -Xss512m -javaagent:${com.github.stephenc:jamm:jar} + -Xmx1024m -Xss512m + -javaagent:${com.github.stephenc:jamm:jar} true false diff --git a/spring-cql/pom.xml b/spring-cql/pom.xml index 65a644cb2..ea141a6ba 100644 --- a/spring-cql/pom.xml +++ b/spring-cql/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 @@ -47,13 +48,7 @@ com.datastax.cassandra - cassandra-driver-core - - - log4j - log4j - - + cassandra-driver-dse javax.enterprise @@ -81,6 +76,50 @@ el-api test + + org.cassandraunit + cassandra-unit + + + slf4j-log4j12 + org.slf4j + + + guava + com.google.guava + + + + + org.apache.cassandra + cassandra-all + 2.0.6 + + + slf4j-log4j12 + org.slf4j + + + guava + com.google.guava + + + + + org.cassandraunit + cassandra-unit-spring + + + guava + com.google.guava + + + + + com.google.guava + guava + 15.0 + diff --git a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java index fd5b0bdc5..f6a7ca656 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java @@ -86,7 +86,6 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini private ReconnectionPolicy reconnectionPolicy; private RetryPolicy retryPolicy; private boolean metricsEnabled = DEFAULT_METRICS_ENABLED; - private boolean deferredInitialization = DEFAULT_DEFERRED_INITIALIZATION; private boolean jmxReportingEnabled = DEFAULT_JMX_REPORTING_ENABLED; private boolean sslEnabled = DEFAULT_SSL_ENABLED; private SSLOptions sslOptions; @@ -163,10 +162,6 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini builder.withRetryPolicy(retryPolicy); } - if (deferredInitialization) { - builder.withDeferredInitialization(); - } - if (!metricsEnabled) { builder.withoutMetrics(); } @@ -260,7 +255,7 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini } finally { if (system != null) { - system.shutdown(); + system.close(); } } } @@ -269,7 +264,7 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini public void destroy() throws Exception { executeSpecsAndScripts(keyspaceDrops, shutdownScripts); - cluster.shutdown(); + cluster.close(); } /** @@ -379,13 +374,6 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini this.password = password; } - /** - * @param deferredInitialization The deferredInitialization to set. - */ - public void setDeferredInitialization(boolean deferredInitialization) { - this.deferredInitialization = deferredInitialization; - } - /** * @param jmxReportingEnabled The jmxReportingEnabled to set. */ diff --git a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlSessionFactoryBean.java b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlSessionFactoryBean.java index af06451fc..1500de3ce 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlSessionFactoryBean.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlSessionFactoryBean.java @@ -108,7 +108,7 @@ public class CassandraCqlSessionFactoryBean implements FactoryBean, Ini public void destroy() throws Exception { executeScripts(shutdownScripts); - session.shutdown(); + session.close(); } /** diff --git a/spring-cql/src/main/java/org/springframework/cassandra/config/xml/CassandraCqlClusterParser.java b/spring-cql/src/main/java/org/springframework/cassandra/config/xml/CassandraCqlClusterParser.java index e0e2ff4ee..2b05e33dd 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/config/xml/CassandraCqlClusterParser.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/config/xml/CassandraCqlClusterParser.java @@ -15,11 +15,6 @@ */ package org.springframework.cassandra.config.xml; -import static org.springframework.cassandra.config.xml.ParsingUtils.addOptionalPropertyReference; -import static org.springframework.cassandra.config.xml.ParsingUtils.addOptionalPropertyValue; -import static org.springframework.cassandra.config.xml.ParsingUtils.addRequiredPropertyValue; -import static org.springframework.cassandra.config.xml.ParsingUtils.getSourceBeanDefinition; - import java.util.ArrayList; import java.util.List; @@ -46,6 +41,11 @@ import com.datastax.driver.core.HostDistance; import com.datastax.driver.core.PoolingOptions; import com.datastax.driver.core.SocketOptions; +import static org.springframework.cassandra.config.xml.ParsingUtils.addOptionalPropertyReference; +import static org.springframework.cassandra.config.xml.ParsingUtils.addOptionalPropertyValue; +import static org.springframework.cassandra.config.xml.ParsingUtils.addRequiredPropertyValue; +import static org.springframework.cassandra.config.xml.ParsingUtils.getSourceBeanDefinition; + /** * Parses the {@literal } element of the XML Configuration. * @@ -97,7 +97,6 @@ public class CassandraCqlClusterParser extends AbstractBeanDefinitionParser { addOptionalPropertyValue(builder, "compressionType", element, "compression", null); addOptionalPropertyValue(builder, "username", element, "username", null); addOptionalPropertyValue(builder, "password", element, "password", null); - addOptionalPropertyValue(builder, "deferredInitialization", element, "deferred-initialization", null); addOptionalPropertyValue(builder, "metricsEnabled", element, "metrics-enabled", null); addOptionalPropertyValue(builder, "jmxReportingEnabled", element, "jmx-reporting-enabled", null); addOptionalPropertyValue(builder, "sslEnabled", element, "ssl-enabled", null); diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/CqlOperations.java b/spring-cql/src/main/java/org/springframework/cassandra/core/CqlOperations.java index a8e51ad28..6b71f77d0 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/CqlOperations.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/CqlOperations.java @@ -32,10 +32,10 @@ import org.springframework.cassandra.core.keyspace.DropKeyspaceSpecification; import org.springframework.cassandra.core.keyspace.DropTableSpecification; import org.springframework.dao.DataAccessException; -import com.datastax.driver.core.Query; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.ResultSetFuture; import com.datastax.driver.core.Session; +import com.datastax.driver.core.Statement; import com.datastax.driver.core.querybuilder.Batch; import com.datastax.driver.core.querybuilder.Delete; import com.datastax.driver.core.querybuilder.Insert; @@ -78,9 +78,9 @@ public interface CqlOperations { /** * Executes the supplied Query and returns nothing. * - * @param query The {@link Query} to execute + * @param query The {@link Statement} to execute */ - void execute(Query query) throws DataAccessException; + void execute(Statement query) throws DataAccessException; /** * Executes the supplied Delete Query and returns nothing. @@ -206,44 +206,44 @@ public interface CqlOperations { /** * Executes the supplied CQL Batch Asynchronously and returns nothing. * - * @param query The {@link Query} to execute + * @param query The {@link Batch} to execute */ void executeAsynchronously(Batch batch) throws DataAccessException; /** * Executes the supplied CQL Query Asynchronously and returns nothing. * - * @param query The {@link Query} to execute + * @param query The {@link Statement} to execute */ - void executeAsynchronously(Query query) throws DataAccessException; + void executeAsynchronously(Statement query) throws DataAccessException; /** * Executes the supplied CQL Query Asynchronously and returns nothing. * - * @param query The {@link Query} to execute + * @param query The {@link Statement} to execute */ - void executeAsynchronously(Query query, Runnable runnable) throws DataAccessException; + void executeAsynchronously(Statement query, Runnable runnable) throws DataAccessException; /** * Executes the supplied CQL Query Asynchronously and returns nothing. * - * @param query The {@link Query} to execute + * @param query The {@link Statement} to execute */ - void executeAsynchronously(Query query, AsynchronousQueryListener listener) throws DataAccessException; + void executeAsynchronously(Statement query, AsynchronousQueryListener listener) throws DataAccessException; /** * Executes the supplied CQL Query Asynchronously and returns nothing. * - * @param query The {@link Query} to execute + * @param query The {@link Statement} to execute */ - void executeAsynchronously(Query query, Runnable runnable, Executor executor) throws DataAccessException; + void executeAsynchronously(Statement query, Runnable runnable, Executor executor) throws DataAccessException; /** * Executes the supplied CQL Query Asynchronously and returns nothing. * - * @param query The {@link Query} to execute + * @param query The {@link Statement} to execute */ - void executeAsynchronously(Query query, AsynchronousQueryListener listener, Executor executor) + void executeAsynchronously(Statement query, AsynchronousQueryListener listener, Executor executor) throws DataAccessException; /** diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/CqlTemplate.java b/spring-cql/src/main/java/org/springframework/cassandra/core/CqlTemplate.java index 52634405a..8d56be6b0 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/CqlTemplate.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/CqlTemplate.java @@ -57,7 +57,6 @@ import com.datastax.driver.core.ColumnDefinitions; import com.datastax.driver.core.ColumnDefinitions.Definition; import com.datastax.driver.core.Host; import com.datastax.driver.core.PreparedStatement; -import com.datastax.driver.core.Query; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.ResultSetFuture; import com.datastax.driver.core.Row; @@ -94,13 +93,13 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { protected static final Logger log = LoggerFactory.getLogger(CqlTemplate.class); /** - * Add common {@link Query} options for all types of queries. + * Add common {@link Statement} options for all types of queries. * * @param q * @param options - * @return the {@link Query} given. + * @return the {@link Statement} given. */ - public static Query addQueryOptions(Query q, QueryOptions options) { + public static Statement addQueryOptions(Statement q, QueryOptions options) { if (options == null) { return q; @@ -224,7 +223,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { } @Override - public void execute(Query query) throws DataAccessException { + public void execute(Statement query) throws DataAccessException { doExecute(query); } @@ -505,7 +504,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { * @param q The query to execute. * @param options The {@link QueryOptions}. May be null. */ - protected ResultSet doExecute(final Query q) { + protected ResultSet doExecute(final Statement q) { return doExecute(new SessionCallback() { @@ -521,7 +520,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { }); } - protected ResultSetFuture doExecuteAsync(final Query q) { + protected ResultSetFuture doExecuteAsync(final Statement q) { return doExecute(new SessionCallback() { @@ -669,12 +668,12 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { } @Override - public void executeAsynchronously(Query query) throws DataAccessException { + public void executeAsynchronously(Statement query) throws DataAccessException { doExecuteAsync(query); } @Override - public void executeAsynchronously(Query query, Runnable listener) throws DataAccessException { + public void executeAsynchronously(Statement query, Runnable listener) throws DataAccessException { executeAsynchronously(query, listener, new Executor() { @Override @@ -685,7 +684,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { } @Override - public void executeAsynchronously(Query query, AsynchronousQueryListener listener) throws DataAccessException { + public void executeAsynchronously(Statement query, AsynchronousQueryListener listener) throws DataAccessException { executeAsynchronously(query, listener, new Executor() { @Override @@ -696,7 +695,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { } @Override - public void executeAsynchronously(final Query query, final Runnable listener, final Executor executor) + public void executeAsynchronously(final Statement query, final Runnable listener, final Executor executor) throws DataAccessException { execute(new SessionCallback() { @Override @@ -709,8 +708,8 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { } @Override - public void executeAsynchronously(final Query query, final AsynchronousQueryListener listener, final Executor executor) - throws DataAccessException { + public void executeAsynchronously(final Statement query, final AsynchronousQueryListener listener, + final Executor executor) throws DataAccessException { execute(new SessionCallback() { @Override @@ -991,7 +990,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { @Override public void truncate(CqlIdentifier tableName) throws DataAccessException { Truncate truncate = QueryBuilder.truncate(tableName.toCql()); - doExecute(truncate.getQueryString(), null); + doExecute(truncate); } @Override @@ -1205,7 +1204,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { @Override public long count(CqlIdentifier tableName) { - return selectCount(QueryBuilder.select().countAll().from(tableName.toCql()).getQueryString()); + return selectCount(QueryBuilder.select().countAll().from(tableName.toCql())); } @Override @@ -1213,9 +1212,9 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations { return count(cqlId(tableName)); } - protected long selectCount(String countQuery) { + protected long selectCount(Select select) { - return query(countQuery, new ResultSetExtractor() { + return query(select, new ResultSetExtractor() { @Override public Long extractData(ResultSet rs) throws DriverException, DataAccessException { diff --git a/spring-cql/src/main/java/org/springframework/cassandra/support/exception/CassandraConnectionFailureException.java b/spring-cql/src/main/java/org/springframework/cassandra/support/exception/CassandraConnectionFailureException.java index c30cf3fb8..21b05bd16 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/support/exception/CassandraConnectionFailureException.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/support/exception/CassandraConnectionFailureException.java @@ -31,14 +31,14 @@ public class CassandraConnectionFailureException extends DataAccessResourceFailu private static final long serialVersionUID = 6299912054261646552L; - private final Map messagesByHost = new HashMap(); + private final Map messagesByHost = new HashMap(); - public CassandraConnectionFailureException(Map messagesByHost, String msg, Throwable cause) { + public CassandraConnectionFailureException(Map map, String msg, Throwable cause) { super(msg, cause); - this.messagesByHost.putAll(messagesByHost); + this.messagesByHost.putAll(map); } - public Map getMessagesByHost() { + public Map getMessagesByHost() { return Collections.unmodifiableMap(messagesByHost); } } diff --git a/spring-cql/src/main/resources/org/springframework/cassandra/config/spring-cql-1.0.xsd b/spring-cql/src/main/resources/org/springframework/cassandra/config/spring-cql-1.0.xsd index 54341d733..c7cece5c2 100644 --- a/spring-cql/src/main/resources/org/springframework/cassandra/config/spring-cql-1.0.xsd +++ b/spring-cql/src/main/resources/org/springframework/cassandra/config/spring-cql-1.0.xsd @@ -172,14 +172,6 @@ Determine whether or not to enable JMX Reporting. Defaults to true. ]]> - - - - - diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java index b6dd1b026..1a1dfb021 100644 --- a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java +++ b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java @@ -63,8 +63,8 @@ public class AbstractEmbeddedCassandraIntegrationTest { } @BeforeClass - public static void startCassandra() throws ConfigurationException, TTransportException, IOException, - InterruptedException { + public static void startCassandra() throws TTransportException, IOException, InterruptedException, + ConfigurationException { EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG); } diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java index 5aef05664..d404fc766 100644 --- a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java +++ b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java @@ -15,9 +15,6 @@ */ package org.springframework.cassandra.test.integration.config.java; -import static org.springframework.cassandra.core.cql.generator.CreateKeyspaceCqlGenerator.toCql; -import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace; - import org.springframework.cassandra.config.CassandraCqlSessionFactoryBean; import org.springframework.cassandra.config.java.AbstractSessionConfiguration; import org.springframework.context.annotation.Configuration; @@ -26,6 +23,9 @@ import org.springframework.util.StringUtils; import com.datastax.driver.core.KeyspaceMetadata; import com.datastax.driver.core.Session; +import static org.springframework.cassandra.core.cql.generator.CreateKeyspaceCqlGenerator.toCql; +import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace; + @Configuration public abstract class AbstractKeyspaceCreatingConfiguration extends AbstractSessionConfiguration { @@ -50,6 +50,6 @@ public abstract class AbstractKeyspaceCreatingConfiguration extends AbstractSess } system.execute(toCql(createKeyspace().name(keyspace).withSimpleReplication())); - system.shutdown(); + system.close(); } } diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/template/CQLOperationsTest.java b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/template/CQLOperationsTest.java index a359b89fa..e40461721 100644 --- a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/template/CQLOperationsTest.java +++ b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/template/CQLOperationsTest.java @@ -29,7 +29,6 @@ import org.cassandraunit.dataset.cql.ClassPathCQLDataSet; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.mortbay.log.Log; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cassandra.core.ConsistencyLevel; @@ -1145,7 +1144,7 @@ public class CQLOperationsTest extends AbstractKeyspaceCreatingIntegrationTest { @Test public void insertAndTruncateQueryObjectTest() { - Log.info("Starting Insert and Truncate Query Object Test"); + log.info("Starting Insert and Truncate Query Object Test"); String tableName = "truncate_test"; diff --git a/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/PropertyPlaceholderNamespaceCreatingXmlConfigTest-context.xml b/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/PropertyPlaceholderNamespaceCreatingXmlConfigTest-context.xml index 7184e41ba..4b17b0902 100644 --- a/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/PropertyPlaceholderNamespaceCreatingXmlConfigTest-context.xml +++ b/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/PropertyPlaceholderNamespaceCreatingXmlConfigTest-context.xml @@ -9,14 +9,16 @@ - + + @@ -35,9 +37,9 @@ diff --git a/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/ppncxct.properties b/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/ppncxct.properties index 5ecb62dcf..b8a4b7dd2 100644 --- a/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/ppncxct.properties +++ b/spring-cql/src/test/resources/org/springframework/cassandra/test/integration/config/xml/ppncxct.properties @@ -1,7 +1,6 @@ cluster.contactPoints=localhost cluster.port=@build.cassandra.native_transport_port@ cluster.compression=SNAPPY -cluster.deferredInit=true cluster.metricsEnabled=false cluster.jmxReportingEnabled=false cluster.reconnection.delayMillis=5000 @@ -12,7 +11,7 @@ dc1.name=DCJAX dc1.rf=2 dc2.name=DCCTL dc2.rf=3 -lb.policy.dcAware.remoteHosts=4 +lb.policy.dcAware.remoteHosts=0 lb.policy.dcAware.localDc=DCJAX auth.username=test auth.password=pass diff --git a/spring-cql/src/test/resources/spring-cassandra.yaml b/spring-cql/src/test/resources/spring-cassandra.yaml index b81dca685..dbc7a242e 100644 --- a/spring-cql/src/test/resources/spring-cassandra.yaml +++ b/spring-cql/src/test/resources/spring-cassandra.yaml @@ -1,6 +1,5 @@ # Cassandra storage config YAML -# NOTE: # See http://wiki.apache.org/cassandra/StorageConfiguration for # full explanations of configuration directives # /NOTE @@ -170,24 +169,6 @@ row_cache_save_period: 0 # Disabled by default, meaning all keys are going to be saved # row_cache_keys_to_save: 100 -# The provider for the row cache to use. -# -# Supported values are: ConcurrentLinkedHashCacheProvider, SerializingCacheProvider -# -# SerializingCacheProvider serialises the contents of the row and stores -# it in native memory, i.e., off the JVM Heap. Serialized rows take -# significantly less memory than "live" rows in the JVM, so you can cache -# more rows in a given memory footprint. And storing the cache off-heap -# means you can use smaller heap sizes, reducing the impact of GC pauses. -# Note however that when a row is requested from the row cache, it must be -# deserialized into the heap for use. -# -# It is also valid to specify the fully-qualified class name to a class -# that implements org.apache.cassandra.cache.IRowCacheProvider. -# -# Defaults to SerializingCacheProvider -row_cache_provider: SerializingCacheProvider - # saved caches saved_caches_directory: target/embeddedCassandra/saved_caches @@ -230,31 +211,6 @@ seed_provider: # Ex: ",," - seeds: "127.0.0.1" -# emergency pressure valve: each time heap usage after a full (CMS) -# garbage collection is above this fraction of the max, Cassandra will -# flush the largest memtables. -# -# Set to 1.0 to disable. Setting this lower than -# CMSInitiatingOccupancyFraction is not likely to be useful. -# -# RELYING ON THIS AS YOUR PRIMARY TUNING MECHANISM WILL WORK POORLY: -# it is most effective under light to moderate load, or read-heavy -# workloads; under truly massive write load, it will often be too -# little, too late. -flush_largest_memtables_at: 0.75 - -# emergency pressure valve #2: the first time heap usage after a full -# (CMS) garbage collection is above this fraction of the max, -# Cassandra will reduce cache maximum _capacity_ to the given fraction -# of the current _size_. Should usually be set substantially above -# flush_largest_memtables_at, since that will have less long-term -# impact on the system. -# -# Set to 1.0 to disable. Setting this lower than -# CMSInitiatingOccupancyFraction is not likely to be useful. -reduce_cache_sizes_at: 0.85 -reduce_cache_capacity_to: 0.6 - # For workloads with more data than can fit in memory, Cassandra's # bottleneck will be reads that need to fetch data from # disk. "concurrent_reads" should be set to (16 * number_of_drives) in diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java index 84fa61774..e3e432b4c 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java @@ -15,8 +15,6 @@ */ package org.springframework.data.cassandra.convert; -import static org.springframework.data.cassandra.repository.support.BasicMapId.*; - import java.io.Serializable; import java.util.Map; @@ -51,6 +49,8 @@ import com.datastax.driver.core.querybuilder.Insert; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Update; +import static org.springframework.data.cassandra.repository.support.BasicMapId.id; + /** * {@link CassandraConverter} that uses a {@link MappingContext} to do sophisticated mapping of domain objects to * {@link Row}. @@ -243,13 +243,18 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem Object value = wrapper.getProperty(prop, prop.getType()); + log.debug("prop.type -> " + prop.getType().getName()); + log.debug("prop.value -> " + value); + if (prop.isCompositePrimaryKey()) { + log.debug("prop is a compositeKey"); writeInsertFromWrapper(BeanWrapper.create(value, conversionService), insert, prop.getCompositePrimaryKeyEntity()); return; } if (value != null) { + log.debug(String.format("Adding insert.value [%s] - [%s]", prop.getColumnName().toCql(), value)); insert.value(prop.getColumnName().toCql(), value); } } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java index fae5dcd09..a7283deb9 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java @@ -23,6 +23,8 @@ import org.springframework.cassandra.core.WriteOptions; import org.springframework.cassandra.core.cql.CqlIdentifier; import org.springframework.data.cassandra.convert.CassandraConverter; +import com.datastax.driver.core.querybuilder.Select; + /** * Operations for interacting with Cassandra. These operations are used by the Repository implementation, but can also * be used directly when that is desired by the developer. @@ -50,10 +52,19 @@ public interface CassandraOperations extends CqlOperations { */ List select(String cql, Class type); + /** + * Execute the Select Query and convert to the list of entities + * + * @param select must not be {@literal null}. + * @param type must not be {@literal null}, mapped entity type. + * @return + */ + List select(Select select, Class type); + T selectOneById(Class type, Object id); /** - * Execute query and convert ResultSet to the entity + * Execute CQL and convert ResultSet to the entity * * @param query must not be {@literal null}. * @param type must not be {@literal null}, mapped entity type. @@ -61,6 +72,15 @@ public interface CassandraOperations extends CqlOperations { */ T selectOne(String cql, Class type); + /** + * Execute Select query and convert ResultSet to the entity + * + * @param query must not be {@literal null}. + * @param type must not be {@literal null}, mapped entity type. + * @return + */ + T selectOne(Select select, Class type); + boolean exists(Class type, Object id); long count(Class type); diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java index 68db1d2ec..dd8b7f680 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java @@ -153,7 +153,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Delete delete = QueryBuilder.delete().from(entity.getTableName().toCql()); appendIdCriteria(delete.where(), entity, id); - execute(delete.getQueryString()); + execute(delete); } @Override @@ -233,7 +233,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation @Override public List selectAll(Class type) { - return select(QueryBuilder.select().all().from(getTableName(type).toCql()).getQueryString(), type); + return select(QueryBuilder.select().all().from(getTableName(type).toCql()), type); } @Override @@ -245,6 +245,14 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation return select(cql, new CassandraConverterRowCallback(cassandraConverter, type)); } + @Override + public List select(Select select, Class type) { + + Assert.notNull(select); + + return select(select, new CassandraConverterRowCallback(cassandraConverter, type)); + } + @Override public List selectBySimpleIds(Class type, Iterable ids) { @@ -259,7 +267,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Select select = QueryBuilder.select().all().from(entity.getTableName().toCql()); select.where(QueryBuilder.in(entity.getIdProperty().getColumnName().toCql(), CollectionUtils.toArray(ids))); - return select(select.getQueryString(), type); + return select(select, type); } @Override @@ -276,7 +284,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Select select = QueryBuilder.select().all().from(entity.getTableName().toCql()); appendIdCriteria(select.where(), entity, id); - return selectOne(select.getQueryString(), type); + return selectOne(select, type); } protected interface ClauseCallback { @@ -353,6 +361,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation return selectOne(cql, new CassandraConverterRowCallback(cassandraConverter, type)); } + @Override + public T selectOne(Select select, Class type) { + return selectOne(select, new CassandraConverterRowCallback(cassandraConverter, type)); + } + @Override public List update(List entities) { return update(entities, null); @@ -421,6 +434,30 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation return result; } + protected List select(final Select query, CassandraConverterRowCallback readRowCallback) { + + ResultSet resultSet = doExecute(new SessionCallback() { + + @Override + public ResultSet doInSession(Session s) throws DataAccessException { + return s.execute(query); + } + }); + + if (resultSet == null) { + return null; + } + + List result = new ArrayList(); + Iterator iterator = resultSet.iterator(); + while (iterator.hasNext()) { + Row row = iterator.next(); + result.add(readRowCallback.doWith(row)); + } + + return result; + } + /** * @param query * @param readRowCallback @@ -445,6 +482,23 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation return null; } + protected T selectOne(Select query, CassandraConverterRowCallback readRowCallback) { + + ResultSet resultSet = query(query); + + Iterator iterator = resultSet.iterator(); + if (iterator.hasNext()) { + Row row = iterator.next(); + T result = readRowCallback.doWith(row); + if (iterator.hasNext()) { + throw new DuplicateKeyException("found two or more results in query " + query); + } + return result; + } + + return null; + } + /** * Perform the deletion on a list of objects * @@ -458,13 +512,10 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Batch b = createDeleteBatchQuery(getTableName(entities.get(0).getClass()).toCql(), entities, options, cassandraConverter); - String query = b.getQueryString(); - logger.debug(query); - if (asynchronously) { - executeAsynchronously(query); + executeAsynchronously(b); } else { - execute(query); + execute(b); } } @@ -474,13 +525,10 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Insert insert = createInsertQuery(getTableName(entity.getClass()).toCql(), entity, options, cassandraConverter); - String query = insert.getQueryString(); - logger.debug(query); - if (asynchronously) { - executeAsynchronously(query); + executeAsynchronously(insert); } else { - execute(query); + execute(insert); } return entity; @@ -493,13 +541,10 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Batch b = createInsertBatchQuery(getTableName(entities.get(0).getClass()).toCql(), entities, options, cassandraConverter); - String query = b.getQueryString(); - logger.debug(query); - if (asychronously) { - executeAsynchronously(query); + executeAsynchronously(b); } else { - execute(query); + execute(b); } return entities; @@ -521,13 +566,10 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Batch b = toUpdateBatchQuery(getTableName(entities.get(0).getClass()).toCql(), entities, options, cassandraConverter); - String query = b.getQueryString(); - logger.debug(query); - if (asychronously) { - executeAsynchronously(query); + executeAsynchronously(b); } else { - execute(query); + execute(b); } return entities; @@ -545,13 +587,10 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Delete delete = createDeleteQuery(getTableName(entity.getClass()).toCql(), entity, options, cassandraConverter); - String query = delete.getQueryString(); - logger.debug(query); - if (asynchronously) { - executeAsynchronously(query); + executeAsynchronously(delete); } else { - execute(query); + execute(delete); } } @@ -568,15 +607,12 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Assert.notNull(entity); - Update q = toUpdateQuery(getTableName(entity.getClass()).toCql(), entity, options, cassandraConverter); - - String query = q.getQueryString(); - logger.debug(query); + Update update = toUpdateQuery(getTableName(entity.getClass()).toCql(), entity, options, cassandraConverter); if (asychronously) { - executeAsynchronously(query); + executeAsynchronously(update); } else { - execute(query); + execute(update); } return entity; @@ -594,19 +630,19 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation public static Insert createInsertQuery(String tableName, Object objectToSave, WriteOptions options, EntityWriter entityWriter) { - Insert q = QueryBuilder.insertInto(tableName); + Insert insert = QueryBuilder.insertInto(tableName); /* * Write properties */ - entityWriter.write(objectToSave, q); + entityWriter.write(objectToSave, insert); /* * Add Query Options */ - CqlTemplate.addWriteOptions(q, options); + CqlTemplate.addWriteOptions(insert, options); - return q; + return insert; } /** @@ -621,19 +657,19 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation public static Update toUpdateQuery(String tableName, Object objectToSave, WriteOptions options, EntityWriter entityWriter) { - Update q = QueryBuilder.update(tableName); + Update update = QueryBuilder.update(tableName); /* * Write properties */ - entityWriter.write(objectToSave, q); + entityWriter.write(objectToSave, update); /* * Add Query Options */ - CqlTemplate.addWriteOptions(q, options); + CqlTemplate.addWriteOptions(update, options); - return q; + return update; } /** @@ -704,14 +740,15 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation EntityWriter entityWriter) { Delete.Selection ds = QueryBuilder.delete(); - Delete q = ds.from(tableName); - Where w = q.where(); + Delete delete = ds.from(tableName); + + Where w = delete.where(); entityWriter.write(object, w); - CqlTemplate.addQueryOptions(q, options); + CqlTemplate.addQueryOptions(delete, options); - return q; + return delete; } /** diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java index 8ead5a34c..017b92a0d 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java @@ -110,6 +110,6 @@ public class SimpleCassandraRepository implements Ty } protected List findAll(Select query) { - return template.select(query.getQueryString(), entityInformation.getJavaType()); + return template.select(query, entityInformation.getJavaType()); } } diff --git a/spring-data-cassandra/src/main/resources/org/springframework/data/cassandra/config/spring-cassandra-1.0.xsd b/spring-data-cassandra/src/main/resources/org/springframework/data/cassandra/config/spring-cassandra-1.0.xsd index 9f7282769..448702c70 100644 --- a/spring-data-cassandra/src/main/resources/org/springframework/data/cassandra/config/spring-cassandra-1.0.xsd +++ b/spring-data-cassandra/src/main/resources/org/springframework/data/cassandra/config/spring-cassandra-1.0.xsd @@ -181,14 +181,6 @@ Determine whether or not to enable JMX Reporting. Defaults to true. ]]> - - - - - diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderTest.java index 92e7dfad1..10bd10254 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderTest.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderTest.java @@ -85,7 +85,7 @@ public class CollectionsRowValueProviderTest extends AbstractSpringDataEmbeddedC Select select = QueryBuilder.select().all().from("bookHistory"); select.where(QueryBuilder.eq("isbn", "123456-1")); - BookHistory b = template.selectOne(select.getQueryString(), BookHistory.class); + BookHistory b = template.selectOne(select, BookHistory.class); Assert.assertNotNull(b.getCheckOuts()); @@ -127,7 +127,7 @@ public class CollectionsRowValueProviderTest extends AbstractSpringDataEmbeddedC Select select = QueryBuilder.select().all().from("bookReference"); select.where(QueryBuilder.eq("isbn", "123456-1")); - BookReference b = template.selectOne(select.getQueryString(), BookReference.class); + BookReference b = template.selectOne(select, BookReference.class); Assert.assertNotNull(b.getReferences()); Assert.assertNotNull(b.getBookmarks()); diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/querymethods/declared/StringBasedCassandraQueryIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/querymethods/declared/StringBasedCassandraQueryIntegrationTests.java index 7aa31f4d7..1edbc2007 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/querymethods/declared/StringBasedCassandraQueryIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/querymethods/declared/StringBasedCassandraQueryIntegrationTests.java @@ -15,10 +15,6 @@ */ package org.springframework.data.cassandra.test.integration.querymethods.declared; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.when; - import java.lang.reflect.Method; import org.junit.Before; @@ -40,6 +36,12 @@ import com.datastax.driver.core.SimpleStatement; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Select; +import static org.hamcrest.Matchers.is; + +import static org.junit.Assert.assertThat; + +import static org.mockito.Mockito.when; + /** * Unit tests for {@link StringBasedCassandraQuery}. */ @@ -74,6 +76,7 @@ public class StringBasedCassandraQueryIntegrationTests { String table = Person.class.getSimpleName().toLowerCase(); Select expected = QueryBuilder.select().all().from(table); + expected.setForceNoValues(true); expected.where(QueryBuilder.eq("lastname", "Matthews")); assertThat(actual.getQueryString(), is(expected.getQueryString())); @@ -93,6 +96,7 @@ public class StringBasedCassandraQueryIntegrationTests { String table = Person.class.getSimpleName().toLowerCase(); Select expected = QueryBuilder.select().all().from(table); + expected.setForceNoValues(true); expected.where(QueryBuilder.eq("lastname", "Matthews")).and(QueryBuilder.eq("firstname", "John")); assertThat(actual.getQueryString(), is(expected.getQueryString())); diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java index 48dc82ea5..12abe6aa0 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java @@ -616,7 +616,7 @@ public class CassandraDataOperationsTest extends AbstractSpringDataEmbeddedCassa Select select = QueryBuilder.select().all().from("book"); select.where(QueryBuilder.eq("isbn", "123456-1")); - Book b = template.selectOne(select.getQueryString(), Book.class); + Book b = template.selectOne(select, Book.class); log.debug("SingleSelect Book Title -> " + b.getTitle()); log.debug("SingleSelect Book Author -> " + b.getAuthor()); @@ -635,7 +635,7 @@ public class CassandraDataOperationsTest extends AbstractSpringDataEmbeddedCassa Select select = QueryBuilder.select().all().from("book"); - List bookz = template.select(select.getQueryString(), Book.class); + List bookz = template.select(select, Book.class); log.debug("Book Count -> " + bookz.size()); diff --git a/spring-data-cassandra/src/test/resources/logback-test.xml b/spring-data-cassandra/src/test/resources/logback-test.xml index d28b214b6..db36a4212 100644 --- a/spring-data-cassandra/src/test/resources/logback-test.xml +++ b/spring-data-cassandra/src/test/resources/logback-test.xml @@ -11,9 +11,10 @@ + - \ No newline at end of file + diff --git a/spring-data-cassandra/src/test/resources/spring-cassandra.yaml b/spring-data-cassandra/src/test/resources/spring-cassandra.yaml index 1fed13912..9a1cd59de 100644 --- a/spring-data-cassandra/src/test/resources/spring-cassandra.yaml +++ b/spring-data-cassandra/src/test/resources/spring-cassandra.yaml @@ -170,24 +170,6 @@ row_cache_save_period: 0 # Disabled by default, meaning all keys are going to be saved # row_cache_keys_to_save: 100 -# The provider for the row cache to use. -# -# Supported values are: ConcurrentLinkedHashCacheProvider, SerializingCacheProvider -# -# SerializingCacheProvider serialises the contents of the row and stores -# it in native memory, i.e., off the JVM Heap. Serialized rows take -# significantly less memory than "live" rows in the JVM, so you can cache -# more rows in a given memory footprint. And storing the cache off-heap -# means you can use smaller heap sizes, reducing the impact of GC pauses. -# Note however that when a row is requested from the row cache, it must be -# deserialized into the heap for use. -# -# It is also valid to specify the fully-qualified class name to a class -# that implements org.apache.cassandra.cache.IRowCacheProvider. -# -# Defaults to SerializingCacheProvider -row_cache_provider: SerializingCacheProvider - # saved caches saved_caches_directory: target/embeddedCassandra/saved_caches @@ -230,31 +212,6 @@ seed_provider: # Ex: ",," - seeds: "127.0.0.1" -# emergency pressure valve: each time heap usage after a full (CMS) -# garbage collection is above this fraction of the max, Cassandra will -# flush the largest memtables. -# -# Set to 1.0 to disable. Setting this lower than -# CMSInitiatingOccupancyFraction is not likely to be useful. -# -# RELYING ON THIS AS YOUR PRIMARY TUNING MECHANISM WILL WORK POORLY: -# it is most effective under light to moderate load, or read-heavy -# workloads; under truly massive write load, it will often be too -# little, too late. -flush_largest_memtables_at: 0.75 - -# emergency pressure valve #2: the first time heap usage after a full -# (CMS) garbage collection is above this fraction of the max, -# Cassandra will reduce cache maximum _capacity_ to the given fraction -# of the current _size_. Should usually be set substantially above -# flush_largest_memtables_at, since that will have less long-term -# impact on the system. -# -# Set to 1.0 to disable. Setting this lower than -# CMSInitiatingOccupancyFraction is not likely to be useful. -reduce_cache_sizes_at: 0.85 -reduce_cache_capacity_to: 0.6 - # For workloads with more data than can fit in memory, Cassandra's # bottleneck will be reads that need to fetch data from # disk. "concurrent_reads" should be set to (16 * number_of_drives) in