DATACASS-114 - Added WriteOptions with TTL support in CqlOperations and
CassandraOperations.
This commit is contained in:
@@ -973,7 +973,7 @@ public interface CqlOperations {
|
||||
* @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);
|
||||
void ingest(String cql, RowIterator rowIterator, WriteOptions options);
|
||||
|
||||
/**
|
||||
* This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then
|
||||
@@ -1001,7 +1001,7 @@ public interface CqlOperations {
|
||||
* @param rows List of List<?> with data to bind to the CQL.
|
||||
* @param options The Query Options Object
|
||||
*/
|
||||
void ingest(String cql, List<List<?>> rows, QueryOptions options);
|
||||
void ingest(String cql, List<List<?>> rows, WriteOptions options);
|
||||
|
||||
/**
|
||||
* This is an operation designed for high performance writes. The cql is used to create a PreparedStatement once, then
|
||||
@@ -1041,7 +1041,7 @@ public interface CqlOperations {
|
||||
* @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);
|
||||
void ingest(String cql, Object[][] rows, WriteOptions options);
|
||||
|
||||
/**
|
||||
* Delete all rows in the table
|
||||
|
||||
@@ -116,6 +116,84 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
|
||||
return q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add common {@link Query} options for Insert queries.
|
||||
*
|
||||
* @param q
|
||||
* @param options
|
||||
* @return the {@link Query} given.
|
||||
*/
|
||||
public static Query addWriteOptions(Insert q, WriteOptions options) {
|
||||
|
||||
if (options == null) {
|
||||
return q;
|
||||
}
|
||||
|
||||
if (options.getConsistencyLevel() != null) {
|
||||
q.setConsistencyLevel(ConsistencyLevelResolver.resolve(options.getConsistencyLevel()));
|
||||
}
|
||||
if (options.getRetryPolicy() != null) {
|
||||
q.setRetryPolicy(RetryPolicyResolver.resolve(options.getRetryPolicy()));
|
||||
}
|
||||
if (options.getTtl() != null) {
|
||||
q.using(QueryBuilder.ttl(options.getTtl()));
|
||||
}
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add common {@link Query} options for Update queries.
|
||||
*
|
||||
* @param q
|
||||
* @param options
|
||||
* @return the {@link Query} given.
|
||||
*/
|
||||
public static Query addWriteOptions(Update q, WriteOptions options) {
|
||||
|
||||
if (options == null) {
|
||||
return q;
|
||||
}
|
||||
|
||||
if (options.getConsistencyLevel() != null) {
|
||||
q.setConsistencyLevel(ConsistencyLevelResolver.resolve(options.getConsistencyLevel()));
|
||||
}
|
||||
if (options.getRetryPolicy() != null) {
|
||||
q.setRetryPolicy(RetryPolicyResolver.resolve(options.getRetryPolicy()));
|
||||
}
|
||||
if (options.getTtl() != null) {
|
||||
q.using(QueryBuilder.ttl(options.getTtl()));
|
||||
}
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add common {@link Query} options for Batch queries.
|
||||
*
|
||||
* @param q
|
||||
* @param options
|
||||
* @return the {@link Query} given.
|
||||
*/
|
||||
public static Query addWriteOptions(Batch q, WriteOptions options) {
|
||||
|
||||
if (options == null) {
|
||||
return q;
|
||||
}
|
||||
|
||||
if (options.getConsistencyLevel() != null) {
|
||||
q.setConsistencyLevel(ConsistencyLevelResolver.resolve(options.getConsistencyLevel()));
|
||||
}
|
||||
if (options.getRetryPolicy() != null) {
|
||||
q.setRetryPolicy(RetryPolicyResolver.resolve(options.getRetryPolicy()));
|
||||
}
|
||||
if (options.getTtl() != null) {
|
||||
q.using(QueryBuilder.ttl(options.getTtl()));
|
||||
}
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add common Query options for all types of queries.
|
||||
*
|
||||
@@ -743,7 +821,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ingest(String cql, RowIterator rowIterator, QueryOptions options) {
|
||||
public void ingest(String cql, RowIterator rowIterator, WriteOptions options) {
|
||||
|
||||
PreparedStatement preparedStatement = getSession().prepare(cql);
|
||||
addPreparedStatementOptions(preparedStatement, options);
|
||||
@@ -759,7 +837,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ingest(String cql, final List<List<?>> rows, QueryOptions options) {
|
||||
public void ingest(String cql, final List<List<?>> rows, WriteOptions options) {
|
||||
|
||||
Assert.notNull(rows);
|
||||
Assert.notEmpty(rows);
|
||||
@@ -788,7 +866,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ingest(String cql, final Object[][] rows, QueryOptions options) {
|
||||
public void ingest(String cql, final Object[][] rows, WriteOptions options) {
|
||||
|
||||
ingest(cql, new RowIterator() {
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2013-2014 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 class WriteOptions extends QueryOptions {
|
||||
|
||||
private Integer ttl;
|
||||
|
||||
/**
|
||||
* @return Returns the ttl.
|
||||
*/
|
||||
public Integer getTtl() {
|
||||
return ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ttl The ttl to set.
|
||||
*/
|
||||
public void setTtl(Integer ttl) {
|
||||
this.ttl = ttl;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -47,6 +47,7 @@ import org.springframework.cassandra.core.RowCallbackHandler;
|
||||
import org.springframework.cassandra.core.RowIterator;
|
||||
import org.springframework.cassandra.core.RowMapper;
|
||||
import org.springframework.cassandra.core.SessionCallback;
|
||||
import org.springframework.cassandra.core.WriteOptions;
|
||||
import org.springframework.cassandra.core.keyspace.CreateTableSpecification;
|
||||
import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
@@ -75,11 +76,11 @@ import static org.junit.Assert.assertTrue;
|
||||
* @author David Webb
|
||||
*
|
||||
*/
|
||||
public class CassandraOperationsTest extends AbstractKeyspaceCreatingIntegrationTest {
|
||||
public class CQLOperationsTest extends AbstractKeyspaceCreatingIntegrationTest {
|
||||
|
||||
private static CqlOperations cqlTemplate;
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(CassandraOperationsTest.class);
|
||||
private static Logger log = LoggerFactory.getLogger(CQLOperationsTest.class);
|
||||
|
||||
/*
|
||||
* Objects used for test data
|
||||
@@ -98,7 +99,7 @@ public class CassandraOperationsTest extends AbstractKeyspaceCreatingIntegration
|
||||
"cassandraOperationsTest-cql-dataload.cql", this.keyspace), CASSANDRA_CONFIG, CASSANDRA_HOST,
|
||||
CASSANDRA_NATIVE_PORT);
|
||||
|
||||
public CassandraOperationsTest() {
|
||||
public CQLOperationsTest() {
|
||||
super();
|
||||
// TODO clear = true;
|
||||
}
|
||||
@@ -144,7 +145,7 @@ public class CassandraOperationsTest extends AbstractKeyspaceCreatingIntegration
|
||||
@Override
|
||||
public Collection<MyHost> mapHosts(Set<Host> host) throws DriverException {
|
||||
|
||||
List<MyHost> list = new LinkedList<CassandraOperationsTest.MyHost>();
|
||||
List<MyHost> list = new LinkedList<CQLOperationsTest.MyHost>();
|
||||
|
||||
for (Host h : host) {
|
||||
MyHost mh = new MyHost();
|
||||
@@ -170,6 +171,9 @@ public class CassandraOperationsTest extends AbstractKeyspaceCreatingIntegration
|
||||
@SuppressWarnings("unchecked")
|
||||
public void ingestionTestListOfList() {
|
||||
|
||||
WriteOptions options = new WriteOptions();
|
||||
options.setTtl(360);
|
||||
|
||||
String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";
|
||||
|
||||
List<List<?>> values = new LinkedList<List<?>>();
|
||||
@@ -178,7 +182,7 @@ public class CassandraOperationsTest extends AbstractKeyspaceCreatingIntegration
|
||||
values.add(new LinkedList<Object>(CollectionUtils.arrayToList(o2)));
|
||||
values.add(new LinkedList<Object>(CollectionUtils.arrayToList(o3)));
|
||||
|
||||
cqlTemplate.ingest(cql, values);
|
||||
cqlTemplate.ingest(cql, values, options);
|
||||
|
||||
// Assert that the rows were inserted into Cassandra
|
||||
Book b1 = getBook((String) o1[0]);
|
||||
Reference in New Issue
Block a user