DATACASS-56 - Allow query options mutation.
We now allow query option mutation through QueryOptions.mutate() returning an initialized builder. The mutation builder is initialized with the state of the QueryOptions object and allows further customization without changing the previous state of the immutable QueryOptions object. QueryOptions queryOptions = …; QueryOptions mutated = queryOptions.mutate().readTimeout(Duration.ofSeconds(5)).build();
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.data.cassandra.core;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.springframework.data.cassandra.core.cql.WriteOptions;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -30,6 +31,7 @@ import com.datastax.driver.core.policies.RetryPolicy;
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class InsertOptions extends WriteOptions {
|
||||
|
||||
private static final InsertOptions EMPTY = new InsertOptionsBuilder().build();
|
||||
@@ -62,6 +64,16 @@ public class InsertOptions extends WriteOptions {
|
||||
return new InsertOptionsBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link InsertOptionsBuilder} to mutate properties of this {@link InsertOptions}.
|
||||
*
|
||||
* @return a new {@link InsertOptionsBuilder} initialized with this {@link InsertOptions}.
|
||||
*/
|
||||
@Override
|
||||
public InsertOptionsBuilder mutate() {
|
||||
return new InsertOptionsBuilder(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@literal true} to apply {@code IF NOT EXISTS} to {@code INSERT} operations.
|
||||
*/
|
||||
@@ -81,6 +93,12 @@ public class InsertOptions extends WriteOptions {
|
||||
|
||||
private InsertOptionsBuilder() {}
|
||||
|
||||
private InsertOptionsBuilder(InsertOptions insertOptions) {
|
||||
|
||||
super(insertOptions);
|
||||
this.ifNotExists = insertOptions.ifNotExists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InsertOptionsBuilder consistencyLevel(com.datastax.driver.core.ConsistencyLevel consistencyLevel) {
|
||||
return (InsertOptionsBuilder) super.consistencyLevel(consistencyLevel);
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.cassandra.core;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.springframework.data.cassandra.core.cql.WriteOptions;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -30,6 +31,7 @@ import com.datastax.driver.core.policies.RetryPolicy;
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class UpdateOptions extends WriteOptions {
|
||||
|
||||
private static final UpdateOptions EMPTY = new UpdateOptionsBuilder().build();
|
||||
@@ -62,6 +64,16 @@ public class UpdateOptions extends WriteOptions {
|
||||
return new UpdateOptionsBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link UpdateOptionsBuilder} to mutate properties of this {@link UpdateOptions}.
|
||||
*
|
||||
* @return a new {@link UpdateOptionsBuilder} initialized with this {@link UpdateOptions}.
|
||||
*/
|
||||
@Override
|
||||
public UpdateOptionsBuilder mutate() {
|
||||
return new UpdateOptionsBuilder(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@literal true} to apply {@code IF EXISTS} to {@code UPDATE} operations.
|
||||
*/
|
||||
@@ -81,6 +93,12 @@ public class UpdateOptions extends WriteOptions {
|
||||
|
||||
private UpdateOptionsBuilder() {}
|
||||
|
||||
private UpdateOptionsBuilder(UpdateOptions updateOptions) {
|
||||
|
||||
super(updateOptions);
|
||||
this.ifExists = updateOptions.ifExists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateOptionsBuilder consistencyLevel(com.datastax.driver.core.ConsistencyLevel consistencyLevel) {
|
||||
return (UpdateOptionsBuilder) super.consistencyLevel(consistencyLevel);
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.cassandra.core.cql;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -32,6 +33,7 @@ import com.datastax.driver.core.policies.RetryPolicy;
|
||||
* @author David Webb
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@EqualsAndHashCode
|
||||
public class QueryOptions {
|
||||
|
||||
private static final QueryOptions EMPTY = QueryOptions.builder().build();
|
||||
@@ -93,6 +95,16 @@ public class QueryOptions {
|
||||
return new QueryOptionsBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link QueryOptionsBuilder} to mutate properties of this {@link QueryOptions}.
|
||||
*
|
||||
* @return a new {@link QueryOptionsBuilder} initialized with this {@link QueryOptions}.
|
||||
* @since 2.0
|
||||
*/
|
||||
public QueryOptionsBuilder mutate() {
|
||||
return new QueryOptionsBuilder(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the the driver {@link ConsistencyLevel}
|
||||
* @since 1.5
|
||||
@@ -156,6 +168,15 @@ public class QueryOptions {
|
||||
|
||||
QueryOptionsBuilder() {}
|
||||
|
||||
QueryOptionsBuilder(QueryOptions queryOptions) {
|
||||
|
||||
this.consistencyLevel = queryOptions.consistencyLevel;
|
||||
this.retryPolicy = queryOptions.retryPolicy;
|
||||
this.tracing = queryOptions.tracing;
|
||||
this.fetchSize = queryOptions.fetchSize;
|
||||
this.readTimeout = queryOptions.readTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link ConsistencyLevel} to use.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.cassandra.core.cql;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -32,6 +33,7 @@ import com.datastax.driver.core.policies.RetryPolicy;
|
||||
* @author Mark Paluch
|
||||
* @see QueryOptions
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class WriteOptions extends QueryOptions {
|
||||
|
||||
private static final WriteOptions EMPTY = new WriteOptionsBuilder().build();
|
||||
@@ -94,6 +96,17 @@ public class WriteOptions extends QueryOptions {
|
||||
return new WriteOptionsBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link WriteOptionsBuilder} to mutate properties of this {@link WriteOptions}.
|
||||
*
|
||||
* @return a new {@link WriteOptionsBuilder} initialized with this {@link WriteOptions}.
|
||||
* @since 2.0
|
||||
*/
|
||||
@Override
|
||||
public WriteOptionsBuilder mutate() {
|
||||
return new WriteOptionsBuilder(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the time to live, if set.
|
||||
*/
|
||||
@@ -102,7 +115,7 @@ public class WriteOptions extends QueryOptions {
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link QueryOptions}.
|
||||
* Builder for {@link WriteOptions}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 1.5
|
||||
@@ -113,6 +126,12 @@ public class WriteOptions extends QueryOptions {
|
||||
|
||||
protected WriteOptionsBuilder() {}
|
||||
|
||||
protected WriteOptionsBuilder(WriteOptions writeOptions) {
|
||||
|
||||
super(writeOptions);
|
||||
this.ttl = writeOptions.ttl;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#consistencyLevel(com.datastax.driver.core.ConsistencyLevel)
|
||||
|
||||
@@ -39,4 +39,18 @@ public class InsertOptionsUnitTests {
|
||||
assertThat(insertOptions.getTtl()).isEqualTo(Duration.ofSeconds(10));
|
||||
assertThat(insertOptions.isIfNotExists()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACASS-56
|
||||
public void buildInsertOptionsMutate() {
|
||||
|
||||
InsertOptions insertOptions = InsertOptions.builder() //
|
||||
.ttl(10) //
|
||||
.withIfNotExists() //
|
||||
.build();
|
||||
|
||||
InsertOptions mutated = insertOptions.mutate().ttl(Duration.ofSeconds(5)).build();
|
||||
|
||||
assertThat(mutated.getTtl()).isEqualTo(Duration.ofSeconds(5));
|
||||
assertThat(mutated.isIfNotExists()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,4 +39,18 @@ public class UpdateOptionsUnitTests {
|
||||
assertThat(updateOptions.getTtl()).isEqualTo(Duration.ofSeconds(10));
|
||||
assertThat(updateOptions.isIfExists()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACASS-56
|
||||
public void buildUpdateOptionsMutate() {
|
||||
|
||||
UpdateOptions updateOptions = UpdateOptions.builder() //
|
||||
.ttl(10) //
|
||||
.withIfExists() //
|
||||
.build();
|
||||
|
||||
UpdateOptions mutated = updateOptions.mutate().ttl(20).build();
|
||||
|
||||
assertThat(mutated.getTtl()).isEqualTo(Duration.ofSeconds(20));
|
||||
assertThat(mutated.isIfExists()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,4 +73,25 @@ public class QueryOptionsUnitTests {
|
||||
|
||||
assertThat(writeOptions.getRetryPolicy()).isEqualTo(DowngradingConsistencyRetryPolicy.INSTANCE);
|
||||
}
|
||||
|
||||
@Test // DATACASS-56
|
||||
public void buildQueryOptionsMutate() {
|
||||
|
||||
QueryOptions queryOptions = QueryOptions.builder() //
|
||||
.consistencyLevel(ConsistencyLevel.ANY) //
|
||||
.retryPolicy(FallthroughRetryPolicy.INSTANCE) //
|
||||
.readTimeout(1, TimeUnit.SECONDS)//
|
||||
.fetchSize(10)//
|
||||
.tracing(true)//
|
||||
.build(); //
|
||||
|
||||
QueryOptions mutated = queryOptions.mutate().readTimeout(Duration.ofSeconds(5)).build();
|
||||
|
||||
assertThat(mutated.getClass()).isEqualTo(QueryOptions.class);
|
||||
assertThat(mutated.getRetryPolicy()).isEqualTo(FallthroughRetryPolicy.INSTANCE);
|
||||
assertThat(mutated.getConsistencyLevel()).isEqualTo(ConsistencyLevel.ANY);
|
||||
assertThat(mutated.getReadTimeout()).isEqualTo(Duration.ofSeconds(5));
|
||||
assertThat(mutated.getFetchSize()).isEqualTo(10);
|
||||
assertThat(mutated.getTracing()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.datastax.driver.core.ConsistencyLevel;
|
||||
@@ -77,4 +78,26 @@ public class WriteOptionsUnitTests {
|
||||
|
||||
assertThat(writeOptions.getRetryPolicy()).isEqualTo(FallthroughRetryPolicy.INSTANCE);
|
||||
}
|
||||
|
||||
@Test // DATACASS-56
|
||||
public void buildWriteOptionsMutate() {
|
||||
|
||||
WriteOptions writeOptions = WriteOptions.builder() //
|
||||
.consistencyLevel(com.datastax.driver.core.ConsistencyLevel.ANY) //
|
||||
.ttl(123) //
|
||||
.retryPolicy(FallthroughRetryPolicy.INSTANCE) //
|
||||
.readTimeout(1)//
|
||||
.fetchSize(10)//
|
||||
.withTracing()//
|
||||
.build(); //
|
||||
|
||||
WriteOptions mutated = writeOptions.mutate().retryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).build();
|
||||
|
||||
assertThat(mutated.getTtl()).isEqualTo(Duration.ofSeconds(123));
|
||||
assertThat(mutated.getRetryPolicy()).isEqualTo(DowngradingConsistencyRetryPolicy.INSTANCE);
|
||||
assertThat(mutated.getConsistencyLevel()).isEqualTo(ConsistencyLevel.ANY);
|
||||
assertThat(mutated.getReadTimeout()).isEqualTo(Duration.ofMillis(1));
|
||||
assertThat(mutated.getFetchSize()).isEqualTo(10);
|
||||
assertThat(mutated.getTracing()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user