Extend supported fields for query options.
We now accept idempotent, routingKey, and routingKeyspace options. Closes #1220
This commit is contained in:
committed by
Mark Paluch
parent
68ddb12e86
commit
367b6ac31c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019-2021 the original author or authors.
|
||||
* Copyright 2019-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -35,6 +36,7 @@ import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
* @since 2.2
|
||||
*/
|
||||
public class DeleteOptions extends WriteOptions {
|
||||
@@ -48,10 +50,11 @@ public class DeleteOptions extends WriteOptions {
|
||||
private DeleteOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
|
||||
@Nullable CqlIdentifier keyspace, @Nullable Integer pageSize, @Nullable ConsistencyLevel serialConsistencyLevel,
|
||||
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifExists,
|
||||
@Nullable Filter ifCondition) {
|
||||
@Nullable Filter ifCondition, @Nullable Boolean idempotent, @Nullable CqlIdentifier routingKeyspace,
|
||||
@Nullable ByteBuffer routingKey) {
|
||||
|
||||
super(consistencyLevel, executionProfileResolver, keyspace, pageSize, serialConsistencyLevel, timeout, ttl,
|
||||
timestamp, tracing);
|
||||
timestamp, tracing, idempotent, routingKeyspace, routingKey);
|
||||
|
||||
this.ifExists = ifExists;
|
||||
this.ifCondition = ifCondition;
|
||||
@@ -289,6 +292,16 @@ public class DeleteOptions extends WriteOptions {
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.WriteOptions.WriteOptionsBuilder#idempotent(boolean)
|
||||
*/
|
||||
@Override
|
||||
public DeleteOptionsBuilder idempotent(boolean idempotent) {
|
||||
|
||||
super.idempotent(idempotent);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.WriteOptions.WriteOptionsBuilder#ttl(int)
|
||||
*/
|
||||
@@ -318,6 +331,25 @@ public class DeleteOptions extends WriteOptions {
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier)
|
||||
*/
|
||||
@Override
|
||||
public DeleteOptionsBuilder routingKeyspace(CqlIdentifier routingKeyspace) {
|
||||
|
||||
super.routingKeyspace(routingKeyspace);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(java.nio.ByteBuffer)
|
||||
*/
|
||||
@Override
|
||||
public DeleteOptionsBuilder routingKey(ByteBuffer routingKey) {
|
||||
|
||||
super.routingKey(routingKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use light-weight transactions by applying {@code IF EXISTS}. Replaces a previous {@link #ifCondition(Filter)}.
|
||||
@@ -382,7 +414,7 @@ public class DeleteOptions extends WriteOptions {
|
||||
|
||||
return new DeleteOptions(this.consistencyLevel, this.executionProfileResolver, this.keyspace, this.pageSize,
|
||||
this.serialConsistencyLevel, this.timeout, this.ttl, this.timestamp, this.tracing, this.ifExists,
|
||||
this.ifCondition);
|
||||
this.ifCondition, this.idempotent, this.routingKeyspace, this.routingKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2021 the original author or authors.
|
||||
* Copyright 2017-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -32,6 +33,7 @@ import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||
* @author Mark Paluch
|
||||
* @author Lukasz Antoniak
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
* @since 2.0
|
||||
*/
|
||||
public class InsertOptions extends WriteOptions {
|
||||
@@ -45,10 +47,11 @@ public class InsertOptions extends WriteOptions {
|
||||
private InsertOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
|
||||
@Nullable CqlIdentifier keyspace, @Nullable Integer pageSize, @Nullable ConsistencyLevel serialConsistencyLevel,
|
||||
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifNotExists,
|
||||
boolean insertNulls) {
|
||||
boolean insertNulls, @Nullable Boolean idempotent, @Nullable CqlIdentifier routingKeyspace,
|
||||
@Nullable ByteBuffer routingKey) {
|
||||
|
||||
super(consistencyLevel, executionProfileResolver, keyspace, pageSize, serialConsistencyLevel, timeout, ttl,
|
||||
timestamp, tracing);
|
||||
timestamp, tracing, idempotent, routingKeyspace, routingKey);
|
||||
|
||||
this.ifNotExists = ifNotExists;
|
||||
this.insertNulls = insertNulls;
|
||||
@@ -317,6 +320,36 @@ public class InsertOptions extends WriteOptions {
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#idempotent(boolean)
|
||||
*/
|
||||
@Override
|
||||
public InsertOptionsBuilder idempotent(boolean idempotent) {
|
||||
|
||||
super.idempotent(idempotent);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier)
|
||||
*/
|
||||
@Override
|
||||
public InsertOptionsBuilder routingKeyspace(CqlIdentifier routingKeyspace) {
|
||||
|
||||
super.routingKeyspace(routingKeyspace);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(java.nio.ByteBuffer)
|
||||
*/
|
||||
@Override
|
||||
public InsertOptionsBuilder routingKey(ByteBuffer routingKey) {
|
||||
|
||||
super.routingKey(routingKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use light-weight transactions by applying {@code IF NOT EXISTS}.
|
||||
*
|
||||
@@ -375,7 +408,7 @@ public class InsertOptions extends WriteOptions {
|
||||
public InsertOptions build() {
|
||||
return new InsertOptions(this.consistencyLevel, this.executionProfileResolver, this.keyspace, this.pageSize,
|
||||
this.serialConsistencyLevel, this.timeout, this.ttl, this.timestamp, this.tracing, this.ifNotExists,
|
||||
this.insertNulls);
|
||||
this.insertNulls, this.idempotent, this.routingKeyspace, this.routingKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2021 the original author or authors.
|
||||
* Copyright 2017-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -36,6 +37,7 @@ import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||
* @author Mark Paluch
|
||||
* @author Lukasz Antoniak
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
* @since 2.0
|
||||
*/
|
||||
public class UpdateOptions extends WriteOptions {
|
||||
@@ -49,10 +51,11 @@ public class UpdateOptions extends WriteOptions {
|
||||
private UpdateOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
|
||||
@Nullable CqlIdentifier keyspace, @Nullable Integer pageSize, @Nullable ConsistencyLevel serialConsistencyLevel,
|
||||
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, boolean ifExists,
|
||||
@Nullable Filter ifCondition) {
|
||||
@Nullable Filter ifCondition, @Nullable Boolean idempotent, @Nullable CqlIdentifier routingKeyspace,
|
||||
@Nullable ByteBuffer routingKey) {
|
||||
|
||||
super(consistencyLevel, executionProfileResolver, keyspace, pageSize, serialConsistencyLevel, timeout, ttl,
|
||||
timestamp, tracing);
|
||||
timestamp, tracing, idempotent, routingKeyspace, routingKey);
|
||||
|
||||
this.ifExists = ifExists;
|
||||
this.ifCondition = ifCondition;
|
||||
@@ -296,6 +299,16 @@ public class UpdateOptions extends WriteOptions {
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.WriteOptions.WriteOptionsBuilder#idempotent(boolean)
|
||||
*/
|
||||
@Override
|
||||
public UpdateOptionsBuilder idempotent(boolean idempotent) {
|
||||
|
||||
super.idempotent(idempotent);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.WriteOptions.WriteOptionsBuilder#ttl(int)
|
||||
*/
|
||||
@@ -325,6 +338,26 @@ public class UpdateOptions extends WriteOptions {
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier)
|
||||
*/
|
||||
@Override
|
||||
public UpdateOptionsBuilder routingKeyspace(CqlIdentifier routingKeyspace) {
|
||||
|
||||
super.routingKeyspace(routingKeyspace);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(java.nio.ByteBuffer)
|
||||
*/
|
||||
@Override
|
||||
public UpdateOptionsBuilder routingKey(ByteBuffer routingKey) {
|
||||
|
||||
super.routingKey(routingKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use light-weight transactions by applying {@code IF EXISTS}. Replaces a previous {@link #ifCondition(Filter)}.
|
||||
*
|
||||
@@ -389,7 +422,7 @@ public class UpdateOptions extends WriteOptions {
|
||||
public UpdateOptions build() {
|
||||
return new UpdateOptions(this.consistencyLevel, this.executionProfileResolver, this.keyspace, this.pageSize,
|
||||
this.serialConsistencyLevel, this.timeout, this.ttl, this.timestamp, this.tracing, this.ifExists,
|
||||
this.ifCondition);
|
||||
this.ifCondition, this.idempotent, this.routingKeyspace, this.routingKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core.cql;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -34,6 +35,7 @@ import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
|
||||
* @author David Webb
|
||||
* @author Mark Paluch
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
*/
|
||||
public class QueryOptions {
|
||||
|
||||
@@ -53,9 +55,16 @@ public class QueryOptions {
|
||||
|
||||
private final @Nullable Boolean tracing;
|
||||
|
||||
private final @Nullable Boolean idempotent;
|
||||
|
||||
private final @Nullable CqlIdentifier routingKeyspace;
|
||||
|
||||
private final @Nullable ByteBuffer routingKey;
|
||||
|
||||
protected QueryOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
|
||||
@Nullable CqlIdentifier keyspace, @Nullable Integer pageSize, @Nullable ConsistencyLevel serialConsistencyLevel,
|
||||
Duration timeout, @Nullable Boolean tracing) {
|
||||
Duration timeout, @Nullable Boolean tracing, @Nullable Boolean idempotent, @Nullable CqlIdentifier routingKeyspace,
|
||||
@Nullable ByteBuffer routingKey) {
|
||||
|
||||
this.consistencyLevel = consistencyLevel;
|
||||
this.executionProfileResolver = executionProfileResolver;
|
||||
@@ -64,6 +73,9 @@ public class QueryOptions {
|
||||
this.serialConsistencyLevel = serialConsistencyLevel;
|
||||
this.timeout = timeout;
|
||||
this.tracing = tracing;
|
||||
this.idempotent = idempotent;
|
||||
this.routingKey = routingKey;
|
||||
this.routingKeyspace = routingKeyspace;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,6 +172,16 @@ public class QueryOptions {
|
||||
return this.tracing;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether query is idempotent. May be {@literal null} if not set.
|
||||
* @since 3.3.2
|
||||
* @see com.datastax.oss.driver.api.core.cql.Statement#setIdempotent(Boolean)
|
||||
*/
|
||||
@Nullable
|
||||
protected Boolean isIdempotent() {
|
||||
return this.idempotent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the keyspace associated with the query. If it is {@literal null}, it means that either keyspace configured
|
||||
* on the statement or from the {@link CqlSession} will be used.
|
||||
@@ -170,6 +192,26 @@ public class QueryOptions {
|
||||
return keyspace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the keyspace used for token-aware routing. May be {@literal null} if token-aware routing is disabled.
|
||||
* @since 3.3.2
|
||||
* @see com.datastax.oss.driver.api.core.cql.Statement#setRoutingKeyspace(CqlIdentifier)
|
||||
*/
|
||||
@Nullable
|
||||
protected CqlIdentifier getRoutingKeyspace() {
|
||||
return this.routingKeyspace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the key used for token-aware routing. May be {@literal null} if token-aware routing is disabled.
|
||||
* @since 3.3.2
|
||||
* @see com.datastax.oss.driver.api.core.cql.Statement#setRoutingKey(ByteBuffer)
|
||||
*/
|
||||
@Nullable
|
||||
protected ByteBuffer getRoutingKey() {
|
||||
return this.routingKey;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
@@ -211,6 +253,18 @@ public class QueryOptions {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(idempotent, options.idempotent)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(routingKeyspace, options.routingKeyspace)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(routingKey, options.routingKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ObjectUtils.nullSafeEquals(keyspace, options.keyspace);
|
||||
}
|
||||
|
||||
@@ -226,7 +280,10 @@ public class QueryOptions {
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(serialConsistencyLevel);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(timeout);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(tracing);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(idempotent);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(keyspace);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(routingKeyspace);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(routingKey);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -244,6 +301,8 @@ public class QueryOptions {
|
||||
|
||||
protected @Nullable CqlIdentifier keyspace;
|
||||
|
||||
protected @Nullable CqlIdentifier routingKeyspace;
|
||||
|
||||
protected @Nullable Integer pageSize;
|
||||
|
||||
protected @Nullable ConsistencyLevel serialConsistencyLevel;
|
||||
@@ -252,6 +311,10 @@ public class QueryOptions {
|
||||
|
||||
protected @Nullable Boolean tracing;
|
||||
|
||||
protected @Nullable Boolean idempotent;
|
||||
|
||||
protected @Nullable ByteBuffer routingKey;
|
||||
|
||||
QueryOptionsBuilder() {}
|
||||
|
||||
QueryOptionsBuilder(QueryOptions queryOptions) {
|
||||
@@ -263,6 +326,9 @@ public class QueryOptions {
|
||||
this.serialConsistencyLevel = queryOptions.serialConsistencyLevel;
|
||||
this.timeout = queryOptions.timeout;
|
||||
this.tracing = queryOptions.tracing;
|
||||
this.idempotent = queryOptions.idempotent;
|
||||
this.routingKeyspace = queryOptions.routingKeyspace;
|
||||
this.routingKey = queryOptions.routingKey;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -471,14 +537,54 @@ public class QueryOptions {
|
||||
return tracing(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set query execution idempotency.
|
||||
*
|
||||
* @param idempotent {@literal true} to mark the query as idempotent.
|
||||
* @return {@code this} {@link QueryOptionsBuilder}
|
||||
*/
|
||||
public QueryOptionsBuilder idempotent(boolean idempotent) {
|
||||
|
||||
this.idempotent = idempotent;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set query routing keyspace.
|
||||
*
|
||||
* @param routingKeyspace the routing keyspace to use for token-aware routing. Can be {@literal null}.
|
||||
* @return {@code this} {@link QueryOptionsBuilder}
|
||||
*/
|
||||
public QueryOptionsBuilder routingKeyspace(CqlIdentifier routingKeyspace) {
|
||||
|
||||
this.routingKeyspace = routingKeyspace;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set query routing key.
|
||||
*
|
||||
* @param routingKey the routing key to use for token-aware routing. Can be {@literal null}.
|
||||
* @return {@code this} {@link QueryOptionsBuilder}
|
||||
*/
|
||||
public QueryOptionsBuilder routingKey(ByteBuffer routingKey) {
|
||||
|
||||
this.routingKey = routingKey;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a new {@link QueryOptions} with the configured values.
|
||||
*
|
||||
* @return a new {@link QueryOptions} with the configured values
|
||||
*/
|
||||
public QueryOptions build() {
|
||||
return new QueryOptions(this.consistencyLevel, this.executionProfileResolver, this.keyspace, this.pageSize,
|
||||
this.serialConsistencyLevel, this.timeout, this.tracing);
|
||||
return new QueryOptions(this.consistencyLevel, this.executionProfileResolver, this.keyspace,
|
||||
this.pageSize, this.serialConsistencyLevel, this.timeout, this.tracing, this.idempotent,
|
||||
this.routingKeyspace, this.routingKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2021 the original author or authors.
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,6 +35,7 @@ import com.datastax.oss.driver.api.querybuilder.update.UpdateStart;
|
||||
* @author Mark Paluch
|
||||
* @author Lukasz Antoniak
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class QueryOptionsUtil {
|
||||
@@ -77,6 +78,19 @@ public abstract class QueryOptionsUtil {
|
||||
// statement wrapped in the conditional null check to avoid additional garbage and added GC pressure.
|
||||
statementToUse = statementToUse.setTracing(Boolean.TRUE.equals(queryOptions.getTracing()));
|
||||
}
|
||||
|
||||
if (queryOptions.isIdempotent() != null) {
|
||||
statementToUse = statementToUse.setIdempotent(queryOptions.isIdempotent());
|
||||
}
|
||||
|
||||
if (queryOptions.getRoutingKeyspace() != null) {
|
||||
statementToUse = statementToUse.setRoutingKeyspace(queryOptions.getRoutingKeyspace());
|
||||
}
|
||||
|
||||
if (queryOptions.getRoutingKey() != null) {
|
||||
statementToUse = statement.setRoutingKey(queryOptions.getRoutingKey());
|
||||
}
|
||||
|
||||
if (queryOptions.getKeyspace() != null) {
|
||||
if (statementToUse instanceof BoundStatement) {
|
||||
throw new IllegalArgumentException("Keyspace cannot be set for a BoundStatement");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core.cql;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -34,6 +35,7 @@ import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||
* @author Mark Paluch
|
||||
* @author Lukasz Antoniak
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
* @see QueryOptions
|
||||
*/
|
||||
public class WriteOptions extends QueryOptions {
|
||||
@@ -46,10 +48,10 @@ public class WriteOptions extends QueryOptions {
|
||||
|
||||
protected WriteOptions(@Nullable ConsistencyLevel consistencyLevel, ExecutionProfileResolver executionProfileResolver,
|
||||
@Nullable CqlIdentifier keyspace, @Nullable Integer pageSize, @Nullable ConsistencyLevel serialConsistencyLevel,
|
||||
Duration timeout, Duration ttl,
|
||||
@Nullable Long timestamp, @Nullable Boolean tracing) {
|
||||
Duration timeout, Duration ttl, @Nullable Long timestamp, @Nullable Boolean tracing, @Nullable Boolean idempotent,
|
||||
@Nullable CqlIdentifier routingKeyspace, @Nullable ByteBuffer routingKey) {
|
||||
|
||||
super(consistencyLevel, executionProfileResolver, keyspace, pageSize, serialConsistencyLevel, timeout, tracing);
|
||||
super(consistencyLevel, executionProfileResolver, keyspace, pageSize, serialConsistencyLevel, timeout, tracing, idempotent, routingKeyspace, routingKey);
|
||||
|
||||
this.ttl = ttl;
|
||||
this.timestamp = timestamp;
|
||||
@@ -285,6 +287,36 @@ public class WriteOptions extends QueryOptions {
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#idempotent(boolean)
|
||||
*/
|
||||
@Override
|
||||
public WriteOptionsBuilder idempotent(boolean idempotent) {
|
||||
|
||||
super.idempotent(idempotent);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier)
|
||||
*/
|
||||
@Override
|
||||
public WriteOptionsBuilder routingKeyspace(CqlIdentifier routingKeyspace) {
|
||||
|
||||
super.routingKeyspace(routingKeyspace);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.cql.QueryOptions.QueryOptionsBuilder#routingKeyspace(java.nio.ByteBuffer)
|
||||
*/
|
||||
@Override
|
||||
public WriteOptionsBuilder routingKey(ByteBuffer routingKey) {
|
||||
|
||||
super.routingKey(routingKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time to live in seconds for write operations.
|
||||
*
|
||||
@@ -357,7 +389,8 @@ public class WriteOptions extends QueryOptions {
|
||||
*/
|
||||
public WriteOptions build() {
|
||||
return new WriteOptions(this.consistencyLevel, this.executionProfileResolver, this.keyspace, this.pageSize,
|
||||
this.serialConsistencyLevel, this.timeout, this.ttl, this.timestamp, this.tracing);
|
||||
this.serialConsistencyLevel, this.timeout, this.ttl, this.timestamp, this.tracing, this.idempotent,
|
||||
this.routingKeyspace, this.routingKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2021 the original author or authors.
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.cassandra.core.cql;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -29,6 +30,7 @@ import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
*/
|
||||
class QueryOptionsUnitTests {
|
||||
|
||||
@@ -41,6 +43,9 @@ class QueryOptionsUnitTests {
|
||||
.pageSize(10) //
|
||||
.tracing(true) //
|
||||
.keyspace(CqlIdentifier.fromCql("ks1")) //
|
||||
.idempotent(true)
|
||||
.routingKeyspace(CqlIdentifier.fromCql("rksl"))
|
||||
.routingKey(ByteBuffer.allocate(1))
|
||||
.build();
|
||||
|
||||
assertThat(queryOptions.getClass()).isEqualTo(QueryOptions.class);
|
||||
@@ -49,6 +54,9 @@ class QueryOptionsUnitTests {
|
||||
assertThat(queryOptions.getPageSize()).isEqualTo(10);
|
||||
assertThat(queryOptions.getTracing()).isTrue();
|
||||
assertThat(queryOptions.getKeyspace()).isEqualTo(CqlIdentifier.fromCql("ks1"));
|
||||
assertThat(queryOptions.isIdempotent()).isEqualTo(true);
|
||||
assertThat(queryOptions.getRoutingKeyspace()).isEqualTo(CqlIdentifier.fromCql("rksl"));
|
||||
assertThat(queryOptions.getRoutingKey()).isEqualTo(ByteBuffer.allocate(1));
|
||||
}
|
||||
|
||||
@Test // DATACASS-56
|
||||
@@ -60,6 +68,9 @@ class QueryOptionsUnitTests {
|
||||
.pageSize(10) //
|
||||
.tracing(true) //
|
||||
.keyspace(CqlIdentifier.fromCql("ks1")) //
|
||||
.idempotent(true)
|
||||
.routingKeyspace(CqlIdentifier.fromCql("rksl"))
|
||||
.routingKey(ByteBuffer.allocate(1))
|
||||
.build();
|
||||
|
||||
QueryOptions mutated = queryOptions.mutate().timeout(Duration.ofSeconds(5)).build();
|
||||
@@ -71,5 +82,8 @@ class QueryOptionsUnitTests {
|
||||
assertThat(mutated.getPageSize()).isEqualTo(10);
|
||||
assertThat(mutated.getTracing()).isTrue();
|
||||
assertThat(mutated.getKeyspace()).isEqualTo(CqlIdentifier.fromCql("ks1"));
|
||||
assertThat(mutated.isIdempotent()).isEqualTo(true);
|
||||
assertThat(mutated.getRoutingKeyspace()).isEqualTo(CqlIdentifier.fromCql("rksl"));
|
||||
assertThat(mutated.getRoutingKey()).isEqualTo(ByteBuffer.allocate(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2021 the original author or authors.
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.cassandra.core.cql;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -38,6 +39,7 @@ import com.datastax.oss.driver.api.core.cql.SimpleStatement;
|
||||
* @author John Blum
|
||||
* @author Mark Paluch
|
||||
* @author Tomasz Lelek
|
||||
* @author Sam Lightfoot
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class QueryOptionsUtilUnitTests {
|
||||
@@ -82,11 +84,17 @@ class QueryOptionsUtilUnitTests {
|
||||
when(simpleStatement.setPageSize(anyInt())).thenReturn(simpleStatement);
|
||||
when(simpleStatement.setTimeout(any())).thenReturn(simpleStatement);
|
||||
when(simpleStatement.setTracing(anyBoolean())).thenReturn(simpleStatement);
|
||||
when(simpleStatement.setIdempotent(anyBoolean())).thenReturn(simpleStatement);
|
||||
when(simpleStatement.setRoutingKeyspace(any(CqlIdentifier.class))).thenReturn(simpleStatement);
|
||||
when(simpleStatement.setRoutingKey(any(ByteBuffer.class))).thenReturn(simpleStatement);
|
||||
|
||||
QueryOptions queryOptions = QueryOptions.builder() //
|
||||
.pageSize(10) //
|
||||
.readTimeout(1, TimeUnit.MINUTES) //
|
||||
.withTracing() //
|
||||
.idempotent(true)
|
||||
.routingKeyspace(CqlIdentifier.fromCql("routing_ks"))
|
||||
.routingKey(ByteBuffer.allocate(1))
|
||||
.build();
|
||||
|
||||
QueryOptionsUtil.addQueryOptions(simpleStatement, queryOptions);
|
||||
@@ -94,6 +102,9 @@ class QueryOptionsUtilUnitTests {
|
||||
verify(simpleStatement).setTimeout(Duration.ofMinutes(1));
|
||||
verify(simpleStatement).setPageSize(10);
|
||||
verify(simpleStatement).setTracing(true);
|
||||
verify(simpleStatement).setIdempotent(true);
|
||||
verify(simpleStatement).setRoutingKeyspace(CqlIdentifier.fromCql("routing_ks"));
|
||||
verify(simpleStatement).setRoutingKey(ByteBuffer.allocate(1));
|
||||
}
|
||||
|
||||
@Test // DATACASS-767
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2021 the original author or authors.
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.cassandra.core.cql;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -31,6 +32,7 @@ import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
|
||||
* Unit tests for {@link WriteOptions}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Sam Lightfoot
|
||||
*/
|
||||
class WriteOptionsUnitTests {
|
||||
|
||||
@@ -44,7 +46,11 @@ class WriteOptionsUnitTests {
|
||||
.readTimeout(1) //
|
||||
.pageSize(10) //
|
||||
.withTracing() //
|
||||
.keyspace(CqlIdentifier.fromCql("my_keyspace")).build();
|
||||
.keyspace(CqlIdentifier.fromCql("my_keyspace"))
|
||||
.idempotent(true)
|
||||
.routingKeyspace(CqlIdentifier.fromCql("routing_keyspace"))
|
||||
.routingKey(ByteBuffer.allocate(1))
|
||||
.build();
|
||||
|
||||
assertThat(writeOptions.getTtl()).isEqualTo(Duration.ofSeconds(123));
|
||||
assertThat(writeOptions.getTimestamp()).isEqualTo(1519000753);
|
||||
@@ -53,6 +59,10 @@ class WriteOptionsUnitTests {
|
||||
assertThat(writeOptions.getPageSize()).isEqualTo(10);
|
||||
assertThat(writeOptions.getTracing()).isTrue();
|
||||
assertThat(writeOptions.getKeyspace()).isEqualTo(CqlIdentifier.fromCql("my_keyspace"));
|
||||
assertThat(writeOptions.isIdempotent()).isEqualTo(true);
|
||||
assertThat(writeOptions.getRoutingKeyspace()).isEqualTo(
|
||||
CqlIdentifier.fromCql("routing_keyspace"));
|
||||
assertThat(writeOptions.getRoutingKey()).isEqualTo(ByteBuffer.allocate(1));
|
||||
}
|
||||
|
||||
@Test // DATACASS-202
|
||||
@@ -76,6 +86,9 @@ class WriteOptionsUnitTests {
|
||||
.readTimeout(1) //
|
||||
.pageSize(10) //
|
||||
.withTracing() //
|
||||
.idempotent(true)
|
||||
.routingKeyspace(CqlIdentifier.fromCql("routing_keyspace"))
|
||||
.routingKey(ByteBuffer.allocate(1))
|
||||
.build();
|
||||
|
||||
WriteOptions mutated = writeOptions.mutate().timeout(Duration.ofMillis(100)).build();
|
||||
@@ -88,5 +101,9 @@ class WriteOptionsUnitTests {
|
||||
assertThat(mutated.getTimeout()).isEqualTo(Duration.ofMillis(100));
|
||||
assertThat(mutated.getPageSize()).isEqualTo(10);
|
||||
assertThat(mutated.getTracing()).isTrue();
|
||||
assertThat(writeOptions.isIdempotent()).isEqualTo(true);
|
||||
assertThat(writeOptions.getRoutingKeyspace()).isEqualTo(
|
||||
CqlIdentifier.fromCql("routing_keyspace"));
|
||||
assertThat(writeOptions.getRoutingKey()).isEqualTo(ByteBuffer.allocate(1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user