From 7729de943629723f51e645d256b533d31cb77970 Mon Sep 17 00:00:00 2001 From: David Webb Date: Mon, 2 Dec 2013 09:43:23 -0500 Subject: [PATCH 1/3] DACASS-48 : WIP : Refactored QueryOptions and supporting classes to spring-cassandra. --- .../cassandra/core/ConsistencyLevel.java | 2 +- .../cassandra/core/ConsistencyLevelResolver.java | 2 +- .../springframework}/cassandra/core/QueryOptions.java | 2 +- .../springframework}/cassandra/core/RetryPolicy.java | 2 +- .../cassandra/core/RetryPolicyResolver.java | 2 +- .../data/cassandra/core/CassandraDataOperations.java | 1 + .../data/cassandra/core/CassandraDataTemplate.java | 1 + .../springframework/data/cassandra/util/CqlUtils.java | 10 +++++----- .../template/CassandraDataOperationsTest.java | 6 +++--- 9 files changed, 15 insertions(+), 13 deletions(-) rename {spring-data-cassandra/src/main/java/org/springframework/data => spring-cassandra/src/main/java/org/springframework}/cassandra/core/ConsistencyLevel.java (94%) rename {spring-data-cassandra/src/main/java/org/springframework/data => spring-cassandra/src/main/java/org/springframework}/cassandra/core/ConsistencyLevelResolver.java (97%) rename {spring-data-cassandra/src/main/java/org/springframework/data => spring-cassandra/src/main/java/org/springframework}/cassandra/core/QueryOptions.java (98%) rename {spring-data-cassandra/src/main/java/org/springframework/data => spring-cassandra/src/main/java/org/springframework}/cassandra/core/RetryPolicy.java (94%) rename {spring-data-cassandra/src/main/java/org/springframework/data => spring-cassandra/src/main/java/org/springframework}/cassandra/core/RetryPolicyResolver.java (97%) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ConsistencyLevel.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/ConsistencyLevel.java similarity index 94% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ConsistencyLevel.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/core/ConsistencyLevel.java index e8b1247f2..018f22eed 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ConsistencyLevel.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/ConsistencyLevel.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.cassandra.core; +package org.springframework.cassandra.core; /** * Generic Consistency Levels associated with Cassandra. diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ConsistencyLevelResolver.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/ConsistencyLevelResolver.java similarity index 97% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ConsistencyLevelResolver.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/core/ConsistencyLevelResolver.java index cb02b869c..f20909412 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ConsistencyLevelResolver.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/ConsistencyLevelResolver.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.cassandra.core; +package org.springframework.cassandra.core; /** * Determine driver consistency level based on ConsistencyLevel diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/QueryOptions.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/QueryOptions.java similarity index 98% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/QueryOptions.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/core/QueryOptions.java index 5d3755dfb..ac0f16772 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/QueryOptions.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/QueryOptions.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.cassandra.core; +package org.springframework.cassandra.core; import java.util.HashMap; import java.util.Map; diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/RetryPolicy.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/RetryPolicy.java similarity index 94% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/RetryPolicy.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/core/RetryPolicy.java index be617f2e5..5264afa3e 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/RetryPolicy.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/RetryPolicy.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.cassandra.core; +package org.springframework.cassandra.core; /** * Retry Policies associated with Cassandra. diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/RetryPolicyResolver.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/RetryPolicyResolver.java similarity index 97% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/RetryPolicyResolver.java rename to spring-cassandra/src/main/java/org/springframework/cassandra/core/RetryPolicyResolver.java index fbff98cb0..dfb93f221 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/RetryPolicyResolver.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/RetryPolicyResolver.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.cassandra.core; +package org.springframework.cassandra.core; import com.datastax.driver.core.policies.DefaultRetryPolicy; import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy; 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 4e0f8aecc..4e352eec1 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 @@ -18,6 +18,7 @@ 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; import com.datastax.driver.core.querybuilder.Select; 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 eea7683a1..84e933f3a 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 @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; import org.springframework.cassandra.core.CassandraTemplate; +import org.springframework.cassandra.core.QueryOptions; import org.springframework.cassandra.core.SessionCallback; import org.springframework.dao.DataAccessException; import org.springframework.dao.DuplicateKeyException; 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 3781d47e9..c219fd2ed 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 @@ -6,12 +6,12 @@ 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.QueryOptions; +import org.springframework.cassandra.core.RetryPolicy; +import org.springframework.cassandra.core.RetryPolicyResolver; import org.springframework.dao.InvalidDataAccessApiUsageException; -import org.springframework.data.cassandra.core.ConsistencyLevel; -import org.springframework.data.cassandra.core.ConsistencyLevelResolver; -import org.springframework.data.cassandra.core.QueryOptions; -import org.springframework.data.cassandra.core.RetryPolicy; -import org.springframework.data.cassandra.core.RetryPolicyResolver; import org.springframework.data.cassandra.exception.EntityWriterException; import org.springframework.data.cassandra.mapping.CassandraPersistentEntity; import org.springframework.data.cassandra.mapping.CassandraPersistentProperty; 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 8034645ad..85951bc10 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 @@ -40,10 +40,10 @@ import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cassandra.core.ConsistencyLevel; +import org.springframework.cassandra.core.QueryOptions; +import org.springframework.cassandra.core.RetryPolicy; import org.springframework.data.cassandra.core.CassandraDataOperations; -import org.springframework.data.cassandra.core.ConsistencyLevel; -import org.springframework.data.cassandra.core.QueryOptions; -import org.springframework.data.cassandra.core.RetryPolicy; import org.springframework.data.cassandra.test.integration.config.TestConfig; import org.springframework.data.cassandra.test.integration.table.Book; import org.springframework.test.context.ContextConfiguration; From 43fffe5e9768510ca66fb5e6884262c260816f52 Mon Sep 17 00:00:00 2001 From: David Webb Date: Mon, 2 Dec 2013 14:07:02 -0500 Subject: [PATCH 2/3] DATACASS-48 : WIP : Add ConsistencyLevel and RetryPolicy to Operations/Template Added optionsByName and options to all relevant operations. Changed main implementation method signatures and all overrides call that now. --- .../cassandra/core/CassandraOperations.java | 75 ++++ .../cassandra/core/CassandraTemplate.java | 357 ++++++++++++++++-- 2 files changed, 397 insertions(+), 35 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 a43ebf085..b53d0fb25 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,6 +66,11 @@ public interface CassandraOperations { */ T query(final String cql, ResultSetExtractor rse) throws DataAccessException; + T query(final String cql, ResultSetExtractor rse, final Map optionsByName) + throws DataAccessException; + + T query(final String cql, ResultSetExtractor rse, final QueryOptions options) throws DataAccessException; + /** * Executes the provided CQL Query asynchronously, and extracts the results with the ResultSetFutureExtractor * @@ -76,6 +81,12 @@ public interface CassandraOperations { */ T queryAsynchronously(final String cql, ResultSetFutureExtractor rse) throws DataAccessException; + T queryAsynchronously(final String cql, ResultSetFutureExtractor rse, final Map optionsByName) + throws DataAccessException; + + T queryAsynchronously(final String cql, ResultSetFutureExtractor rse, final QueryOptions options) + throws DataAccessException; + /** * Executes the provided CQL Query, and then processes the results with the RowCallbackHandler. * @@ -85,6 +96,11 @@ public interface CassandraOperations { */ void query(final String cql, RowCallbackHandler rch) throws DataAccessException; + void query(final String cql, RowCallbackHandler rch, final Map optionsByName) + throws DataAccessException; + + void query(final String cql, RowCallbackHandler rch, final QueryOptions options) throws DataAccessException; + /** * Processes the ResultSet through the RowCallbackHandler and return nothing. This is used internal to the Template * for core operations, but is made available through Operations in the event you have a ResultSet to process. The @@ -106,6 +122,11 @@ public interface CassandraOperations { */ List query(final String cql, RowMapper rowMapper) throws DataAccessException; + List query(final String cql, RowMapper rowMapper, final Map optionsByName) + throws DataAccessException; + + List query(final String cql, RowMapper rowMapper, final QueryOptions options) throws DataAccessException; + /** * Processes the ResultSet through the RowMapper and returns the List of mapped Rows. This is used internal to the * Template for core operations, but is made available through Operations in the event you have a ResultSet to @@ -270,6 +291,12 @@ 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; + /** * 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 @@ -282,6 +309,12 @@ public interface CassandraOperations { */ void query(final String cql, PreparedStatementBinder psb, RowCallbackHandler rch) throws DataAccessException; + void query(final String cql, PreparedStatementBinder psb, RowCallbackHandler rch, + final Map optionsByName) throws DataAccessException; + + void query(final String cql, PreparedStatementBinder psb, RowCallbackHandler rch, final QueryOptions options) + 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 @@ -296,6 +329,12 @@ public interface CassandraOperations { */ List query(final String cql, PreparedStatementBinder psb, RowMapper rowMapper) throws DataAccessException; + List query(final String cql, PreparedStatementBinder psb, RowMapper rowMapper, + final Map optionsByName) throws DataAccessException; + + List query(final String cql, PreparedStatementBinder psb, RowMapper rowMapper, final QueryOptions options) + 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 @@ -308,6 +347,12 @@ public interface CassandraOperations { */ T query(PreparedStatementCreator psc, ResultSetExtractor rse) throws DataAccessException; + T query(PreparedStatementCreator psc, ResultSetExtractor rse, final Map optionsByName) + throws DataAccessException; + + T query(PreparedStatementCreator psc, ResultSetExtractor rse, final QueryOptions options) + 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 @@ -319,6 +364,12 @@ public interface CassandraOperations { */ void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException; + void query(PreparedStatementCreator psc, RowCallbackHandler rch, final Map optionsByName) + throws DataAccessException; + + void query(PreparedStatementCreator psc, RowCallbackHandler rch, final QueryOptions options) + 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 @@ -331,6 +382,12 @@ public interface CassandraOperations { */ List query(PreparedStatementCreator psc, RowMapper rowMapper) throws DataAccessException; + List query(PreparedStatementCreator psc, RowMapper rowMapper, final Map optionsByName) + throws DataAccessException; + + 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 @@ -342,6 +399,12 @@ public interface CassandraOperations { * @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; + + T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse, + final QueryOptions options) throws DataAccessException; + T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse) throws DataAccessException; @@ -356,6 +419,12 @@ public interface CassandraOperations { * @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; + + void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch, + final QueryOptions options) throws DataAccessException; + void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch) throws DataAccessException; @@ -370,6 +439,12 @@ public interface CassandraOperations { * @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; + + List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowMapper rowMapper, + final QueryOptions options) throws DataAccessException; + List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowMapper rowMapper) throws DataAccessException; 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 a9f3a5987..896938bd6 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 @@ -33,10 +33,13 @@ import com.datastax.driver.core.DataType; import com.datastax.driver.core.Host; import com.datastax.driver.core.Metadata; import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Query; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.ResultSetFuture; import com.datastax.driver.core.Row; import com.datastax.driver.core.Session; +import com.datastax.driver.core.SimpleStatement; +import com.datastax.driver.core.Statement; import com.datastax.driver.core.exceptions.DriverException; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Truncate; @@ -87,7 +90,33 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void execute(final String cql) throws DataAccessException { - doExecute(cql); + doExecute(cql, new HashMap()); + } + + /* (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); + } + })); + } + + /* (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) + throws DataAccessException { + Assert.notNull(options); + return queryAsynchronously(cql, rse, options.toMap()); } /* (non-Javadoc) @@ -95,69 +124,120 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public T queryAsynchronously(final String cql, ResultSetFutureExtractor rse) throws DataAccessException { - return rse.extractData(execute(new SessionCallback() { - @Override - public ResultSetFuture doInSession(Session s) throws DataAccessException { - return s.executeAsync(cql); - } - })); + return queryAsynchronously(cql, rse, new HashMap()); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.ResultSetExtractor) */ - public T query(String cql, ResultSetExtractor rse) throws DataAccessException { - ResultSet rs = doExecute(cql); + 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); } + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.ResultSetExtractor, java.util.Map) + */ + @Override + public T query(String cql, ResultSetExtractor rse) throws DataAccessException { + return query(cql, rse, new HashMap()); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.ResultSetExtractor, org.springframework.cassandra.core.QueryOptions) + */ + @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); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowCallbackHandler, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public void query(String cql, RowCallbackHandler rch, QueryOptions options) throws DataAccessException { + Assert.notNull(options); + query(cql, rch, options.toMap()); + } + /* (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 { - process(doExecute(cql), rch); + query(cql, rch, new HashMap()); + } + + /* (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); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.RowMapper, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public List query(String cql, RowMapper rowMapper, QueryOptions options) throws DataAccessException { + Assert.notNull(options); + return query(cql, rowMapper, options.toMap()); } /* (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 process(doExecute(cql), rowMapper); + return query(cql, rowMapper, new HashMap()); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryForList(java.lang.String) */ public List> queryForListOfMap(String cql) throws DataAccessException { - return processListOfMap(doExecute(cql)); + return processListOfMap(doExecute(cql, new HashMap())); } /* (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), elementType); + return processList(doExecute(cql, new HashMap()), elementType); } /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#queryForMap(java.lang.String) */ public Map queryForMap(String cql) throws DataAccessException { - return processMap(doExecute(cql)); + return processMap(doExecute(cql, new HashMap())); } /* (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), requiredType); + return processOne(doExecute(cql, new HashMap()), 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), rowMapper); + return processOne(doExecute(cql, new HashMap()), rowMapper); } /** @@ -185,7 +265,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param callback * @return */ - protected ResultSet doExecute(final String cql) { + protected ResultSet doExecute(final String cql, final Map optionsByName) { logger.info(cql); @@ -193,7 +273,9 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public ResultSet doInSession(Session s) throws DataAccessException { - return s.execute(cql); + SimpleStatement statement = new SimpleStatement(cql); + addQueryOptions(statement, optionsByName); + return s.execute(statement); } }); } @@ -204,12 +286,13 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @param callback * @return */ - protected ResultSet doExecute(final BoundStatement bs) { + protected ResultSet doExecute(final BoundStatement bs, final Map optionsByName) { return doExecute(new SessionCallback() { @Override public ResultSet doInSession(Session s) throws DataAccessException { + addQueryOptions(bs, optionsByName); return s.execute(bs); } }); @@ -443,12 +526,52 @@ 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()); + } + /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.ResultSetExtractor) */ @Override public T query(PreparedStatementCreator psc, ResultSetExtractor rse) throws DataAccessException { - return query(psc, null, rse); + return query(psc, rse, new HashMap()); + } + + /* (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); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.RowCallbackHandler, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public void query(PreparedStatementCreator psc, RowCallbackHandler rch, QueryOptions options) + throws DataAccessException { + Assert.notNull(options); + query(psc, rch, options.toMap()); } /* (non-Javadoc) @@ -456,7 +579,27 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException { - query(psc, null, rch); + query(psc, rch, new HashMap()); + } + + /* (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); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.RowMapper, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public List query(PreparedStatementCreator psc, RowMapper rowMapper, QueryOptions options) + throws DataAccessException { + Assert.notNull(options); + return query(psc, rowMapper, options.toMap()); } /* (non-Javadoc) @@ -464,14 +607,14 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public List query(PreparedStatementCreator psc, RowMapper rowMapper) throws DataAccessException { - return query(psc, null, rowMapper); + return query(psc, rowMapper, new HashMap()); } /* (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) - throws DataAccessException { + 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"); @@ -485,18 +628,58 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe } else { bs = ps.bind(); } - rs = doExecute(bs); + 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); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.ResultSetExtractor, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public T query(String cql, PreparedStatementBinder psb, ResultSetExtractor rse, QueryOptions options) + throws DataAccessException { + Assert.notNull(options); + return query(cql, psb, rse, options.toMap()); + } + /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.PreparedStatementSetter, org.springframework.cassandra.core.ResultSetExtractor) */ @Override public T query(String cql, PreparedStatementBinder psb, ResultSetExtractor rse) throws DataAccessException { - return query(new SimplePreparedStatementCreator(cql), psb, rse); + return query(cql, psb, rse, new HashMap()); + } + + /* (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); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowCallbackHandler, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public void query(String cql, PreparedStatementBinder psb, RowCallbackHandler rch, QueryOptions options) + throws DataAccessException { + Assert.notNull(options); + query(cql, psb, rch, options.toMap()); } /* (non-Javadoc) @@ -504,7 +687,27 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public void query(String cql, PreparedStatementBinder psb, RowCallbackHandler rch) throws DataAccessException { - query(new SimplePreparedStatementCreator(cql), psb, rch); + query(cql, psb, rch, new HashMap()); + } + + /* (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); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(java.lang.String, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.RowMapper, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public List query(String cql, PreparedStatementBinder psb, RowMapper rowMapper, QueryOptions options) + throws DataAccessException { + Assert.notNull(options); + return query(cql, psb, rowMapper, options.toMap()); } /* (non-Javadoc) @@ -512,15 +715,15 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe */ @Override public List query(String cql, PreparedStatementBinder psb, RowMapper rowMapper) throws DataAccessException { - return query(new SimplePreparedStatementCreator(cql), psb, rowMapper); + return query(cql, psb, rowMapper, new HashMap()); } /* (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) - throws DataAccessException { + 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"); @@ -533,7 +736,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe } else { bs = ps.bind(); } - rs = doExecute(bs); + rs = doExecute(bs, optionsByName); process(rs, rch); return null; } @@ -544,8 +747,8 @@ 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.RowMapper) */ @Override - public List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowMapper rowMapper) - throws DataAccessException { + 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"); @@ -558,7 +761,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe } else { bs = ps.bind(); } - rs = doExecute(bs); + rs = doExecute(bs, optionsByName); return process(rs, rowMapper); } @@ -627,6 +830,90 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe @Override public void truncate(String tableName) throws DataAccessException { Truncate truncate = QueryBuilder.truncate(tableName); - doExecute(truncate.getQueryString()); + doExecute(truncate.getQueryString(), new HashMap()); } + + /** + * Add common Query options for all types of queries. + * + * @param q + * @param optionsByName + */ + public 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))); + } + + } + + /* (non-Javadoc) + * @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()); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#query(org.springframework.cassandra.core.PreparedStatementCreator, org.springframework.cassandra.core.PreparedStatementBinder, org.springframework.cassandra.core.ResultSetExtractor) + */ + @Override + public T query(PreparedStatementCreator psc, PreparedStatementBinder psb, ResultSetExtractor rse) + throws DataAccessException { + return query(psc, psb, rse, new HashMap()); + } + + /* (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()); + } + + /* (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, PreparedStatementBinder psb, RowCallbackHandler rch) + throws DataAccessException { + query(psc, psb, rch, new HashMap()); + } + + /* (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()); + } + + /* (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, PreparedStatementBinder psb, RowMapper rowMapper) + throws DataAccessException { + return query(psc, psb, rowMapper, new HashMap()); + } + } \ No newline at end of file From 819804d98792e8461007bb539758217f5498e8d1 Mon Sep 17 00:00:00 2001 From: David Webb Date: Mon, 2 Dec 2013 14:59:09 -0500 Subject: [PATCH 3/3] DATACASS-48 : CLOSED: Added javadoc for new overload methods to specify Query Options. --- .../cassandra/core/CassandraOperations.java | 378 +++++++++++++++++- .../cassandra/core/CassandraTemplate.java | 91 ++++- 2 files changed, 446 insertions(+), 23 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 b53d0fb25..02d8d9ec6 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,9 +66,29 @@ 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. + * + * @param cql The Query + * @param rse The implementation for extracting the ResultSet + * @param options Query Options Object + * + * @return + * @throws DataAccessException + */ T query(final String cql, ResultSetExtractor rse, final QueryOptions options) throws DataAccessException; /** @@ -81,9 +101,27 @@ 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 + * + * @param cql The Query + * @param rse The implementation for extracting the future results + * @param options Query Options Object + * @return + * @throws DataAccessException + */ T queryAsynchronously(final String cql, ResultSetFutureExtractor rse, final QueryOptions options) throws DataAccessException; @@ -96,9 +134,25 @@ 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. + * + * @param cql The Query + * @param rch The implementation for processing the rows returned. + * @param options Query Options Object + * @throws DataAccessException + */ void query(final String cql, RowCallbackHandler rch, final QueryOptions options) throws DataAccessException; /** @@ -122,9 +176,27 @@ 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. + * + * @param cql The Query + * @param rowMapper The implementation for mapping all rows + * @param options Query Options Object + * @return List of processed by the RowMapper + * @throws DataAccessException + */ List query(final String cql, RowMapper rowMapper, final QueryOptions options) throws DataAccessException; /** @@ -309,9 +381,31 @@ 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 + * 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 options The Query Options Object + * @throws DataAccessException + */ void query(final String cql, PreparedStatementBinder psb, RowCallbackHandler rch, final QueryOptions options) throws DataAccessException; @@ -329,9 +423,35 @@ 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 + * 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 options The Query Options Object + * @return List of for each Row returned from the Query. + * @throws DataAccessException + */ List query(final String cql, PreparedStatementBinder psb, RowMapper rowMapper, final QueryOptions options) throws DataAccessException; @@ -347,9 +467,31 @@ 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 + * ResultSetExtractor implementation provided by the Application Code. + * + * @param psc The implementation to create the PreparedStatement + * @param rse Implementation for extracting from the ResultSet + * @param options The Query Options Object + * @return Type which is the output of the ResultSetExtractor + * @throws DataAccessException + */ T query(PreparedStatementCreator psc, ResultSetExtractor rse, final QueryOptions options) throws DataAccessException; @@ -364,9 +506,29 @@ 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 + * RowCallbackHandler and nothing is returned. + * + * @param psc The implementation to create the PreparedStatement + * @param rch The implementation to process Results + * @param options The Query Options Object + * @throws DataAccessException + */ void query(PreparedStatementCreator psc, RowCallbackHandler rch, final QueryOptions options) throws DataAccessException; @@ -382,12 +544,64 @@ 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 + * 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 options The Query Options Object + * @return List of Type mapped from each Row in the Results + * @throws DataAccessException + */ 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 + * 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 options The Query Options Object + * @return Type which is the output of the ResultSetExtractor + * @throws DataAccessException + */ + T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse, + 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 @@ -399,15 +613,39 @@ public interface CassandraOperations { * @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; - - T query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final ResultSetExtractor rse, - final QueryOptions options) throws DataAccessException; - 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 + * 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 options The Query Options Object + * @return Type which is the output of the ResultSetExtractor + * @throws DataAccessException + */ + void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch, + 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 @@ -419,15 +657,39 @@ public interface CassandraOperations { * @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; - - void query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowCallbackHandler rch, - final QueryOptions options) throws DataAccessException; - 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 + * 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 options The Query Options Object + * @return Type which is the output of the ResultSetExtractor + * @throws DataAccessException + */ + List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final 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 @@ -439,12 +701,6 @@ public interface CassandraOperations { * @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; - - List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowMapper rowMapper, - final QueryOptions options) throws DataAccessException; - List query(PreparedStatementCreator psc, final PreparedStatementBinder psb, final RowMapper rowMapper) throws DataAccessException; @@ -473,6 +729,36 @@ 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. + * + *

+ * 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 options The Query Options Object + */ + void ingest(String cql, RowIterator rowIterator, QueryOptions options); + /** * 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. @@ -487,6 +773,34 @@ 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. + * + *

+ * 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 options The Query Options Object + */ + void ingest(String cql, List> rows, QueryOptions options); + /** * 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. @@ -500,6 +814,34 @@ 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. + * + *

+ * 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 options The Query Options Object + */ + void ingest(String cql, Object[][] rows, QueryOptions options); + /** * 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 896938bd6..88d45da7c 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 @@ -772,9 +772,10 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, org.springframework.cassandra.core.RowProvider, int) */ @Override - public void ingest(String cql, RowIterator rowIterator) { + 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())); @@ -782,12 +783,30 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe } + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, org.springframework.cassandra.core.RowIterator, org.springframework.cassandra.core.QueryOptions) + */ + @Override + public void ingest(String cql, RowIterator rowIterator, QueryOptions options) { + Assert.notNull(options); + ingest(cql, rowIterator, options.toMap()); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, org.springframework.cassandra.core.RowIterator) + */ + @Override + public void ingest(String cql, RowIterator rowIterator) { + ingest(cql, rowIterator, new HashMap()); + } + /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, java.util.List) */ @Override - public void ingest(String cql, List> rows) { + public void ingest(String cql, List> rows, Map optionsByName) { + Assert.notNull(optionsByName); Assert.notNull(rows); Assert.notEmpty(rows); @@ -797,15 +816,34 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe values[i++] = row.toArray(); } - ingest(cql, values); + ingest(cql, values, optionsByName); } + /* (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, new HashMap()); + } + /* (non-Javadoc) * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, java.lang.Object[][]) */ @Override - public void ingest(String cql, final Object[][] rows) { + public void ingest(String cql, final Object[][] rows, final Map optionsByName) { + + Assert.notNull(optionsByName); ingest(cql, new RowIterator() { @@ -821,7 +859,24 @@ 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()); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.core.CassandraOperations#ingest(java.lang.String, java.lang.Object[][]) + */ + @Override + public void ingest(String cql, final Object[][] rows) { + ingest(cql, rows, new HashMap()); } /* (non-Javadoc) @@ -859,6 +914,32 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe } + /** + * Add common Query options for all types of queries. + * + * @param q + * @param optionsByName + */ + public static void addPreparedStatementOptions(PreparedStatement s, Map optionsByName) { + + if (optionsByName == 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 (optionsByName.get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY) != null) { + s.setRetryPolicy(RetryPolicyResolver.resolve((RetryPolicy) optionsByName + .get(QueryOptions.QueryOptionMapKeys.RETRY_POLICY))); + } + + } + /* (non-Javadoc) * @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) */