Skip to content

Commit 837d1ec

Browse files
committed
Apply zero TTL to modifying statements.
We now apply a zero TTL if it was set. Previously, we omitted zero TTLs and dropped these. Closes #1535
1 parent 06b1f1a commit 837d1ec

File tree

6 files changed

+25
-9
lines changed

6 files changed

+25
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private DeleteOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProf
5151
@Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace, @Nullable Integer pageSize,
5252
@Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
5353
@Nullable ConsistencyLevel serialConsistencyLevel,
54-
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifExists,
54+
Duration timeout, @Nullable Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifExists,
5555
@Nullable Filter ifCondition) {
5656

5757
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ private InsertOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProf
4848
@Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace, @Nullable Integer pageSize,
4949
@Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
5050
@Nullable ConsistencyLevel serialConsistencyLevel,
51-
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifNotExists,
51+
Duration timeout, @Nullable Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing,
52+
boolean ifNotExists,
5253
boolean insertNulls) {
5354

5455
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public class UpdateOptions extends WriteOptions {
5151
private UpdateOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
5252
@Nullable Filter ifCondition, boolean ifExists, @Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace,
5353
@Nullable Integer pageSize, @Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
54-
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, Duration ttl, @Nullable Long timestamp,
54+
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, @Nullable Duration ttl,
55+
@Nullable Long timestamp,
5556
@Nullable Boolean tracing) {
5657

5758
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.function.BiFunction;
2020

2121
import org.springframework.data.cassandra.core.cql.util.Bindings;
22+
import org.springframework.lang.Nullable;
2223
import org.springframework.util.Assert;
2324

2425
import com.datastax.oss.driver.api.core.cql.BatchStatement;
@@ -205,8 +206,8 @@ private static int getTtlSeconds(Duration ttl) {
205206
return Math.toIntExact(ttl.getSeconds());
206207
}
207208

208-
private static boolean hasTtl(Duration ttl) {
209-
return !ttl.isZero() && !ttl.isNegative();
209+
private static boolean hasTtl(@Nullable Duration ttl) {
210+
return ttl != null && !ttl.isNegative();
210211
}
211212

212213
/**

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ public class WriteOptions extends QueryOptions {
4343

4444
private static final WriteOptions EMPTY = new WriteOptionsBuilder().build();
4545

46-
private final Duration ttl;
46+
private final @Nullable Duration ttl;
4747

4848
private final @Nullable Long timestamp;
4949

5050
protected WriteOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
5151
@Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace, @Nullable Integer pageSize,
5252
@Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
53-
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, Duration ttl, @Nullable Long timestamp,
53+
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, @Nullable Duration ttl,
54+
@Nullable Long timestamp,
5455
@Nullable Boolean tracing) {
5556

5657
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,
@@ -92,8 +93,9 @@ public WriteOptionsBuilder mutate() {
9293
}
9394

9495
/**
95-
* @return the time to live, if set.
96+
* @return the time to live, if set, otherwise {@literal null}.
9697
*/
98+
@Nullable
9799
public Duration getTtl() {
98100
return this.ttl;
99101
}

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/StatementFactoryUnitTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,12 @@ void insertShouldApplyQueryOptions() {
339339
StatementBuilder<RegularInsert> insert = statementFactory.insert(person, queryOptions);
340340

341341
SimpleStatement statement = insert.build();
342+
assertThat(statement.getQuery()).isEqualTo("INSERT INTO person (id) VALUES (?)");
342343
assertThat(statement.getExecutionProfileName()).isEqualTo("foo");
343344
assertThat(statement.getSerialConsistencyLevel()).isEqualTo(DefaultConsistencyLevel.QUORUM);
344345
}
345346

346-
@Test // GH-1401
347+
@Test // GH-1401, GH-1535
347348
void insertWithOptionsShouldRenderBindMarkers() {
348349

349350
Person person = new Person();
@@ -358,6 +359,16 @@ void insertWithOptionsShouldRenderBindMarkers() {
358359

359360
assertThat(statement.getQuery()).isEqualTo("INSERT INTO person (id) VALUES (?) USING TIMESTAMP ? AND TTL ?");
360361
assertThat(statement.getPositionalValues()).containsExactly("foo", 1234L, 10);
362+
363+
queryOptions = WriteOptions.builder() //
364+
.ttl(Duration.ZERO).timestamp(1234).build();
365+
366+
insert = statementFactory.insert(person, queryOptions);
367+
368+
statement = insert.build(ParameterHandling.BY_INDEX);
369+
370+
assertThat(statement.getQuery()).isEqualTo("INSERT INTO person (id) VALUES (?) USING TIMESTAMP ? AND TTL ?");
371+
assertThat(statement.getPositionalValues()).containsExactly("foo", 1234L, 0);
361372
}
362373

363374
@Test // DATACASS-656

0 commit comments

Comments
 (0)