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
This commit is contained in:
Mark Paluch
2024-11-15 09:33:01 +01:00
parent 4fcc710d88
commit e066461811
6 changed files with 25 additions and 9 deletions

View File

@@ -51,7 +51,7 @@ public class DeleteOptions extends WriteOptions {
@Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace, @Nullable Integer pageSize,
@Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
@Nullable ConsistencyLevel serialConsistencyLevel,
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifExists,
Duration timeout, @Nullable Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifExists,
@Nullable Filter ifCondition) {
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,

View File

@@ -48,7 +48,8 @@ public class InsertOptions extends WriteOptions {
@Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace, @Nullable Integer pageSize,
@Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
@Nullable ConsistencyLevel serialConsistencyLevel,
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifNotExists,
Duration timeout, @Nullable Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing,
boolean ifNotExists,
boolean insertNulls) {
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,

View File

@@ -51,7 +51,8 @@ public class UpdateOptions extends WriteOptions {
private UpdateOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
@Nullable Filter ifCondition, boolean ifExists, @Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace,
@Nullable Integer pageSize, @Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, Duration ttl, @Nullable Long timestamp,
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, @Nullable Duration ttl,
@Nullable Long timestamp,
@Nullable Boolean tracing) {
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,

View File

@@ -19,6 +19,7 @@ import java.time.Duration;
import java.util.function.BiFunction;
import org.springframework.data.cassandra.core.cql.util.Bindings;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import com.datastax.oss.driver.api.core.cql.BatchStatement;
@@ -205,8 +206,8 @@ public abstract class QueryOptionsUtil {
return Math.toIntExact(ttl.getSeconds());
}
private static boolean hasTtl(Duration ttl) {
return !ttl.isZero() && !ttl.isNegative();
private static boolean hasTtl(@Nullable Duration ttl) {
return ttl != null && !ttl.isNegative();
}
/**

View File

@@ -43,14 +43,15 @@ public class WriteOptions extends QueryOptions {
private static final WriteOptions EMPTY = new WriteOptionsBuilder().build();
private final Duration ttl;
private final @Nullable Duration ttl;
private final @Nullable Long timestamp;
protected WriteOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
@Nullable Boolean idempotent, @Nullable CqlIdentifier keyspace, @Nullable Integer pageSize,
@Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey,
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, Duration ttl, @Nullable Long timestamp,
@Nullable ConsistencyLevel serialConsistencyLevel, Duration timeout, @Nullable Duration ttl,
@Nullable Long timestamp,
@Nullable Boolean tracing) {
super(consistencyLevel, executionProfileResolver, idempotent, keyspace, pageSize, routingKeyspace, routingKey,
@@ -92,8 +93,9 @@ public class WriteOptions extends QueryOptions {
}
/**
* @return the time to live, if set.
* @return the time to live, if set, otherwise {@literal null}.
*/
@Nullable
public Duration getTtl() {
return this.ttl;
}

View File

@@ -398,11 +398,12 @@ class StatementFactoryUnitTests {
StatementBuilder<RegularInsert> insert = statementFactory.insert(person, queryOptions);
SimpleStatement statement = insert.build();
assertThat(statement.getQuery()).isEqualTo("INSERT INTO person (id) VALUES (?)");
assertThat(statement.getExecutionProfileName()).isEqualTo("foo");
assertThat(statement.getSerialConsistencyLevel()).isEqualTo(DefaultConsistencyLevel.QUORUM);
}
@Test // GH-1401
@Test // GH-1401, GH-1535
void insertWithOptionsShouldRenderBindMarkers() {
Person person = new Person();
@@ -417,6 +418,16 @@ class StatementFactoryUnitTests {
assertThat(statement.getQuery()).isEqualTo("INSERT INTO person (id) VALUES (?) USING TIMESTAMP ? AND TTL ?");
assertThat(statement.getPositionalValues()).containsExactly("foo", 1234L, 10);
queryOptions = WriteOptions.builder() //
.ttl(Duration.ZERO).timestamp(1234).build();
insert = statementFactory.insert(person, queryOptions);
statement = insert.build(ParameterHandling.BY_INDEX);
assertThat(statement.getQuery()).isEqualTo("INSERT INTO person (id) VALUES (?) USING TIMESTAMP ? AND TTL ?");
assertThat(statement.getPositionalValues()).containsExactly("foo", 1234L, 0);
}
@Test // DATACASS-656