30
30
import com .datastax .oss .driver .api .core .cql .BatchType ;
31
31
import com .datastax .oss .driver .api .core .cql .BatchableStatement ;
32
32
import com .datastax .oss .driver .api .core .cql .SimpleStatement ;
33
+ import com .datastax .oss .driver .api .core .cql .Statement ;
33
34
import com .datastax .oss .driver .api .core .metadata .Node ;
34
35
import com .datastax .oss .driver .api .core .metadata .token .Token ;
35
36
import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableList ;
41
42
import java .util .Iterator ;
42
43
import java .util .List ;
43
44
import java .util .Map ;
45
+ import java .util .stream .StreamSupport ;
44
46
import net .jcip .annotations .Immutable ;
45
47
import org .slf4j .Logger ;
46
48
import org .slf4j .LoggerFactory ;
@@ -68,6 +70,7 @@ public class DefaultBatchStatement implements BatchStatement {
68
70
private final Duration timeout ;
69
71
private final Node node ;
70
72
private final int nowInSeconds ;
73
+ private final boolean isLWT ;
71
74
72
75
public DefaultBatchStatement (
73
76
BatchType batchType ,
@@ -88,7 +91,8 @@ public DefaultBatchStatement(
88
91
ConsistencyLevel serialConsistencyLevel ,
89
92
Duration timeout ,
90
93
Node node ,
91
- int nowInSeconds ) {
94
+ int nowInSeconds ,
95
+ Boolean isLWT ) {
92
96
for (BatchableStatement <?> statement : statements ) {
93
97
if (statement != null
94
98
&& (statement .getConsistencyLevel () != null
@@ -120,6 +124,11 @@ public DefaultBatchStatement(
120
124
this .timeout = timeout ;
121
125
this .node = node ;
122
126
this .nowInSeconds = nowInSeconds ;
127
+ if (isLWT != null ) {
128
+ this .isLWT = isLWT ;
129
+ } else {
130
+ this .isLWT = StreamSupport .stream (statements .spliterator (), false ).anyMatch (Statement ::isLWT );
131
+ }
123
132
}
124
133
125
134
@ NonNull
@@ -150,7 +159,8 @@ public BatchStatement setBatchType(@NonNull BatchType newBatchType) {
150
159
serialConsistencyLevel ,
151
160
timeout ,
152
161
node ,
153
- nowInSeconds );
162
+ nowInSeconds ,
163
+ isLWT );
154
164
}
155
165
156
166
@ NonNull
@@ -175,7 +185,8 @@ public BatchStatement setKeyspace(@Nullable CqlIdentifier newKeyspace) {
175
185
serialConsistencyLevel ,
176
186
timeout ,
177
187
node ,
178
- nowInSeconds );
188
+ nowInSeconds ,
189
+ isLWT );
179
190
}
180
191
181
192
@ NonNull
@@ -204,7 +215,8 @@ public BatchStatement add(@NonNull BatchableStatement<?> statement) {
204
215
serialConsistencyLevel ,
205
216
timeout ,
206
217
node ,
207
- nowInSeconds );
218
+ nowInSeconds ,
219
+ isLWT || statement .isLWT ());
208
220
}
209
221
}
210
222
@@ -237,7 +249,10 @@ public BatchStatement addAll(@NonNull Iterable<? extends BatchableStatement<?>>
237
249
serialConsistencyLevel ,
238
250
timeout ,
239
251
node ,
240
- nowInSeconds );
252
+ nowInSeconds ,
253
+ isLWT
254
+ || StreamSupport .stream (newStatements .spliterator (), false )
255
+ .anyMatch (Statement ::isLWT ));
241
256
}
242
257
}
243
258
@@ -268,7 +283,8 @@ public BatchStatement clear() {
268
283
serialConsistencyLevel ,
269
284
timeout ,
270
285
node ,
271
- nowInSeconds );
286
+ nowInSeconds ,
287
+ isLWT );
272
288
}
273
289
274
290
@ NonNull
@@ -304,7 +320,8 @@ public BatchStatement setPagingState(ByteBuffer newPagingState) {
304
320
serialConsistencyLevel ,
305
321
timeout ,
306
322
node ,
307
- nowInSeconds );
323
+ nowInSeconds ,
324
+ isLWT );
308
325
}
309
326
310
327
@ Override
@@ -334,7 +351,8 @@ public BatchStatement setPageSize(int newPageSize) {
334
351
serialConsistencyLevel ,
335
352
timeout ,
336
353
node ,
337
- nowInSeconds );
354
+ nowInSeconds ,
355
+ isLWT );
338
356
}
339
357
340
358
@ Nullable
@@ -365,7 +383,8 @@ public BatchStatement setConsistencyLevel(@Nullable ConsistencyLevel newConsiste
365
383
serialConsistencyLevel ,
366
384
timeout ,
367
385
node ,
368
- nowInSeconds );
386
+ nowInSeconds ,
387
+ isLWT );
369
388
}
370
389
371
390
@ Nullable
@@ -397,7 +416,8 @@ public BatchStatement setSerialConsistencyLevel(
397
416
newSerialConsistencyLevel ,
398
417
timeout ,
399
418
node ,
400
- nowInSeconds );
419
+ nowInSeconds ,
420
+ isLWT );
401
421
}
402
422
403
423
@ Override
@@ -427,7 +447,8 @@ public BatchStatement setExecutionProfileName(@Nullable String newConfigProfileN
427
447
serialConsistencyLevel ,
428
448
timeout ,
429
449
node ,
430
- nowInSeconds );
450
+ nowInSeconds ,
451
+ isLWT );
431
452
}
432
453
433
454
@ Override
@@ -457,7 +478,8 @@ public DefaultBatchStatement setExecutionProfile(@Nullable DriverExecutionProfil
457
478
serialConsistencyLevel ,
458
479
timeout ,
459
480
node ,
460
- nowInSeconds );
481
+ nowInSeconds ,
482
+ isLWT );
461
483
}
462
484
463
485
@ Override
@@ -522,7 +544,8 @@ public BatchStatement setRoutingKeyspace(CqlIdentifier newRoutingKeyspace) {
522
544
serialConsistencyLevel ,
523
545
timeout ,
524
546
node ,
525
- nowInSeconds );
547
+ nowInSeconds ,
548
+ isLWT );
526
549
}
527
550
528
551
@ NonNull
@@ -547,7 +570,8 @@ public BatchStatement setNode(@Nullable Node newNode) {
547
570
serialConsistencyLevel ,
548
571
timeout ,
549
572
newNode ,
550
- nowInSeconds );
573
+ nowInSeconds ,
574
+ isLWT );
551
575
}
552
576
553
577
@ Nullable
@@ -593,7 +617,8 @@ public BatchStatement setRoutingKey(ByteBuffer newRoutingKey) {
593
617
serialConsistencyLevel ,
594
618
timeout ,
595
619
node ,
596
- nowInSeconds );
620
+ nowInSeconds ,
621
+ isLWT );
597
622
}
598
623
599
624
@ Override
@@ -633,7 +658,8 @@ public BatchStatement setRoutingToken(Token newRoutingToken) {
633
658
serialConsistencyLevel ,
634
659
timeout ,
635
660
node ,
636
- nowInSeconds );
661
+ nowInSeconds ,
662
+ isLWT );
637
663
}
638
664
639
665
@ NonNull
@@ -664,7 +690,8 @@ public DefaultBatchStatement setCustomPayload(@NonNull Map<String, ByteBuffer> n
664
690
serialConsistencyLevel ,
665
691
timeout ,
666
692
node ,
667
- nowInSeconds );
693
+ nowInSeconds ,
694
+ isLWT );
668
695
}
669
696
670
697
@ Override
@@ -700,7 +727,8 @@ public DefaultBatchStatement setIdempotent(Boolean newIdempotence) {
700
727
serialConsistencyLevel ,
701
728
timeout ,
702
729
node ,
703
- nowInSeconds );
730
+ nowInSeconds ,
731
+ isLWT );
704
732
}
705
733
706
734
@ Override
@@ -730,7 +758,8 @@ public BatchStatement setTracing(boolean newTracing) {
730
758
serialConsistencyLevel ,
731
759
timeout ,
732
760
node ,
733
- nowInSeconds );
761
+ nowInSeconds ,
762
+ isLWT );
734
763
}
735
764
736
765
@ Override
@@ -760,7 +789,8 @@ public BatchStatement setQueryTimestamp(long newTimestamp) {
760
789
serialConsistencyLevel ,
761
790
timeout ,
762
791
node ,
763
- nowInSeconds );
792
+ nowInSeconds ,
793
+ isLWT );
764
794
}
765
795
766
796
@ NonNull
@@ -785,7 +815,8 @@ public BatchStatement setTimeout(@Nullable Duration newTimeout) {
785
815
serialConsistencyLevel ,
786
816
newTimeout ,
787
817
node ,
788
- nowInSeconds );
818
+ nowInSeconds ,
819
+ isLWT );
789
820
}
790
821
791
822
@ Override
@@ -815,11 +846,37 @@ public BatchStatement setNowInSeconds(int newNowInSeconds) {
815
846
serialConsistencyLevel ,
816
847
timeout ,
817
848
node ,
818
- newNowInSeconds );
849
+ newNowInSeconds ,
850
+ isLWT );
851
+ }
852
+
853
+ @ NonNull
854
+ public BatchStatement setIsLWT (boolean newIsLWT ) {
855
+ return new DefaultBatchStatement (
856
+ batchType ,
857
+ statements ,
858
+ executionProfileName ,
859
+ executionProfile ,
860
+ keyspace ,
861
+ routingKeyspace ,
862
+ routingKey ,
863
+ routingToken ,
864
+ customPayload ,
865
+ idempotent ,
866
+ tracing ,
867
+ timestamp ,
868
+ pagingState ,
869
+ pageSize ,
870
+ consistencyLevel ,
871
+ serialConsistencyLevel ,
872
+ timeout ,
873
+ node ,
874
+ nowInSeconds ,
875
+ newIsLWT );
819
876
}
820
877
821
878
@ Override
822
879
public boolean isLWT () {
823
- return false ;
880
+ return isLWT ;
824
881
}
825
882
}
0 commit comments