Skip to content

Commit acc867a

Browse files
samueldlightfootmp911de
authored andcommitted
Support configurable batch types for batch operations.
Closes #1174 Original pull request: #1175.
1 parent 86648d3 commit acc867a

File tree

6 files changed

+77
-5
lines changed

6 files changed

+77
-5
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
* @author Mark Paluch
4040
* @author John Blum
4141
* @author Anup Sabbi
42+
* @author Sam Lightfoot
4243
* @since 1.5
4344
*/
4445
class CassandraBatchTemplate implements CassandraBatchOperations {
4546

4647
private final AtomicBoolean executed = new AtomicBoolean();
4748

48-
private final BatchStatementBuilder batch = BatchStatement.builder(BatchType.LOGGED);
49+
private final BatchStatementBuilder batch;
4950

5051
private final CassandraConverter converter;
5152

@@ -61,10 +62,23 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
6162
* @param operations must not be {@literal null}.
6263
*/
6364
CassandraBatchTemplate(CassandraOperations operations) {
65+
this(operations, BatchType.LOGGED);
66+
}
67+
68+
/**
69+
* Create a new {@link CassandraBatchTemplate} given {@link CassandraOperations} and {@link BatchType}.
70+
*
71+
* @param operations must not be {@literal null}.
72+
* @param batchType must not be {@literal null}.
73+
* @since 3.3.0
74+
*/
75+
CassandraBatchTemplate(CassandraOperations operations, BatchType batchType) {
6476

6577
Assert.notNull(operations, "CassandraOperations must not be null");
78+
Assert.notNull(batchType, "BatchType must not be null");
6679

6780
this.operations = operations;
81+
this.batch = BatchStatement.builder(batchType);
6882
this.converter = operations.getConverter();
6983
this.mappingContext = this.converter.getMappingContext();
7084
this.statementFactory = new StatementFactory(new UpdateMapper(converter));

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.lang.Nullable;
3232

3333
import com.datastax.oss.driver.api.core.CqlIdentifier;
34+
import com.datastax.oss.driver.api.core.cql.BatchType;
3435
import com.datastax.oss.driver.api.core.cql.ResultSet;
3536
import com.datastax.oss.driver.api.core.cql.Statement;
3637

@@ -42,6 +43,7 @@
4243
* @author David Webb
4344
* @author Matthew Adams
4445
* @author Mark Paluch
46+
* @author Sam Lightfoot
4547
* @see CassandraTemplate
4648
* @see CqlOperations
4749
* @see Statement
@@ -52,12 +54,22 @@ public interface CassandraOperations extends FluentCassandraOperations {
5254

5355
/**
5456
* Returns a new {@link CassandraBatchOperations}. Each {@link CassandraBatchOperations} instance can be executed only
55-
* once so you might want to obtain new {@link CassandraBatchOperations} instances for each batch.
57+
* once, so you might want to obtain new {@link CassandraBatchOperations} instances for each batch.
5658
*
5759
* @return a new {@link CassandraBatchOperations} associated with the given entity class.
5860
*/
5961
CassandraBatchOperations batchOps();
6062

63+
/**
64+
* Returns a new {@link CassandraBatchOperations}. Each {@link CassandraBatchOperations} instance can be executed only
65+
* once, so you might want to obtain new {@link CassandraBatchOperations} instances for each batch.
66+
*
67+
* @param batchType must not be {@literal null}.
68+
* @return a new {@link ReactiveCassandraBatchOperations} associated with the given entity class.
69+
* @since 3.3.0
70+
*/
71+
CassandraBatchOperations batchOps(BatchType batchType);
72+
6173
/**
6274
* Expose the underlying {@link CqlOperations} to allow CQL operations.
6375
*

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

+10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.datastax.oss.driver.api.core.CqlSession;
7474
import com.datastax.oss.driver.api.core.DriverException;
7575
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
76+
import com.datastax.oss.driver.api.core.cql.BatchType;
7677
import com.datastax.oss.driver.api.core.cql.BoundStatement;
7778
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
7879
import com.datastax.oss.driver.api.core.cql.ResultSet;
@@ -109,6 +110,7 @@
109110
* @author Mark Paluch
110111
* @author John Blum
111112
* @author Lukasz Antoniak
113+
* @author Sam Lightfoot
112114
* @see org.springframework.data.cassandra.core.CassandraOperations
113115
* @since 2.0
114116
*/
@@ -202,6 +204,14 @@ public CassandraBatchOperations batchOps() {
202204
return new CassandraBatchTemplate(this);
203205
}
204206

207+
/* (non-Javadoc)
208+
* @see org.springframework.data.cassandra.core.CassandraOperations#batchOps(com.datastax.oss.driver.api.core.cql.BatchType)
209+
*/
210+
@Override
211+
public CassandraBatchOperations batchOps(BatchType batchType) {
212+
return new CassandraBatchTemplate(this, batchType);
213+
}
214+
205215
/* (non-Javadoc)
206216
* @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
207217
*/

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@
4646
*
4747
* @author Oleh Dokuka
4848
* @author Mark Paluch
49+
* @author Sam Lightfoot
4950
* @since 2.1
5051
*/
5152
class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations {
5253

5354
private final AtomicBoolean executed = new AtomicBoolean();
5455

55-
private final BatchStatementBuilder batch = BatchStatement.builder(BatchType.LOGGED);
56+
private final BatchStatementBuilder batch;
5657

5758
private final CassandraConverter converter;
5859

@@ -70,10 +71,23 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
7071
* @param operations must not be {@literal null}.
7172
*/
7273
ReactiveCassandraBatchTemplate(ReactiveCassandraOperations operations) {
74+
this(operations, BatchType.LOGGED);
75+
}
76+
77+
/**
78+
* Create a new {@link CassandraBatchTemplate} given {@link CassandraOperations} and {@link BatchType}.
79+
*
80+
* @param operations must not be {@literal null}.
81+
* @param batchType must not be {@literal null}.
82+
* @since 3.3.0
83+
*/
84+
ReactiveCassandraBatchTemplate(ReactiveCassandraOperations operations, BatchType batchType) {
7385

7486
Assert.notNull(operations, "CassandraOperations must not be null");
87+
Assert.notNull(batchType, "BatchType must not be null");
7588

7689
this.operations = operations;
90+
this.batch = BatchStatement.builder(batchType);
7791
this.converter = operations.getConverter();
7892
this.mappingContext = this.converter.getMappingContext();
7993
this.statementFactory = new StatementFactory(new UpdateMapper(converter));
@@ -155,7 +169,6 @@ public Mono<WriteResult> execute() {
155169
public ReactiveCassandraBatchOperations withTimestamp(long timestamp) {
156170

157171
assertNotExecuted();
158-
159172
this.batch.setQueryTimestamp(timestamp);
160173

161174
return this;

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.data.cassandra.core.query.Update;
3030
import org.springframework.data.domain.Slice;
3131

32+
import com.datastax.oss.driver.api.core.cql.BatchType;
3233
import com.datastax.oss.driver.api.core.cql.Statement;
3334

3435
/**
@@ -38,6 +39,7 @@
3839
* @author Mark Paluch
3940
* @author Hleb Albau
4041
* @author Oleh Dokuka
42+
* @author Sam Lightfoot
4143
* @since 2.0
4244
* @see ReactiveCassandraTemplate
4345
* @see ReactiveCqlOperations
@@ -51,14 +53,25 @@ public interface ReactiveCassandraOperations extends ReactiveFluentCassandraOper
5153

5254
/**
5355
* Returns a new {@link ReactiveCassandraBatchOperations}. Each {@link ReactiveCassandraBatchOperations} instance can
54-
* be executed only once so you might want to obtain new {@link ReactiveCassandraBatchOperations} instances for each
56+
* be executed only once, so you might want to obtain new {@link ReactiveCassandraBatchOperations} instances for each
5557
* batch.
5658
*
5759
* @return a new {@link ReactiveCassandraBatchOperations} associated with the given entity class.
5860
* @since 2.1
5961
*/
6062
ReactiveCassandraBatchOperations batchOps();
6163

64+
/**
65+
* Returns a new {@link ReactiveCassandraBatchOperations}. Each {@link ReactiveCassandraBatchOperations} instance can
66+
* be executed only once, so you might want to obtain new {@link ReactiveCassandraBatchOperations} instances for each
67+
* batch.
68+
*
69+
* @param batchType must not be {@literal null}.
70+
* @return a new {@link ReactiveCassandraBatchOperations} associated with the given entity class.
71+
* @since 3.3.0
72+
*/
73+
ReactiveCassandraBatchOperations batchOps(BatchType batchType);
74+
6275
/**
6376
* Expose the underlying {@link ReactiveCqlOperations} to allow CQL operations.
6477
*

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

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import com.datastax.oss.driver.api.core.DriverException;
7070
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
7171
import com.datastax.oss.driver.api.core.context.DriverContext;
72+
import com.datastax.oss.driver.api.core.cql.BatchType;
7273
import com.datastax.oss.driver.api.core.cql.BoundStatement;
7374
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
7475
import com.datastax.oss.driver.api.core.cql.Row;
@@ -105,6 +106,7 @@
105106
* @author John Blum
106107
* @author Lukasz Antoniak
107108
* @author Hleb Albau
109+
* @author Sam Lightfoot
108110
* @since 2.0
109111
*/
110112
public class ReactiveCassandraTemplate
@@ -199,6 +201,14 @@ public ReactiveCassandraBatchOperations batchOps() {
199201
return new ReactiveCassandraBatchTemplate(this);
200202
}
201203

204+
/* (non-Javadoc)
205+
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#batchOps(com.datastax.oss.driver.api.core.cql.BatchType)
206+
*/
207+
@Override
208+
public ReactiveCassandraBatchOperations batchOps(BatchType batchType) {
209+
return new ReactiveCassandraBatchTemplate(this, batchType);
210+
}
211+
202212
/* (non-Javadoc)
203213
* @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
204214
*/

0 commit comments

Comments
 (0)