Skip to content

Commit d806d32

Browse files
committed
Add consistencyLevel setter for batchOps
1 parent 25260ad commit d806d32

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraBatchTemplate.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.cassandra.core;
1717

18+
import com.datastax.oss.driver.api.core.ConsistencyLevel;
1819
import java.util.Arrays;
1920
import java.util.concurrent.atomic.AtomicBoolean;
2021

@@ -42,6 +43,7 @@
4243
* @since 1.5
4344
*/
4445
class CassandraBatchTemplate implements CassandraBatchOperations {
46+
//public class CassandraBatchTemplate implements CassandraBatchOperations {
4547

4648
private final AtomicBoolean executed = new AtomicBoolean();
4749

@@ -60,8 +62,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
6062
*
6163
* @param operations must not be {@literal null}.
6264
*/
63-
CassandraBatchTemplate(CassandraOperations operations) {
64-
65+
public CassandraBatchTemplate(CassandraOperations operations) {
6566
Assert.notNull(operations, "CassandraOperations must not be null");
6667

6768
this.operations = operations;
@@ -70,6 +71,16 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
7071
this.statementFactory = new StatementFactory(new UpdateMapper(converter));
7172
}
7273

74+
/**
75+
* Create a new {@link CassandraBatchTemplate} given {@link CassandraOperations} with {@link ConsistencyLevel}.
76+
*
77+
* @param operations must not be {@literal null}.
78+
*/
79+
public CassandraBatchTemplate(CassandraOperations operations, ConsistencyLevel consistencyLevel) {
80+
this(operations);
81+
batch.setConsistencyLevel(consistencyLevel);
82+
}
83+
7384
/**
7485
* Return a reference to the configured {@link CassandraConverter} used to map {@link Object Objects} to
7586
* {@link com.datastax.driver.core.Row Rows}.

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.cassandra.core;
1717

18+
import com.datastax.oss.driver.api.core.ConsistencyLevel;
1819
import java.util.Iterator;
1920
import java.util.List;
2021
import java.util.stream.Stream;
@@ -58,6 +59,14 @@ public interface CassandraOperations extends FluentCassandraOperations {
5859
*/
5960
CassandraBatchOperations batchOps();
6061

62+
/**
63+
* Returns a new {@link CassandraBatchOperations}. Each {@link CassandraBatchOperations} instance can be executed only
64+
* once so you might want to obtain new {@link CassandraBatchOperations} instances for each batch.
65+
*
66+
* @return a new {@link CassandraBatchOperations} associated with the given entity class.
67+
*/
68+
CassandraBatchOperations batchOps(ConsistencyLevel consistencyLevel);
69+
6170
/**
6271
* Expose the underlying {@link CqlOperations} to allow CQL operations.
6372
*

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.cassandra.core;
1717

18+
import com.datastax.oss.driver.api.core.ConsistencyLevel;
1819
import java.util.List;
1920
import java.util.function.Consumer;
2021
import java.util.function.Function;
@@ -202,6 +203,12 @@ public CassandraBatchOperations batchOps() {
202203
return new CassandraBatchTemplate(this);
203204
}
204205

206+
@Override
207+
public CassandraBatchOperations batchOps(ConsistencyLevel consistencyLevel) {
208+
return new CassandraBatchTemplate(this, consistencyLevel);
209+
}
210+
211+
205212
/* (non-Javadoc)
206213
* @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
207214
*/

0 commit comments

Comments
 (0)