From 25ec3f59c811cd026c384c1bf79fc3e9a1de01ce Mon Sep 17 00:00:00 2001 From: David Webb Date: Fri, 6 Dec 2013 16:27:19 -0500 Subject: [PATCH 01/21] Code review cleanup. Commit test for new repo. --- .../cassandra/core/CassandraTemplate.java | 27 ++++++++++++------- .../cassandra/core/CqlProvider.java | 26 ------------------ .../core/PreparedStatementCreatorImpl.java | 6 +---- .../core/SimplePreparedStatementCreator.java | 6 +---- 4 files changed, 20 insertions(+), 45 deletions(-) delete mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/core/CqlProvider.java diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java index 026f7ff4f..fb3dc87d7 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -805,19 +806,27 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, java.util.List) */ @Override - public void ingest(String cql, List> rows, Map optionsByName) { + public void ingest(String cql, final List> rows, Map optionsByName) { Assert.notNull(optionsByName); Assert.notNull(rows); Assert.notEmpty(rows); - Object[][] values = new Object[rows.size()][]; - int i = 0; - for (List row : rows) { - values[i++] = row.toArray(); - } + ingest(cql, new RowIterator() { - ingest(cql, values, optionsByName); + Iterator> i = rows.iterator(); + + @Override + public Object[] next() { + return i.next().toArray(); + } + + @Override + public boolean hasNext() { + return i.hasNext(); + } + + }, optionsByName); } @@ -895,7 +904,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param q * @param optionsByName */ - public static void addQueryOptions(Query q, Map optionsByName) { + protected static void addQueryOptions(Query q, Map optionsByName) { if (optionsByName == null) { return; @@ -921,7 +930,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param q * @param optionsByName */ - public static void addPreparedStatementOptions(PreparedStatement s, Map optionsByName) { + protected static void addPreparedStatementOptions(PreparedStatement s, Map optionsByName) { if (optionsByName == null) { return; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CqlProvider.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CqlProvider.java deleted file mode 100644 index 7b0ddd59d..000000000 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CqlProvider.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2011-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.cassandra.core; - -/** - * @author David Webb - * - */ -public interface CqlProvider { - - String getCql(); - -} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/PreparedStatementCreatorImpl.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/PreparedStatementCreatorImpl.java index cbf5afd43..44a14f730 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/PreparedStatementCreatorImpl.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/PreparedStatementCreatorImpl.java @@ -26,7 +26,7 @@ import com.datastax.driver.core.exceptions.DriverException; * @author David Webb * */ -public class PreparedStatementCreatorImpl implements PreparedStatementCreator, CqlProvider, PreparedStatementBinder { +public class PreparedStatementCreatorImpl implements PreparedStatementCreator, PreparedStatementBinder { private final String cql; private List values; @@ -54,10 +54,6 @@ public class PreparedStatementCreatorImpl implements PreparedStatementCreator, C } - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CqlProvider#getCql() - */ - @Override public String getCql() { return this.cql; } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/SimplePreparedStatementCreator.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/SimplePreparedStatementCreator.java index f2ae91a5c..b3a63e158 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/SimplePreparedStatementCreator.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/SimplePreparedStatementCreator.java @@ -25,7 +25,7 @@ import com.datastax.driver.core.exceptions.DriverException; * @author David Webb * */ -public class SimplePreparedStatementCreator implements PreparedStatementCreator, CqlProvider { +public class SimplePreparedStatementCreator implements PreparedStatementCreator { private final String cql; @@ -39,10 +39,6 @@ public class SimplePreparedStatementCreator implements PreparedStatementCreator, this.cql = cql; } - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CqlProvider#getCql() - */ - @Override public String getCql() { return this.cql; } From 73b0d286864594c797201d4f370cafd40b553606 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 11:27:55 -0600 Subject: [PATCH 02/21] limiting native_transport_max_threads to 64 due to Mac limit --- spring-cassandra/src/test/resources/cassandra.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cassandra/src/test/resources/cassandra.yaml b/spring-cassandra/src/test/resources/cassandra.yaml index 82fcfc5ad..8fc6462aa 100644 --- a/spring-cassandra/src/test/resources/cassandra.yaml +++ b/spring-cassandra/src/test/resources/cassandra.yaml @@ -339,7 +339,7 @@ native_transport_port: 9042 # transport is used. They are similar to rpc_min_threads and rpc_max_threads, # though the defaults differ slightly. # native_transport_min_threads: 16 -# native_transport_max_threads: 128 +native_transport_max_threads: 64 # Whether to start the thrift rpc server. start_rpc: true From 22b6a53a88d4ba285ed2c9ddbf5e2bf1d81bd598 Mon Sep 17 00:00:00 2001 From: David Webb Date: Mon, 9 Dec 2013 13:30:51 -0500 Subject: [PATCH 03/21] DATACASS-32 : WIP : Removed optionsByName for Query Options override. --- .../cassandra/core/CassandraOperations.java | 210 --------- .../cassandra/core/CassandraTemplate.java | 432 ++++++------------ 2 files changed, 130 insertions(+), 512 deletions(-) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraOperations.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraOperations.java index 02d8d9ec6..31bee51a9 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraOperations.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraOperations.java @@ -66,19 +66,6 @@ public interface CassandraOperations { */ T query(final String cql, ResultSetExtractor rse) throws DataAccessException; - /** - * Executes the provided CQL Query, and extracts the results with the ResultSetExtractor. - * - * @param cql The Query - * @param rse The implementation for extracting the ResultSet - * @param optionsByName Query Options Map - * - * @return - * @throws DataAccessException - */ - T query(final String cql, ResultSetExtractor rse, final Map optionsByName) - throws DataAccessException; - /** * Executes the provided CQL Query, and extracts the results with the ResultSetExtractor. * @@ -101,18 +88,6 @@ public interface CassandraOperations { */ T queryAsynchronously(final String cql, ResultSetFutureExtractor rse) throws DataAccessException; - /** - * Executes the provided CQL Query asynchronously, and extracts the results with the ResultSetFutureExtractor - * - * @param cql The Query - * @param rse The implementation for extracting the future results - * @param optionsByName Query Options Map - * @return - * @throws DataAccessException - */ - T queryAsynchronously(final String cql, ResultSetFutureExtractor rse, final Map optionsByName) - throws DataAccessException; - /** * Executes the provided CQL Query asynchronously, and extracts the results with the ResultSetFutureExtractor * @@ -134,17 +109,6 @@ public interface CassandraOperations { */ void query(final String cql, RowCallbackHandler rch) throws DataAccessException; - /** - * Executes the provided CQL Query, and then processes the results with the RowCallbackHandler. - * - * @param cql The Query - * @param rch The implementation for processing the rows returned. - * @param options Query Options Map - * @throws DataAccessException - */ - void query(final String cql, RowCallbackHandler rch, final Map optionsByName) - throws DataAccessException; - /** * Executes the provided CQL Query, and then processes the results with the RowCallbackHandler. * @@ -176,18 +140,6 @@ public interface CassandraOperations { */ List query(final String cql, RowMapper rowMapper) throws DataAccessException; - /** - * Executes the provided CQL Query, and maps all Rows returned with the supplied RowMapper. - * - * @param cql The Query - * @param rowMapper The implementation for mapping all rows - * @param optionsByName Query Options Map - * @return List of processed by the RowMapper - * @throws DataAccessException - */ - List query(final String cql, RowMapper rowMapper, final Map optionsByName) - throws DataAccessException; - /** * Executes the provided CQL Query, and maps all Rows returned with the supplied RowMapper. * @@ -363,9 +315,6 @@ public interface CassandraOperations { */ T query(final String cql, PreparedStatementBinder psb, ResultSetExtractor rse) throws DataAccessException; - T query(final String cql, PreparedStatementBinder psb, ResultSetExtractor rse, - final Map optionsByName) throws DataAccessException; - T query(final String cql, PreparedStatementBinder psb, ResultSetExtractor rse, final QueryOptions options) throws DataAccessException; @@ -381,20 +330,6 @@ public interface CassandraOperations { */ void query(final String cql, PreparedStatementBinder psb, RowCallbackHandler rch) throws DataAccessException; - /** - * Converts the CQL provided into a {@link SimplePreparedStatementCreator}. Then, the PreparedStatementBinder will - * bind its values to the bind variables in the provided CQL String. The results of the PreparedStatement are - * processed with the RowCallbackHandler implementation provided and nothing is returned. - * - * @param cql The Query to Prepare - * @param psb The Binding implementation - * @param rch The RowCallbackHandler for processing the ResultSet - * @param optionsByName The Query Options Map - * @throws DataAccessException - */ - void query(final String cql, PreparedStatementBinder psb, RowCallbackHandler rch, - final Map optionsByName) throws DataAccessException; - /** * Converts the CQL provided into a {@link SimplePreparedStatementCreator}. Then, the PreparedStatementBinder will * bind its values to the bind variables in the provided CQL String. The results of the PreparedStatement are @@ -423,22 +358,6 @@ public interface CassandraOperations { */ List query(final String cql, PreparedStatementBinder psb, RowMapper rowMapper) throws DataAccessException; - /** - * Converts the CQL provided into a {@link SimplePreparedStatementCreator}. Then, the PreparedStatementBinder will - * bind its values to the bind variables in the provided CQL String. The results of the PreparedStatement are - * processed with the RowMapper implementation provided and a List is returned with elements of Type for each Row - * returned. - * - * @param cql The Query to Prepare - * @param psb The Binding implementation - * @param rowMapper The implementation for Mapping a Row to Type - * @param optionsByName The Query Options Map - * @return List of for each Row returned from the Query. - * @throws DataAccessException - */ - List query(final String cql, PreparedStatementBinder psb, RowMapper rowMapper, - final Map optionsByName) throws DataAccessException; - /** * Converts the CQL provided into a {@link SimplePreparedStatementCreator}. Then, the PreparedStatementBinder will * bind its values to the bind variables in the provided CQL String. The results of the PreparedStatement are @@ -467,20 +386,6 @@ public interface CassandraOperations { */ T query(PreparedStatementCreator psc, ResultSetExtractor rse) throws DataAccessException; - /** - * Uses the provided PreparedStatementCreator to prepare a new Session call. This can only be used for CQL - * Statements that do not have data binding. The results of the PreparedStatement are processed with - * ResultSetExtractor implementation provided by the Application Code. - * - * @param psc The implementation to create the PreparedStatement - * @param rse Implementation for extracting from the ResultSet - * @param optionsByName The Query Options Map - * @return Type which is the output of the ResultSetExtractor - * @throws DataAccessException - */ - T query(PreparedStatementCreator psc, ResultSetExtractor rse, final Map optionsByName) - throws DataAccessException; - /** * Uses the provided PreparedStatementCreator to prepare a new Session call. This can only be used for CQL * Statements that do not have data binding. The results of the PreparedStatement are processed with @@ -506,19 +411,6 @@ public interface CassandraOperations { */ void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException; - /** - * Uses the provided PreparedStatementCreator to prepare a new Session call. This can only be used for CQL - * Statements that do not have data binding. The results of the PreparedStatement are processed with - * RowCallbackHandler and nothing is returned. - * - * @param psc The implementation to create the PreparedStatement - * @param rch The implementation to process Results - * @param optionsByName The Query Options Map - * @throws DataAccessException - */ - void query(PreparedStatementCreator psc, RowCallbackHandler rch, final Map optionsByName) - throws DataAccessException; - /** * Uses the provided PreparedStatementCreator to prepare a new Session call. This can only be used for CQL * Statements that do not have data binding. The results of the PreparedStatement are processed with @@ -544,20 +436,6 @@ public interface CassandraOperations { */ List query(PreparedStatementCreator psc, RowMapper rowMapper) throws DataAccessException; - /** - * Uses the provided PreparedStatementCreator to prepare a new Session call. This can only be used for CQL - * Statements that do not have data binding. The results of the PreparedStatement are processed with RowMapper - * implementation provided and a List is returned with elements of Type for each Row returned. - * - * @param psc The implementation to create the PreparedStatement - * @param rowMapper The implementation for mapping each Row returned. - * @param optionsByName The Query Options Map - * @return List of Type mapped from each Row in the Results - * @throws DataAccessException - */ - List query(PreparedStatementCreator psc, RowMapper rowMapper, final Map optionsByName) - throws DataAccessException; - /** * Uses the provided PreparedStatementCreator to prepare a new Session call. This can only be used for CQL * Statements that do not have data binding. The results of the PreparedStatement are processed with RowMapper @@ -572,21 +450,6 @@ public interface CassandraOperations { List query(PreparedStatementCreator psc, RowMapper rowMapper, final QueryOptions options) throws DataAccessException; - /** - * Uses the provided PreparedStatementCreator to prepare a new Session call. Binds the values from the - * PreparedStatementBinder to the available bind variables. The results of the PreparedStatement are processed with - * ResultSetExtractor implementation provided by the Application Code. - * - * @param psc The implementation to create the PreparedStatement - * @param psb The implementation to bind variables to values - * @param rse Implementation for extracting from the ResultSet - * @param optionsByName The Query Options Map - * @return Type which is the output of the ResultSetExtractor - * @throws DataAccessException - */ - T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse, - final Map optionsByName) throws DataAccessException; - /** * Uses the provided PreparedStatementCreator to prepare a new Session call. Binds the values from the * PreparedStatementBinder to the available bind variables. The results of the PreparedStatement are processed with @@ -616,21 +479,6 @@ public interface CassandraOperations { T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse) throws DataAccessException; - /** - * Uses the provided PreparedStatementCreator to prepare a new Session call. Binds the values from the - * PreparedStatementBinder to the available bind variables. The results of the PreparedStatement are processed with - * RowCallbackHandler and nothing is returned. - * - * @param psc The implementation to create the PreparedStatement - * @param psb The implementation to bind variables to values - * @param rch The implementation to process Results - * @param optionsByName The Query Options Map - * @return Type which is the output of the ResultSetExtractor - * @throws DataAccessException - */ - void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch, - final Map optionsByName) throws DataAccessException; - /** * Uses the provided PreparedStatementCreator to prepare a new Session call. Binds the values from the * PreparedStatementBinder to the available bind variables. The results of the PreparedStatement are processed with @@ -660,21 +508,6 @@ public interface CassandraOperations { void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch) throws DataAccessException; - /** - * Uses the provided PreparedStatementCreator to prepare a new Session call. Binds the values from the - * PreparedStatementBinder to the available bind variables. The results of the PreparedStatement are processed with - * RowMapper implementation provided and a List is returned with elements of Type for each Row returned. - * - * @param psc The implementation to create the PreparedStatement - * @param psb The implementation to bind variables to values - * @param rowMapper The implementation for mapping each Row returned. - * @param optionsByName The Query Options Map - * @return Type which is the output of the ResultSetExtractor - * @throws DataAccessException - */ - List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowMapper rowMapper, - final Map optionsByName) throws DataAccessException; - /** * Uses the provided PreparedStatementCreator to prepare a new Session call. Binds the values from the * PreparedStatementBinder to the available bind variables. The results of the PreparedStatement are processed with @@ -729,21 +562,6 @@ public interface CassandraOperations { */ Session getSession(); - /** - * This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then - * all row values are bound to the single PreparedStatement and executed against the Session. - * - *

- * This is used internally by the other ingest() methods, but can be used if you want to write your own RowIterator. - * The Object[] length returned by the next() implementation must match the number of bind variables in the CQL. - *

- * - * @param cql The CQL - * @param rowIterator Implementation to provide the Object[] to be bound to the CQL. - * @param optionsByName The Query Options Map - */ - void ingest(String cql, RowIterator rowIterator, Map optionsByName); - /** * This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then * all row values are bound to the single PreparedStatement and executed against the Session. @@ -773,20 +591,6 @@ public interface CassandraOperations { */ void ingest(String cql, RowIterator rowIterator); - /** - * This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then - * all row values are bound to the single PreparedStatement and executed against the Session. - * - *

- * The List length must match the number of bind variables in the CQL. - *

- * - * @param cql The CQL - * @param rows List of List with data to bind to the CQL. - * @param optionsByName The Query Options Map - */ - void ingest(String cql, List> rows, Map optionsByName); - /** * This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then * all row values are bound to the single PreparedStatement and executed against the Session. @@ -814,20 +618,6 @@ public interface CassandraOperations { */ void ingest(String cql, List> rows); - /** - * This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then - * all row values are bound to the single PreparedStatement and executed against the Session. - * - *

- * The Object[] length of the nested array must match the number of bind variables in the CQL. - *

- * - * @param cql The CQL - * @param rows Object array of Object array of values to bind to the CQL. - * @param optionsByName The Query Options Map - */ - void ingest(String cql, Object[][] rows, Map optionsByName); - /** * This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then * all row values are bound to the single PreparedStatement and executed against the Session. diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java index fb3dc87d7..cb0d43913 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java @@ -17,7 +17,6 @@ package org.springframework.cassandra.core; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -31,7 +30,6 @@ import org.springframework.util.Assert; import com.datastax.driver.core.BoundStatement; import com.datastax.driver.core.ColumnDefinitions; import com.datastax.driver.core.ColumnDefinitions.Definition; -import com.datastax.driver.core.DataType; import com.datastax.driver.core.Host; import com.datastax.driver.core.Metadata; import com.datastax.driver.core.PreparedStatement; @@ -92,33 +90,23 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void execute(final String cql) throws DataAccessException { - doExecute(cql, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#queryAsynchronously(java.lang.String, org.springframework.cassandra.core.ResultSetFutureExtractor, java.util.Map) - */ - @Override - public T queryAsynchronously(final String cql, ResultSetFutureExtractor rse, - final Map optionsByName) throws DataAccessException { - return rse.extractData(execute(new SessionCallback() { - @Override - public ResultSetFuture doInSession(Session s) throws DataAccessException { - Statement statement = new SimpleStatement(cql); - addQueryOptions(statement, optionsByName); - return s.executeAsync(statement); - } - })); + doExecute(cql, null); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryAsynchronously(java.lang.String, org.springframework.cassandra.core.ResultSetFutureExtractor, org.springframework.cassandra.core.QueryOptions) */ @Override - public T queryAsynchronously(String cql, ResultSetFutureExtractor rse, QueryOptions options) + public T queryAsynchronously(final String cql, ResultSetFutureExtractor rse, final QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return queryAsynchronously(cql, rse, options.toMap()); + return rse.extractData(execute(new SessionCallback() { + @Override + public ResultSetFuture doInSession(Session s) throws DataAccessException { + Statement statement = new SimpleStatement(cql); + addQueryOptions(statement, options); + return s.executeAsync(statement); + } + })); } /* (non-Javadoc) @@ -126,18 +114,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public T queryAsynchronously(final String cql, ResultSetFutureExtractor rse) throws DataAccessException { - return queryAsynchronously(cql, rse, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.ResultSetExtractor) - */ - public T query(String cql, ResultSetExtractor rse, Map optionsByName) - throws DataAccessException { - Assert.notNull(cql); - Assert.notNull(optionsByName); - ResultSet rs = doExecute(cql, optionsByName); - return rse.extractData(rs); + return queryAsynchronously(cql, rse, null); } /* (non-Javadoc) @@ -145,7 +122,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public T query(String cql, ResultSetExtractor rse) throws DataAccessException { - return query(cql, rse, Collections. emptyMap()); + return query(cql, rse, null); } /* (non-Javadoc) @@ -153,16 +130,9 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public T query(String cql, ResultSetExtractor rse, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(cql, rse, options.toMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowCallbackHandler, java.util.Map) - */ - @Override - public void query(String cql, RowCallbackHandler rch, Map optionsByName) throws DataAccessException { - process(doExecute(cql, optionsByName), rch); + Assert.notNull(cql); + ResultSet rs = doExecute(cql, options); + return rse.extractData(rs); } /* (non-Javadoc) @@ -170,25 +140,14 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void query(String cql, RowCallbackHandler rch, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - query(cql, rch, options.toMap()); + process(doExecute(cql, options), rch); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowCallbackHandler) */ public void query(String cql, RowCallbackHandler rch) throws DataAccessException { - query(cql, rch, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowMapper, java.util.Map) - */ - @Override - public List query(String cql, RowMapper rowMapper, Map optionsByName) - throws DataAccessException { - Assert.notNull(optionsByName); - return process(doExecute(cql, optionsByName), rowMapper); + query(cql, rch, null); } /* (non-Javadoc) @@ -196,50 +155,49 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public List query(String cql, RowMapper rowMapper, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(cql, rowMapper, options.toMap()); + return process(doExecute(cql, options), rowMapper); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowMapper) */ public List query(String cql, RowMapper rowMapper) throws DataAccessException { - return query(cql, rowMapper, Collections. emptyMap()); + return query(cql, rowMapper, null); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryForList(java.lang.String) */ public List> queryForListOfMap(String cql) throws DataAccessException { - return processListOfMap(doExecute(cql, Collections. emptyMap())); + return processListOfMap(doExecute(cql, null)); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryForList(java.lang.String, java.lang.Class) */ public List queryForList(String cql, Class elementType) throws DataAccessException { - return processList(doExecute(cql, Collections. emptyMap()), elementType); + return processList(doExecute(cql, null), elementType); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryForMap(java.lang.String) */ public Map queryForMap(String cql) throws DataAccessException { - return processMap(doExecute(cql, Collections. emptyMap())); + return processMap(doExecute(cql, null)); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryForObject(java.lang.String, java.lang.Class) */ public T queryForObject(String cql, Class requiredType) throws DataAccessException { - return processOne(doExecute(cql, Collections. emptyMap()), requiredType); + return processOne(doExecute(cql, null), requiredType); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryForObject(java.lang.String, org.springframework.cassandra.core.RowMapper) */ public T queryForObject(String cql, RowMapper rowMapper) throws DataAccessException { - return processOne(doExecute(cql, Collections. emptyMap()), rowMapper); + return processOne(doExecute(cql, null), rowMapper); } /** @@ -267,7 +225,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param callback * @return */ - protected ResultSet doExecute(final String cql, final Map optionsByName) { + protected ResultSet doExecute(final String cql, final QueryOptions options) { logger.info(cql); @@ -276,7 +234,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public ResultSet doInSession(Session s) throws DataAccessException { SimpleStatement statement = new SimpleStatement(cql); - addQueryOptions(statement, optionsByName); + addQueryOptions(statement, options); return s.execute(statement); } }); @@ -288,13 +246,13 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param callback * @return */ - protected ResultSet doExecute(final BoundStatement bs, final Map optionsByName) { + protected ResultSet doExecute(final BoundStatement bs, final QueryOptions options) { return doExecute(new SessionCallback() { @Override public ResultSet doInSession(Session s) throws DataAccessException { - addQueryOptions(bs, optionsByName); + addQueryOptions(bs, options); return s.execute(bs); } }); @@ -326,7 +284,6 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe for (Definition def : cols.asList()) { String name = def.getName(); - DataType dataType = def.getType(); map.put(name, def.getType().deserialize(row.getBytesUnsafe(name))); } @@ -528,24 +485,13 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe return execute(new SimplePreparedStatementCreator(cql), action); } - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.ResultSetExtractor, java.util.Map) - */ - @Override - public T query(PreparedStatementCreator psc, ResultSetExtractor rse, Map optionsByName) - throws DataAccessException { - Assert.notNull(optionsByName); - return query(psc, null, rse, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.ResultSetExtractor, org.springframework.cassandra.core.QueryOptions) */ @Override public T query(PreparedStatementCreator psc, ResultSetExtractor rse, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(psc, rse, options.toMap()); + return query(psc, null, rse, options); } /* (non-Javadoc) @@ -553,17 +499,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public T query(PreparedStatementCreator psc, ResultSetExtractor rse) throws DataAccessException { - return query(psc, rse, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.RowCallbackHandler, java.util.Map) - */ - @Override - public void query(PreparedStatementCreator psc, RowCallbackHandler rch, Map optionsByName) - throws DataAccessException { - Assert.notNull(optionsByName); - query(psc, null, rch, optionsByName); + return query(psc, rse, null); } /* (non-Javadoc) @@ -572,8 +508,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public void query(PreparedStatementCreator psc, RowCallbackHandler rch, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - query(psc, rch, options.toMap()); + query(psc, null, rch, options); } /* (non-Javadoc) @@ -581,17 +516,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException { - query(psc, rch, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.RowMapper, java.util.Map) - */ - @Override - public List query(PreparedStatementCreator psc, RowMapper rowMapper, Map optionsByName) - throws DataAccessException { - Assert.notNull(optionsByName); - return query(psc, null, rowMapper, optionsByName); + query(psc, rch, null); } /* (non-Javadoc) @@ -600,8 +525,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public List query(PreparedStatementCreator psc, RowMapper rowMapper, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(psc, rowMapper, options.toMap()); + return query(psc, null, rowMapper, options); } /* (non-Javadoc) @@ -609,41 +533,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public List query(PreparedStatementCreator psc, RowMapper rowMapper) throws DataAccessException { - return query(psc, rowMapper, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.PreparedStatementSetter, org.springframework.cassandra.core.ResultSetExtractor) - */ - public T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse, - final Map optionsByName) throws DataAccessException { - - Assert.notNull(rse, "ResultSetExtractor must not be null"); - logger.debug("Executing prepared CQL query"); - - return execute(psc, new PreparedStatementCallback() { - public T doInPreparedStatement(PreparedStatement ps) throws DriverException { - ResultSet rs = null; - BoundStatement bs = null; - if (psb != null) { - bs = psb.bindValues(ps); - } else { - bs = ps.bind(); - } - rs = doExecute(bs, optionsByName); - return rse.extractData(rs); - } - }); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.ResultSetExtractor, java.util.Map) - */ - @Override - public T query(String cql, PreparedStatementBinder psb, ResultSetExtractor rse, - Map optionsByName) throws DataAccessException { - Assert.notNull(optionsByName); - return query(new SimplePreparedStatementCreator(cql), psb, rse, optionsByName); + return query(psc, rowMapper, null); } /* (non-Javadoc) @@ -652,8 +542,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public T query(String cql, PreparedStatementBinder psb, ResultSetExtractor rse, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(cql, psb, rse, options.toMap()); + return query(new SimplePreparedStatementCreator(cql), psb, rse, options); } /* (non-Javadoc) @@ -661,17 +550,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public T query(String cql, PreparedStatementBinder psb, ResultSetExtractor rse) throws DataAccessException { - return query(cql, psb, rse, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowCallbackHandler, java.util.Map) - */ - @Override - public void query(String cql, PreparedStatementBinder psb, RowCallbackHandler rch, Map optionsByName) - throws DataAccessException { - Assert.notNull(optionsByName); - query(new SimplePreparedStatementCreator(cql), psb, rch, optionsByName); + return query(cql, psb, rse, null); } /* (non-Javadoc) @@ -680,8 +559,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public void query(String cql, PreparedStatementBinder psb, RowCallbackHandler rch, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - query(cql, psb, rch, options.toMap()); + query(new SimplePreparedStatementCreator(cql), psb, rch, options); } /* (non-Javadoc) @@ -689,17 +567,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void query(String cql, PreparedStatementBinder psb, RowCallbackHandler rch) throws DataAccessException { - query(cql, psb, rch, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowMapper, java.util.Map) - */ - @Override - public List query(String cql, PreparedStatementBinder psb, RowMapper rowMapper, - Map optionsByName) throws DataAccessException { - Assert.notNull(optionsByName); - return query(new SimplePreparedStatementCreator(cql), psb, rowMapper, optionsByName); + query(cql, psb, rch, null); } /* (non-Javadoc) @@ -708,8 +576,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public List query(String cql, PreparedStatementBinder psb, RowMapper rowMapper, QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(cql, psb, rowMapper, options.toMap()); + return query(new SimplePreparedStatementCreator(cql), psb, rowMapper, options); } /* (non-Javadoc) @@ -717,72 +584,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public List query(String cql, PreparedStatementBinder psb, RowMapper rowMapper) throws DataAccessException { - return query(cql, psb, rowMapper, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowCallbackHandler) - */ - @Override - public void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch, - final Map optionsByName) throws DataAccessException { - Assert.notNull(rch, "RowCallbackHandler must not be null"); - logger.debug("Executing prepared CQL query"); - - execute(psc, new PreparedStatementCallback() { - public Object doInPreparedStatement(PreparedStatement ps) throws DriverException { - ResultSet rs = null; - BoundStatement bs = null; - if (psb != null) { - bs = psb.bindValues(ps); - } else { - bs = ps.bind(); - } - rs = doExecute(bs, optionsByName); - process(rs, rch); - return null; - } - }); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowMapper) - */ - @Override - public List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, - final RowMapper rowMapper, final Map optionsByName) throws DataAccessException { - Assert.notNull(rowMapper, "RowMapper must not be null"); - logger.debug("Executing prepared CQL query"); - - return execute(psc, new PreparedStatementCallback>() { - public List doInPreparedStatement(PreparedStatement ps) throws DriverException { - ResultSet rs = null; - BoundStatement bs = null; - if (psb != null) { - bs = psb.bindValues(ps); - } else { - bs = ps.bind(); - } - rs = doExecute(bs, optionsByName); - - return process(rs, rowMapper); - } - }); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, org.springframework.cassandra.core.RowProvider, int) - */ - @Override - public void ingest(String cql, RowIterator rowIterator, Map optionsByName) { - - PreparedStatement preparedStatement = getSession().prepare(cql); - addPreparedStatementOptions(preparedStatement, optionsByName); - - while (rowIterator.hasNext()) { - getSession().execute(preparedStatement.bind(rowIterator.next())); - } - + return query(cql, psb, rowMapper, null); } /* (non-Javadoc) @@ -790,8 +592,13 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void ingest(String cql, RowIterator rowIterator, QueryOptions options) { - Assert.notNull(options); - ingest(cql, rowIterator, options.toMap()); + + PreparedStatement preparedStatement = getSession().prepare(cql); + addPreparedStatementOptions(preparedStatement, options); + + while (rowIterator.hasNext()) { + getSession().execute(preparedStatement.bind(rowIterator.next())); + } } /* (non-Javadoc) @@ -799,16 +606,15 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void ingest(String cql, RowIterator rowIterator) { - ingest(cql, rowIterator, Collections. emptyMap()); + ingest(cql, rowIterator, null); } /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, java.util.List) + * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, java.util.List, org.springframework.cassandra.core.QueryOptions) */ @Override - public void ingest(String cql, final List> rows, Map optionsByName) { + public void ingest(String cql, final List> rows, QueryOptions options) { - Assert.notNull(optionsByName); Assert.notNull(rows); Assert.notEmpty(rows); @@ -826,34 +632,23 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe return i.hasNext(); } - }, optionsByName); + }, options); } - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, java.util.List, org.springframework.cassandra.core.QueryOptions) - */ - @Override - public void ingest(String cql, List> rows, QueryOptions options) { - Assert.notNull(options); - ingest(cql, rows, options.toMap()); - } - /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, java.util.List) */ @Override public void ingest(String cql, List> rows) { - ingest(cql, rows, Collections. emptyMap()); + ingest(cql, rows, null); } /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, java.lang.Object[][]) + * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, java.lang.Object[][], org.springframework.cassandra.core.QueryOptions) */ @Override - public void ingest(String cql, final Object[][] rows, final Map optionsByName) { - - Assert.notNull(optionsByName); + public void ingest(String cql, final Object[][] rows, QueryOptions options) { ingest(cql, new RowIterator() { @@ -869,16 +664,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe return index < rows.length; } - }, optionsByName); - } - - /* (non-Javadoc) - * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, java.lang.Object[][], org.springframework.cassandra.core.QueryOptions) - */ - @Override - public void ingest(String cql, final Object[][] rows, QueryOptions options) { - Assert.notNull(options); - ingest(cql, rows, options.toMap()); + }, options); } /* (non-Javadoc) @@ -886,7 +672,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void ingest(String cql, final Object[][] rows) { - ingest(cql, rows, Collections. emptyMap()); + ingest(cql, rows, null); } /* (non-Javadoc) @@ -895,7 +681,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public void truncate(String tableName) throws DataAccessException { Truncate truncate = QueryBuilder.truncate(tableName); - doExecute(truncate.getQueryString(), Collections. emptyMap()); + doExecute(truncate.getQueryString(), null); } /** @@ -904,22 +690,20 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param q * @param optionsByName */ - protected static void addQueryOptions(Query q, Map optionsByName) { + protected static void addQueryOptions(Query q, QueryOptions options) { - if (optionsByName == null) { + if (options == null) { return; } /* * Add Query Options */ - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL) != null) { - q.setConsistencyLevel(ConsistencyLevelResolver.resolve((ConsistencyLevel) optionsByName - .get(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL))); + if (options.getConsistencyLevel() != null) { + q.setConsistencyLevel(ConsistencyLevelResolver.resolve(options.getConsistencyLevel())); } - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY) != null) { - q.setRetryPolicy(RetryPolicyResolver.resolve((RetryPolicy) optionsByName - .get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY))); + if (options.getRetryPolicy() != null) { + q.setRetryPolicy(RetryPolicyResolver.resolve(options.getRetryPolicy())); } } @@ -930,22 +714,20 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param q * @param optionsByName */ - protected static void addPreparedStatementOptions(PreparedStatement s, Map optionsByName) { + protected static void addPreparedStatementOptions(PreparedStatement s, QueryOptions options) { - if (optionsByName == null) { + if (options == null) { return; } /* * Add Query Options */ - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL) != null) { - s.setConsistencyLevel(ConsistencyLevelResolver.resolve((ConsistencyLevel) optionsByName - .get(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL))); + if (options.getConsistencyLevel() != null) { + s.setConsistencyLevel(ConsistencyLevelResolver.resolve(options.getConsistencyLevel())); } - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY) != null) { - s.setRetryPolicy(RetryPolicyResolver.resolve((RetryPolicy) optionsByName - .get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY))); + if (options.getRetryPolicy() != null) { + s.setRetryPolicy(RetryPolicyResolver.resolve(options.getRetryPolicy())); } } @@ -954,10 +736,25 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.ResultSetExtractor, org.springframework.cassandra.core.QueryOptions) */ @Override - public T query(PreparedStatementCreator psc, PreparedStatementBinder psb, ResultSetExtractor rse, - QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(psc, psb, rse, options.toMap()); + public T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse, + final QueryOptions options) throws DataAccessException { + + Assert.notNull(rse, "ResultSetExtractor must not be null"); + logger.debug("Executing prepared CQL query"); + + return execute(psc, new PreparedStatementCallback() { + public T doInPreparedStatement(PreparedStatement ps) throws DriverException { + ResultSet rs = null; + BoundStatement bs = null; + if (psb != null) { + bs = psb.bindValues(ps); + } else { + bs = ps.bind(); + } + rs = doExecute(bs, options); + return rse.extractData(rs); + } + }); } /* (non-Javadoc) @@ -966,17 +763,33 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public T query(PreparedStatementCreator psc, PreparedStatementBinder psb, ResultSetExtractor rse) throws DataAccessException { - return query(psc, psb, rse, Collections. emptyMap()); + return query(psc, psb, rse, null); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowCallbackHandler, org.springframework.cassandra.core.QueryOptions) */ @Override - public void query(PreparedStatementCreator psc, PreparedStatementBinder psb, RowCallbackHandler rch, - QueryOptions options) throws DataAccessException { - Assert.notNull(options); - query(psc, psb, rch, options.toMap()); + public void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch, + final QueryOptions options) throws DataAccessException { + + Assert.notNull(rch, "RowCallbackHandler must not be null"); + logger.debug("Executing prepared CQL query"); + + execute(psc, new PreparedStatementCallback() { + public Object doInPreparedStatement(PreparedStatement ps) throws DriverException { + ResultSet rs = null; + BoundStatement bs = null; + if (psb != null) { + bs = psb.bindValues(ps); + } else { + bs = ps.bind(); + } + rs = doExecute(bs, options); + process(rs, rch); + return null; + } + }); } /* (non-Javadoc) @@ -985,17 +798,32 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public void query(PreparedStatementCreator psc, PreparedStatementBinder psb, RowCallbackHandler rch) throws DataAccessException { - query(psc, psb, rch, Collections. emptyMap()); + query(psc, psb, rch, null); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowMapper, org.springframework.cassandra.core.QueryOptions) */ @Override - public List query(PreparedStatementCreator psc, PreparedStatementBinder psb, RowMapper rowMapper, - QueryOptions options) throws DataAccessException { - Assert.notNull(options); - return query(psc, psb, rowMapper, options.toMap()); + public List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, + final RowMapper rowMapper, final QueryOptions options) throws DataAccessException { + Assert.notNull(rowMapper, "RowMapper must not be null"); + logger.debug("Executing prepared CQL query"); + + return execute(psc, new PreparedStatementCallback>() { + public List doInPreparedStatement(PreparedStatement ps) throws DriverException { + ResultSet rs = null; + BoundStatement bs = null; + if (psb != null) { + bs = psb.bindValues(ps); + } else { + bs = ps.bind(); + } + rs = doExecute(bs, options); + + return process(rs, rowMapper); + } + }); } /* (non-Javadoc) @@ -1004,7 +832,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public List query(PreparedStatementCreator psc, PreparedStatementBinder psb, RowMapper rowMapper) throws DataAccessException { - return query(psc, psb, rowMapper, Collections. emptyMap()); + return query(psc, psb, rowMapper, null); } } \ No newline at end of file From a177562b277c9ea2a9ccf4e1fcb7128dc9583bca Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 13:16:16 -0600 Subject: [PATCH 04/21] wip: moved base c* config to spring-cassandra --- .../AbstractCassandraConfiguration.java | 69 +++++++++++++++++++ ...ractSpringDataCassandraConfiguration.java} | 61 ++-------------- .../test/integration/config/TestConfig.java | 4 +- 3 files changed, 78 insertions(+), 56 deletions(-) create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java rename spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/{AbstractCassandraConfiguration.java => AbstractSpringDataCassandraConfiguration.java} (79%) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java new file mode 100644 index 000000000..3c3465bd7 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java @@ -0,0 +1,69 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config; + +import org.springframework.cassandra.core.CassandraOperations; +import org.springframework.cassandra.core.CassandraTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; + +/** + * Base class for Spring Cassandra configuration using JavaConfig. + * + * @author Alex Shvid + * @author Matthew T. Adams + */ +@Configuration +public abstract class AbstractCassandraConfiguration { + + /** + * The name of the keyspace to connect to. If {@literal null} or empty, then the system keyspace will be used. + */ + protected abstract String getKeyspaceName(); + + /** + * The {@link Cluster} instance to connect to. Must not be null. + */ + @Bean + public abstract Cluster cluster(); + + /** + * Creates a {@link Session} using the {@link Cluster} instance configured in {@link #cluster()}. + * + * @see #cluster() + */ + @Bean + public Session session() { + String keyspace = getKeyspaceName(); + if (StringUtils.hasText(keyspace)) { + return cluster().connect(keyspace); + } else { + return cluster().connect(); + } + } + + /** + * A {@link CassandraTemplate} created from the {@link Session} returned by {@link #session()}. + */ + @Bean + public CassandraOperations cassandraTemplate() { + return new CassandraTemplate(session()); + } +} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractCassandraConfiguration.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java similarity index 79% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractCassandraConfiguration.java rename to spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java index c8cc1b262..51ba42aab 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractCassandraConfiguration.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java @@ -20,6 +20,7 @@ import java.util.Set; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.cassandra.config.AbstractCassandraConfiguration; import org.springframework.cassandra.core.CassandraOperations; import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.context.annotation.Bean; @@ -47,51 +48,14 @@ import com.datastax.driver.core.Session; * Base class for Spring Data Cassandra configuration using JavaConfig. * * @author Alex Shvid + * @author Matthew T. Adams */ @Configuration -public abstract class AbstractCassandraConfiguration implements BeanClassLoaderAware { - - /** - * Used by CassandraTemplate and CassandraAdminTemplate - */ +public abstract class AbstractSpringDataCassandraConfiguration extends AbstractCassandraConfiguration implements + BeanClassLoaderAware { private ClassLoader beanClassLoader; - /** - * Return the name of the keyspace to connect to. - * - * @return must not be {@literal null}. - */ - protected abstract String getKeyspaceName(); - - /** - * Return the {@link Cluster} instance to connect to. - * - * @return - * @throws Exception - */ - @Bean - public abstract Cluster cluster() throws Exception; - - /** - * Creates a {@link Session} to be used by the {@link SpringDataKeyspace}. Will use the {@link Cluster} instance - * configured in {@link #cluster()}. - * - * @see #cluster() - * @see #Keyspace() - * @return - * @throws Exception - */ - @Bean - public Session session() throws Exception { - String keyspace = getKeyspaceName(); - if (StringUtils.hasText(keyspace)) { - return cluster().connect(keyspace); - } else { - return cluster().connect(); - } - } - /** * Creates a {@link SpringDataKeyspace} to be used by the {@link CassandraTemplate}. Will use the {@link Session} * instance configured in {@link #session()} and {@link CassandraConverter} configured in {@link #converter()}. @@ -109,8 +73,8 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA /** * Return the base package to scan for mapped {@link Table}s. Will return the package name of the configuration class' * (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending - * {@link AbstractCassandraConfiguration} the base package will be considered {@code com.acme} unless the method is - * overriden to implement alternate behaviour. + * {@link AbstractSpringDataCassandraConfiguration} the base package will be considered {@code com.acme} unless the + * method is overriden to implement alternate behaviour. * * @return the base package to scan for mapped {@link Table} classes or {@literal null} to not enable scanning for * entities. @@ -119,17 +83,6 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA return getClass().getPackage().getName(); } - /** - * Creates a {@link CassandraTemplate}. - * - * @return - * @throws Exception - */ - @Bean - public CassandraOperations cassandraTemplate() throws Exception { - return new CassandraTemplate(session()); - } - /** * Creates a {@link CassandraAdminTemplate}. * @@ -185,7 +138,7 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) { initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), - AbstractCassandraConfiguration.class.getClassLoader())); + AbstractSpringDataCassandraConfiguration.class.getClassLoader())); } } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java index 415f5393e..aec114209 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java @@ -5,7 +5,7 @@ import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.cassandra.core.SessionFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.cassandra.config.AbstractCassandraConfiguration; +import org.springframework.data.cassandra.config.AbstractSpringDataCassandraConfiguration; import org.springframework.data.cassandra.core.CassandraDataOperations; import org.springframework.data.cassandra.core.CassandraDataTemplate; import org.springframework.data.cassandra.core.CassandraKeyspaceFactoryBean; @@ -20,7 +20,7 @@ import com.datastax.driver.core.Cluster.Builder; * */ @Configuration -public class TestConfig extends AbstractCassandraConfiguration { +public class TestConfig extends AbstractSpringDataCassandraConfiguration { public static final String keyspace = "test"; From 98389dd40a0233a5c5dad13b8369ed6c6bfe93ed Mon Sep 17 00:00:00 2001 From: David Webb Date: Mon, 9 Dec 2013 15:21:10 -0500 Subject: [PATCH 05/21] DATACASS-32 : Completed : Refactoring completed after code review. --- .../cassandra/core/CassandraTemplate.java | 4 +- .../core/CassandraDataOperations.java | 185 --------- .../cassandra/core/CassandraDataTemplate.java | 384 ++++-------------- .../data/cassandra/util/CqlUtils.java | 76 ++-- .../template/CassandraDataOperationsTest.java | 195 +-------- 5 files changed, 101 insertions(+), 743 deletions(-) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java index cb0d43913..98fb7203a 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java @@ -690,7 +690,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param q * @param optionsByName */ - protected static void addQueryOptions(Query q, QueryOptions options) { + public static void addQueryOptions(Query q, QueryOptions options) { if (options == null) { return; @@ -714,7 +714,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param q * @param optionsByName */ - protected static void addPreparedStatementOptions(PreparedStatement s, QueryOptions options) { + public static void addPreparedStatementOptions(PreparedStatement s, QueryOptions options) { if (options == null) { return; diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataOperations.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataOperations.java index 4e352eec1..217052354 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataOperations.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataOperations.java @@ -16,7 +16,6 @@ package org.springframework.data.cassandra.core; import java.util.List; -import java.util.Map; import org.springframework.cassandra.core.QueryOptions; import org.springframework.data.cassandra.convert.CassandraConverter; @@ -122,22 +121,6 @@ public interface CassandraDataOperations { */ T insert(T entity, QueryOptions options); - /** - * @param entity - * @param tableName - * @param options - * @return - */ - T insert(T entity, Map optionsByName); - - /** - * @param entity - * @param tableName - * @param optionsByName - * @return - */ - T insert(T entity, String tableName, Map optionsByName); - /** * Insert the given list of objects to the table by annotation table name. * @@ -163,14 +146,6 @@ public interface CassandraDataOperations { */ List insert(List entities, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List insert(List entities, Map optionsByName); - /** * @param entities * @param tableName @@ -179,14 +154,6 @@ public interface CassandraDataOperations { */ List insert(List entities, String tableName, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List insert(List entities, String tableName, Map optionsByName); - /** * Insert the given object to the table by id. * @@ -209,14 +176,6 @@ public interface CassandraDataOperations { */ T insertAsynchronously(T entity, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - * @return - */ - T insertAsynchronously(T entity, Map optionsByName); - /** * @param entity * @param tableName @@ -225,14 +184,6 @@ public interface CassandraDataOperations { */ T insertAsynchronously(T entity, String tableName, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - * @return - */ - T insertAsynchronously(T entity, String tableName, Map optionsByName); - /** * Insert the given object to the table by id. * @@ -255,14 +206,6 @@ public interface CassandraDataOperations { */ List insertAsynchronously(List entities, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List insertAsynchronously(List entities, Map optionsByName); - /** * @param entities * @param tableName @@ -271,14 +214,6 @@ public interface CassandraDataOperations { */ List insertAsynchronously(List entities, String tableName, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List insertAsynchronously(List entities, String tableName, Map optionsByName); - /** * Insert the given object to the table by id. * @@ -301,14 +236,6 @@ public interface CassandraDataOperations { */ T update(T entity, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - * @return - */ - T update(T entity, Map optionsByName); - /** * @param entity * @param tableName @@ -317,14 +244,6 @@ public interface CassandraDataOperations { */ T update(T entity, String tableName, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - * @return - */ - T update(T entity, String tableName, Map optionsByName); - /** * Insert the given object to the table by id. * @@ -347,14 +266,6 @@ public interface CassandraDataOperations { */ List update(List entities, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List update(List entities, Map optionsByName); - /** * @param entities * @param tableName @@ -363,14 +274,6 @@ public interface CassandraDataOperations { */ List update(List entities, String tableName, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List update(List entities, String tableName, Map optionsByName); - /** * Insert the given object to the table by id. * @@ -393,14 +296,6 @@ public interface CassandraDataOperations { */ T updateAsynchronously(T entity, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - * @return - */ - T updateAsynchronously(T entity, Map optionsByName); - /** * @param entity * @param tableName @@ -409,14 +304,6 @@ public interface CassandraDataOperations { */ T updateAsynchronously(T entity, String tableName, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - * @return - */ - T updateAsynchronously(T entity, String tableName, Map optionsByName); - /** * Insert the given object to the table by id. * @@ -439,14 +326,6 @@ public interface CassandraDataOperations { */ List updateAsynchronously(List entities, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List updateAsynchronously(List entities, Map optionsByName); - /** * @param entities * @param tableName @@ -455,14 +334,6 @@ public interface CassandraDataOperations { */ List updateAsynchronously(List entities, String tableName, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - * @return - */ - List updateAsynchronously(List entities, String tableName, Map optionsByName); - /** * Remove the given object from the table by id. * @@ -485,13 +356,6 @@ public interface CassandraDataOperations { */ void delete(T entity, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - */ - void delete(T entity, Map optionsByName); - /** * @param entity * @param tableName @@ -499,13 +363,6 @@ public interface CassandraDataOperations { */ void delete(T entity, String tableName, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - */ - void delete(T entity, String tableName, Map optionsByName); - /** * Remove the given object from the table by id. * @@ -528,13 +385,6 @@ public interface CassandraDataOperations { */ void delete(List entities, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - */ - void delete(List entities, Map optionsByName); - /** * @param entities * @param tableName @@ -542,13 +392,6 @@ public interface CassandraDataOperations { */ void delete(List entities, String tableName, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - */ - void delete(List entities, String tableName, Map optionsByName); - /** * Remove the given object from the table by id. * @@ -563,13 +406,6 @@ public interface CassandraDataOperations { */ void deleteAsynchronously(T entity, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - */ - void deleteAsynchronously(T entity, Map optionsByName); - /** * @param entity * @param tableName @@ -577,13 +413,6 @@ public interface CassandraDataOperations { */ void deleteAsynchronously(T entity, String tableName, QueryOptions options); - /** - * @param entity - * @param tableName - * @param optionsByName - */ - void deleteAsynchronously(T entity, String tableName, Map optionsByName); - /** * Removes the given object from the given table. * @@ -614,13 +443,6 @@ public interface CassandraDataOperations { */ void deleteAsynchronously(List entities, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - */ - void deleteAsynchronously(List entities, Map optionsByName); - /** * @param entities * @param tableName @@ -628,13 +450,6 @@ public interface CassandraDataOperations { */ void deleteAsynchronously(List entities, String tableName, QueryOptions options); - /** - * @param entities - * @param tableName - * @param optionsByName - */ - void deleteAsynchronously(List entities, String tableName, Map optionsByName); - /** * Returns the underlying {@link CassandraConverter}. * diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java index e5d878536..69368d468 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java @@ -21,7 +21,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Set; import org.springframework.cassandra.core.CassandraTemplate; @@ -78,8 +77,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr /* * Required elements for successful Template Operations. These can be set with the Constructor, or wired in * later. - * - * TODO - DW - Discuss Autowiring these. */ private String keyspace; private CassandraConverter cassandraConverter; @@ -150,16 +147,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr delete(entities, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List, java.util.Map) - */ - @Override - public void delete(List entities, Map optionsByName) { - String tableName = getTableName(entities.get(0).getClass()); - Assert.notNull(tableName); - delete(entities, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List, org.springframework.data.cassandra.core.QueryOptions) */ @@ -176,19 +163,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr @Override public void delete(List entities, String tableName) { - delete(entities, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List, java.lang.String, java.util.Map) - */ - @Override - public void delete(List entities, String tableName, Map optionsByName) { - Assert.notNull(entities); - Assert.notEmpty(entities); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - doBatchDelete(tableName, entities, optionsByName, false); + delete(entities, tableName, null); } /* (non-Javadoc) @@ -196,7 +171,10 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public void delete(List entities, String tableName, QueryOptions options) { - delete(entities, tableName, options.toMap()); + Assert.notNull(entities); + Assert.notEmpty(entities); + Assert.notNull(tableName); + doBatchDelete(tableName, entities, options, false); } /* (non-Javadoc) @@ -209,16 +187,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr delete(entity, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object, java.util.Map) - */ - @Override - public void delete(T entity, Map optionsByName) { - String tableName = getTableName(entity.getClass()); - Assert.notNull(tableName); - delete(entity, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions) */ @@ -234,19 +202,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public void delete(T entity, String tableName) { - - delete(entity, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object, java.lang.String, java.util.Map) - */ - @Override - public void delete(T entity, String tableName, Map optionsByName) { - Assert.notNull(entity); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - doDelete(tableName, entity, optionsByName, false); + delete(entity, tableName, null); } /* (non-Javadoc) @@ -254,7 +210,9 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public void delete(T entity, String tableName, QueryOptions options) { - delete(entity, tableName, options.toMap()); + Assert.notNull(entity); + Assert.notNull(tableName); + doDelete(tableName, entity, options, false); } /* (non-Javadoc) @@ -267,16 +225,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr deleteAsynchronously(entities, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.util.List, java.util.Map) - */ - @Override - public void deleteAsynchronously(List entities, Map optionsByName) { - String tableName = getTableName(entities.get(0).getClass()); - Assert.notNull(tableName); - deleteAsynchronously(entities, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.util.List, org.springframework.data.cassandra.core.QueryOptions) */ @@ -292,20 +240,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public void deleteAsynchronously(List entities, String tableName) { - - insertAsynchronously(entities, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.util.List, java.lang.String, java.util.Map) - */ - @Override - public void deleteAsynchronously(List entities, String tableName, Map optionsByName) { - Assert.notNull(entities); - Assert.notEmpty(entities); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - doBatchDelete(tableName, entities, optionsByName, true); + deleteAsynchronously(entities, tableName, null); } /* (non-Javadoc) @@ -313,7 +248,10 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public void deleteAsynchronously(List entities, String tableName, QueryOptions options) { - deleteAsynchronously(entities, tableName, options.toMap()); + Assert.notNull(entities); + Assert.notEmpty(entities); + Assert.notNull(tableName); + doBatchDelete(tableName, entities, options, true); } /* (non-Javadoc) @@ -326,16 +264,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr deleteAsynchronously(entity, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.lang.Object, java.util.Map) - */ - @Override - public void deleteAsynchronously(T entity, Map optionsByName) { - String tableName = getTableName(entity.getClass()); - Assert.notNull(tableName); - deleteAsynchronously(entity, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions) */ @@ -351,19 +279,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public void deleteAsynchronously(T entity, String tableName) { - - deleteAsynchronously(entity, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsynchronously(java.lang.Object, java.lang.String, java.util.Map) - */ - @Override - public void deleteAsynchronously(T entity, String tableName, Map optionsByName) { - Assert.notNull(entity); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - doDelete(tableName, entity, optionsByName, true); + deleteAsynchronously(entity, tableName, null); } /* (non-Javadoc) @@ -371,7 +287,9 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public void deleteAsynchronously(T entity, String tableName, QueryOptions options) { - deleteAsynchronously(entity, tableName, options.toMap()); + Assert.notNull(entity); + Assert.notNull(tableName); + doDelete(tableName, entity, options, true); } /** @@ -419,16 +337,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return insert(entities, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List, java.util.Map) - */ - @Override - public List insert(List entities, Map optionsByName) { - String tableName = getTableName(entities.get(0).getClass()); - Assert.notNull(tableName); - return insert(entities, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List, org.springframework.data.cassandra.core.QueryOptions) */ @@ -444,20 +352,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List insert(List entities, String tableName) { - - return insert(entities, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List, java.lang.String, java.util.Map) - */ - @Override - public List insert(List entities, String tableName, Map optionsByName) { - Assert.notNull(entities); - Assert.notEmpty(entities); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - return doBatchInsert(tableName, entities, optionsByName, false); + return insert(entities, tableName, null); } /* (non-Javadoc) @@ -465,7 +360,10 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List insert(List entities, String tableName, QueryOptions options) { - return insert(entities, tableName, options.toMap()); + Assert.notNull(entities); + Assert.notEmpty(entities); + Assert.notNull(tableName); + return doBatchInsert(tableName, entities, options, false); } /* (non-Javadoc) @@ -478,16 +376,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return insert(entity, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, java.util.Map) - */ - @Override - public T insert(T entity, Map optionsByName) { - String tableName = determineTableName(entity); - Assert.notNull(tableName); - return insert(entity, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions) */ @@ -503,19 +391,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public T insert(T entity, String tableName) { - - return insert(entity, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, java.lang.String, java.util.Map) - */ - @Override - public T insert(T entity, String tableName, Map optionsByName) { - Assert.notNull(entity); - Assert.notNull(tableName); - ensureNotIterable(entity); - return doInsert(tableName, entity, optionsByName, false); + return insert(entity, tableName, null); } /* (non-Javadoc) @@ -523,7 +399,10 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public T insert(T entity, String tableName, QueryOptions options) { - return insert(entity, tableName, options.toMap()); + Assert.notNull(entity); + Assert.notNull(tableName); + ensureNotIterable(entity); + return doInsert(tableName, entity, options, false); } /* (non-Javadoc) @@ -536,16 +415,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return insertAsynchronously(entities, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List, java.util.Map) - */ - @Override - public List insertAsynchronously(List entities, Map optionsByName) { - String tableName = getTableName(entities.get(0).getClass()); - Assert.notNull(tableName); - return insertAsynchronously(entities, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List, org.springframework.data.cassandra.core.QueryOptions) */ @@ -561,20 +430,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List insertAsynchronously(List entities, String tableName) { - - return insertAsynchronously(entities, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List, java.lang.String, java.util.Map) - */ - @Override - public List insertAsynchronously(List entities, String tableName, Map optionsByName) { - Assert.notNull(entities); - Assert.notEmpty(entities); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - return doBatchInsert(tableName, entities, optionsByName, true); + return insertAsynchronously(entities, tableName, null); } /* (non-Javadoc) @@ -582,7 +438,10 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List insertAsynchronously(List entities, String tableName, QueryOptions options) { - return insertAsynchronously(entities, tableName, options.toMap()); + Assert.notNull(entities); + Assert.notEmpty(entities); + Assert.notNull(tableName); + return doBatchInsert(tableName, entities, options, true); } /* (non-Javadoc) @@ -595,16 +454,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return insertAsynchronously(entity, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object, java.util.Map) - */ - @Override - public T insertAsynchronously(T entity, Map optionsByName) { - String tableName = determineTableName(entity); - Assert.notNull(tableName); - return insertAsynchronously(entity, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions) */ @@ -620,22 +469,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public T insertAsynchronously(T entity, String tableName) { - - return insertAsynchronously(entity, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object, java.lang.String, java.util.Map) - */ - @Override - public T insertAsynchronously(T entity, String tableName, Map optionsByName) { - Assert.notNull(entity); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - - ensureNotIterable(entity); - - return doInsert(tableName, entity, optionsByName, true); + return insertAsynchronously(entity, tableName, null); } /* (non-Javadoc) @@ -643,7 +477,12 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public T insertAsynchronously(T entity, String tableName, QueryOptions options) { - return insertAsynchronously(entity, tableName, options.toMap()); + Assert.notNull(entity); + Assert.notNull(tableName); + + ensureNotIterable(entity); + + return doInsert(tableName, entity, options, true); } /* (non-Javadoc) @@ -688,16 +527,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return update(entities, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List, java.util.Map) - */ - @Override - public List update(List entities, Map optionsByName) { - String tableName = getTableName(entities.get(0).getClass()); - Assert.notNull(tableName); - return update(entities, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List, org.springframework.data.cassandra.core.QueryOptions) */ @@ -713,20 +542,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List update(List entities, String tableName) { - - return update(entities, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List, java.lang.String, java.util.Map) - */ - @Override - public List update(List entities, String tableName, Map optionsByName) { - Assert.notNull(entities); - Assert.notEmpty(entities); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - return doBatchUpdate(tableName, entities, optionsByName, false); + return update(entities, tableName, null); } /* (non-Javadoc) @@ -734,7 +550,10 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List update(List entities, String tableName, QueryOptions options) { - return update(entities, tableName, options.toMap()); + Assert.notNull(entities); + Assert.notEmpty(entities); + Assert.notNull(tableName); + return doBatchUpdate(tableName, entities, options, false); } /* (non-Javadoc) @@ -747,16 +566,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return update(entity, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object, java.util.Map) - */ - @Override - public T update(T entity, Map optionsByName) { - String tableName = getTableName(entity.getClass()); - Assert.notNull(tableName); - return update(entity, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions) */ @@ -772,19 +581,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public T update(T entity, String tableName) { - - return update(entity, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object, java.lang.String, java.util.Map) - */ - @Override - public T update(T entity, String tableName, Map optionsByName) { - Assert.notNull(entity); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - return doUpdate(tableName, entity, optionsByName, false); + return update(entity, tableName, null); } /* (non-Javadoc) @@ -792,7 +589,9 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public T update(T entity, String tableName, QueryOptions options) { - return update(entity, tableName, options.toMap()); + Assert.notNull(entity); + Assert.notNull(tableName); + return doUpdate(tableName, entity, options, false); } /* (non-Javadoc) @@ -805,16 +604,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return updateAsynchronously(entities, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List, java.util.Map) - */ - @Override - public List updateAsynchronously(List entities, Map optionsByName) { - String tableName = getTableName(entities.get(0).getClass()); - Assert.notNull(tableName); - return updateAsynchronously(entities, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List, org.springframework.data.cassandra.core.QueryOptions) */ @@ -830,20 +619,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List updateAsynchronously(List entities, String tableName) { - - return updateAsynchronously(entities, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List, java.lang.String, java.util.Map) - */ - @Override - public List updateAsynchronously(List entities, String tableName, Map optionsByName) { - Assert.notNull(entities); - Assert.notEmpty(entities); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - return doBatchUpdate(tableName, entities, optionsByName, true); + return updateAsynchronously(entities, tableName, null); } /* (non-Javadoc) @@ -851,7 +627,10 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public List updateAsynchronously(List entities, String tableName, QueryOptions options) { - return updateAsynchronously(entities, tableName, options.toMap()); + Assert.notNull(entities); + Assert.notEmpty(entities); + Assert.notNull(tableName); + return doBatchUpdate(tableName, entities, options, true); } /* (non-Javadoc) @@ -864,16 +643,6 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr return updateAsynchronously(entity, tableName); } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object, java.util.Map) - */ - @Override - public T updateAsynchronously(T entity, Map optionsByName) { - String tableName = getTableName(entity.getClass()); - Assert.notNull(tableName); - return updateAsynchronously(entity, tableName, optionsByName); - } - /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object, org.springframework.data.cassandra.core.QueryOptions) */ @@ -890,18 +659,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr @Override public T updateAsynchronously(T entity, String tableName) { - return updateAsynchronously(entity, tableName, Collections. emptyMap()); - } - - /* (non-Javadoc) - * @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object, java.lang.String, java.util.Map) - */ - @Override - public T updateAsynchronously(T entity, String tableName, Map optionsByName) { - Assert.notNull(entity); - Assert.notNull(tableName); - Assert.notNull(optionsByName); - return doUpdate(tableName, entity, optionsByName, true); + return updateAsynchronously(entity, tableName, null); } /* (non-Javadoc) @@ -909,7 +667,9 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ @Override public T updateAsynchronously(T entity, String tableName, QueryOptions options) { - return updateAsynchronously(entity, tableName, options.toMap()); + Assert.notNull(entity); + Assert.notNull(tableName); + return doUpdate(tableName, entity, options, true); } /** @@ -1026,14 +786,14 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr * @param tableName * @param objectToRemove */ - protected void doBatchDelete(final String tableName, final List entities, Map optionsByName, + protected void doBatchDelete(final String tableName, final List entities, final QueryOptions options, final boolean deleteAsynchronously) { Assert.notEmpty(entities); try { - final Batch b = CqlUtils.toDeleteBatchQuery(keyspace, tableName, entities, optionsByName, cassandraConverter); + final Batch b = CqlUtils.toDeleteBatchQuery(keyspace, tableName, entities, options, cassandraConverter); logger.info(b.toString()); doExecute(new SessionCallback() { @@ -1067,14 +827,14 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr * @param insertAsychronously * @return */ - protected List doBatchInsert(final String tableName, final List entities, - Map optionsByName, final boolean insertAsychronously) { + protected List doBatchInsert(final String tableName, final List entities, final QueryOptions options, + final boolean insertAsychronously) { Assert.notEmpty(entities); try { - final Batch b = CqlUtils.toInsertBatchQuery(keyspace, tableName, entities, optionsByName, cassandraConverter); + final Batch b = CqlUtils.toInsertBatchQuery(keyspace, tableName, entities, options, cassandraConverter); logger.info(b.getQueryString()); return doExecute(new SessionCallback>() { @@ -1108,14 +868,14 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr * @param updateAsychronously * @return */ - protected List doBatchUpdate(final String tableName, final List entities, - Map optionsByName, final boolean updateAsychronously) { + protected List doBatchUpdate(final String tableName, final List entities, final QueryOptions options, + final boolean updateAsychronously) { Assert.notEmpty(entities); try { - final Batch b = CqlUtils.toUpdateBatchQuery(keyspace, tableName, entities, optionsByName, cassandraConverter); + final Batch b = CqlUtils.toUpdateBatchQuery(keyspace, tableName, entities, options, cassandraConverter); logger.info(b.toString()); return doExecute(new SessionCallback>() { @@ -1146,12 +906,12 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr * @param tableName * @param objectToRemove */ - protected void doDelete(final String tableName, final T objectToRemove, Map optionsByName, + protected void doDelete(final String tableName, final T objectToRemove, final QueryOptions options, final boolean deleteAsynchronously) { try { - final Query q = CqlUtils.toDeleteQuery(keyspace, tableName, objectToRemove, optionsByName, cassandraConverter); + final Query q = CqlUtils.toDeleteQuery(keyspace, tableName, objectToRemove, options, cassandraConverter); logger.info(q.toString()); doExecute(new SessionCallback() { @@ -1201,12 +961,12 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr * @param tableName * @param entity */ - protected T doInsert(final String tableName, final T entity, final Map optionsByName, + protected T doInsert(final String tableName, final T entity, final QueryOptions options, final boolean insertAsychronously) { try { - final Query q = CqlUtils.toInsertQuery(keyspace, tableName, entity, optionsByName, cassandraConverter); + final Query q = CqlUtils.toInsertQuery(keyspace, tableName, entity, options, cassandraConverter); logger.info(q.toString()); if (q.getConsistencyLevel() != null) { @@ -1248,12 +1008,12 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr * @param updateAsychronously * @return */ - protected T doUpdate(final String tableName, final T entity, final Map optionsByName, + protected T doUpdate(final String tableName, final T entity, final QueryOptions options, final boolean updateAsychronously) { try { - final Query q = CqlUtils.toUpdateQuery(keyspace, tableName, entity, optionsByName, cassandraConverter); + final Query q = CqlUtils.toUpdateQuery(keyspace, tableName, entity, options, cassandraConverter); logger.info(q.toString()); return doExecute(new SessionCallback() { diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java index 1da50f119..550a6d754 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java @@ -2,15 +2,11 @@ package org.springframework.data.cassandra.util; import java.util.ArrayList; import java.util.List; -import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.cassandra.core.ConsistencyLevel; -import org.springframework.cassandra.core.ConsistencyLevelResolver; +import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.cassandra.core.QueryOptions; -import org.springframework.cassandra.core.RetryPolicy; -import org.springframework.cassandra.core.RetryPolicyResolver; import org.springframework.cassandra.core.cql.generator.CreateTableCqlGenerator; import org.springframework.cassandra.core.keyspace.CreateTableSpecification; import org.springframework.data.cassandra.convert.CassandraConverter; @@ -155,7 +151,7 @@ public abstract class CqlUtils { * @throws EntityWriterException */ public static Query toInsertQuery(String keyspaceName, String tableName, final Object objectToSave, - Map optionsByName, EntityWriter entityWriter) throws EntityWriterException { + QueryOptions options, EntityWriter entityWriter) throws EntityWriterException { final Insert q = QueryBuilder.insertInto(keyspaceName, tableName); @@ -167,13 +163,13 @@ public abstract class CqlUtils { /* * Add Query Options */ - addQueryOptions(q, optionsByName); + CassandraTemplate.addQueryOptions(q, options); /* * Add TTL to Insert object */ - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.TTL) != null) { - q.using(QueryBuilder.ttl((Integer) optionsByName.get(QueryOptions.QueryOptionMapKeys.TTL))); + if (options != null && options.getTtl() != null) { + q.using(QueryBuilder.ttl(options.getTtl())); } return q; @@ -193,7 +189,7 @@ public abstract class CqlUtils { * @throws EntityWriterException */ public static Query toUpdateQuery(String keyspaceName, String tableName, final Object objectToSave, - Map optionsByName, EntityWriter entityWriter) throws EntityWriterException { + QueryOptions options, EntityWriter entityWriter) throws EntityWriterException { final Update q = QueryBuilder.update(keyspaceName, tableName); @@ -205,13 +201,13 @@ public abstract class CqlUtils { /* * Add Query Options */ - addQueryOptions(q, optionsByName); + CassandraTemplate.addQueryOptions(q, options); /* * Add TTL to Insert object */ - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.TTL) != null) { - q.using(QueryBuilder.ttl((Integer) optionsByName.get(QueryOptions.QueryOptionMapKeys.TTL))); + if (options != null && options.getTtl() != null) { + q.using(QueryBuilder.ttl(options.getTtl())); } return q; @@ -231,7 +227,7 @@ public abstract class CqlUtils { * @throws EntityWriterException */ public static Batch toUpdateBatchQuery(final String keyspaceName, final String tableName, - final List objectsToSave, Map optionsByName, EntityWriter entityWriter) + final List objectsToSave, QueryOptions options, EntityWriter entityWriter) throws EntityWriterException { /* @@ -241,11 +237,14 @@ public abstract class CqlUtils { for (final T objectToSave : objectsToSave) { - b.add((Statement) toUpdateQuery(keyspaceName, tableName, objectToSave, optionsByName, entityWriter)); + b.add((Statement) toUpdateQuery(keyspaceName, tableName, objectToSave, options, entityWriter)); } - addQueryOptions(b, optionsByName); + /* + * Add Query Options + */ + CassandraTemplate.addQueryOptions(b, options); return b; @@ -264,7 +263,7 @@ public abstract class CqlUtils { * @throws EntityWriterException */ public static Batch toInsertBatchQuery(final String keyspaceName, final String tableName, - final List objectsToSave, Map optionsByName, EntityWriter entityWriter) + final List objectsToSave, QueryOptions options, EntityWriter entityWriter) throws EntityWriterException { /* @@ -274,11 +273,14 @@ public abstract class CqlUtils { for (final T objectToSave : objectsToSave) { - b.add((Statement) toInsertQuery(keyspaceName, tableName, objectToSave, optionsByName, entityWriter)); + b.add((Statement) toInsertQuery(keyspaceName, tableName, objectToSave, options, entityWriter)); } - addQueryOptions(b, optionsByName); + /* + * Add Query Options + */ + CassandraTemplate.addQueryOptions(b, options); return b; @@ -296,7 +298,7 @@ public abstract class CqlUtils { * @throws EntityWriterException */ public static Query toDeleteQuery(String keyspace, String tableName, final Object objectToRemove, - Map optionsByName, EntityWriter entityWriter) throws EntityWriterException { + QueryOptions options, EntityWriter entityWriter) throws EntityWriterException { final Delete.Selection ds = QueryBuilder.delete(); final Delete q = ds.from(keyspace, tableName); @@ -307,7 +309,7 @@ public abstract class CqlUtils { */ entityWriter.write(objectToRemove, w); - addQueryOptions(q, optionsByName); + CassandraTemplate.addQueryOptions(q, options); return q; @@ -363,7 +365,7 @@ public abstract class CqlUtils { * @throws EntityWriterException */ public static Batch toDeleteBatchQuery(String keyspaceName, String tableName, List entities, - Map optionsByName, EntityWriter entityWriter) throws EntityWriterException { + QueryOptions options, EntityWriter entityWriter) throws EntityWriterException { /* * Return variable is a Batch statement @@ -372,40 +374,14 @@ public abstract class CqlUtils { for (final T objectToSave : entities) { - b.add((Statement) toDeleteQuery(keyspaceName, tableName, objectToSave, optionsByName, entityWriter)); + b.add((Statement) toDeleteQuery(keyspaceName, tableName, objectToSave, options, entityWriter)); } - addQueryOptions(b, optionsByName); + CassandraTemplate.addQueryOptions(b, options); return b; } - /** - * Add common Query options for all types of queries. - * - * @param q - * @param optionsByName - */ - private static void addQueryOptions(Query q, Map optionsByName) { - - if (optionsByName == null) { - return; - } - - /* - * Add Query Options - */ - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL) != null) { - q.setConsistencyLevel(ConsistencyLevelResolver.resolve((ConsistencyLevel) optionsByName - .get(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL))); - } - if (optionsByName.get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY) != null) { - q.setRetryPolicy(RetryPolicyResolver.resolve((RetryPolicy) optionsByName - .get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY))); - } - - } - } 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 85951bc10..9a462246e 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 @@ -22,8 +22,6 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import junit.framework.Assert; - import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.thrift.transport.TTransportException; import org.cassandraunit.CassandraCQLUnit; @@ -33,6 +31,7 @@ import org.cassandraunit.dataset.yaml.ClassPathYamlDataSet; import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.After; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; @@ -128,22 +127,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.insert(b3, "book", options); - /* - * Test Single Insert with entity - */ - Book b4 = new Book(); - b4.setIsbn("123456-4"); - b4.setTitle("Spring Data Cassandra Guide"); - b4.setAuthor("Cassandra Guru"); - b4.setPages(465); - - Map optionsByName = new HashMap(); - optionsByName.put(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL, ConsistencyLevel.ALL); - optionsByName.put(QueryOptions.QueryOptionMapKeys.RETRY_POLICY, RetryPolicy.FALLTHROUGH); - optionsByName.put(QueryOptions.QueryOptionMapKeys.TTL, 30); - - cassandraDataTemplate.insert(b4, "book", optionsByName); - /* * Test Single Insert with entity */ @@ -155,17 +138,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.insert(b5, options); - /* - * Test Single Insert with entity - */ - Book b6 = new Book(); - b6.setIsbn("123456-6"); - b6.setTitle("Spring Data Cassandra Guide"); - b6.setAuthor("Cassandra Guru"); - b6.setPages(465); - - cassandraDataTemplate.insert(b6, optionsByName); - } @Test @@ -214,13 +186,6 @@ public class CassandraDataOperationsTest { b4.setAuthor("Cassandra Guru"); b4.setPages(465); - Map optionsByName = new HashMap(); - optionsByName.put(QueryOptions.QueryOptionMapKeys.CONSISTENCY_LEVEL, ConsistencyLevel.ALL); - optionsByName.put(QueryOptions.QueryOptionMapKeys.RETRY_POLICY, RetryPolicy.FALLTHROUGH); - optionsByName.put(QueryOptions.QueryOptionMapKeys.TTL, 30); - - cassandraDataTemplate.insertAsynchronously(b4, "book", optionsByName); - /* * Test Single Insert with entity */ @@ -232,17 +197,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.insertAsynchronously(b5, options); - /* - * Test Single Insert with entity - */ - Book b6 = new Book(); - b6.setIsbn("123456-6"); - b6.setTitle("Spring Data Cassandra Guide"); - b6.setAuthor("Cassandra Guru"); - b6.setPages(465); - - cassandraDataTemplate.insertAsynchronously(b6, optionsByName); - } @Test @@ -273,16 +227,8 @@ public class CassandraDataOperationsTest { books = getBookList(20); - cassandraDataTemplate.insert(books, "book", optionsByName); - - books = getBookList(20); - cassandraDataTemplate.insert(books, options); - books = getBookList(20); - - cassandraDataTemplate.insert(books, optionsByName); - } @Test @@ -313,16 +259,8 @@ public class CassandraDataOperationsTest { books = getBookList(20); - cassandraDataTemplate.insertAsynchronously(books, "book", optionsByName); - - books = getBookList(20); - cassandraDataTemplate.insertAsynchronously(books, options); - books = getBookList(20); - - cassandraDataTemplate.insertAsynchronously(books, optionsByName); - } /** @@ -389,17 +327,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.update(b3, "book", options); - /* - * Test Single Insert with entity - */ - Book b4 = new Book(); - b4.setIsbn("123456-4"); - b4.setTitle("Spring Data Cassandra Book"); - b4.setAuthor("Cassandra Guru"); - b4.setPages(465); - - cassandraDataTemplate.update(b4, "book", optionsByName); - /* * Test Single Insert with entity */ @@ -411,17 +338,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.update(b5, options); - /* - * Test Single Insert with entity - */ - Book b6 = new Book(); - b6.setIsbn("123456-6"); - b6.setTitle("Spring Data Cassandra Book"); - b6.setAuthor("Cassandra Guru"); - b6.setPages(465); - - cassandraDataTemplate.update(b6, optionsByName); - } @Test @@ -468,17 +384,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.updateAsynchronously(b3, "book", options); - /* - * Test Single Insert with entity - */ - Book b4 = new Book(); - b4.setIsbn("123456-4"); - b4.setTitle("Spring Data Cassandra Book"); - b4.setAuthor("Cassandra Guru"); - b4.setPages(465); - - cassandraDataTemplate.updateAsynchronously(b4, "book", optionsByName); - /* * Test Single Insert with entity */ @@ -490,17 +395,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.updateAsynchronously(b5, options); - /* - * Test Single Insert with entity - */ - Book b6 = new Book(); - b6.setIsbn("123456-6"); - b6.setTitle("Spring Data Cassandra Book"); - b6.setAuthor("Cassandra Guru"); - b6.setPages(465); - - cassandraDataTemplate.updateAsynchronously(b6, optionsByName); - } @Test @@ -543,28 +437,12 @@ public class CassandraDataOperationsTest { books = getBookList(20); - cassandraDataTemplate.insert(books, "book", optionsByName); - - alterBooks(books); - - cassandraDataTemplate.update(books, "book", optionsByName); - - books = getBookList(20); - cassandraDataTemplate.insert(books, options); alterBooks(books); cassandraDataTemplate.update(books, options); - books = getBookList(20); - - cassandraDataTemplate.insert(books, optionsByName); - - alterBooks(books); - - cassandraDataTemplate.update(books, optionsByName); - } @Test @@ -607,28 +485,12 @@ public class CassandraDataOperationsTest { books = getBookList(20); - cassandraDataTemplate.insert(books, "book", optionsByName); - - alterBooks(books); - - cassandraDataTemplate.updateAsynchronously(books, "book", optionsByName); - - books = getBookList(20); - cassandraDataTemplate.insert(books, options); alterBooks(books); cassandraDataTemplate.updateAsynchronously(books, options); - books = getBookList(20); - - cassandraDataTemplate.insert(books, optionsByName); - - alterBooks(books); - - cassandraDataTemplate.updateAsynchronously(books, optionsByName); - } /** @@ -677,14 +539,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.delete(b3, "book", options); - /* - * Test Single Insert with entity - */ - Book b4 = new Book(); - b4.setIsbn("123456-4"); - - cassandraDataTemplate.delete(b4, "book", optionsByName); - /* * Test Single Insert with entity */ @@ -693,14 +547,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.delete(b5, options); - /* - * Test Single Insert with entity - */ - Book b6 = new Book(); - b6.setIsbn("123456-6"); - - cassandraDataTemplate.delete(b6, optionsByName); - } @Test @@ -737,14 +583,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.deleteAsynchronously(b3, "book", options); - /* - * Test Single Insert with entity - */ - Book b4 = new Book(); - b4.setIsbn("123456-4"); - - cassandraDataTemplate.deleteAsynchronously(b4, "book", optionsByName); - /* * Test Single Insert with entity */ @@ -753,13 +591,6 @@ public class CassandraDataOperationsTest { cassandraDataTemplate.deleteAsynchronously(b5, options); - /* - * Test Single Insert with entity - */ - Book b6 = new Book(); - b6.setIsbn("123456-6"); - - cassandraDataTemplate.deleteAsynchronously(b6, optionsByName); } @Test @@ -796,22 +627,10 @@ public class CassandraDataOperationsTest { books = getBookList(20); - cassandraDataTemplate.insert(books, "book", optionsByName); - - cassandraDataTemplate.delete(books, "book", optionsByName); - - books = getBookList(20); - cassandraDataTemplate.insert(books, options); cassandraDataTemplate.delete(books, options); - books = getBookList(20); - - cassandraDataTemplate.insert(books, optionsByName); - - cassandraDataTemplate.delete(books, optionsByName); - } @Test @@ -848,22 +667,10 @@ public class CassandraDataOperationsTest { books = getBookList(20); - cassandraDataTemplate.insert(books, "book", optionsByName); - - cassandraDataTemplate.deleteAsynchronously(books, "book", optionsByName); - - books = getBookList(20); - cassandraDataTemplate.insert(books, options); cassandraDataTemplate.deleteAsynchronously(books, options); - books = getBookList(20); - - cassandraDataTemplate.insert(books, optionsByName); - - cassandraDataTemplate.deleteAsynchronously(books, optionsByName); - } @Test From c1d30dcc52096fd41bdd7c8b5e463f0a3145d748 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 14:23:39 -0600 Subject: [PATCH 06/21] wip: beginning to move appropriate config to spring-cassandra --- .../cassandra/config/CompressionType.java | 25 + .../cassandra/config/KeyspaceAttributes.java | 107 +++++ .../config/PoolingOptionsConfig.java | 62 +++ .../cassandra/config/SocketOptionsConfig.java | 89 ++++ .../cassandra/config/TableAttributes.java | 49 ++ .../AbstractCassandraConfiguration.java | 2 +- .../cassandra/config/xml/BeanNames.java | 31 ++ .../xml/CassandraClusterFactoryBean.java | 264 ++++++++++ .../config/xml/CassandraClusterParser.java | 126 +++++ .../config/xml/CassandraKeyspaceParser.java | 131 +++++ .../config/xml/CassandraNamespaceHandler.java | 36 ++ .../xml/CassandraSessionFactoryBean.java | 157 ++++++ .../config/xml/CassandraSessionParser.java | 69 +++ .../resources/META-INF/spring.handlers.todo | 1 + .../resources/META-INF/spring.schemas.todo | 2 + .../resources/META-INF/spring.tooling.todo | 4 + .../cassandra/config/spring-cassandra-1.0.xsd | 453 ++++++++++++++++++ .../cassandra/config/spring-cassandra.gif | Bin 0 -> 581 bytes ...tractSpringDataCassandraConfiguration.java | 4 +- .../config/CassandraKeyspaceFactoryBean.java | 340 +++++++++++++ .../main/resources/META-INF/spring.tooling | 2 +- 21 files changed, 1949 insertions(+), 5 deletions(-) create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/CompressionType.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/TableAttributes.java rename spring-cassandra/src/main/java/org/springframework/cassandra/config/{ => java}/AbstractCassandraConfiguration.java (97%) create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterFactoryBean.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java create mode 100644 spring-cassandra/src/main/resources/META-INF/spring.handlers.todo create mode 100644 spring-cassandra/src/main/resources/META-INF/spring.schemas.todo create mode 100644 spring-cassandra/src/main/resources/META-INF/spring.tooling.todo create mode 100644 spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd create mode 100644 spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra.gif create mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceFactoryBean.java diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/CompressionType.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CompressionType.java new file mode 100644 index 000000000..4e9248ada --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CompressionType.java @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config; + +/** + * Simple enumeration for the various compression types. + * + * @author Alex Shvid + */ +public enum CompressionType { + NONE, SNAPPY; +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java new file mode 100644 index 000000000..53b74c182 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java @@ -0,0 +1,107 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config; + +import java.util.Collection; + +/** + * Keyspace attributes are used for manipulation around keyspace at the startup. Auto property defines the way how to do + * this. Other attributes used to ensure or update keyspace settings. + * + * @author Alex Shvid + */ +public class KeyspaceAttributes { + + public static final String DEFAULT_REPLICATION_STRATEGY = "SimpleStrategy"; + public static final int DEFAULT_REPLICATION_FACTOR = 1; + public static final boolean DEFAULT_DURABLE_WRITES = true; + + /* + * auto possible values: + * validate: validate the keyspace, makes no changes. + * update: update the keyspace. + * create: creates the keyspace, destroying previous data. + * create-drop: drop the keyspace at the end of the session. + */ + public static final String AUTO_VALIDATE = "validate"; + public static final String AUTO_UPDATE = "update"; + public static final String AUTO_CREATE = "create"; + public static final String AUTO_CREATE_DROP = "create-drop"; + + private String auto = AUTO_VALIDATE; + private String replicationStrategy = DEFAULT_REPLICATION_STRATEGY; + private int replicationFactor = DEFAULT_REPLICATION_FACTOR; + private boolean durableWrites = DEFAULT_DURABLE_WRITES; + + private Collection tables; + + public String getAuto() { + return auto; + } + + public void setAuto(String auto) { + this.auto = auto; + } + + public boolean isValidate() { + return AUTO_VALIDATE.equals(auto); + } + + public boolean isUpdate() { + return AUTO_UPDATE.equals(auto); + } + + public boolean isCreate() { + return AUTO_CREATE.equals(auto); + } + + public boolean isCreateDrop() { + return AUTO_CREATE_DROP.equals(auto); + } + + public String getReplicationStrategy() { + return replicationStrategy; + } + + public void setReplicationStrategy(String replicationStrategy) { + this.replicationStrategy = replicationStrategy; + } + + public int getReplicationFactor() { + return replicationFactor; + } + + public void setReplicationFactor(int replicationFactor) { + this.replicationFactor = replicationFactor; + } + + public boolean isDurableWrites() { + return durableWrites; + } + + public void setDurableWrites(boolean durableWrites) { + this.durableWrites = durableWrites; + } + + public Collection getTables() { + return tables; + } + + public void setTables(Collection tables) { + this.tables = tables; + } + +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java new file mode 100644 index 000000000..497051ba2 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java @@ -0,0 +1,62 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config; + +/** + * Pooling options POJO. Can be remote or local. + * + * @author Alex Shvid + */ +public class PoolingOptionsConfig { + + private Integer minSimultaneousRequests; + private Integer maxSimultaneousRequests; + private Integer coreConnections; + private Integer maxConnections; + + public Integer getMinSimultaneousRequests() { + return minSimultaneousRequests; + } + + public void setMinSimultaneousRequests(Integer minSimultaneousRequests) { + this.minSimultaneousRequests = minSimultaneousRequests; + } + + public Integer getMaxSimultaneousRequests() { + return maxSimultaneousRequests; + } + + public void setMaxSimultaneousRequests(Integer maxSimultaneousRequests) { + this.maxSimultaneousRequests = maxSimultaneousRequests; + } + + public Integer getCoreConnections() { + return coreConnections; + } + + public void setCoreConnections(Integer coreConnections) { + this.coreConnections = coreConnections; + } + + public Integer getMaxConnections() { + return maxConnections; + } + + public void setMaxConnections(Integer maxConnections) { + this.maxConnections = maxConnections; + } + +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java new file mode 100644 index 000000000..b658f36e9 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java @@ -0,0 +1,89 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config; + +/** + * Socket options POJO. Uses to configure Netty. + * + * @author Alex Shvid + */ +public class SocketOptionsConfig { + + private Integer connectTimeoutMls; + private Boolean keepAlive; + private Boolean reuseAddress; + private Integer soLinger; + private Boolean tcpNoDelay; + private Integer receiveBufferSize; + private Integer sendBufferSize; + + public Integer getConnectTimeoutMls() { + return connectTimeoutMls; + } + + public void setConnectTimeoutMls(Integer connectTimeoutMls) { + this.connectTimeoutMls = connectTimeoutMls; + } + + public Boolean getKeepAlive() { + return keepAlive; + } + + public void setKeepAlive(Boolean keepAlive) { + this.keepAlive = keepAlive; + } + + public Boolean getReuseAddress() { + return reuseAddress; + } + + public void setReuseAddress(Boolean reuseAddress) { + this.reuseAddress = reuseAddress; + } + + public Integer getSoLinger() { + return soLinger; + } + + public void setSoLinger(Integer soLinger) { + this.soLinger = soLinger; + } + + public Boolean getTcpNoDelay() { + return tcpNoDelay; + } + + public void setTcpNoDelay(Boolean tcpNoDelay) { + this.tcpNoDelay = tcpNoDelay; + } + + public Integer getReceiveBufferSize() { + return receiveBufferSize; + } + + public void setReceiveBufferSize(Integer receiveBufferSize) { + this.receiveBufferSize = receiveBufferSize; + } + + public Integer getSendBufferSize() { + return sendBufferSize; + } + + public void setSendBufferSize(Integer sendBufferSize) { + this.sendBufferSize = sendBufferSize; + } + +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/TableAttributes.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/TableAttributes.java new file mode 100644 index 000000000..ce6d26137 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/TableAttributes.java @@ -0,0 +1,49 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config; + +/** + * Table attributes are used for manipulation around table at the startup (create/update/validate). + * + * @author Alex Shvid + */ +public class TableAttributes { + + private String entity; + private String name; + + public String getEntity() { + return entity; + } + + public void setEntity(String entity) { + this.entity = entity; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "TableAttributes [entity=" + entity + "]"; + } + +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java similarity index 97% rename from spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java index 3c3465bd7..434007340 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.cassandra.config; +package org.springframework.cassandra.config.java; import org.springframework.cassandra.core.CassandraOperations; import org.springframework.cassandra.core.CassandraTemplate; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java new file mode 100644 index 000000000..aa5dabf59 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +/** + * @author Alex Shvid + * @author David Webb + */ +public final class BeanNames { + + private BeanNames() { + } + + public static final String CASSANDRA_CLUSTER = "cassandra-cluster"; + public static final String CASSANDRA_KEYSPACE = "cassandra-keyspace"; + public static final String CASSANDRA_SESSION = "cassandra-session"; + +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterFactoryBean.java new file mode 100644 index 000000000..f6a27af01 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterFactoryBean.java @@ -0,0 +1,264 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.cassandra.config.CompressionType; +import org.springframework.cassandra.config.PoolingOptionsConfig; +import org.springframework.cassandra.config.SocketOptionsConfig; +import org.springframework.cassandra.support.CassandraExceptionTranslator; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.support.PersistenceExceptionTranslator; +import org.springframework.util.StringUtils; + +import com.datastax.driver.core.AuthProvider; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.HostDistance; +import com.datastax.driver.core.PoolingOptions; +import com.datastax.driver.core.ProtocolOptions.Compression; +import com.datastax.driver.core.SocketOptions; +import com.datastax.driver.core.policies.LoadBalancingPolicy; +import com.datastax.driver.core.policies.ReconnectionPolicy; +import com.datastax.driver.core.policies.RetryPolicy; + +/** + * Convenient factory for configuring a Cassandra Cluster. + * + * @author Alex Shvid + * @author Matthew T. Adams + */ + +public class CassandraClusterFactoryBean implements FactoryBean, InitializingBean, DisposableBean, + PersistenceExceptionTranslator { + + private static final int DEFAULT_PORT = 9042; + + private Cluster cluster; + + private String contactPoints; + private int port = DEFAULT_PORT; + private CompressionType compressionType; + + private PoolingOptionsConfig localPoolingOptions; + private PoolingOptionsConfig remotePoolingOptions; + private SocketOptionsConfig socketOptions; + + private AuthProvider authProvider; + private LoadBalancingPolicy loadBalancingPolicy; + private ReconnectionPolicy reconnectionPolicy; + private RetryPolicy retryPolicy; + + private boolean metricsEnabled = true; + + private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator(); + + public Cluster getObject() throws Exception { + return cluster; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#getObjectType() + */ + public Class getObjectType() { + return Cluster.class; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#isSingleton() + */ + public boolean isSingleton() { + return true; + } + + /* + * (non-Javadoc) + * @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException) + */ + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + return exceptionTranslator.translateExceptionIfPossible(ex); + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + public void afterPropertiesSet() throws Exception { + + if (!StringUtils.hasText(contactPoints)) { + throw new IllegalArgumentException("at least one server is required"); + } + + Cluster.Builder builder = Cluster.builder(); + + builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(contactPoints)).withPort(port); + + if (compressionType != null) { + builder.withCompression(convertCompressionType(compressionType)); + } + + if (localPoolingOptions != null) { + builder.withPoolingOptions(configPoolingOptions(HostDistance.LOCAL, localPoolingOptions)); + } + + if (remotePoolingOptions != null) { + builder.withPoolingOptions(configPoolingOptions(HostDistance.REMOTE, remotePoolingOptions)); + } + + if (socketOptions != null) { + builder.withSocketOptions(configSocketOptions(socketOptions)); + } + + if (authProvider != null) { + builder.withAuthProvider(authProvider); + } + + if (loadBalancingPolicy != null) { + builder.withLoadBalancingPolicy(loadBalancingPolicy); + } + + if (reconnectionPolicy != null) { + builder.withReconnectionPolicy(reconnectionPolicy); + } + + if (retryPolicy != null) { + builder.withRetryPolicy(retryPolicy); + } + + if (!metricsEnabled) { + builder.withoutMetrics(); + } + + Cluster cluster = builder.build(); + + // initialize property + this.cluster = cluster; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.DisposableBean#destroy() + */ + public void destroy() throws Exception { + this.cluster.shutdown(); + } + + public void setContactPoints(String contactPoints) { + this.contactPoints = contactPoints; + } + + public void setPort(int port) { + this.port = port; + } + + public void setCompressionType(CompressionType compressionType) { + this.compressionType = compressionType; + } + + public void setLocalPoolingOptions(PoolingOptionsConfig localPoolingOptions) { + this.localPoolingOptions = localPoolingOptions; + } + + public void setRemotePoolingOptions(PoolingOptionsConfig remotePoolingOptions) { + this.remotePoolingOptions = remotePoolingOptions; + } + + public void setSocketOptions(SocketOptionsConfig socketOptions) { + this.socketOptions = socketOptions; + } + + public void setAuthProvider(AuthProvider authProvider) { + this.authProvider = authProvider; + } + + public void setLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) { + this.loadBalancingPolicy = loadBalancingPolicy; + } + + public void setReconnectionPolicy(ReconnectionPolicy reconnectionPolicy) { + this.reconnectionPolicy = reconnectionPolicy; + } + + public void setRetryPolicy(RetryPolicy retryPolicy) { + this.retryPolicy = retryPolicy; + } + + public void setMetricsEnabled(boolean metricsEnabled) { + this.metricsEnabled = metricsEnabled; + } + + private static Compression convertCompressionType(CompressionType type) { + switch (type) { + case NONE: + return Compression.NONE; + case SNAPPY: + return Compression.SNAPPY; + } + throw new IllegalArgumentException("unknown compression type " + type); + } + + private static PoolingOptions configPoolingOptions(HostDistance hostDistance, PoolingOptionsConfig config) { + PoolingOptions poolingOptions = new PoolingOptions(); + + if (config.getMinSimultaneousRequests() != null) { + poolingOptions + .setMinSimultaneousRequestsPerConnectionThreshold(hostDistance, config.getMinSimultaneousRequests()); + } + if (config.getMaxSimultaneousRequests() != null) { + poolingOptions + .setMaxSimultaneousRequestsPerConnectionThreshold(hostDistance, config.getMaxSimultaneousRequests()); + } + if (config.getCoreConnections() != null) { + poolingOptions.setCoreConnectionsPerHost(hostDistance, config.getCoreConnections()); + } + if (config.getMaxConnections() != null) { + poolingOptions.setMaxConnectionsPerHost(hostDistance, config.getMaxConnections()); + } + + return poolingOptions; + } + + private static SocketOptions configSocketOptions(SocketOptionsConfig config) { + SocketOptions socketOptions = new SocketOptions(); + + if (config.getConnectTimeoutMls() != null) { + socketOptions.setConnectTimeoutMillis(config.getConnectTimeoutMls()); + } + if (config.getKeepAlive() != null) { + socketOptions.setKeepAlive(config.getKeepAlive()); + } + if (config.getReuseAddress() != null) { + socketOptions.setReuseAddress(config.getReuseAddress()); + } + if (config.getSoLinger() != null) { + socketOptions.setSoLinger(config.getSoLinger()); + } + if (config.getTcpNoDelay() != null) { + socketOptions.setTcpNoDelay(config.getTcpNoDelay()); + } + if (config.getReceiveBufferSize() != null) { + socketOptions.setReceiveBufferSize(config.getReceiveBufferSize()); + } + if (config.getSendBufferSize() != null) { + socketOptions.setSendBufferSize(config.getSendBufferSize()); + } + + return socketOptions; + } +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java new file mode 100644 index 000000000..fc3201e0e --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java @@ -0,0 +1,126 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import java.util.List; + +import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.cassandra.config.CompressionType; +import org.springframework.cassandra.config.PoolingOptionsConfig; +import org.springframework.cassandra.config.SocketOptionsConfig; +import org.springframework.util.StringUtils; +import org.springframework.util.xml.DomUtils; +import org.w3c.dom.Element; + +/** + * Parser for <cluster;gt; definitions. + * + * @author Alex Shvid + * @author Matthew T. Adams + */ + +public class CassandraClusterParser extends AbstractSimpleBeanDefinitionParser { + + @Override + protected Class getBeanClass(Element element) { + return CassandraClusterFactoryBean.class; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext) + */ + @Override + protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) + throws BeanDefinitionStoreException { + + String id = super.resolveId(element, definition, parserContext); + return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_CLUSTER; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + + String contactPoints = element.getAttribute("contactPoints"); + if (StringUtils.hasText(contactPoints)) { + builder.addPropertyValue("contactPoints", contactPoints); + } + + String port = element.getAttribute("port"); + if (StringUtils.hasText(port)) { + builder.addPropertyValue("port", port); + } + + String compression = element.getAttribute("compression"); + if (StringUtils.hasText(compression)) { + builder.addPropertyValue("compressionType", CompressionType.valueOf(compression)); + } + + postProcess(builder, element); + } + + @Override + protected void postProcess(BeanDefinitionBuilder builder, Element element) { + List subElements = DomUtils.getChildElements(element); + + // parse nested elements + for (Element subElement : subElements) { + String name = subElement.getLocalName(); + + if ("local-pooling-options".equals(name)) { + builder.addPropertyValue("localPoolingOptions", parsePoolingOptions(subElement)); + } else if ("remote-pooling-options".equals(name)) { + builder.addPropertyValue("remotePoolingOptions", parsePoolingOptions(subElement)); + } else if ("socket-options".equals(name)) { + builder.addPropertyValue("socketOptions", parseSocketOptions(subElement)); + } + } + + } + + private BeanDefinition parsePoolingOptions(Element element) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PoolingOptionsConfig.class); + + // TODO + // ParsingUtils.setPropertyValue(builder, element, "min-simultaneous-requests", "minSimultaneousRequests"); + // ParsingUtils.setPropertyValue(builder, element, "max-simultaneous-requests", "maxSimultaneousRequests"); + // ParsingUtils.setPropertyValue(builder, element, "core-connections", "coreConnections"); + // ParsingUtils.setPropertyValue(builder, element, "max-connections", "maxConnections"); + + return builder.getBeanDefinition(); + } + + private BeanDefinition parseSocketOptions(Element element) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SocketOptionsConfig.class); + + // TODO + // ParsingUtils.setPropertyValue(builder, element, "connect-timeout-mls", "connectTimeoutMls"); + // ParsingUtils.setPropertyValue(builder, element, "keep-alive", "keepAlive"); + // ParsingUtils.setPropertyValue(builder, element, "reuse-address", "reuseAddress"); + // ParsingUtils.setPropertyValue(builder, element, "so-linger", "soLinger"); + // ParsingUtils.setPropertyValue(builder, element, "tcp-no-delay", "tcpNoDelay"); + // ParsingUtils.setPropertyValue(builder, element, "receive-buffer-size", "receiveBufferSize"); + // ParsingUtils.setPropertyValue(builder, element, "send-buffer-size", "sendBufferSize"); + + return builder.getBeanDefinition(); + } + +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java new file mode 100644 index 000000000..37ee4988f --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java @@ -0,0 +1,131 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import java.util.List; + +import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.ManagedList; +import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.cassandra.config.KeyspaceAttributes; +import org.springframework.cassandra.config.TableAttributes; +import org.springframework.util.StringUtils; +import org.springframework.util.xml.DomUtils; +import org.w3c.dom.Element; + +/** + * Parser for <keyspace;gt; definitions. + * + * @author Alex Shvid + */ + +public class CassandraKeyspaceParser extends AbstractSimpleBeanDefinitionParser { + + @Override + protected Class getBeanClass(Element element) { + return CassandraSessionFactoryBean.class; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext) + */ + @Override + protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) + throws BeanDefinitionStoreException { + + String id = super.resolveId(element, definition, parserContext); + return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_KEYSPACE; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + + String name = element.getAttribute("name"); + if (StringUtils.hasText(name)) { + builder.addPropertyValue("keyspace", name); + } + + String clusterRef = element.getAttribute("cassandra-cluster-ref"); + if (!StringUtils.hasText(clusterRef)) { + clusterRef = BeanNames.CASSANDRA_CLUSTER; + } + builder.addPropertyReference("cluster", clusterRef); + + String converterRef = element.getAttribute("cassandra-converter-ref"); + if (StringUtils.hasText(converterRef)) { + builder.addPropertyReference("converter", converterRef); + } + + postProcess(builder, element); + } + + @Override + protected void postProcess(BeanDefinitionBuilder builder, Element element) { + List subElements = DomUtils.getChildElements(element); + + // parse nested elements + for (Element subElement : subElements) { + String name = subElement.getLocalName(); + + if ("keyspace-attributes".equals(name)) { + builder.addPropertyValue("keyspaceAttributes", parseKeyspaceAttributes(subElement)); + } + } + + } + + private BeanDefinition parseKeyspaceAttributes(Element element) { + BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(KeyspaceAttributes.class); + + // TODO + // ParsingUtils.setPropertyValue(defBuilder, element, "auto", "auto"); + // ParsingUtils.setPropertyValue(defBuilder, element, "replication-strategy", "replicationStrategy"); + // ParsingUtils.setPropertyValue(defBuilder, element, "replication-factor", "replicationFactor"); + // ParsingUtils.setPropertyValue(defBuilder, element, "durable-writes", "durableWrites"); + + List subElements = DomUtils.getChildElements(element); + ManagedList tables = new ManagedList(subElements.size()); + + // parse nested elements + for (Element subElement : subElements) { + String name = subElement.getLocalName(); + + if ("table".equals(name)) { + tables.add(parseTable(subElement)); + } + } + if (!tables.isEmpty()) { + defBuilder.addPropertyValue("tables", tables); + } + + return defBuilder.getBeanDefinition(); + } + + private BeanDefinition parseTable(Element element) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(TableAttributes.class); + + // TODO + // ParsingUtils.setPropertyValue(builder, element, "entity", "entity"); + // ParsingUtils.setPropertyValue(builder, element, "name", "name"); + + return builder.getBeanDefinition(); + } +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java new file mode 100644 index 000000000..0a2ad6518 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java @@ -0,0 +1,36 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; + +/** + * Namespace handler for <cassandra;gt;. + * + * @author Alex Shvid + */ + +public class CassandraNamespaceHandler extends NamespaceHandlerSupport { + + public void init() { + + registerBeanDefinitionParser("cluster", new CassandraClusterParser()); + registerBeanDefinitionParser("keyspace", new CassandraKeyspaceParser()); + registerBeanDefinitionParser("session", new CassandraSessionParser()); + + } + +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java new file mode 100644 index 000000000..0be9a5feb --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java @@ -0,0 +1,157 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.cassandra.config.KeyspaceAttributes; +import org.springframework.cassandra.support.CassandraExceptionTranslator; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.support.PersistenceExceptionTranslator; +import org.springframework.util.StringUtils; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.Session; + +/** + * Convenient factory for configuring a Cassandra Session. Session is a thread safe singleton and created per a + * keyspace. So, it is enough to have one session per application. + * + * @author Alex Shvid + * @author Matthew T. Adams + */ + +public class CassandraSessionFactoryBean implements FactoryBean, InitializingBean, DisposableBean, + BeanClassLoaderAware, PersistenceExceptionTranslator { + + private static final Logger log = LoggerFactory.getLogger(CassandraSessionFactoryBean.class); + + public static final String DEFAULT_REPLICATION_STRATEGY = "SimpleStrategy"; + public static final int DEFAULT_REPLICATION_FACTOR = 1; + + private ClassLoader beanClassLoader; + + private Cluster cluster; + private Session session; + private String keyspace; + + private KeyspaceAttributes keyspaceAttributes; + + private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator(); + + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + + public Session getObject() { + return session; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#getObjectType() + */ + public Class getObjectType() { + return Session.class; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#isSingleton() + */ + public boolean isSingleton() { + return true; + } + + /* + * (non-Javadoc) + * @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException) + */ + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + return exceptionTranslator.translateExceptionIfPossible(ex); + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + public void afterPropertiesSet() throws Exception { + + if (cluster == null) { + throw new IllegalArgumentException("at least one cluster is required"); + } + + this.session = StringUtils.hasText(this.keyspace) ? cluster.connect(keyspace) : cluster.connect(); + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.DisposableBean#destroy() + */ + public void destroy() throws Exception { + + this.session.shutdown(); + } + + public void setKeyspace(String keyspace) { + this.keyspace = keyspace; + } + + public void setCluster(Cluster cluster) { + this.cluster = cluster; + } + + public void setKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes) { + this.keyspaceAttributes = keyspaceAttributes; + } + + private static String compareKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes, + KeyspaceMetadata keyspaceMetadata) { + if (keyspaceAttributes.isDurableWrites() != keyspaceMetadata.isDurableWrites()) { + return "durableWrites"; + } + Map replication = keyspaceMetadata.getReplication(); + String replicationFactorStr = replication.get("replication_factor"); + if (replicationFactorStr == null) { + return "replication_factor"; + } + try { + int replicationFactor = Integer.parseInt(replicationFactorStr); + if (keyspaceAttributes.getReplicationFactor() != replicationFactor) { + return "replication_factor"; + } + } catch (NumberFormatException e) { + return "replication_factor"; + } + + String attributesStrategy = keyspaceAttributes.getReplicationStrategy(); + if (attributesStrategy.indexOf('.') == -1) { + attributesStrategy = "org.apache.cassandra.locator." + attributesStrategy; + } + String replicationStrategy = replication.get("class"); + if (!attributesStrategy.equals(replicationStrategy)) { + return "replication_class"; + } + return null; + } +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java new file mode 100644 index 000000000..357ce17a6 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java @@ -0,0 +1,69 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.cassandra.core.SessionFactoryBean; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * Parser for <session;gt; definitions. + * + * @author David Webb + */ + +public class CassandraSessionParser extends AbstractSimpleBeanDefinitionParser { + + @Override + protected Class getBeanClass(Element element) { + return SessionFactoryBean.class; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext) + */ + @Override + protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) + throws BeanDefinitionStoreException { + + String id = super.resolveId(element, definition, parserContext); + return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_SESSION; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + + String keyspaceRef = element.getAttribute("cassandra-keyspace-ref"); + if (!StringUtils.hasText(keyspaceRef)) { + keyspaceRef = BeanNames.CASSANDRA_KEYSPACE; + } + builder.addPropertyReference("keyspace", keyspaceRef); + + postProcess(builder, element); + } + + @Override + protected void postProcess(BeanDefinitionBuilder builder, Element element) { + + } + +} diff --git a/spring-cassandra/src/main/resources/META-INF/spring.handlers.todo b/spring-cassandra/src/main/resources/META-INF/spring.handlers.todo new file mode 100644 index 000000000..f5bd3781b --- /dev/null +++ b/spring-cassandra/src/main/resources/META-INF/spring.handlers.todo @@ -0,0 +1 @@ +http\://www.springframework.org/schema/cassandra=org.springframework.cassandra.config.xml.CassandraNamespaceHandler diff --git a/spring-cassandra/src/main/resources/META-INF/spring.schemas.todo b/spring-cassandra/src/main/resources/META-INF/spring.schemas.todo new file mode 100644 index 000000000..9a41e8fcd --- /dev/null +++ b/spring-cassandra/src/main/resources/META-INF/spring.schemas.todo @@ -0,0 +1,2 @@ +http\://www.springframework.org/schema/cassandra/spring-cassandra-1.0.xsd=org/springframework/cassandra/config/spring-cassandra-1.0.xsd +http\://www.springframework.org/schema/cassandra/spring-cassandra.xsd=org/springframework/cassandra/config/spring-cassandra-1.0.xsd \ No newline at end of file diff --git a/spring-cassandra/src/main/resources/META-INF/spring.tooling.todo b/spring-cassandra/src/main/resources/META-INF/spring.tooling.todo new file mode 100644 index 000000000..a339bc54e --- /dev/null +++ b/spring-cassandra/src/main/resources/META-INF/spring.tooling.todo @@ -0,0 +1,4 @@ +# Tooling related information for the cassandra namespace +http\://www.springframework.org/schema/cassandra@name=Spring Cassandra Namespace +http\://www.springframework.org/schema/cassandra@prefix=cassandra +http\://www.springframework.org/schema/cassandra@icon=org/springframework/data/cassandra/config/spring-cassandra.gif diff --git a/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd b/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd new file mode 100644 index 000000000..98c1f9eff --- /dev/null +++ b/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd @@ -0,0 +1,453 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The name of the Cassandra Cluster definition (by + default "cassandra-cluster") + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The name of the Keyspace definition (by default + "cassandra-keyspace") + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra.gif b/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra.gif new file mode 100644 index 0000000000000000000000000000000000000000..20ed1f9a4438054835c3bd7231c59dcc36d9f24e GIT binary patch literal 581 zcmZ?wbhEHb6krfwc*ekR>cRs}r)GW6W=W@M$1Xhi{Pm|(La%4WG|h-<))@;Tnzydr ze|7(^se4|>h4R zUy{Z383{M%q|7Yz$#T`0m|!y{*)GRZ@9L!3yxD&uuMPIl1|0Luj6Z zdPkV$k=o$-X|CH!1CBLB?5vH+vQyt$61=G-B*R91O}5{ryoi-KQOi@qrbqb9j0v0; z5;QF^;Q#;s3^WFcKUo+V7~&apK=y#*gn@lgLwr+nOKUS18yg1`6IWXkmxPoeFOQ^9 zUmMe;Dbs|Q`WZ#VWF+Oqg&7ylnJUT8uzIpIkARk*zGf@q8bK!uB^^Jrmfe#@w3X~5 zjco%=nvW{7>YA$?xVgIcdp2DZF^sU&u#O1|bhtZ*RXNt(TP-*$)Z^}A1#USb$B=N< rFkfeu(hb`mG&f7x?8#9)=yFn#m0d_d!a, InitializingBean, DisposableBean, + BeanClassLoaderAware, PersistenceExceptionTranslator { + + private static final Logger log = LoggerFactory.getLogger(CassandraKeyspaceFactoryBean.class); + + public static final String DEFAULT_REPLICATION_STRATEGY = "SimpleStrategy"; + public static final int DEFAULT_REPLICATION_FACTOR = 1; + + private ClassLoader beanClassLoader; + + private Cluster cluster; + private Session session; + private String keyspace; + + private CassandraConverter converter; + private MappingContext, CassandraPersistentProperty> mappingContext; + + private SpringDataKeyspace keyspaceBean; + + private KeyspaceAttributes keyspaceAttributes; + + private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator(); + + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + + public SpringDataKeyspace getObject() { + return keyspaceBean; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#getObjectType() + */ + public Class getObjectType() { + return Session.class; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#isSingleton() + */ + public boolean isSingleton() { + return true; + } + + /* + * (non-Javadoc) + * @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException) + */ + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + return exceptionTranslator.translateExceptionIfPossible(ex); + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + public void afterPropertiesSet() throws Exception { + + if (this.converter == null) { + this.converter = getDefaultCassandraConverter(); + } + this.mappingContext = this.converter.getMappingContext(); + + if (cluster == null) { + throw new IllegalArgumentException("at least one cluster is required"); + } + + Session session = null; + session = cluster.connect(); + + if (StringUtils.hasText(keyspace)) { + + KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace.toLowerCase()); + boolean keyspaceExists = keyspaceMetadata != null; + boolean keyspaceCreated = false; + + if (keyspaceExists) { + log.info("keyspace exists " + keyspaceMetadata.asCQLQuery()); + } + + if (keyspaceAttributes == null) { + keyspaceAttributes = new KeyspaceAttributes(); + } + + // drop the old keyspace if needed + if (keyspaceExists && (keyspaceAttributes.isCreate() || keyspaceAttributes.isCreateDrop())) { + log.info("Drop keyspace " + keyspace + " on afterPropertiesSet"); + session.execute("DROP KEYSPACE " + keyspace + ";"); + keyspaceExists = false; + } + + // create the new keyspace if needed + if (!keyspaceExists + && (keyspaceAttributes.isCreate() || keyspaceAttributes.isCreateDrop() || keyspaceAttributes.isUpdate())) { + + String query = String + .format( + "CREATE KEYSPACE %1$s WITH replication = { 'class' : '%2$s', 'replication_factor' : %3$d } AND DURABLE_WRITES = %4$b", + keyspace, keyspaceAttributes.getReplicationStrategy(), keyspaceAttributes.getReplicationFactor(), + keyspaceAttributes.isDurableWrites()); + + log.info("Create keyspace " + keyspace + " on afterPropertiesSet " + query); + + session.execute(query); + keyspaceCreated = true; + } + + // update keyspace if needed + if (keyspaceAttributes.isUpdate() && !keyspaceCreated) { + + if (compareKeyspaceAttributes(keyspaceAttributes, keyspaceMetadata) != null) { + + String query = String + .format( + "ALTER KEYSPACE %1$s WITH replication = { 'class' : '%2$s', 'replication_factor' : %3$d } AND DURABLE_WRITES = %4$b", + keyspace, keyspaceAttributes.getReplicationStrategy(), keyspaceAttributes.getReplicationFactor(), + keyspaceAttributes.isDurableWrites()); + + log.info("Update keyspace " + keyspace + " on afterPropertiesSet " + query); + session.execute(query); + } + + } + + // validate keyspace if needed + if (keyspaceAttributes.isValidate()) { + + if (!keyspaceExists) { + throw new InvalidDataAccessApiUsageException("keyspace '" + keyspace + "' not found in the Cassandra"); + } + + String errorField = compareKeyspaceAttributes(keyspaceAttributes, keyspaceMetadata); + if (errorField != null) { + throw new InvalidDataAccessApiUsageException(errorField + " attribute is not much in the keyspace '" + + keyspace + "'"); + } + + } + + session.execute("USE " + keyspace); + + if (!CollectionUtils.isEmpty(keyspaceAttributes.getTables())) { + + for (TableAttributes tableAttributes : keyspaceAttributes.getTables()) { + + String entityClassName = tableAttributes.getEntity(); + Class entityClass = ClassUtils.forName(entityClassName, this.beanClassLoader); + CassandraPersistentEntity entity = determineEntity(entityClass); + String useTableName = tableAttributes.getName() != null ? tableAttributes.getName() : entity.getTable(); + + if (keyspaceCreated) { + createNewTable(session, useTableName, entity); + } else if (keyspaceAttributes.isUpdate()) { + TableMetadata table = keyspaceMetadata.getTable(useTableName.toLowerCase()); + if (table == null) { + createNewTable(session, useTableName, entity); + } else { + // alter table columns + for (String cql : CqlUtils.alterTable(useTableName, entity, table)) { + log.info("Execute on keyspace " + keyspace + " CQL " + cql); + session.execute(cql); + } + } + } else if (keyspaceAttributes.isValidate()) { + TableMetadata table = keyspaceMetadata.getTable(useTableName.toLowerCase()); + if (table == null) { + throw new InvalidDataAccessApiUsageException("not found table " + useTableName + " for entity " + + entityClassName); + } + // validate columns + List alter = CqlUtils.alterTable(useTableName, entity, table); + if (!alter.isEmpty()) { + throw new InvalidDataAccessApiUsageException("invalid table " + useTableName + " for entity " + + entityClassName + ". modify it by " + alter); + } + } + + // System.out.println("tableAttributes, entityClass=" + entityClass + ", table = " + entity.getTable()); + + } + } + + } + + // initialize property + this.session = session; + + this.keyspaceBean = new SpringDataKeyspace(keyspace, session, converter); + } + + private void createNewTable(Session session, String useTableName, CassandraPersistentEntity entity) + throws NoHostAvailableException { + String cql = CqlUtils.createTable(useTableName, entity, converter); + log.info("Execute on keyspace " + keyspace + " CQL " + cql); + session.execute(cql); + for (String indexCQL : CqlUtils.createIndexes(useTableName, entity)) { + log.info("Execute on keyspace " + keyspace + " CQL " + indexCQL); + session.execute(indexCQL); + } + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.DisposableBean#destroy() + */ + public void destroy() throws Exception { + + if (StringUtils.hasText(keyspace) && keyspaceAttributes != null && keyspaceAttributes.isCreateDrop()) { + log.info("Drop keyspace " + keyspace + " on destroy"); + session.execute("USE system"); + session.execute("DROP KEYSPACE " + keyspace); + } + this.session.shutdown(); + } + + public void setKeyspace(String keyspace) { + this.keyspace = keyspace; + } + + public void setCluster(Cluster cluster) { + this.cluster = cluster; + } + + public void setKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes) { + this.keyspaceAttributes = keyspaceAttributes; + } + + public void setConverter(CassandraConverter converter) { + this.converter = converter; + } + + private static String compareKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes, + KeyspaceMetadata keyspaceMetadata) { + if (keyspaceAttributes.isDurableWrites() != keyspaceMetadata.isDurableWrites()) { + return "durableWrites"; + } + Map replication = keyspaceMetadata.getReplication(); + String replicationFactorStr = replication.get("replication_factor"); + if (replicationFactorStr == null) { + return "replication_factor"; + } + try { + int replicationFactor = Integer.parseInt(replicationFactorStr); + if (keyspaceAttributes.getReplicationFactor() != replicationFactor) { + return "replication_factor"; + } + } catch (NumberFormatException e) { + return "replication_factor"; + } + + String attributesStrategy = keyspaceAttributes.getReplicationStrategy(); + if (attributesStrategy.indexOf('.') == -1) { + attributesStrategy = "org.apache.cassandra.locator." + attributesStrategy; + } + String replicationStrategy = replication.get("class"); + if (!attributesStrategy.equals(replicationStrategy)) { + return "replication_class"; + } + return null; + } + + CassandraPersistentEntity determineEntity(Class entityClass) { + + if (entityClass == null) { + throw new InvalidDataAccessApiUsageException( + "No class parameter provided, entity table name can't be determined!"); + } + + CassandraPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); + if (entity == null) { + throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class " + + entityClass.getName()); + } + return entity; + } + + private static final CassandraConverter getDefaultCassandraConverter() { + MappingCassandraConverter converter = new MappingCassandraConverter(new CassandraMappingContext()); + converter.afterPropertiesSet(); + return converter; + } +} diff --git a/spring-data-cassandra/src/main/resources/META-INF/spring.tooling b/spring-data-cassandra/src/main/resources/META-INF/spring.tooling index 3769be0bd..bdc47bdbb 100644 --- a/spring-data-cassandra/src/main/resources/META-INF/spring.tooling +++ b/spring-data-cassandra/src/main/resources/META-INF/spring.tooling @@ -1,4 +1,4 @@ # Tooling related information for the cassandra namespace -http\://www.springframework.org/schema/data/cassandra@name=Cassandra Namespace +http\://www.springframework.org/schema/data/cassandra@name=Spring Data Cassandra Namespace http\://www.springframework.org/schema/data/cassandra@prefix=cassandra http\://www.springframework.org/schema/data/cassandra@icon=org/springframework/data/cassandra/config/spring-cassandra.gif From 00ec7a203c6793deca27571a8f9e6d63cfc21f65 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 14:49:24 -0600 Subject: [PATCH 07/21] wip --- spring-cassandra/pom.xml | 5 +++ .../test/integration/config/Config.java | 29 +++++++++++++++ .../test/integration/config/ConfigTest.java | 36 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java diff --git a/spring-cassandra/pom.xml b/spring-cassandra/pom.xml index db65767f3..a5c552d78 100644 --- a/spring-cassandra/pom.xml +++ b/spring-cassandra/pom.xml @@ -72,6 +72,11 @@ + + cglib + cglib-nodep + test + javax.el el-api diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java new file mode 100644 index 000000000..2ac144240 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java @@ -0,0 +1,29 @@ +package org.springframework.cassandra.test.integration.config; + +import org.springframework.cassandra.config.java.AbstractCassandraConfiguration; +import org.springframework.context.annotation.Configuration; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Cluster.Builder; + +@Configuration +public class Config extends AbstractCassandraConfiguration { + + public static final String KEYSPACE = "testy"; + + public Cluster cluster; + + @Override + protected String getKeyspaceName() { + return KEYSPACE; + } + + @Override + public Cluster cluster() { + Builder builder = Cluster.builder(); + + builder.addContactPoint("localhost").withPort(9042); + + return cluster = builder.build(); + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java new file mode 100644 index 000000000..f4b1e9bd9 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java @@ -0,0 +1,36 @@ +package org.springframework.cassandra.test.integration.config; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.inject.Inject; + +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.thrift.transport.TTransportException; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.datastax.driver.core.Session; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Config.class) +public class ConfigTest { + + @BeforeClass + public static void startCassandra() throws ConfigurationException, TTransportException, IOException { + EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml"); + } + + @Inject + Session session; + + @Test + public void test() { + assertNotNull(session); + } +} From 7913b76b2aab777b50620108c0907b1c54af3601 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 15:23:24 -0600 Subject: [PATCH 08/21] config class working w/minimal surface test --- .../cassandra/test/integration/config/Config.java | 4 +--- .../cassandra/test/integration/config/ConfigTest.java | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java index 2ac144240..8dd9eb2d8 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java @@ -9,13 +9,11 @@ import com.datastax.driver.core.Cluster.Builder; @Configuration public class Config extends AbstractCassandraConfiguration { - public static final String KEYSPACE = "testy"; - public Cluster cluster; @Override protected String getKeyspaceName() { - return KEYSPACE; + return null; } @Override diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java index f4b1e9bd9..8739b8358 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java @@ -32,5 +32,9 @@ public class ConfigTest { @Test public void test() { assertNotNull(session); + + session + .execute("CREATE KEYSPACE testy WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); + session.execute("USE testy"); } } From fd46046255f598c1e649d66b82944ed3118e3ed3 Mon Sep 17 00:00:00 2001 From: David Webb Date: Mon, 9 Dec 2013 16:51:39 -0500 Subject: [PATCH 09/21] DATACASS-50 : Completed : Removed unneeded log config and added SLF4J Bridges. DATACASS-51 : Completed : Renames resources for spring-cassandra and removed unneeded resources. --- pom.xml | 17 +++++++++++++++-- spring-cassandra/pom.xml | 6 ++++++ ...bstractEmbeddedCassandraIntegrationTest.java | 2 +- .../src/test/resources/cassandra-keyspace.yaml | 3 --- .../src/test/resources/cql-dataload.cql | 3 --- .../src/test/resources/log4j.properties | 6 ------ .../resources/{logback.xml => logback-test.xml} | 0 .../src/test/resources/logging.properties | 1 - .../{cassandra.yaml => spring-cassandra.yaml} | 0 spring-data-cassandra-distribution/pom.xml | 3 +++ .../convert/CassandraPropertyValueProvider.java | 3 +-- 11 files changed, 26 insertions(+), 18 deletions(-) delete mode 100644 spring-cassandra/src/test/resources/cassandra-keyspace.yaml delete mode 100644 spring-cassandra/src/test/resources/cql-dataload.cql delete mode 100644 spring-cassandra/src/test/resources/log4j.properties rename spring-cassandra/src/test/resources/{logback.xml => logback-test.xml} (100%) delete mode 100644 spring-cassandra/src/test/resources/logging.properties rename spring-cassandra/src/test/resources/{cassandra.yaml => spring-cassandra.yaml} (100%) diff --git a/pom.xml b/pom.xml index 6a52dc867..17abe6c1e 100644 --- a/pom.xml +++ b/pom.xml @@ -27,11 +27,14 @@ + UTF-8 + UTF-8 multi spring-data-cassandra 1.6.2.RELEASE 1.2.0.1 1.0.4-dse + 2.16 @@ -123,10 +126,19 @@ ${spring} + org.slf4j - slf4j-api - 1.7.4 + log4j-over-slf4j + ${slf4j} + test + + + + org.slf4j + jul-to-slf4j + ${slf4j} + test @@ -246,6 +258,7 @@ org.apache.maven.plugins maven-failsafe-plugin + ${failsafe.version} -Xmx2048m -XX:MaxPermSize=512m false diff --git a/spring-cassandra/pom.xml b/spring-cassandra/pom.xml index db65767f3..d80da0219 100644 --- a/spring-cassandra/pom.xml +++ b/spring-cassandra/pom.xml @@ -44,6 +44,12 @@ com.datastax.cassandra cassandra-driver-core + + + log4j + log4j + + javax.enterprise diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java index 22dd2544d..2d89db056 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java @@ -16,7 +16,7 @@ import com.datastax.driver.core.Session; public abstract class AbstractEmbeddedCassandraIntegrationTest { - protected final static String CASSANDRA_CONFIG = "cassandra.yaml"; + protected final static String CASSANDRA_CONFIG = "spring-cassandra.yaml"; protected final static String CASSANDRA_HOST = "localhost"; protected final static int CASSANDRA_NATIVE_PORT = 9042; diff --git a/spring-cassandra/src/test/resources/cassandra-keyspace.yaml b/spring-cassandra/src/test/resources/cassandra-keyspace.yaml deleted file mode 100644 index a0e13da6e..000000000 --- a/spring-cassandra/src/test/resources/cassandra-keyspace.yaml +++ /dev/null @@ -1,3 +0,0 @@ -name: test -replicationFactor: 1 -strategy: org.apache.cassandra.locator.SimpleStrategy \ No newline at end of file diff --git a/spring-cassandra/src/test/resources/cql-dataload.cql b/spring-cassandra/src/test/resources/cql-dataload.cql deleted file mode 100644 index e38d18d36..000000000 --- a/spring-cassandra/src/test/resources/cql-dataload.cql +++ /dev/null @@ -1,3 +0,0 @@ -create table book (isbn text, title text, author text, pages int, PRIMARY KEY (isbn)); -create table book_alt (isbn text, title text, author text, pages int, PRIMARY KEY (isbn)); -/*insert into book (isbn, title, author, pages) values ('999999999', 'Book of Nines', 'Nine Nine', 999);*/ \ No newline at end of file diff --git a/spring-cassandra/src/test/resources/log4j.properties b/spring-cassandra/src/test/resources/log4j.properties deleted file mode 100644 index 6e2ec3286..000000000 --- a/spring-cassandra/src/test/resources/log4j.properties +++ /dev/null @@ -1,6 +0,0 @@ -log4j.rootLogger=WARN, stdout -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n -log4j.logger.org.springframework.data.cassandra=INFO - diff --git a/spring-cassandra/src/test/resources/logback.xml b/spring-cassandra/src/test/resources/logback-test.xml similarity index 100% rename from spring-cassandra/src/test/resources/logback.xml rename to spring-cassandra/src/test/resources/logback-test.xml diff --git a/spring-cassandra/src/test/resources/logging.properties b/spring-cassandra/src/test/resources/logging.properties deleted file mode 100644 index 2f5ec24dd..000000000 --- a/spring-cassandra/src/test/resources/logging.properties +++ /dev/null @@ -1 +0,0 @@ -handlers = org.slf4j.bridge.SLF4JBridgeHandler \ No newline at end of file diff --git a/spring-cassandra/src/test/resources/cassandra.yaml b/spring-cassandra/src/test/resources/spring-cassandra.yaml similarity index 100% rename from spring-cassandra/src/test/resources/cassandra.yaml rename to spring-cassandra/src/test/resources/spring-cassandra.yaml diff --git a/spring-data-cassandra-distribution/pom.xml b/spring-data-cassandra-distribution/pom.xml index 84ca20dbc..72402004f 100644 --- a/spring-data-cassandra-distribution/pom.xml +++ b/spring-data-cassandra-distribution/pom.xml @@ -21,6 +21,7 @@ ${basedir}/.. SDCASS + 1.0.3 @@ -28,10 +29,12 @@ org.apache.maven.plugins maven-assembly-plugin + 2.3 org.codehaus.mojo wagon-maven-plugin + ${wagon.version} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraPropertyValueProvider.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraPropertyValueProvider.java index 109086254..1c0c11653 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraPropertyValueProvider.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraPropertyValueProvider.java @@ -20,7 +20,6 @@ import java.nio.ByteBuffer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.cassandra.mapping.CassandraPersistentProperty; -import org.springframework.data.cassandra.util.CqlUtils; import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator; import org.springframework.data.mapping.model.PropertyValueProvider; import org.springframework.data.mapping.model.SpELExpressionEvaluator; @@ -74,7 +73,7 @@ public class CassandraPropertyValueProvider implements PropertyValueProvider Date: Mon, 9 Dec 2013 16:35:55 -0600 Subject: [PATCH 10/21] wip: java config looking good --- .../cassandra/config/KeyspaceAttributes.java | 54 +----------------- .../config/PoolingOptionsConfig.java | 4 +- .../cassandra/config/SocketOptionsConfig.java | 4 +- .../java/AbstractCassandraConfiguration.java | 4 +- .../config/AbstractIntegrationTest.java | 32 +++++++++++ .../AbstractIntegrationTestConfiguration.java | 20 +++++++ ...AbstractKeyspaceCreatingConfiguration.java | 57 +++++++++++++++++++ .../test/integration/config/Config.java | 19 +------ .../test/integration/config/ConfigTest.java | 31 +--------- .../config/IntegrationTestUtils.java | 16 ++++++ .../config/KeyspaceCreatingConfig.java | 14 +++++ .../config/KeyspaceCreatingConfigTest.java | 13 +++++ .../src/test/resources/cassandra.yaml | 2 +- ...tractSpringDataCassandraConfiguration.java | 2 +- .../test/integration/config/TestConfig.java | 2 +- 15 files changed, 168 insertions(+), 106 deletions(-) create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTestConfiguration.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractKeyspaceCreatingConfiguration.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfig.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfigTest.java diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java index 53b74c182..052315434 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/KeyspaceAttributes.java @@ -15,13 +15,11 @@ */ package org.springframework.cassandra.config; -import java.util.Collection; - /** - * Keyspace attributes are used for manipulation around keyspace at the startup. Auto property defines the way how to do - * this. Other attributes used to ensure or update keyspace settings. + * Keyspace attributes. * * @author Alex Shvid + * @author Matthew T. Adams */ public class KeyspaceAttributes { @@ -29,49 +27,10 @@ public class KeyspaceAttributes { public static final int DEFAULT_REPLICATION_FACTOR = 1; public static final boolean DEFAULT_DURABLE_WRITES = true; - /* - * auto possible values: - * validate: validate the keyspace, makes no changes. - * update: update the keyspace. - * create: creates the keyspace, destroying previous data. - * create-drop: drop the keyspace at the end of the session. - */ - public static final String AUTO_VALIDATE = "validate"; - public static final String AUTO_UPDATE = "update"; - public static final String AUTO_CREATE = "create"; - public static final String AUTO_CREATE_DROP = "create-drop"; - - private String auto = AUTO_VALIDATE; private String replicationStrategy = DEFAULT_REPLICATION_STRATEGY; private int replicationFactor = DEFAULT_REPLICATION_FACTOR; private boolean durableWrites = DEFAULT_DURABLE_WRITES; - private Collection tables; - - public String getAuto() { - return auto; - } - - public void setAuto(String auto) { - this.auto = auto; - } - - public boolean isValidate() { - return AUTO_VALIDATE.equals(auto); - } - - public boolean isUpdate() { - return AUTO_UPDATE.equals(auto); - } - - public boolean isCreate() { - return AUTO_CREATE.equals(auto); - } - - public boolean isCreateDrop() { - return AUTO_CREATE_DROP.equals(auto); - } - public String getReplicationStrategy() { return replicationStrategy; } @@ -95,13 +54,4 @@ public class KeyspaceAttributes { public void setDurableWrites(boolean durableWrites) { this.durableWrites = durableWrites; } - - public Collection getTables() { - return tables; - } - - public void setTables(Collection tables) { - this.tables = tables; - } - } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java index 497051ba2..e982e217d 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/PoolingOptionsConfig.java @@ -16,9 +16,10 @@ package org.springframework.cassandra.config; /** - * Pooling options POJO. Can be remote or local. + * Pooling options. * * @author Alex Shvid + * @author Matthew T. Adams */ public class PoolingOptionsConfig { @@ -58,5 +59,4 @@ public class PoolingOptionsConfig { public void setMaxConnections(Integer maxConnections) { this.maxConnections = maxConnections; } - } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java index b658f36e9..562377415 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/SocketOptionsConfig.java @@ -16,9 +16,10 @@ package org.springframework.cassandra.config; /** - * Socket options POJO. Uses to configure Netty. + * Socket options. * * @author Alex Shvid + * @author Matthew T. Adams */ public class SocketOptionsConfig { @@ -85,5 +86,4 @@ public class SocketOptionsConfig { public void setSendBufferSize(Integer sendBufferSize) { this.sendBufferSize = sendBufferSize; } - } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java index 434007340..216166953 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java @@ -36,7 +36,7 @@ public abstract class AbstractCassandraConfiguration { /** * The name of the keyspace to connect to. If {@literal null} or empty, then the system keyspace will be used. */ - protected abstract String getKeyspaceName(); + protected abstract String getKeyspace(); /** * The {@link Cluster} instance to connect to. Must not be null. @@ -51,7 +51,7 @@ public abstract class AbstractCassandraConfiguration { */ @Bean public Session session() { - String keyspace = getKeyspaceName(); + String keyspace = getKeyspace(); if (StringUtils.hasText(keyspace)) { return cluster().connect(keyspace); } else { diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java new file mode 100644 index 000000000..a731283af --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java @@ -0,0 +1,32 @@ +package org.springframework.cassandra.test.integration.config; + +import java.io.IOException; + +import javax.inject.Inject; + +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.thrift.transport.TTransportException; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.datastax.driver.core.Session; + +@RunWith(SpringJUnit4ClassRunner.class) +public abstract class AbstractIntegrationTest { + + @BeforeClass + public static void startCassandra() throws ConfigurationException, TTransportException, IOException { + EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml"); + } + + @Inject + public Session session; + + @Before + public void assertSession() { + IntegrationTestUtils.assertSession(session); + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTestConfiguration.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTestConfiguration.java new file mode 100644 index 000000000..6c92065a7 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTestConfiguration.java @@ -0,0 +1,20 @@ +package org.springframework.cassandra.test.integration.config; + +import org.springframework.cassandra.config.java.AbstractCassandraConfiguration; +import org.springframework.context.annotation.Configuration; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Cluster.Builder; + +@Configuration +public abstract class AbstractIntegrationTestConfiguration extends AbstractCassandraConfiguration { + + @Override + public Cluster cluster() { + Builder builder = Cluster.builder(); + + builder.addContactPoint("localhost").withPort(9042); + + return builder.build(); + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractKeyspaceCreatingConfiguration.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractKeyspaceCreatingConfiguration.java new file mode 100644 index 000000000..e23408760 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractKeyspaceCreatingConfiguration.java @@ -0,0 +1,57 @@ +package org.springframework.cassandra.test.integration.config; + +import org.springframework.cassandra.config.KeyspaceAttributes; +import org.springframework.cassandra.config.PoolingOptionsConfig; +import org.springframework.cassandra.config.SocketOptionsConfig; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; + +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.Session; + +@Configuration +public abstract class AbstractKeyspaceCreatingConfiguration extends AbstractIntegrationTestConfiguration { + + @Override + public Session session() { + + createKeyspaceIfNecessary(); + + return super.session(); + } + + protected void createKeyspaceIfNecessary() { + String keyspace = getKeyspace(); + if (!StringUtils.hasText(keyspace)) { + return; + } + + Session system = cluster().connect(); + KeyspaceMetadata kmd = system.getCluster().getMetadata().getKeyspace(keyspace); + if (kmd != null) { + return; + } + + // TODO: use KeyspaceBuilder to build keyspace with attributes & options + + system.execute("CREATE KEYSPACE " + keyspace + + " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); + system.shutdown(); + } + + protected KeyspaceAttributes getKeyspaceAttributes() { + return null; + } + + protected PoolingOptionsConfig getLocalPoolingOptionsConfig() { + return null; + } + + protected PoolingOptionsConfig getRemotePoolingOptionsConfig() { + return null; + } + + protected SocketOptionsConfig getSocketOptionsConfig() { + return null; + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java index 8dd9eb2d8..a4e977e3c 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java @@ -1,27 +1,12 @@ package org.springframework.cassandra.test.integration.config; -import org.springframework.cassandra.config.java.AbstractCassandraConfiguration; import org.springframework.context.annotation.Configuration; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Cluster.Builder; - @Configuration -public class Config extends AbstractCassandraConfiguration { - - public Cluster cluster; +public class Config extends AbstractIntegrationTestConfiguration { @Override - protected String getKeyspaceName() { + protected String getKeyspace() { return null; } - - @Override - public Cluster cluster() { - Builder builder = Cluster.builder(); - - builder.addContactPoint("localhost").withPort(9042); - - return cluster = builder.build(); - } } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java index 8739b8358..e91bad306 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java @@ -1,40 +1,15 @@ package org.springframework.cassandra.test.integration.config; -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.inject.Inject; - -import org.apache.cassandra.exceptions.ConfigurationException; -import org.apache.thrift.transport.TTransportException; -import org.cassandraunit.utils.EmbeddedCassandraServerHelper; -import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.datastax.driver.core.Session; - -@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) -public class ConfigTest { - - @BeforeClass - public static void startCassandra() throws ConfigurationException, TTransportException, IOException { - EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml"); - } - - @Inject - Session session; +public class ConfigTest extends AbstractIntegrationTest { @Test public void test() { - assertNotNull(session); - session - .execute("CREATE KEYSPACE testy WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); - session.execute("USE testy"); + .execute("CREATE KEYSPACE ConfigTest WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); + session.execute("USE ConfigTest"); } } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java new file mode 100644 index 000000000..a638a665c --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java @@ -0,0 +1,16 @@ +package org.springframework.cassandra.test.integration.config; + +import static org.junit.Assert.assertNotNull; + +import com.datastax.driver.core.Session; + +public class IntegrationTestUtils { + + public static void assertSession(Session session) { + assertNotNull(session); + } + + public static void assertKeyspaceExists(String keyspace, Session session) { + assertNotNull(session.getCluster().getMetadata().getKeyspace(KeyspaceCreatingConfig.KEYSPACE)); + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfig.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfig.java new file mode 100644 index 000000000..16a02d275 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfig.java @@ -0,0 +1,14 @@ +package org.springframework.cassandra.test.integration.config; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class KeyspaceCreatingConfig extends AbstractKeyspaceCreatingConfiguration { + + public static final String KEYSPACE = "kcc"; + + @Override + protected String getKeyspace() { + return KEYSPACE; + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfigTest.java new file mode 100644 index 000000000..eec886594 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfigTest.java @@ -0,0 +1,13 @@ +package org.springframework.cassandra.test.integration.config; + +import org.junit.Test; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = KeyspaceCreatingConfig.class) +public class KeyspaceCreatingConfigTest extends AbstractIntegrationTest { + + @Test + public void test() { + IntegrationTestUtils.assertKeyspaceExists(KeyspaceCreatingConfig.KEYSPACE, session); + } +} diff --git a/spring-cassandra/src/test/resources/cassandra.yaml b/spring-cassandra/src/test/resources/cassandra.yaml index 8fc6462aa..df542f82e 100644 --- a/spring-cassandra/src/test/resources/cassandra.yaml +++ b/spring-cassandra/src/test/resources/cassandra.yaml @@ -339,7 +339,7 @@ native_transport_port: 9042 # transport is used. They are similar to rpc_min_threads and rpc_max_threads, # though the defaults differ slightly. # native_transport_min_threads: 16 -native_transport_max_threads: 64 +native_transport_max_threads: 48 # Whether to start the thrift rpc server. start_rpc: true diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java index 22127752b..0d2fcd986 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java @@ -65,7 +65,7 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC */ @Bean public SpringDataKeyspace keyspace() throws Exception { - return new SpringDataKeyspace(getKeyspaceName(), session(), converter()); + return new SpringDataKeyspace(getKeyspace(), session(), converter()); } /** diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java index aec114209..7a55f61ff 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java @@ -28,7 +28,7 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration { * @see org.springframework.data.cassandra.config.AbstractCassandraConfiguration#getKeyspaceName() */ @Override - protected String getKeyspaceName() { + protected String getKeyspace() { return keyspace; } From f46a7ef99118258ae3de0f87b3299923d0d3c273 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 16:43:19 -0600 Subject: [PATCH 11/21] better fix for too many thread C* errors: fork tests --- pom.xml | 7 ++++--- spring-cassandra/src/test/resources/cassandra.yaml | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 6a52dc867..17b44d6df 100644 --- a/pom.xml +++ b/pom.xml @@ -64,7 +64,7 @@ Alex Shvid a at shvid.com - Project Lead + Project Lead Developer -8 @@ -227,7 +227,7 @@ org.apache.maven.plugins maven-surefire-plugin - -Xmx2048m -XX:MaxPermSize=512m + -Xmx2048m -XX:MaxPermSize=512m methods 10 false @@ -247,7 +247,8 @@ org.apache.maven.plugins maven-failsafe-plugin - -Xmx2048m -XX:MaxPermSize=512m + always + -Xmx2048m -XX:MaxPermSize=512m false **/test/integration/**/*.java diff --git a/spring-cassandra/src/test/resources/cassandra.yaml b/spring-cassandra/src/test/resources/cassandra.yaml index df542f82e..67c59aba4 100644 --- a/spring-cassandra/src/test/resources/cassandra.yaml +++ b/spring-cassandra/src/test/resources/cassandra.yaml @@ -339,7 +339,7 @@ native_transport_port: 9042 # transport is used. They are similar to rpc_min_threads and rpc_max_threads, # though the defaults differ slightly. # native_transport_min_threads: 16 -native_transport_max_threads: 48 +#native_transport_max_threads: 48 # Whether to start the thrift rpc server. start_rpc: true From 4f78a0b57f797917839fdc725da2f8c5542e1708 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 16:47:35 -0600 Subject: [PATCH 12/21] wip: changed after cassandra.yaml -> spring-cassandra.yaml --- .../test/integration/config/AbstractIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java index a731283af..e5bd39dff 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java @@ -19,7 +19,7 @@ public abstract class AbstractIntegrationTest { @BeforeClass public static void startCassandra() throws ConfigurationException, TTransportException, IOException { - EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml"); + EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml"); } @Inject From dea16b47be9ea7dc5624a7d854d47589231678c3 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 16:43:19 -0600 Subject: [PATCH 13/21] better fix for too many threads C* error: fork tests --- pom.xml | 7 ++++--- spring-cassandra/src/test/resources/spring-cassandra.yaml | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 17abe6c1e..b6ad3dc88 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ Alex Shvid a at shvid.com - Project Lead + Project Lead Developer -8 @@ -239,7 +239,7 @@ org.apache.maven.plugins maven-surefire-plugin - -Xmx2048m -XX:MaxPermSize=512m + -Xmx2048m -XX:MaxPermSize=512m methods 10 false @@ -260,7 +260,8 @@ maven-failsafe-plugin ${failsafe.version} - -Xmx2048m -XX:MaxPermSize=512m + always + -Xmx2048m -XX:MaxPermSize=512m false **/test/integration/**/*.java diff --git a/spring-cassandra/src/test/resources/spring-cassandra.yaml b/spring-cassandra/src/test/resources/spring-cassandra.yaml index 8fc6462aa..67c59aba4 100644 --- a/spring-cassandra/src/test/resources/spring-cassandra.yaml +++ b/spring-cassandra/src/test/resources/spring-cassandra.yaml @@ -339,7 +339,7 @@ native_transport_port: 9042 # transport is used. They are similar to rpc_min_threads and rpc_max_threads, # though the defaults differ slightly. # native_transport_min_threads: 16 -native_transport_max_threads: 64 +#native_transport_max_threads: 48 # Whether to start the thrift rpc server. start_rpc: true From ffdeaa9fe8b0a38c2e2ecf605483372723734179 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Tue, 10 Dec 2013 14:17:56 -0600 Subject: [PATCH 14/21] spring-cassandra now supporting xml config --- .../cassandra/config/xml/BeanNames.java | 3 +- .../config/xml/CassandraClusterParser.java | 33 +- .../config/xml/CassandraKeyspaceParser.java | 17 +- .../config/xml/CassandraNamespaceHandler.java | 8 +- .../xml/CassandraSessionFactoryBean.java | 62 +--- .../config/xml/CassandraSessionParser.java | 26 +- .../xml/CassandraTemplateFactoryBean.java | 63 ++++ .../config/xml/CassandraTemplateParser.java | 57 +++ .../cassandra/config/xml/ParsingUtils.java | 33 ++ .../cassandra/core/SessionFactoryBean.java | 87 ----- .../{spring.handlers.todo => spring.handlers} | 0 .../{spring.schemas.todo => spring.schemas} | 0 .../{spring.tooling.todo => spring.tooling} | 0 .../cassandra/config/spring-cassandra-1.0.xsd | 212 +++++------ ...tractEmbeddedCassandraIntegrationTest.java | 31 +- .../config/AbstractIntegrationTest.java | 32 -- .../config/IntegrationTestUtils.java | 2 +- .../config/java/AbstractIntegrationTest.java | 23 ++ .../AbstractIntegrationTestConfiguration.java | 2 +- ...AbstractKeyspaceCreatingConfiguration.java | 2 +- .../integration/config/{ => java}/Config.java | 2 +- .../config/{ => java}/ConfigTest.java | 2 +- .../{ => java}/KeyspaceCreatingConfig.java | 2 +- .../KeyspaceCreatingConfigTest.java | 3 +- .../config/xml/MinimalXmlConfigTest.java | 38 ++ .../integration/config/xml/XmlConfigTest.java | 30 ++ .../xml/MinimalXmlConfigTest-context.xml | 14 + .../config/xml/XmlConfigTest-context.xml | 33 ++ .../config/xml/xmlconfigtest.properties} | 6 +- .../CassandraNamespaceTests-context.xml | 55 --- .../CassandraClusterFactoryBean.java | 0 .../cassandra/config/CompressionType.java | 25 -- .../config/PoolingOptionsConfig.java | 62 ---- .../cassandra/config/SocketOptionsConfig.java | 89 ----- .../core/CassandraKeyspaceFactoryBean.java | 341 ------------------ 35 files changed, 450 insertions(+), 945 deletions(-) create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/ParsingUtils.java delete mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/core/SessionFactoryBean.java rename spring-cassandra/src/main/resources/META-INF/{spring.handlers.todo => spring.handlers} (100%) rename spring-cassandra/src/main/resources/META-INF/{spring.schemas.todo => spring.schemas} (100%) rename spring-cassandra/src/main/resources/META-INF/{spring.tooling.todo => spring.tooling} (100%) delete mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractIntegrationTest.java rename spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/{ => java}/AbstractIntegrationTestConfiguration.java (88%) rename spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/{ => java}/AbstractKeyspaceCreatingConfiguration.java (95%) rename spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/{ => java}/Config.java (75%) rename spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/{ => java}/ConfigTest.java (85%) rename spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/{ => java}/KeyspaceCreatingConfig.java (80%) rename spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/{ => java}/KeyspaceCreatingConfigTest.java (69%) create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest.java create mode 100644 spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest-context.xml create mode 100644 spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest-context.xml rename spring-cassandra/src/test/resources/org/springframework/{data/cassandra/test/integration/config/cassandra.properties => cassandra/test/integration/config/xml/xmlconfigtest.properties} (62%) delete mode 100644 spring-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml rename spring-data-cassandra/src/main/java/org/springframework/data/cassandra/{core => config}/CassandraClusterFactoryBean.java (100%) delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CompressionType.java delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/PoolingOptionsConfig.java delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/SocketOptionsConfig.java delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraKeyspaceFactoryBean.java diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java index aa5dabf59..07f3537dd 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/BeanNames.java @@ -18,6 +18,7 @@ package org.springframework.cassandra.config.xml; /** * @author Alex Shvid * @author David Webb + * @author Matthew T. Adams */ public final class BeanNames { @@ -27,5 +28,5 @@ public final class BeanNames { public static final String CASSANDRA_CLUSTER = "cassandra-cluster"; public static final String CASSANDRA_KEYSPACE = "cassandra-keyspace"; public static final String CASSANDRA_SESSION = "cassandra-session"; - + public static final String CASSANDRA_TEMPLATE = "cassandra-template"; } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java index fc3201e0e..4158dedca 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java @@ -74,15 +74,14 @@ public class CassandraClusterParser extends AbstractSimpleBeanDefinitionParser { builder.addPropertyValue("compressionType", CompressionType.valueOf(compression)); } - postProcess(builder, element); + parseChildElements(builder, element); } - @Override - protected void postProcess(BeanDefinitionBuilder builder, Element element) { - List subElements = DomUtils.getChildElements(element); + protected void parseChildElements(BeanDefinitionBuilder builder, Element element) { + List elements = DomUtils.getChildElements(element); // parse nested elements - for (Element subElement : subElements) { + for (Element subElement : elements) { String name = subElement.getLocalName(); if ("local-pooling-options".equals(name)) { @@ -99,11 +98,10 @@ public class CassandraClusterParser extends AbstractSimpleBeanDefinitionParser { private BeanDefinition parsePoolingOptions(Element element) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PoolingOptionsConfig.class); - // TODO - // ParsingUtils.setPropertyValue(builder, element, "min-simultaneous-requests", "minSimultaneousRequests"); - // ParsingUtils.setPropertyValue(builder, element, "max-simultaneous-requests", "maxSimultaneousRequests"); - // ParsingUtils.setPropertyValue(builder, element, "core-connections", "coreConnections"); - // ParsingUtils.setPropertyValue(builder, element, "max-connections", "maxConnections"); + ParsingUtils.setPropertyValue(builder, element, "min-simultaneous-requests", "minSimultaneousRequests"); + ParsingUtils.setPropertyValue(builder, element, "max-simultaneous-requests", "maxSimultaneousRequests"); + ParsingUtils.setPropertyValue(builder, element, "core-connections", "coreConnections"); + ParsingUtils.setPropertyValue(builder, element, "max-connections", "maxConnections"); return builder.getBeanDefinition(); } @@ -111,14 +109,13 @@ public class CassandraClusterParser extends AbstractSimpleBeanDefinitionParser { private BeanDefinition parseSocketOptions(Element element) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SocketOptionsConfig.class); - // TODO - // ParsingUtils.setPropertyValue(builder, element, "connect-timeout-mls", "connectTimeoutMls"); - // ParsingUtils.setPropertyValue(builder, element, "keep-alive", "keepAlive"); - // ParsingUtils.setPropertyValue(builder, element, "reuse-address", "reuseAddress"); - // ParsingUtils.setPropertyValue(builder, element, "so-linger", "soLinger"); - // ParsingUtils.setPropertyValue(builder, element, "tcp-no-delay", "tcpNoDelay"); - // ParsingUtils.setPropertyValue(builder, element, "receive-buffer-size", "receiveBufferSize"); - // ParsingUtils.setPropertyValue(builder, element, "send-buffer-size", "sendBufferSize"); + ParsingUtils.setPropertyValue(builder, element, "connect-timeout-mls", "connectTimeoutMls"); + ParsingUtils.setPropertyValue(builder, element, "keep-alive", "keepAlive"); + ParsingUtils.setPropertyValue(builder, element, "reuse-address", "reuseAddress"); + ParsingUtils.setPropertyValue(builder, element, "so-linger", "soLinger"); + ParsingUtils.setPropertyValue(builder, element, "tcp-no-delay", "tcpNoDelay"); + ParsingUtils.setPropertyValue(builder, element, "receive-buffer-size", "receiveBufferSize"); + ParsingUtils.setPropertyValue(builder, element, "send-buffer-size", "sendBufferSize"); return builder.getBeanDefinition(); } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java index 37ee4988f..d98be4d2f 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java @@ -31,9 +31,10 @@ import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; /** - * Parser for <keyspace;gt; definitions. + * Parser for <keyspace> definitions. * * @author Alex Shvid + * @author Matthew T. Adams */ public class CassandraKeyspaceParser extends AbstractSimpleBeanDefinitionParser { @@ -95,11 +96,10 @@ public class CassandraKeyspaceParser extends AbstractSimpleBeanDefinitionParser private BeanDefinition parseKeyspaceAttributes(Element element) { BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(KeyspaceAttributes.class); - // TODO - // ParsingUtils.setPropertyValue(defBuilder, element, "auto", "auto"); - // ParsingUtils.setPropertyValue(defBuilder, element, "replication-strategy", "replicationStrategy"); - // ParsingUtils.setPropertyValue(defBuilder, element, "replication-factor", "replicationFactor"); - // ParsingUtils.setPropertyValue(defBuilder, element, "durable-writes", "durableWrites"); + ParsingUtils.setPropertyValue(defBuilder, element, "auto", "auto"); + ParsingUtils.setPropertyValue(defBuilder, element, "replication-strategy", "replicationStrategy"); + ParsingUtils.setPropertyValue(defBuilder, element, "replication-factor", "replicationFactor"); + ParsingUtils.setPropertyValue(defBuilder, element, "durable-writes", "durableWrites"); List subElements = DomUtils.getChildElements(element); ManagedList tables = new ManagedList(subElements.size()); @@ -122,9 +122,8 @@ public class CassandraKeyspaceParser extends AbstractSimpleBeanDefinitionParser private BeanDefinition parseTable(Element element) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(TableAttributes.class); - // TODO - // ParsingUtils.setPropertyValue(builder, element, "entity", "entity"); - // ParsingUtils.setPropertyValue(builder, element, "name", "name"); + ParsingUtils.setPropertyValue(builder, element, "entity", "entity"); + ParsingUtils.setPropertyValue(builder, element, "name", "name"); return builder.getBeanDefinition(); } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java index 0a2ad6518..4563c3081 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java @@ -18,9 +18,10 @@ package org.springframework.cassandra.config.xml; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; /** - * Namespace handler for <cassandra;gt;. + * Namespace handler for <cassandra> elements. * * @author Alex Shvid + * @author Matthew T. Adams */ public class CassandraNamespaceHandler extends NamespaceHandlerSupport { @@ -28,9 +29,8 @@ public class CassandraNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("cluster", new CassandraClusterParser()); - registerBeanDefinitionParser("keyspace", new CassandraKeyspaceParser()); + // registerBeanDefinitionParser("keyspace", new CassandraKeyspaceParser()); registerBeanDefinitionParser("session", new CassandraSessionParser()); - + registerBeanDefinitionParser("template", new CassandraTemplateParser()); } - } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java index 0be9a5feb..01396dee3 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java @@ -15,54 +15,41 @@ */ package org.springframework.cassandra.config.xml; -import java.util.Map; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; -import org.springframework.cassandra.config.KeyspaceAttributes; import org.springframework.cassandra.support.CassandraExceptionTranslator; import org.springframework.dao.DataAccessException; import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.util.StringUtils; import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.KeyspaceMetadata; import com.datastax.driver.core.Session; /** - * Convenient factory for configuring a Cassandra Session. Session is a thread safe singleton and created per a - * keyspace. So, it is enough to have one session per application. + * Factory for configuring a Cassandra {@link Session}, which is a thread-safe singleton. As such, it is sufficient to + * have one {@link Session} per application and keyspace. * * @author Alex Shvid * @author Matthew T. Adams */ public class CassandraSessionFactoryBean implements FactoryBean, InitializingBean, DisposableBean, - BeanClassLoaderAware, PersistenceExceptionTranslator { + PersistenceExceptionTranslator { private static final Logger log = LoggerFactory.getLogger(CassandraSessionFactoryBean.class); public static final String DEFAULT_REPLICATION_STRATEGY = "SimpleStrategy"; public static final int DEFAULT_REPLICATION_FACTOR = 1; - private ClassLoader beanClassLoader; - private Cluster cluster; private Session session; - private String keyspace; - - private KeyspaceAttributes keyspaceAttributes; + private String keyspaceName; private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator(); - public void setBeanClassLoader(ClassLoader classLoader) { - this.beanClassLoader = classLoader; - } - public Session getObject() { return session; } @@ -101,7 +88,7 @@ public class CassandraSessionFactoryBean implements FactoryBean, Initia throw new IllegalArgumentException("at least one cluster is required"); } - this.session = StringUtils.hasText(this.keyspace) ? cluster.connect(keyspace) : cluster.connect(); + this.session = StringUtils.hasText(this.keyspaceName) ? cluster.connect(keyspaceName) : cluster.connect(); } /* @@ -109,49 +96,14 @@ public class CassandraSessionFactoryBean implements FactoryBean, Initia * @see org.springframework.beans.factory.DisposableBean#destroy() */ public void destroy() throws Exception { - this.session.shutdown(); } - public void setKeyspace(String keyspace) { - this.keyspace = keyspace; + public void setKeyspaceName(String keyspaceName) { + this.keyspaceName = keyspaceName; } public void setCluster(Cluster cluster) { this.cluster = cluster; } - - public void setKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes) { - this.keyspaceAttributes = keyspaceAttributes; - } - - private static String compareKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes, - KeyspaceMetadata keyspaceMetadata) { - if (keyspaceAttributes.isDurableWrites() != keyspaceMetadata.isDurableWrites()) { - return "durableWrites"; - } - Map replication = keyspaceMetadata.getReplication(); - String replicationFactorStr = replication.get("replication_factor"); - if (replicationFactorStr == null) { - return "replication_factor"; - } - try { - int replicationFactor = Integer.parseInt(replicationFactorStr); - if (keyspaceAttributes.getReplicationFactor() != replicationFactor) { - return "replication_factor"; - } - } catch (NumberFormatException e) { - return "replication_factor"; - } - - String attributesStrategy = keyspaceAttributes.getReplicationStrategy(); - if (attributesStrategy.indexOf('.') == -1) { - attributesStrategy = "org.apache.cassandra.locator." + attributesStrategy; - } - String replicationStrategy = replication.get("class"); - if (!attributesStrategy.equals(replicationStrategy)) { - return "replication_class"; - } - return null; - } } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java index 357ce17a6..94ee88a22 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java @@ -20,21 +20,21 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.cassandra.core.SessionFactoryBean; import org.springframework.util.StringUtils; import org.w3c.dom.Element; /** - * Parser for <session;gt; definitions. + * Parser for <session> definitions. * * @author David Webb + * @author Matthew T. Adams */ public class CassandraSessionParser extends AbstractSimpleBeanDefinitionParser { @Override protected Class getBeanClass(Element element) { - return SessionFactoryBean.class; + return CassandraSessionFactoryBean.class; } /* @@ -52,18 +52,16 @@ public class CassandraSessionParser extends AbstractSimpleBeanDefinitionParser { @Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - String keyspaceRef = element.getAttribute("cassandra-keyspace-ref"); - if (!StringUtils.hasText(keyspaceRef)) { - keyspaceRef = BeanNames.CASSANDRA_KEYSPACE; + String keyspaceName = element.getAttribute("keyspace-name"); + if (!StringUtils.hasText(keyspaceName)) { + keyspaceName = null; } - builder.addPropertyReference("keyspace", keyspaceRef); + builder.addPropertyValue("keyspaceName", keyspaceName); - postProcess(builder, element); + String clusterRef = element.getAttribute("cluster-ref"); + if (!StringUtils.hasText(clusterRef)) { + clusterRef = BeanNames.CASSANDRA_CLUSTER; + } + builder.addPropertyReference("cluster", clusterRef); } - - @Override - protected void postProcess(BeanDefinitionBuilder builder, Element element) { - - } - } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java new file mode 100644 index 000000000..414561b7e --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java @@ -0,0 +1,63 @@ +/* + * Copyright 2011-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.cassandra.core.CassandraTemplate; + +import com.datastax.driver.core.Session; + +/** + * Factory for configuring a {@link CassandraTemplate}. + * + * @author Matthew T. Adams + */ + +public class CassandraTemplateFactoryBean implements FactoryBean, InitializingBean { + + private static final Logger log = LoggerFactory.getLogger(CassandraTemplateFactoryBean.class); + + private CassandraTemplate template; + private Session session; + + public CassandraTemplate getObject() { + return template; + } + + public Class getObjectType() { + return CassandraTemplate.class; + } + + public boolean isSingleton() { + return true; + } + + public void afterPropertiesSet() throws Exception { + + if (session == null) { + throw new IllegalStateException("session is required"); + } + + this.template = new CassandraTemplate(session); + } + + public void setSession(Session session) { + this.session = session; + } +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java new file mode 100644 index 000000000..2fa5d35ad --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java @@ -0,0 +1,57 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.config.xml; + +import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * Parser for <template> definitions. + * + * @author David Webb + * @author Matthew T. Adams + */ + +public class CassandraTemplateParser extends AbstractSimpleBeanDefinitionParser { + + @Override + protected Class getBeanClass(Element element) { + return CassandraTemplateFactoryBean.class; + } + + @Override + protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) + throws BeanDefinitionStoreException { + + String id = super.resolveId(element, definition, parserContext); + return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_TEMPLATE; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + + String sessionRef = element.getAttribute("session-ref"); + if (!StringUtils.hasText(sessionRef)) { + sessionRef = BeanNames.CASSANDRA_SESSION; + } + builder.addPropertyReference("session", sessionRef); + } +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/ParsingUtils.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/ParsingUtils.java new file mode 100644 index 000000000..826c26e3c --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/ParsingUtils.java @@ -0,0 +1,33 @@ +package org.springframework.cassandra.config.xml; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +public class ParsingUtils { + + /** + * Configures a property value for the given property name reading the attribute of the given name from the given + * {@link Element} if the attribute is configured. + * + * @param builder must not be {@literal null}. + * @param element must not be {@literal null}. + * @param attrName must not be {@literal null} or empty. + * @param propertyName must not be {@literal null} or empty. + */ + public static void setPropertyValue(BeanDefinitionBuilder builder, Element element, String attrName, + String propertyName) { + + Assert.notNull(builder, "BeanDefinitionBuilder must not be null!"); + Assert.notNull(element, "Element must not be null!"); + Assert.hasText(attrName, "Attribute name must not be null!"); + Assert.hasText(propertyName, "Property name must not be null!"); + + String attr = element.getAttribute(attrName); + + if (StringUtils.hasText(attr)) { + builder.addPropertyValue(propertyName, attr); + } + } +} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/SessionFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/SessionFactoryBean.java deleted file mode 100644 index f753a322f..000000000 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/SessionFactoryBean.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2011-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.cassandra.core; - -import org.springframework.beans.factory.FactoryBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.cassandra.core.Keyspace; - -import com.datastax.driver.core.Session; - -/** - * @author David Webb - * - */ -public class SessionFactoryBean implements FactoryBean, InitializingBean { - - private Keyspace keyspace; - - public SessionFactoryBean() { - } - - public SessionFactoryBean(Keyspace keyspace) { - setKeyspace(keyspace); - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - @Override - public void afterPropertiesSet() throws Exception { - if (keyspace == null) { - throw new IllegalStateException("Keyspace required."); - } - } - - /** - * @return Returns the keyspace. - */ - public Keyspace getKeyspace() { - return keyspace; - } - - /** - * @param keyspace The keyspace to set. - */ - public void setKeyspace(Keyspace keyspace) { - this.keyspace = keyspace; - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#getObject() - */ - @Override - public Session getObject() { - return keyspace.getSession(); - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#getObjectType() - */ - @Override - public Class getObjectType() { - return Session.class; - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#isSingleton() - */ - @Override - public boolean isSingleton() { - return true; - } - -} diff --git a/spring-cassandra/src/main/resources/META-INF/spring.handlers.todo b/spring-cassandra/src/main/resources/META-INF/spring.handlers similarity index 100% rename from spring-cassandra/src/main/resources/META-INF/spring.handlers.todo rename to spring-cassandra/src/main/resources/META-INF/spring.handlers diff --git a/spring-cassandra/src/main/resources/META-INF/spring.schemas.todo b/spring-cassandra/src/main/resources/META-INF/spring.schemas similarity index 100% rename from spring-cassandra/src/main/resources/META-INF/spring.schemas.todo rename to spring-cassandra/src/main/resources/META-INF/spring.schemas diff --git a/spring-cassandra/src/main/resources/META-INF/spring.tooling.todo b/spring-cassandra/src/main/resources/META-INF/spring.tooling similarity index 100% rename from spring-cassandra/src/main/resources/META-INF/spring.tooling.todo rename to spring-cassandra/src/main/resources/META-INF/spring.tooling diff --git a/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd b/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd index 98c1f9eff..7f4c437e4 100644 --- a/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd +++ b/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd @@ -12,14 +12,14 @@ + ]]> @@ -29,11 +29,25 @@ Defines a Cassandra Session instance used for accessing Cassandra Keyspace'. + + + + + + + + + + + @@ -43,6 +57,20 @@ Defines a Cassandra Cluster instance used for accessing Cassandra'. + + + + + + + + + + + - + - + @@ -174,20 +204,6 @@ RetryPolicy implementation. - - - - - - - - - - - - + + ]]> - - - - - - - - - - - - +The name of the Cassandra keyspace. + ]]> @@ -239,12 +237,11 @@ The reference to a CassandraConverter instance. Default is null. - + - + @@ -335,55 +332,12 @@ Sets the SO_SNDBUF socket option. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + ]]> + ]]> - - - - - - - - - - - - - +Whether to support durable writes in the Cassandra keyspace. Default value is 'true'. + ]]> @@ -425,29 +362,44 @@ Table name override. +The name of the Session definition; default is 'cassandra-session'. + ]]> - + + + + + - +The name of a Cassandra Keyspace. No default; for the system keyspace, use the empty string. + ]]> + + + + + + + + + + + + + - - - - - - - - - - \ No newline at end of file diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java index 2d89db056..4b5127af6 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java @@ -7,8 +7,6 @@ import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.thrift.transport.TTransportException; import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.KeyspaceMetadata; @@ -20,12 +18,20 @@ public abstract class AbstractEmbeddedCassandraIntegrationTest { protected final static String CASSANDRA_HOST = "localhost"; protected final static int CASSANDRA_NATIVE_PORT = 9042; - @BeforeClass - public static void beforeClass() throws ConfigurationException, TTransportException, IOException, + public static void startCassandra() throws ConfigurationException, TTransportException, IOException, InterruptedException { EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG); } + public AbstractEmbeddedCassandraIntegrationTest() { + try { + startCassandra(); + } catch (Exception e) { + throw new RuntimeException(e); + } + connect(); + } + /** * Whether to clear the cluster before the next test. */ @@ -47,6 +53,10 @@ public abstract class AbstractEmbeddedCassandraIntegrationTest { */ protected Session session; + protected String keyspace() { + return keyspace; + } + /** * Returns whether we're currently connected to the cluster. */ @@ -58,23 +68,22 @@ public abstract class AbstractEmbeddedCassandraIntegrationTest { return Cluster.builder().addContactPoint(CASSANDRA_HOST).withPort(CASSANDRA_NATIVE_PORT).build(); } - @Before - public void before() { + public void connect() { if (connect && !connected()) { cluster = cluster(); - if (keyspace == null) { + if (keyspace() == null) { session = cluster.connect(); } else { - KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace); + KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace()); if (kmd == null) { // then create keyspace session = cluster.connect(); - session.execute("CREATE KEYSPACE " + keyspace + session.execute("CREATE KEYSPACE " + keyspace() + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};"); - session.execute("USE " + keyspace + ";"); + session.execute("USE " + keyspace() + ";"); } else {// else keyspace already exists - session = cluster.connect(keyspace); + session = cluster.connect(keyspace()); } } } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java deleted file mode 100644 index e5bd39dff..000000000 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.springframework.cassandra.test.integration.config; - -import java.io.IOException; - -import javax.inject.Inject; - -import org.apache.cassandra.exceptions.ConfigurationException; -import org.apache.thrift.transport.TTransportException; -import org.cassandraunit.utils.EmbeddedCassandraServerHelper; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.datastax.driver.core.Session; - -@RunWith(SpringJUnit4ClassRunner.class) -public abstract class AbstractIntegrationTest { - - @BeforeClass - public static void startCassandra() throws ConfigurationException, TTransportException, IOException { - EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml"); - } - - @Inject - public Session session; - - @Before - public void assertSession() { - IntegrationTestUtils.assertSession(session); - } -} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java index a638a665c..bac0245c1 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/IntegrationTestUtils.java @@ -11,6 +11,6 @@ public class IntegrationTestUtils { } public static void assertKeyspaceExists(String keyspace, Session session) { - assertNotNull(session.getCluster().getMetadata().getKeyspace(KeyspaceCreatingConfig.KEYSPACE)); + assertNotNull(session.getCluster().getMetadata().getKeyspace(keyspace)); } } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractIntegrationTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractIntegrationTest.java new file mode 100644 index 000000000..0ac2b001c --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractIntegrationTest.java @@ -0,0 +1,23 @@ +package org.springframework.cassandra.test.integration.config.java; + +import javax.inject.Inject; + +import org.junit.Before; +import org.junit.runner.RunWith; +import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest; +import org.springframework.cassandra.test.integration.config.IntegrationTestUtils; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.datastax.driver.core.Session; + +@RunWith(SpringJUnit4ClassRunner.class) +public abstract class AbstractIntegrationTest extends AbstractEmbeddedCassandraIntegrationTest { + + @Inject + public Session session; + + @Before + public void assertSession() { + IntegrationTestUtils.assertSession(session); + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTestConfiguration.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractIntegrationTestConfiguration.java similarity index 88% rename from spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTestConfiguration.java rename to spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractIntegrationTestConfiguration.java index 6c92065a7..d2b5fb7f5 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractIntegrationTestConfiguration.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractIntegrationTestConfiguration.java @@ -1,4 +1,4 @@ -package org.springframework.cassandra.test.integration.config; +package org.springframework.cassandra.test.integration.config.java; import org.springframework.cassandra.config.java.AbstractCassandraConfiguration; import org.springframework.context.annotation.Configuration; diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractKeyspaceCreatingConfiguration.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java similarity index 95% rename from spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractKeyspaceCreatingConfiguration.java rename to spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java index e23408760..f0ceefb33 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/AbstractKeyspaceCreatingConfiguration.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java @@ -1,4 +1,4 @@ -package org.springframework.cassandra.test.integration.config; +package org.springframework.cassandra.test.integration.config.java; import org.springframework.cassandra.config.KeyspaceAttributes; import org.springframework.cassandra.config.PoolingOptionsConfig; diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/Config.java similarity index 75% rename from spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java rename to spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/Config.java index a4e977e3c..8a1abf636 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/Config.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/Config.java @@ -1,4 +1,4 @@ -package org.springframework.cassandra.test.integration.config; +package org.springframework.cassandra.test.integration.config.java; import org.springframework.context.annotation.Configuration; diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/ConfigTest.java similarity index 85% rename from spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java rename to spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/ConfigTest.java index e91bad306..d00de3a59 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/ConfigTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/ConfigTest.java @@ -1,4 +1,4 @@ -package org.springframework.cassandra.test.integration.config; +package org.springframework.cassandra.test.integration.config.java; import org.junit.Test; import org.springframework.test.context.ContextConfiguration; diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfig.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfig.java similarity index 80% rename from spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfig.java rename to spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfig.java index 16a02d275..13d75dc69 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfig.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfig.java @@ -1,4 +1,4 @@ -package org.springframework.cassandra.test.integration.config; +package org.springframework.cassandra.test.integration.config.java; import org.springframework.context.annotation.Configuration; diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfigTest.java similarity index 69% rename from spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfigTest.java rename to spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfigTest.java index eec886594..26cf12b70 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/KeyspaceCreatingConfigTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfigTest.java @@ -1,6 +1,7 @@ -package org.springframework.cassandra.test.integration.config; +package org.springframework.cassandra.test.integration.config.java; import org.junit.Test; +import org.springframework.cassandra.test.integration.config.IntegrationTestUtils; import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(classes = KeyspaceCreatingConfig.class) diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest.java new file mode 100644 index 000000000..760c00192 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest.java @@ -0,0 +1,38 @@ +package org.springframework.cassandra.test.integration.config.xml; + +import static org.junit.Assert.*; + +import javax.inject.Inject; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.cassandra.core.CassandraOperations; +import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest; +import org.springframework.cassandra.test.integration.config.IntegrationTestUtils; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.datastax.driver.core.Session; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class MinimalXmlConfigTest extends AbstractEmbeddedCassandraIntegrationTest { + + protected String keyspace() { + return "minimalxmlconfigtest"; + } + + @Inject + Session s; + + @Inject + CassandraOperations ops; + + @Test + public void test() { + IntegrationTestUtils.assertSession(s); + IntegrationTestUtils.assertKeyspaceExists(keyspace(), s); + + assertNotNull(ops); + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest.java new file mode 100644 index 000000000..b8eb27ea8 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest.java @@ -0,0 +1,30 @@ +package org.springframework.cassandra.test.integration.config.xml; + +import javax.inject.Inject; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest; +import org.springframework.cassandra.test.integration.config.IntegrationTestUtils; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.datastax.driver.core.Session; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class XmlConfigTest extends AbstractEmbeddedCassandraIntegrationTest { + + protected String keyspace() { + return "xmlconfigtest"; + } + + @Inject + Session s; + + @Test + public void test() { + IntegrationTestUtils.assertSession(s); + IntegrationTestUtils.assertKeyspaceExists(keyspace(), s); + } +} diff --git a/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest-context.xml b/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest-context.xml new file mode 100644 index 000000000..e0939fdc3 --- /dev/null +++ b/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/MinimalXmlConfigTest-context.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest-context.xml b/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest-context.xml new file mode 100644 index 000000000..39e2ff612 --- /dev/null +++ b/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/XmlConfigTest-context.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + diff --git a/spring-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/cassandra.properties b/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/xmlconfigtest.properties similarity index 62% rename from spring-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/cassandra.properties rename to spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/xmlconfigtest.properties index 6a0dd3197..2d7a6eb68 100644 --- a/spring-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/cassandra.properties +++ b/spring-cassandra/src/test/resources/org/springframework/cassandra/test/integration/config/xml/xmlconfigtest.properties @@ -1,7 +1,3 @@ cassandra.contactPoints=localhost cassandra.port=9042 -cassandra.keyspace=TestKS123 - - - - +cassandra.keyspace=xmlconfigtest diff --git a/spring-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml b/spring-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml deleted file mode 100644 index 4050ec523..000000000 --- a/spring-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraClusterFactoryBean.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterFactoryBean.java similarity index 100% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraClusterFactoryBean.java rename to spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterFactoryBean.java diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CompressionType.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CompressionType.java deleted file mode 100644 index c74c36676..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CompressionType.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2010-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.config; - -/** - * Simple enumeration for the various compression types. - * - * @author Alex Shvid - */ -public enum CompressionType { - NONE, SNAPPY; -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/PoolingOptionsConfig.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/PoolingOptionsConfig.java deleted file mode 100644 index 4ba96539e..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/PoolingOptionsConfig.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2011-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.config; - -/** - * Pooling options POJO. Can be remote or local. - * - * @author Alex Shvid - */ -public class PoolingOptionsConfig { - - private Integer minSimultaneousRequests; - private Integer maxSimultaneousRequests; - private Integer coreConnections; - private Integer maxConnections; - - public Integer getMinSimultaneousRequests() { - return minSimultaneousRequests; - } - - public void setMinSimultaneousRequests(Integer minSimultaneousRequests) { - this.minSimultaneousRequests = minSimultaneousRequests; - } - - public Integer getMaxSimultaneousRequests() { - return maxSimultaneousRequests; - } - - public void setMaxSimultaneousRequests(Integer maxSimultaneousRequests) { - this.maxSimultaneousRequests = maxSimultaneousRequests; - } - - public Integer getCoreConnections() { - return coreConnections; - } - - public void setCoreConnections(Integer coreConnections) { - this.coreConnections = coreConnections; - } - - public Integer getMaxConnections() { - return maxConnections; - } - - public void setMaxConnections(Integer maxConnections) { - this.maxConnections = maxConnections; - } - -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/SocketOptionsConfig.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/SocketOptionsConfig.java deleted file mode 100644 index 1e72c7742..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/SocketOptionsConfig.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2011-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.config; - -/** - * Socket options POJO. Uses to configure Netty. - * - * @author Alex Shvid - */ -public class SocketOptionsConfig { - - private Integer connectTimeoutMls; - private Boolean keepAlive; - private Boolean reuseAddress; - private Integer soLinger; - private Boolean tcpNoDelay; - private Integer receiveBufferSize; - private Integer sendBufferSize; - - public Integer getConnectTimeoutMls() { - return connectTimeoutMls; - } - - public void setConnectTimeoutMls(Integer connectTimeoutMls) { - this.connectTimeoutMls = connectTimeoutMls; - } - - public Boolean getKeepAlive() { - return keepAlive; - } - - public void setKeepAlive(Boolean keepAlive) { - this.keepAlive = keepAlive; - } - - public Boolean getReuseAddress() { - return reuseAddress; - } - - public void setReuseAddress(Boolean reuseAddress) { - this.reuseAddress = reuseAddress; - } - - public Integer getSoLinger() { - return soLinger; - } - - public void setSoLinger(Integer soLinger) { - this.soLinger = soLinger; - } - - public Boolean getTcpNoDelay() { - return tcpNoDelay; - } - - public void setTcpNoDelay(Boolean tcpNoDelay) { - this.tcpNoDelay = tcpNoDelay; - } - - public Integer getReceiveBufferSize() { - return receiveBufferSize; - } - - public void setReceiveBufferSize(Integer receiveBufferSize) { - this.receiveBufferSize = receiveBufferSize; - } - - public Integer getSendBufferSize() { - return sendBufferSize; - } - - public void setSendBufferSize(Integer sendBufferSize) { - this.sendBufferSize = sendBufferSize; - } - -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraKeyspaceFactoryBean.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraKeyspaceFactoryBean.java deleted file mode 100644 index 06d7bb784..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraKeyspaceFactoryBean.java +++ /dev/null @@ -1,341 +0,0 @@ -/* - * Copyright 2011-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.core; - -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.BeanClassLoaderAware; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.FactoryBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.cassandra.support.CassandraExceptionTranslator; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.InvalidDataAccessApiUsageException; -import org.springframework.dao.support.PersistenceExceptionTranslator; -import org.springframework.data.cassandra.config.KeyspaceAttributes; -import org.springframework.data.cassandra.config.TableAttributes; -import org.springframework.data.cassandra.convert.CassandraConverter; -import org.springframework.data.cassandra.convert.MappingCassandraConverter; -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.util.CqlUtils; -import org.springframework.data.mapping.context.MappingContext; -import org.springframework.util.ClassUtils; -import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; - -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.KeyspaceMetadata; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.TableMetadata; -import com.datastax.driver.core.exceptions.NoHostAvailableException; - -/** - * Convenient factory for configuring a Cassandra Session. Session is a thread safe singleton and created per a - * keyspace. So, it is enough to have one session per application. - * - * @author Alex Shvid - */ - -public class CassandraKeyspaceFactoryBean implements FactoryBean, InitializingBean, DisposableBean, - BeanClassLoaderAware, PersistenceExceptionTranslator { - - private static final Logger log = LoggerFactory.getLogger(CassandraKeyspaceFactoryBean.class); - - public static final String DEFAULT_REPLICATION_STRATEGY = "SimpleStrategy"; - public static final int DEFAULT_REPLICATION_FACTOR = 1; - - private ClassLoader beanClassLoader; - - private Cluster cluster; - private Session session; - private String keyspace; - - private CassandraConverter converter; - private MappingContext, CassandraPersistentProperty> mappingContext; - - private SpringDataKeyspace keyspaceBean; - - private KeyspaceAttributes keyspaceAttributes; - - private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator(); - - public void setBeanClassLoader(ClassLoader classLoader) { - this.beanClassLoader = classLoader; - } - - public SpringDataKeyspace getObject() { - return keyspaceBean; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#getObjectType() - */ - public Class getObjectType() { - return Session.class; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#isSingleton() - */ - public boolean isSingleton() { - return true; - } - - /* - * (non-Javadoc) - * @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException) - */ - public DataAccessException translateExceptionIfPossible(RuntimeException ex) { - return exceptionTranslator.translateExceptionIfPossible(ex); - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - public void afterPropertiesSet() throws Exception { - - if (this.converter == null) { - this.converter = getDefaultCassandraConverter(); - } - this.mappingContext = this.converter.getMappingContext(); - - if (cluster == null) { - throw new IllegalArgumentException("at least one cluster is required"); - } - - Session session = null; - session = cluster.connect(); - - if (StringUtils.hasText(keyspace)) { - - KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace.toLowerCase()); - boolean keyspaceExists = keyspaceMetadata != null; - boolean keyspaceCreated = false; - - if (keyspaceExists) { - log.info("keyspace exists " + keyspaceMetadata.asCQLQuery()); - } - - if (keyspaceAttributes == null) { - keyspaceAttributes = new KeyspaceAttributes(); - } - - // drop the old keyspace if needed - if (keyspaceExists && (keyspaceAttributes.isCreate() || keyspaceAttributes.isCreateDrop())) { - log.info("Drop keyspace " + keyspace + " on afterPropertiesSet"); - session.execute("DROP KEYSPACE " + keyspace + ";"); - keyspaceExists = false; - } - - // create the new keyspace if needed - if (!keyspaceExists - && (keyspaceAttributes.isCreate() || keyspaceAttributes.isCreateDrop() || keyspaceAttributes.isUpdate())) { - - String query = String - .format( - "CREATE KEYSPACE %1$s WITH replication = { 'class' : '%2$s', 'replication_factor' : %3$d } AND DURABLE_WRITES = %4$b", - keyspace, keyspaceAttributes.getReplicationStrategy(), keyspaceAttributes.getReplicationFactor(), - keyspaceAttributes.isDurableWrites()); - - log.info("Create keyspace " + keyspace + " on afterPropertiesSet " + query); - - session.execute(query); - keyspaceCreated = true; - } - - // update keyspace if needed - if (keyspaceAttributes.isUpdate() && !keyspaceCreated) { - - if (compareKeyspaceAttributes(keyspaceAttributes, keyspaceMetadata) != null) { - - String query = String - .format( - "ALTER KEYSPACE %1$s WITH replication = { 'class' : '%2$s', 'replication_factor' : %3$d } AND DURABLE_WRITES = %4$b", - keyspace, keyspaceAttributes.getReplicationStrategy(), keyspaceAttributes.getReplicationFactor(), - keyspaceAttributes.isDurableWrites()); - - log.info("Update keyspace " + keyspace + " on afterPropertiesSet " + query); - session.execute(query); - } - - } - - // validate keyspace if needed - if (keyspaceAttributes.isValidate()) { - - if (!keyspaceExists) { - throw new InvalidDataAccessApiUsageException("keyspace '" + keyspace + "' not found in the Cassandra"); - } - - String errorField = compareKeyspaceAttributes(keyspaceAttributes, keyspaceMetadata); - if (errorField != null) { - throw new InvalidDataAccessApiUsageException(errorField + " attribute is not much in the keyspace '" - + keyspace + "'"); - } - - } - - session.execute("USE " + keyspace); - - if (!CollectionUtils.isEmpty(keyspaceAttributes.getTables())) { - - for (TableAttributes tableAttributes : keyspaceAttributes.getTables()) { - - String entityClassName = tableAttributes.getEntity(); - Class entityClass = ClassUtils.forName(entityClassName, this.beanClassLoader); - CassandraPersistentEntity entity = determineEntity(entityClass); - String useTableName = tableAttributes.getName() != null ? tableAttributes.getName() : entity.getTable(); - - if (keyspaceCreated) { - createNewTable(session, useTableName, entity); - } else if (keyspaceAttributes.isUpdate()) { - TableMetadata table = keyspaceMetadata.getTable(useTableName.toLowerCase()); - if (table == null) { - createNewTable(session, useTableName, entity); - } else { - // alter table columns - for (String cql : CqlUtils.alterTable(useTableName, entity, table)) { - log.info("Execute on keyspace " + keyspace + " CQL " + cql); - session.execute(cql); - } - } - } else if (keyspaceAttributes.isValidate()) { - TableMetadata table = keyspaceMetadata.getTable(useTableName.toLowerCase()); - if (table == null) { - throw new InvalidDataAccessApiUsageException("not found table " + useTableName + " for entity " - + entityClassName); - } - // validate columns - List alter = CqlUtils.alterTable(useTableName, entity, table); - if (!alter.isEmpty()) { - throw new InvalidDataAccessApiUsageException("invalid table " + useTableName + " for entity " - + entityClassName + ". modify it by " + alter); - } - } - - // System.out.println("tableAttributes, entityClass=" + entityClass + ", table = " + entity.getTable()); - - } - } - - } - - // initialize property - this.session = session; - - this.keyspaceBean = new SpringDataKeyspace(keyspace, session, converter); - } - - private void createNewTable(Session session, String useTableName, CassandraPersistentEntity entity) - throws NoHostAvailableException { - String cql = CqlUtils.createTable(useTableName, entity, converter); - log.info("Execute on keyspace " + keyspace + " CQL " + cql); - session.execute(cql); - for (String indexCQL : CqlUtils.createIndexes(useTableName, entity)) { - log.info("Execute on keyspace " + keyspace + " CQL " + indexCQL); - session.execute(indexCQL); - } - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - - if (StringUtils.hasText(keyspace) && keyspaceAttributes != null && keyspaceAttributes.isCreateDrop()) { - log.info("Drop keyspace " + keyspace + " on destroy"); - session.execute("USE system"); - session.execute("DROP KEYSPACE " + keyspace); - } - this.session.shutdown(); - } - - public void setKeyspace(String keyspace) { - this.keyspace = keyspace; - } - - public void setCluster(Cluster cluster) { - this.cluster = cluster; - } - - public void setKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes) { - this.keyspaceAttributes = keyspaceAttributes; - } - - public void setConverter(CassandraConverter converter) { - this.converter = converter; - } - - private static String compareKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes, - KeyspaceMetadata keyspaceMetadata) { - if (keyspaceAttributes.isDurableWrites() != keyspaceMetadata.isDurableWrites()) { - return "durableWrites"; - } - Map replication = keyspaceMetadata.getReplication(); - String replicationFactorStr = replication.get("replication_factor"); - if (replicationFactorStr == null) { - return "replication_factor"; - } - try { - int replicationFactor = Integer.parseInt(replicationFactorStr); - if (keyspaceAttributes.getReplicationFactor() != replicationFactor) { - return "replication_factor"; - } - } catch (NumberFormatException e) { - return "replication_factor"; - } - - String attributesStrategy = keyspaceAttributes.getReplicationStrategy(); - if (attributesStrategy.indexOf('.') == -1) { - attributesStrategy = "org.apache.cassandra.locator." + attributesStrategy; - } - String replicationStrategy = replication.get("class"); - if (!attributesStrategy.equals(replicationStrategy)) { - return "replication_class"; - } - return null; - } - - CassandraPersistentEntity determineEntity(Class entityClass) { - - if (entityClass == null) { - throw new InvalidDataAccessApiUsageException( - "No class parameter provided, entity table name can't be determined!"); - } - - CassandraPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); - if (entity == null) { - throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class " - + entityClass.getName()); - } - return entity; - } - - private static final CassandraConverter getDefaultCassandraConverter() { - MappingCassandraConverter converter = new MappingCassandraConverter(new CassandraMappingContext()); - converter.afterPropertiesSet(); - return converter; - } -} From 4045f52e6f925457738d2f3264e2aec302d553e0 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Tue, 10 Dec 2013 14:27:21 -0600 Subject: [PATCH 15/21] switched from *Template to *Operations in C*TemplateFactoryBean --- .../config/java/AbstractCassandraConfiguration.java | 12 ++++-------- .../config/xml/CassandraTemplateFactoryBean.java | 10 +++++----- .../java/AbstractKeyspaceCreatingConfiguration.java | 2 +- .../test/integration/config/java/Config.java | 2 +- .../config/java/KeyspaceCreatingConfig.java | 2 +- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java index 216166953..955256a70 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/java/AbstractCassandraConfiguration.java @@ -36,7 +36,7 @@ public abstract class AbstractCassandraConfiguration { /** * The name of the keyspace to connect to. If {@literal null} or empty, then the system keyspace will be used. */ - protected abstract String getKeyspace(); + protected abstract String getKeyspaceName(); /** * The {@link Cluster} instance to connect to. Must not be null. @@ -51,19 +51,15 @@ public abstract class AbstractCassandraConfiguration { */ @Bean public Session session() { - String keyspace = getKeyspace(); - if (StringUtils.hasText(keyspace)) { - return cluster().connect(keyspace); - } else { - return cluster().connect(); - } + String keyspaceName = getKeyspaceName(); + return StringUtils.hasText(keyspaceName) ? cluster().connect(keyspaceName) : cluster().connect(); } /** * A {@link CassandraTemplate} created from the {@link Session} returned by {@link #session()}. */ @Bean - public CassandraOperations cassandraTemplate() { + public CassandraOperations template() { return new CassandraTemplate(session()); } } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java index 414561b7e..cccaad77e 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java @@ -19,6 +19,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.cassandra.core.CassandraOperations; import org.springframework.cassandra.core.CassandraTemplate; import com.datastax.driver.core.Session; @@ -28,20 +29,19 @@ import com.datastax.driver.core.Session; * * @author Matthew T. Adams */ - -public class CassandraTemplateFactoryBean implements FactoryBean, InitializingBean { +public class CassandraTemplateFactoryBean implements FactoryBean, InitializingBean { private static final Logger log = LoggerFactory.getLogger(CassandraTemplateFactoryBean.class); private CassandraTemplate template; private Session session; - public CassandraTemplate getObject() { + public CassandraOperations getObject() { return template; } - public Class getObjectType() { - return CassandraTemplate.class; + public Class getObjectType() { + return CassandraOperations.class; } public boolean isSingleton() { diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java index f0ceefb33..fe7417356 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/AbstractKeyspaceCreatingConfiguration.java @@ -21,7 +21,7 @@ public abstract class AbstractKeyspaceCreatingConfiguration extends AbstractInte } protected void createKeyspaceIfNecessary() { - String keyspace = getKeyspace(); + String keyspace = getKeyspaceName(); if (!StringUtils.hasText(keyspace)) { return; } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/Config.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/Config.java index 8a1abf636..a6c26d5e2 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/Config.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/Config.java @@ -6,7 +6,7 @@ import org.springframework.context.annotation.Configuration; public class Config extends AbstractIntegrationTestConfiguration { @Override - protected String getKeyspace() { + protected String getKeyspaceName() { return null; } } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfig.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfig.java index 13d75dc69..39c4efcb3 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfig.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/config/java/KeyspaceCreatingConfig.java @@ -8,7 +8,7 @@ public class KeyspaceCreatingConfig extends AbstractKeyspaceCreatingConfiguratio public static final String KEYSPACE = "kcc"; @Override - protected String getKeyspace() { + protected String getKeyspaceName() { return KEYSPACE; } } From ddc9b1b7319049513a5d9fdb70366f3fdcda75f8 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Tue, 10 Dec 2013 16:10:15 -0600 Subject: [PATCH 16/21] wip: commented some tests in sdc* after xml schema changes --- ...tractSpringDataCassandraConfiguration.java | 5 +- .../data/cassandra/config/BeanNames.java | 31 -- .../config/CassandraClusterFactoryBean.java | 263 ----------- .../config/CassandraClusterParser.java | 118 ----- .../config/CassandraKeyspaceParser.java | 3 +- .../config/CassandraNamespaceHandler.java | 6 +- .../config/CassandraSessionParser.java | 69 --- .../cassandra/config/KeyspaceAttributes.java | 33 +- .../data/cassandra/util/CqlUtils.java | 2 +- .../cassandra/config/spring-cassandra-1.0.xsd | 418 +----------------- .../config/CassandraNamespaceTests.java | 12 +- .../test/integration/config/TestConfig.java | 27 +- .../UserRepositoryIntegrationTests.java | 18 +- .../CassandraNamespaceTests-context.xml | 17 +- ...UserRepositoryIntegrationTests-context.xml | 11 +- 15 files changed, 56 insertions(+), 977 deletions(-) delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/BeanNames.java delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterFactoryBean.java delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterParser.java delete mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraSessionParser.java diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java index 0d2fcd986..8c589d127 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java @@ -65,7 +65,7 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC */ @Bean public SpringDataKeyspace keyspace() throws Exception { - return new SpringDataKeyspace(getKeyspace(), session(), converter()); + return new SpringDataKeyspace(getKeyspaceName(), session(), converter()); } /** @@ -88,7 +88,7 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC * @throws Exception */ @Bean - public CassandraAdminOperations cassandraAdminTemplate() throws Exception { + public CassandraAdminOperations adminTemplate() throws Exception { return new CassandraAdminTemplate(keyspace()); } @@ -150,5 +150,4 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC public void setBeanClassLoader(ClassLoader classLoader) { this.beanClassLoader = classLoader; } - } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/BeanNames.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/BeanNames.java deleted file mode 100644 index 762b206fe..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/BeanNames.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2011 by the original author(s). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.config; - -/** - * @author Alex Shvid - * @author David Webb - */ -public final class BeanNames { - - private BeanNames() { - } - - public static final String CASSANDRA_CLUSTER = "cassandra-cluster"; - public static final String CASSANDRA_KEYSPACE = "cassandra-keyspace"; - public static final String CASSANDRA_SESSION = "cassandra-session"; - -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterFactoryBean.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterFactoryBean.java deleted file mode 100644 index 5221028cb..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterFactoryBean.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright 2011-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.core; - -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.FactoryBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.cassandra.support.CassandraExceptionTranslator; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.support.PersistenceExceptionTranslator; -import org.springframework.data.cassandra.config.CompressionType; -import org.springframework.data.cassandra.config.PoolingOptionsConfig; -import org.springframework.data.cassandra.config.SocketOptionsConfig; -import org.springframework.util.StringUtils; - -import com.datastax.driver.core.AuthProvider; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.HostDistance; -import com.datastax.driver.core.PoolingOptions; -import com.datastax.driver.core.ProtocolOptions.Compression; -import com.datastax.driver.core.SocketOptions; -import com.datastax.driver.core.policies.LoadBalancingPolicy; -import com.datastax.driver.core.policies.ReconnectionPolicy; -import com.datastax.driver.core.policies.RetryPolicy; - -/** - * Convenient factory for configuring a Cassandra Cluster. - * - * @author Alex Shvid - */ - -public class CassandraClusterFactoryBean implements FactoryBean, InitializingBean, DisposableBean, - PersistenceExceptionTranslator { - - private static final int DEFAULT_PORT = 9042; - - private Cluster cluster; - - private String contactPoints; - private int port = DEFAULT_PORT; - private CompressionType compressionType; - - private PoolingOptionsConfig localPoolingOptions; - private PoolingOptionsConfig remotePoolingOptions; - private SocketOptionsConfig socketOptions; - - private AuthProvider authProvider; - private LoadBalancingPolicy loadBalancingPolicy; - private ReconnectionPolicy reconnectionPolicy; - private RetryPolicy retryPolicy; - - private boolean metricsEnabled = true; - - private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator(); - - public Cluster getObject() throws Exception { - return cluster; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#getObjectType() - */ - public Class getObjectType() { - return Cluster.class; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#isSingleton() - */ - public boolean isSingleton() { - return true; - } - - /* - * (non-Javadoc) - * @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException) - */ - public DataAccessException translateExceptionIfPossible(RuntimeException ex) { - return exceptionTranslator.translateExceptionIfPossible(ex); - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - public void afterPropertiesSet() throws Exception { - - if (!StringUtils.hasText(contactPoints)) { - throw new IllegalArgumentException("at least one server is required"); - } - - Cluster.Builder builder = Cluster.builder(); - - builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(contactPoints)).withPort(port); - - if (compressionType != null) { - builder.withCompression(convertCompressionType(compressionType)); - } - - if (localPoolingOptions != null) { - builder.withPoolingOptions(configPoolingOptions(HostDistance.LOCAL, localPoolingOptions)); - } - - if (remotePoolingOptions != null) { - builder.withPoolingOptions(configPoolingOptions(HostDistance.REMOTE, remotePoolingOptions)); - } - - if (socketOptions != null) { - builder.withSocketOptions(configSocketOptions(socketOptions)); - } - - if (authProvider != null) { - builder.withAuthProvider(authProvider); - } - - if (loadBalancingPolicy != null) { - builder.withLoadBalancingPolicy(loadBalancingPolicy); - } - - if (reconnectionPolicy != null) { - builder.withReconnectionPolicy(reconnectionPolicy); - } - - if (retryPolicy != null) { - builder.withRetryPolicy(retryPolicy); - } - - if (!metricsEnabled) { - builder.withoutMetrics(); - } - - Cluster cluster = builder.build(); - - // initialize property - this.cluster = cluster; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - this.cluster.shutdown(); - } - - public void setContactPoints(String contactPoints) { - this.contactPoints = contactPoints; - } - - public void setPort(int port) { - this.port = port; - } - - public void setCompressionType(CompressionType compressionType) { - this.compressionType = compressionType; - } - - public void setLocalPoolingOptions(PoolingOptionsConfig localPoolingOptions) { - this.localPoolingOptions = localPoolingOptions; - } - - public void setRemotePoolingOptions(PoolingOptionsConfig remotePoolingOptions) { - this.remotePoolingOptions = remotePoolingOptions; - } - - public void setSocketOptions(SocketOptionsConfig socketOptions) { - this.socketOptions = socketOptions; - } - - public void setAuthProvider(AuthProvider authProvider) { - this.authProvider = authProvider; - } - - public void setLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) { - this.loadBalancingPolicy = loadBalancingPolicy; - } - - public void setReconnectionPolicy(ReconnectionPolicy reconnectionPolicy) { - this.reconnectionPolicy = reconnectionPolicy; - } - - public void setRetryPolicy(RetryPolicy retryPolicy) { - this.retryPolicy = retryPolicy; - } - - public void setMetricsEnabled(boolean metricsEnabled) { - this.metricsEnabled = metricsEnabled; - } - - private static Compression convertCompressionType(CompressionType type) { - switch (type) { - case NONE: - return Compression.NONE; - case SNAPPY: - return Compression.SNAPPY; - } - throw new IllegalArgumentException("unknown compression type " + type); - } - - private static PoolingOptions configPoolingOptions(HostDistance hostDistance, PoolingOptionsConfig config) { - PoolingOptions poolingOptions = new PoolingOptions(); - - if (config.getMinSimultaneousRequests() != null) { - poolingOptions - .setMinSimultaneousRequestsPerConnectionThreshold(hostDistance, config.getMinSimultaneousRequests()); - } - if (config.getMaxSimultaneousRequests() != null) { - poolingOptions - .setMaxSimultaneousRequestsPerConnectionThreshold(hostDistance, config.getMaxSimultaneousRequests()); - } - if (config.getCoreConnections() != null) { - poolingOptions.setCoreConnectionsPerHost(hostDistance, config.getCoreConnections()); - } - if (config.getMaxConnections() != null) { - poolingOptions.setMaxConnectionsPerHost(hostDistance, config.getMaxConnections()); - } - - return poolingOptions; - } - - private static SocketOptions configSocketOptions(SocketOptionsConfig config) { - SocketOptions socketOptions = new SocketOptions(); - - if (config.getConnectTimeoutMls() != null) { - socketOptions.setConnectTimeoutMillis(config.getConnectTimeoutMls()); - } - if (config.getKeepAlive() != null) { - socketOptions.setKeepAlive(config.getKeepAlive()); - } - if (config.getReuseAddress() != null) { - socketOptions.setReuseAddress(config.getReuseAddress()); - } - if (config.getSoLinger() != null) { - socketOptions.setSoLinger(config.getSoLinger()); - } - if (config.getTcpNoDelay() != null) { - socketOptions.setTcpNoDelay(config.getTcpNoDelay()); - } - if (config.getReceiveBufferSize() != null) { - socketOptions.setReceiveBufferSize(config.getReceiveBufferSize()); - } - if (config.getSendBufferSize() != null) { - socketOptions.setSendBufferSize(config.getSendBufferSize()); - } - - return socketOptions; - } -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterParser.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterParser.java deleted file mode 100644 index 1718a283f..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraClusterParser.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2011-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.config; - -import java.util.List; - -import org.springframework.beans.factory.BeanDefinitionStoreException; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.AbstractBeanDefinition; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.data.cassandra.core.CassandraClusterFactoryBean; -import org.springframework.data.config.ParsingUtils; -import org.springframework.util.StringUtils; -import org.springframework.util.xml.DomUtils; -import org.w3c.dom.Element; - -/** - * Parser for <cluster;gt; definitions. - * - * @author Alex Shvid - */ - -public class CassandraClusterParser extends AbstractSimpleBeanDefinitionParser { - - @Override - protected Class getBeanClass(Element element) { - return CassandraClusterFactoryBean.class; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext) - */ - @Override - protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) - throws BeanDefinitionStoreException { - - String id = super.resolveId(element, definition, parserContext); - return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_CLUSTER; - } - - @Override - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - - String contactPoints = element.getAttribute("contactPoints"); - if (StringUtils.hasText(contactPoints)) { - builder.addPropertyValue("contactPoints", contactPoints); - } - - String port = element.getAttribute("port"); - if (StringUtils.hasText(port)) { - builder.addPropertyValue("port", port); - } - - String compression = element.getAttribute("compression"); - if (StringUtils.hasText(compression)) { - builder.addPropertyValue("compressionType", CompressionType.valueOf(compression)); - } - - postProcess(builder, element); - } - - @Override - protected void postProcess(BeanDefinitionBuilder builder, Element element) { - List subElements = DomUtils.getChildElements(element); - - // parse nested elements - for (Element subElement : subElements) { - String name = subElement.getLocalName(); - - if ("local-pooling-options".equals(name)) { - builder.addPropertyValue("localPoolingOptions", parsePoolingOptions(subElement)); - } else if ("remote-pooling-options".equals(name)) { - builder.addPropertyValue("remotePoolingOptions", parsePoolingOptions(subElement)); - } else if ("socket-options".equals(name)) { - builder.addPropertyValue("socketOptions", parseSocketOptions(subElement)); - } - } - - } - - private BeanDefinition parsePoolingOptions(Element element) { - BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(PoolingOptionsConfig.class); - ParsingUtils.setPropertyValue(defBuilder, element, "min-simultaneous-requests", "minSimultaneousRequests"); - ParsingUtils.setPropertyValue(defBuilder, element, "max-simultaneous-requests", "maxSimultaneousRequests"); - ParsingUtils.setPropertyValue(defBuilder, element, "core-connections", "coreConnections"); - ParsingUtils.setPropertyValue(defBuilder, element, "max-connections", "maxConnections"); - return defBuilder.getBeanDefinition(); - } - - private BeanDefinition parseSocketOptions(Element element) { - BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(SocketOptionsConfig.class); - ParsingUtils.setPropertyValue(defBuilder, element, "connect-timeout-mls", "connectTimeoutMls"); - ParsingUtils.setPropertyValue(defBuilder, element, "keep-alive", "keepAlive"); - ParsingUtils.setPropertyValue(defBuilder, element, "reuse-address", "reuseAddress"); - ParsingUtils.setPropertyValue(defBuilder, element, "so-linger", "soLinger"); - ParsingUtils.setPropertyValue(defBuilder, element, "tcp-no-delay", "tcpNoDelay"); - ParsingUtils.setPropertyValue(defBuilder, element, "receive-buffer-size", "receiveBufferSize"); - ParsingUtils.setPropertyValue(defBuilder, element, "send-buffer-size", "sendBufferSize"); - return defBuilder.getBeanDefinition(); - } - -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceParser.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceParser.java index a9a724452..3b3c99e09 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceParser.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceParser.java @@ -24,7 +24,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.ManagedList; import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.data.cassandra.core.CassandraKeyspaceFactoryBean; +import org.springframework.cassandra.config.KeyspaceAttributes; +import org.springframework.cassandra.config.xml.BeanNames; import org.springframework.data.config.ParsingUtils; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java index c69c39cf6..ace033ece 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java @@ -18,7 +18,7 @@ package org.springframework.data.cassandra.config; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; /** - * Namespace handler for <cassandra;gt;. + * Namespace handler for <cassandra>. * * @author Alex Shvid */ @@ -27,10 +27,6 @@ public class CassandraNamespaceHandler extends NamespaceHandlerSupport { public void init() { - registerBeanDefinitionParser("cluster", new CassandraClusterParser()); registerBeanDefinitionParser("keyspace", new CassandraKeyspaceParser()); - registerBeanDefinitionParser("session", new CassandraSessionParser()); - } - } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraSessionParser.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraSessionParser.java deleted file mode 100644 index f99cfb5bf..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraSessionParser.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2011-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.cassandra.config; - -import org.springframework.beans.factory.BeanDefinitionStoreException; -import org.springframework.beans.factory.support.AbstractBeanDefinition; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.cassandra.core.SessionFactoryBean; -import org.springframework.util.StringUtils; -import org.w3c.dom.Element; - -/** - * Parser for <session;gt; definitions. - * - * @author David Webb - */ - -public class CassandraSessionParser extends AbstractSimpleBeanDefinitionParser { - - @Override - protected Class getBeanClass(Element element) { - return SessionFactoryBean.class; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext) - */ - @Override - protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) - throws BeanDefinitionStoreException { - - String id = super.resolveId(element, definition, parserContext); - return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_SESSION; - } - - @Override - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - - String keyspaceRef = element.getAttribute("cassandra-keyspace-ref"); - if (!StringUtils.hasText(keyspaceRef)) { - keyspaceRef = BeanNames.CASSANDRA_KEYSPACE; - } - builder.addPropertyReference("keyspace", keyspaceRef); - - postProcess(builder, element); - } - - @Override - protected void postProcess(BeanDefinitionBuilder builder, Element element) { - - } - -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/KeyspaceAttributes.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/KeyspaceAttributes.java index 748b94726..0d4d3d1df 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/KeyspaceAttributes.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/KeyspaceAttributes.java @@ -23,11 +23,7 @@ import java.util.Collection; * * @author Alex Shvid */ -public class KeyspaceAttributes { - - public static final String DEFAULT_REPLICATION_STRATEGY = "SimpleStrategy"; - public static final int DEFAULT_REPLICATION_FACTOR = 1; - public static final boolean DEFAULT_DURABLE_WRITES = true; +public class KeyspaceAttributes extends org.springframework.cassandra.config.KeyspaceAttributes { /* * auto possible values: @@ -42,9 +38,6 @@ public class KeyspaceAttributes { public static final String AUTO_CREATE_DROP = "create-drop"; private String auto = AUTO_VALIDATE; - private String replicationStrategy = DEFAULT_REPLICATION_STRATEGY; - private int replicationFactor = DEFAULT_REPLICATION_FACTOR; - private boolean durableWrites = DEFAULT_DURABLE_WRITES; private Collection tables; @@ -72,30 +65,6 @@ public class KeyspaceAttributes { return AUTO_CREATE_DROP.equals(auto); } - public String getReplicationStrategy() { - return replicationStrategy; - } - - public void setReplicationStrategy(String replicationStrategy) { - this.replicationStrategy = replicationStrategy; - } - - public int getReplicationFactor() { - return replicationFactor; - } - - public void setReplicationFactor(int replicationFactor) { - this.replicationFactor = replicationFactor; - } - - public boolean isDurableWrites() { - return durableWrites; - } - - public void setDurableWrites(boolean durableWrites) { - this.durableWrites = durableWrites; - } - public Collection getTables() { return tables; } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java index 550a6d754..6d27204a7 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/util/CqlUtils.java @@ -355,7 +355,7 @@ public abstract class CqlUtils { /** * Create a Batch Query object for multiple deletes. * - * @param keyspace + * @param keyspaceName * @param tableName * @param entities * @param entity 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 72094d834..44daecc74 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 @@ -1,244 +1,37 @@ + + - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - The name of the Cassandra Cluster definition (by - default "cassandra-cluster") - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - The name of the Keyspace definition (by default - "cassandra-keyspace") - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + + + @@ -251,159 +44,6 @@ The reference to a CassandraConverter instance. Default is null. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -421,34 +61,4 @@ Table name override. - - - - - The name of the Session definition (by default - "cassandra-session") - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java index 5117d0e3f..2e2bdc0d7 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java @@ -8,19 +8,15 @@ import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.cassandra.core.SpringDataKeyspace; import org.springframework.context.ApplicationContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.core.SpringDataKeyspace; import org.springframework.util.Assert; import com.datastax.driver.core.Cluster; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration +// @RunWith(SpringJUnit4ClassRunner.class) +// @ContextConfiguration public class CassandraNamespaceTests { @Autowired @@ -32,7 +28,7 @@ public class CassandraNamespaceTests { EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml"); } - @Test + // @Test public void testSingleton() throws Exception { Object cluster = ctx.getBean("cassandra-cluster"); Assert.notNull(cluster); diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java index 7a55f61ff..61bde9db6 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java @@ -1,14 +1,14 @@ package org.springframework.data.cassandra.test.integration.config; +import org.springframework.cassandra.config.xml.CassandraSessionFactoryBean; import org.springframework.cassandra.core.CassandraOperations; import org.springframework.cassandra.core.CassandraTemplate; -import org.springframework.cassandra.core.SessionFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.config.AbstractSpringDataCassandraConfiguration; +import org.springframework.data.cassandra.config.CassandraKeyspaceFactoryBean; import org.springframework.data.cassandra.core.CassandraDataOperations; import org.springframework.data.cassandra.core.CassandraDataTemplate; -import org.springframework.data.cassandra.core.CassandraKeyspaceFactoryBean; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster.Builder; @@ -17,30 +17,24 @@ import com.datastax.driver.core.Cluster.Builder; * Setup any spring configuration for unit tests * * @author David Webb - * + * @author Matthew T. Adams */ @Configuration public class TestConfig extends AbstractSpringDataCassandraConfiguration { - public static final String keyspace = "test"; + public static final String keyspaceName = "test"; - /* (non-Javadoc) - * @see org.springframework.data.cassandra.config.AbstractCassandraConfiguration#getKeyspaceName() - */ @Override - protected String getKeyspace() { - return keyspace; + protected String getKeyspaceName() { + return keyspaceName; } - /* (non-Javadoc) - * @see org.springframework.data.cassandra.config.AbstractCassandraConfiguration#cluster() - */ @Override @Bean public Cluster cluster() { Builder builder = Cluster.builder(); - builder.addContactPoint("127.0.0.1"); + builder.addContactPoint("127.0.0.1").withPort(9042); return builder.build(); } @@ -52,15 +46,14 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration { bean.setKeyspace("test"); return bean; - } @Bean - public SessionFactoryBean sessionFactoryBean() { + public CassandraSessionFactoryBean sessionFactoryBean() { - SessionFactoryBean bean = new SessionFactoryBean(keyspaceFactoryBean().getObject()); + CassandraSessionFactoryBean bean = new CassandraSessionFactoryBean(); + bean.setCluster(cluster()); return bean; - } @Bean diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests.java index fc33c7a07..18005b797 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests.java @@ -32,13 +32,9 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.cassandra.core.CassandraDataOperations; import org.springframework.data.cassandra.test.integration.table.User; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.google.common.collect.Lists; @@ -48,8 +44,8 @@ import com.google.common.collect.Lists; * @author Alex Shvid * */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +// @ContextConfiguration +// @RunWith(SpringJUnit4ClassRunner.class) public class UserRepositoryIntegrationTests { @Autowired @@ -104,7 +100,7 @@ public class UserRepositoryIntegrationTests { all = dataOperations.insert(Arrays.asList(tom, bob, alice, scott)); } - @Test + // @Test public void findsUserById() throws Exception { User user = repository.findOne(bob.getUsername()); @@ -113,7 +109,7 @@ public class UserRepositoryIntegrationTests { } - @Test + // @Test public void findsAll() throws Exception { List result = Lists.newArrayList(repository.findAll()); assertThat(result.size(), is(all.size())); @@ -121,7 +117,7 @@ public class UserRepositoryIntegrationTests { } - @Test + // @Test public void findsAllWithGivenIds() { Iterable result = repository.findAll(Arrays.asList(bob.getUsername(), tom.getUsername())); @@ -129,7 +125,7 @@ public class UserRepositoryIntegrationTests { assertThat(result, not(hasItems(alice, scott))); } - @Test + // @Test public void deletesUserCorrectly() throws Exception { repository.delete(tom); @@ -140,7 +136,7 @@ public class UserRepositoryIntegrationTests { assertThat(result, not(hasItem(tom))); } - @Test + // @Test public void deletesUserByIdCorrectly() { repository.delete(tom.getUsername().toString()); diff --git a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml index 4050ec523..56723073e 100644 --- a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml +++ b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests-context.xml @@ -1,6 +1,7 @@ - - - @@ -34,7 +35,7 @@ - @@ -43,13 +44,11 @@ - + - + - - - + diff --git a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests-context.xml b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests-context.xml index 27a117f07..3601a030b 100644 --- a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests-context.xml +++ b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryIntegrationTests-context.xml @@ -1,6 +1,7 @@ - - - @@ -36,7 +37,7 @@ - @@ -45,7 +46,7 @@ - + From 5bb8a1c5069cf78a6605f9db345f09ff23b4090f Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Tue, 10 Dec 2013 16:25:38 -0600 Subject: [PATCH 17/21] removed unused xml -related code --- .../config/xml/CassandraKeyspaceParser.java | 130 ------------------ .../config/xml/CassandraNamespaceHandler.java | 1 - .../cassandra/config/spring-cassandra-1.0.xsd | 63 --------- 3 files changed, 194 deletions(-) delete mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java deleted file mode 100644 index d98be4d2f..000000000 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraKeyspaceParser.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright 2011-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.cassandra.config.xml; - -import java.util.List; - -import org.springframework.beans.factory.BeanDefinitionStoreException; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.AbstractBeanDefinition; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.ManagedList; -import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.cassandra.config.KeyspaceAttributes; -import org.springframework.cassandra.config.TableAttributes; -import org.springframework.util.StringUtils; -import org.springframework.util.xml.DomUtils; -import org.w3c.dom.Element; - -/** - * Parser for <keyspace> definitions. - * - * @author Alex Shvid - * @author Matthew T. Adams - */ - -public class CassandraKeyspaceParser extends AbstractSimpleBeanDefinitionParser { - - @Override - protected Class getBeanClass(Element element) { - return CassandraSessionFactoryBean.class; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext) - */ - @Override - protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) - throws BeanDefinitionStoreException { - - String id = super.resolveId(element, definition, parserContext); - return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_KEYSPACE; - } - - @Override - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - - String name = element.getAttribute("name"); - if (StringUtils.hasText(name)) { - builder.addPropertyValue("keyspace", name); - } - - String clusterRef = element.getAttribute("cassandra-cluster-ref"); - if (!StringUtils.hasText(clusterRef)) { - clusterRef = BeanNames.CASSANDRA_CLUSTER; - } - builder.addPropertyReference("cluster", clusterRef); - - String converterRef = element.getAttribute("cassandra-converter-ref"); - if (StringUtils.hasText(converterRef)) { - builder.addPropertyReference("converter", converterRef); - } - - postProcess(builder, element); - } - - @Override - protected void postProcess(BeanDefinitionBuilder builder, Element element) { - List subElements = DomUtils.getChildElements(element); - - // parse nested elements - for (Element subElement : subElements) { - String name = subElement.getLocalName(); - - if ("keyspace-attributes".equals(name)) { - builder.addPropertyValue("keyspaceAttributes", parseKeyspaceAttributes(subElement)); - } - } - - } - - private BeanDefinition parseKeyspaceAttributes(Element element) { - BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(KeyspaceAttributes.class); - - ParsingUtils.setPropertyValue(defBuilder, element, "auto", "auto"); - ParsingUtils.setPropertyValue(defBuilder, element, "replication-strategy", "replicationStrategy"); - ParsingUtils.setPropertyValue(defBuilder, element, "replication-factor", "replicationFactor"); - ParsingUtils.setPropertyValue(defBuilder, element, "durable-writes", "durableWrites"); - - List subElements = DomUtils.getChildElements(element); - ManagedList tables = new ManagedList(subElements.size()); - - // parse nested elements - for (Element subElement : subElements) { - String name = subElement.getLocalName(); - - if ("table".equals(name)) { - tables.add(parseTable(subElement)); - } - } - if (!tables.isEmpty()) { - defBuilder.addPropertyValue("tables", tables); - } - - return defBuilder.getBeanDefinition(); - } - - private BeanDefinition parseTable(Element element) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(TableAttributes.class); - - ParsingUtils.setPropertyValue(builder, element, "entity", "entity"); - ParsingUtils.setPropertyValue(builder, element, "name", "name"); - - return builder.getBeanDefinition(); - } -} diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java index 4563c3081..f09b3071c 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraNamespaceHandler.java @@ -29,7 +29,6 @@ public class CassandraNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("cluster", new CassandraClusterParser()); - // registerBeanDefinitionParser("keyspace", new CassandraKeyspaceParser()); registerBeanDefinitionParser("session", new CassandraSessionParser()); registerBeanDefinitionParser("template", new CassandraTemplateParser()); } diff --git a/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd b/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd index 7f4c437e4..03cd992b8 100644 --- a/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd +++ b/spring-cassandra/src/main/resources/org/springframework/cassandra/config/spring-cassandra-1.0.xsd @@ -57,20 +57,6 @@ Defines a Cassandra Cluster. - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -331,33 +295,6 @@ Sets the SO_SNDBUF socket option. - - - - - - - - - - - - - - - - - - From ccd47543b580b0bc2d02839e15350459d0ae28a8 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Thu, 12 Dec 2013 09:31:28 -0600 Subject: [PATCH 18/21] moved factorybean classes from config.xml to config package --- .../config/{xml => }/CassandraClusterFactoryBean.java | 5 +---- .../config/{xml => }/CassandraSessionFactoryBean.java | 2 +- .../config/{xml => }/CassandraTemplateFactoryBean.java | 2 +- .../cassandra/config/xml/CassandraClusterParser.java | 1 + .../cassandra/config/xml/CassandraSessionParser.java | 1 + .../cassandra/config/xml/CassandraTemplateParser.java | 1 + .../data/cassandra/test/integration/config/TestConfig.java | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) rename spring-cassandra/src/main/java/org/springframework/cassandra/config/{xml => }/CassandraClusterFactoryBean.java (97%) rename spring-cassandra/src/main/java/org/springframework/cassandra/config/{xml => }/CassandraSessionFactoryBean.java (98%) rename spring-cassandra/src/main/java/org/springframework/cassandra/config/{xml => }/CassandraTemplateFactoryBean.java (97%) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraClusterFactoryBean.java similarity index 97% rename from spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterFactoryBean.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraClusterFactoryBean.java index f6a27af01..ea21b424c 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterFactoryBean.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraClusterFactoryBean.java @@ -13,14 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.cassandra.config.xml; +package org.springframework.cassandra.config; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; -import org.springframework.cassandra.config.CompressionType; -import org.springframework.cassandra.config.PoolingOptionsConfig; -import org.springframework.cassandra.config.SocketOptionsConfig; import org.springframework.cassandra.support.CassandraExceptionTranslator; import org.springframework.dao.DataAccessException; import org.springframework.dao.support.PersistenceExceptionTranslator; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraSessionFactoryBean.java similarity index 98% rename from spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraSessionFactoryBean.java index 01396dee3..2a5168db7 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionFactoryBean.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraSessionFactoryBean.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.cassandra.config.xml; +package org.springframework.cassandra.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraTemplateFactoryBean.java similarity index 97% rename from spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraTemplateFactoryBean.java index cccaad77e..660ac9c85 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateFactoryBean.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/CassandraTemplateFactoryBean.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.cassandra.config.xml; +package org.springframework.cassandra.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java index 4158dedca..e454049c0 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraClusterParser.java @@ -23,6 +23,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.cassandra.config.CassandraClusterFactoryBean; import org.springframework.cassandra.config.CompressionType; import org.springframework.cassandra.config.PoolingOptionsConfig; import org.springframework.cassandra.config.SocketOptionsConfig; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java index 94ee88a22..7b8c05d92 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraSessionParser.java @@ -20,6 +20,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.cassandra.config.CassandraSessionFactoryBean; import org.springframework.util.StringUtils; import org.w3c.dom.Element; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java index 2fa5d35ad..c796ec9b2 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/xml/CassandraTemplateParser.java @@ -20,6 +20,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.cassandra.config.CassandraTemplateFactoryBean; import org.springframework.util.StringUtils; import org.w3c.dom.Element; diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java index 61bde9db6..79cf57d42 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java @@ -1,6 +1,6 @@ package org.springframework.data.cassandra.test.integration.config; -import org.springframework.cassandra.config.xml.CassandraSessionFactoryBean; +import org.springframework.cassandra.config.CassandraSessionFactoryBean; import org.springframework.cassandra.core.CassandraOperations; import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.context.annotation.Bean; From 3c04ddf00848254e411bc866711618e31f541a5d Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Thu, 12 Dec 2013 09:45:26 -0600 Subject: [PATCH 19/21] moved Keyspace class from s-c to s-d-c in prep for complete removal --- .../java/org/springframework/data}/cassandra/core/Keyspace.java | 2 +- .../springframework/data/cassandra/core/SpringDataKeyspace.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) rename {spring-cassandra/src/main/java/org/springframework => spring-data-cassandra/src/main/java/org/springframework/data}/cassandra/core/Keyspace.java (96%) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/Keyspace.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/Keyspace.java similarity index 96% rename from spring-cassandra/src/main/java/org/springframework/cassandra/core/Keyspace.java rename to spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/Keyspace.java index 6149ce3f5..7135a475b 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/Keyspace.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/Keyspace.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.cassandra.core; +package org.springframework.data.cassandra.core; import com.datastax.driver.core.Session; diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/SpringDataKeyspace.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/SpringDataKeyspace.java index 065d7f7f8..9e1aaa0e3 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/SpringDataKeyspace.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/SpringDataKeyspace.java @@ -1,6 +1,5 @@ package org.springframework.data.cassandra.core; -import org.springframework.cassandra.core.Keyspace; import org.springframework.data.cassandra.convert.CassandraConverter; import org.springframework.util.Assert; From 4a1b5fa543787cfea043ae72ff60af673b1c7558 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Thu, 12 Dec 2013 11:18:06 -0600 Subject: [PATCH 20/21] renamed partition() -> partitioned(), primary() -> clustered() as appropriate --- .../generator/CreateTableCqlGenerator.java | 10 +++---- .../core/keyspace/ColumnSpecification.java | 26 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java index 655a5f559..c13d456ab 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java @@ -65,14 +65,14 @@ public class CreateTableCqlGenerator extends TableCqlGenerator partitionKeys = new ArrayList(); - List clusteredKeys = new ArrayList(); + List clusterKeys = new ArrayList(); for (ColumnSpecification col : spec().getColumns()) { col.toCql(cql).append(", "); if (col.getKeyType() == PARTITIONED) { partitionKeys.add(col); } else if (col.getKeyType() == CLUSTERED) { - clusteredKeys.add(col); + clusterKeys.add(col); } } @@ -91,11 +91,11 @@ public class CreateTableCqlGenerator extends TableCqlGenerator options = spec().getOptions(); diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java index db4d78c16..d232a612a 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java @@ -31,9 +31,9 @@ import com.datastax.driver.core.DataType; * Builder class to help construct CQL statements that involve column manipulation. Not threadsafe. *

* Use {@link #name(String)} and {@link #type(String)} to set the name and type of the column, respectively. To specify - * a PRIMARY KEY column, use {@link #primary()} or {@link #primary(Ordering)}. To specify that the - * PRIMARY KEY column is or is part of the partition key, use {@link #partition()} instead of - * {@link #primary()} or {@link #primary(Ordering)}. + * a PRIMARY KEY column, use {@link #clustered()} or {@link #clustered(Ordering)}. To specify that the + * PRIMARY KEY column is or is part of the partition key, use {@link #partitioned()} instead of + * {@link #clustered()} or {@link #clustered(Ordering)}. * * @author Matthew T. Adams * @author Alex Shvid @@ -77,8 +77,8 @@ public class ColumnSpecification { * * @return this */ - public ColumnSpecification partition() { - return partition(true); + public ColumnSpecification partitioned() { + return partitioned(true); } /** @@ -88,20 +88,20 @@ public class ColumnSpecification { * * @return this */ - public ColumnSpecification partition(boolean partition) { - this.keyType = partition ? PARTITIONED : null; + public ColumnSpecification partitioned(boolean partitioned) { + this.keyType = partitioned ? PARTITIONED : null; this.ordering = null; return this; } /** - * Identifies this column as a primary key column with default ordering. Sets the column's {@link #keyType} to + * Identifies this column as a clustered key column with default ordering. Sets the column's {@link #keyType} to * {@link PrimaryKeyType#CLUSTERED} and its {@link #ordering} to {@link #DEFAULT_ORDERING}. * * @return this */ - public ColumnSpecification primary() { - return primary(DEFAULT_ORDERING); + public ColumnSpecification clustered() { + return clustered(DEFAULT_ORDERING); } /** @@ -110,8 +110,8 @@ public class ColumnSpecification { * * @return this */ - public ColumnSpecification primary(Ordering order) { - return primary(order, true); + public ColumnSpecification clustered(Ordering order) { + return clustered(order, true); } /** @@ -121,7 +121,7 @@ public class ColumnSpecification { * * @return this */ - public ColumnSpecification primary(Ordering order, boolean primary) { + public ColumnSpecification clustered(Ordering order, boolean primary) { this.keyType = primary ? CLUSTERED : null; this.ordering = primary ? order : null; return this; From 9811cadb568c8f4e67b7d86cfd191eceefe85158 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Thu, 12 Dec 2013 11:30:25 -0600 Subject: [PATCH 21/21] updated/added javadocs --- .../core/keyspace/ColumnChangeSpecification.java | 5 +++++ .../cassandra/core/keyspace/ColumnSpecification.java | 12 ++++++------ .../core/keyspace/ColumnTypeChangeSpecification.java | 5 +++++ .../core/keyspace/DropColumnSpecification.java | 5 +++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnChangeSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnChangeSpecification.java index 2c9eebc8c..6cea473f8 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnChangeSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnChangeSpecification.java @@ -18,6 +18,11 @@ package org.springframework.cassandra.core.keyspace; import static org.springframework.cassandra.core.cql.CqlStringUtils.checkIdentifier; import static org.springframework.cassandra.core.cql.CqlStringUtils.identifize; +/** + * Base class for column change specifications. + * + * @author Matthew T. Adams + */ public abstract class ColumnChangeSpecification { private String name; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java index d232a612a..9c2d0ba03 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnSpecification.java @@ -28,11 +28,11 @@ import org.springframework.cassandra.core.Ordering; import com.datastax.driver.core.DataType; /** - * Builder class to help construct CQL statements that involve column manipulation. Not threadsafe. + * Builder class to specify columns. *

* Use {@link #name(String)} and {@link #type(String)} to set the name and type of the column, respectively. To specify - * a PRIMARY KEY column, use {@link #clustered()} or {@link #clustered(Ordering)}. To specify that the - * PRIMARY KEY column is or is part of the partition key, use {@link #partitioned()} instead of + * a clustered PRIMARY KEY column, use {@link #clustered()} or {@link #clustered(Ordering)}. To specify + * that the PRIMARY KEY column is or is part of the partition key, use {@link #partitioned()} instead of * {@link #clustered()} or {@link #clustered(Ordering)}. * * @author Matthew T. Adams @@ -105,7 +105,7 @@ public class ColumnSpecification { } /** - * Identifies this column as a primary key column with the given ordering. Sets the column's {@link #keyType} to + * Identifies this column as a clustered key column with the given ordering. Sets the column's {@link #keyType} to * {@link PrimaryKeyType#CLUSTERED} and its {@link #ordering} to the given {@link Ordering}. * * @return this @@ -115,8 +115,8 @@ public class ColumnSpecification { } /** - * Toggles the identification of this column as a primary key column. If the given boolean is true, then - * sets the column's {@link #keyType} to {@link PrimaryKeyType#PARTITIONED} and {@link #ordering} to the given + * Toggles the identification of this column as a clustered key column. If the given boolean is true, + * then sets the column's {@link #keyType} to {@link PrimaryKeyType#PARTITIONED} and {@link #ordering} to the given * {@link Ordering} , else sets both {@link #keyType} and {@link #ordering} to null. * * @return this diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnTypeChangeSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnTypeChangeSpecification.java index c978623f2..814cfcd7a 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnTypeChangeSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/ColumnTypeChangeSpecification.java @@ -19,6 +19,11 @@ import org.springframework.util.Assert; import com.datastax.driver.core.DataType; +/** + * Base class for column changes that include {@link DataType} information. + * + * @author Matthew T. Adams + */ public abstract class ColumnTypeChangeSpecification extends ColumnChangeSpecification { private DataType type; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropColumnSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropColumnSpecification.java index 19adf19b1..62828fd18 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropColumnSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropColumnSpecification.java @@ -15,6 +15,11 @@ */ package org.springframework.cassandra.core.keyspace; +/** + * A specification to drop a column. + * + * @author Matthew T. Adams + */ public class DropColumnSpecification extends ColumnChangeSpecification { public DropColumnSpecification(String name) {