Skip to content

Commit f06b1bb

Browse files
committed
Replace supportsKeyspaceOnQuery in favor of supports(ProtocolFeature)
1 parent 12409d3 commit f06b1bb

File tree

8 files changed

+38
-15
lines changed

8 files changed

+38
-15
lines changed

driver-core/src/main/java/com/datastax/driver/core/Cluster.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.datastax.driver.core.exceptions.*;
1919
import com.datastax.driver.core.policies.*;
20+
import com.datastax.driver.core.ProtocolVersion.ProtocolFeature;
2021
import com.datastax.driver.core.utils.MoreFutures;
2122
import com.google.common.annotations.VisibleForTesting;
2223
import com.google.common.base.Functions;
@@ -2289,7 +2290,7 @@ private Connection prepareAllQueries(Host host, Connection reusedConnection) thr
22892290
// Empty string mean no particular keyspace to set
22902291
// Optimization: Only change keyspace for older protocol versions as newer protocols allow
22912292
// specifying keyspace on prepared statement.
2292-
if (!protocolVersion().supportsKeyspaceOnQuery() && !keyspace.isEmpty())
2293+
if (!protocolVersion().supports(ProtocolFeature.KEYSPACE_ON_QUERY) && !keyspace.isEmpty())
22932294
connection.setKeyspace(keyspace);
22942295

22952296
List<Connection.Future> futures = new ArrayList<Connection.Future>(preparedQueries.size());

driver-core/src/main/java/com/datastax/driver/core/ProtocolVersion.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,17 @@ public ProtocolVersion getLowerSupported() {
8282
}
8383

8484
/**
85-
* Determines whether or not this version supports setting keyspace on a per-query basis.
85+
* Determines whether or not this version supports the given feature.
8686
*
87+
* @param feature the feature to test against this version.
8788
* @return true if supported, false otherwise.
8889
*/
89-
public boolean supportsKeyspaceOnQuery() {
90-
return this == V5;
90+
public boolean supports(ProtocolFeature feature) {
91+
switch (feature) {
92+
case KEYSPACE_ON_QUERY:
93+
return this == V5;
94+
}
95+
return false;
9196
}
9297

9398
private static final Map<Integer, ProtocolVersion> INT_TO_VERSION;
@@ -113,4 +118,15 @@ public static ProtocolVersion fromInt(int i) {
113118
throw new IllegalArgumentException("No protocol version matching integer version " + i);
114119
return version;
115120
}
121+
122+
/**
123+
* A listing of features that may or not apply to a given {@link ProtocolVersion}. Used by
124+
* {@link ProtocolVersion#supports} to communicate whether features are supported by a given version.
125+
*/
126+
public enum ProtocolFeature {
127+
/**
128+
* Whether or not a version supports setting keyspace on a per-query basis.
129+
*/
130+
KEYSPACE_ON_QUERY
131+
}
116132
}

driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.codahale.metrics.Timer;
1919
import com.datastax.driver.core.exceptions.*;
20+
import com.datastax.driver.core.ProtocolVersion.ProtocolFeature;
2021
import com.datastax.driver.core.policies.RetryPolicy;
2122
import com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type;
2223
import com.datastax.driver.core.policies.SpeculativeExecutionPolicy.SpeculativeExecutionPlan;
@@ -651,7 +652,7 @@ public void onSet(Connection connection, Message.Response response, long latency
651652
String prepareKeyspace = toPrepare.getQueryKeyspace();
652653
// Only allow preparing with a different keyspace than configured connection keyspace if
653654
// protocol supports it.
654-
if (!err.serverProtocolVersion.supportsKeyspaceOnQuery()
655+
if (!err.serverProtocolVersion.supports(ProtocolFeature.KEYSPACE_ON_QUERY)
655656
&& prepareKeyspace != null
656657
&& (currentKeyspace == null || !currentKeyspace.equals(prepareKeyspace))) {
657658
// This shouldn't happen in normal use, because a user shouldn't try to execute

driver-core/src/main/java/com/datastax/driver/core/Requests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datastax.driver.core;
1717

18+
import com.datastax.driver.core.ProtocolVersion.ProtocolFeature;
1819
import com.google.common.base.Preconditions;
1920
import com.google.common.collect.ImmutableMap;
2021
import io.netty.buffer.ByteBuf;
@@ -378,7 +379,7 @@ void encode(ByteBuf dest, ProtocolVersion version) {
378379
CBUtil.writeConsistencyLevel(serialConsistency, dest);
379380
if (version.compareTo(ProtocolVersion.V3) >= 0 && flags.contains(QueryFlag.DEFAULT_TIMESTAMP))
380381
dest.writeLong(defaultTimestamp);
381-
if (version.supportsKeyspaceOnQuery() && flags.contains(QueryFlag.KEYSPACE))
382+
if (version.supports(ProtocolFeature.KEYSPACE_ON_QUERY) && flags.contains(QueryFlag.KEYSPACE))
382383
CBUtil.writeString(keyspace, dest);
383384
break;
384385
default:
@@ -416,7 +417,7 @@ int encodedSize(ProtocolVersion version) {
416417
size += CBUtil.sizeOfConsistencyLevel(serialConsistency);
417418
if (version == ProtocolVersion.V3 && flags.contains(QueryFlag.DEFAULT_TIMESTAMP))
418419
size += 8;
419-
if (version.supportsKeyspaceOnQuery() && flags.contains(QueryFlag.KEYSPACE))
420+
if (version.supports(ProtocolFeature.KEYSPACE_ON_QUERY) && flags.contains(QueryFlag.KEYSPACE))
420421
size += CBUtil.sizeOfString(keyspace);
421422
return size;
422423
default:
@@ -560,7 +561,7 @@ void encode(ByteBuf dest, ProtocolVersion version) {
560561
CBUtil.writeConsistencyLevel(serialConsistency, dest);
561562
if (flags.contains(QueryFlag.DEFAULT_TIMESTAMP))
562563
dest.writeLong(defaultTimestamp);
563-
if (version.supportsKeyspaceOnQuery() && flags.contains(QueryFlag.KEYSPACE))
564+
if (version.supports(ProtocolFeature.KEYSPACE_ON_QUERY) && flags.contains(QueryFlag.KEYSPACE))
564565
CBUtil.writeString(keyspace, dest);
565566
break;
566567
default:
@@ -582,7 +583,7 @@ int encodedSize(ProtocolVersion version) {
582583
size += CBUtil.sizeOfConsistencyLevel(serialConsistency);
583584
if (flags.contains(QueryFlag.DEFAULT_TIMESTAMP))
584585
size += 8;
585-
if (version.supportsKeyspaceOnQuery() && flags.contains(QueryFlag.KEYSPACE))
586+
if (version.supports(ProtocolFeature.KEYSPACE_ON_QUERY) && flags.contains(QueryFlag.KEYSPACE))
586587
size += CBUtil.sizeOfString(keyspace);
587588
return size;
588589
default:
@@ -605,7 +606,7 @@ static class Prepare extends Message.Request {
605606
public void encode(Prepare msg, ByteBuf dest, ProtocolVersion version) {
606607
CBUtil.writeLongString(msg.query, dest);
607608

608-
if (version.supportsKeyspaceOnQuery()) {
609+
if (version.supports(ProtocolFeature.KEYSPACE_ON_QUERY)) {
609610
// if keyspace is present write 0x1 for prepare flags.
610611
if (msg.keyspace != null) {
611612
dest.writeInt(0x01);
@@ -620,7 +621,7 @@ public void encode(Prepare msg, ByteBuf dest, ProtocolVersion version) {
620621
public int encodedSize(Prepare msg, ProtocolVersion version) {
621622
int size = CBUtil.sizeOfLongString(msg.query);
622623

623-
if (version.supportsKeyspaceOnQuery() && msg.keyspace != null)
624+
if (version.supports(ProtocolFeature.KEYSPACE_ON_QUERY) && msg.keyspace != null)
624625
size += CBUtil.sizeOfString(msg.keyspace);
625626
return size;
626627
}

driver-core/src/main/java/com/datastax/driver/core/SessionManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.datastax.driver.core;
1717

1818
import com.datastax.driver.core.Message.Response;
19+
import com.datastax.driver.core.ProtocolVersion.ProtocolFeature;
1920
import com.datastax.driver.core.exceptions.DriverInternalError;
2021
import com.datastax.driver.core.exceptions.InvalidQueryException;
2122
import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
@@ -214,7 +215,7 @@ public ListenableFuture<PreparedStatement> apply(Response response) {
214215
Responses.Result.Prepared pmsg = (Responses.Result.Prepared) rm;
215216
String keyspaceToUse = poolsState.keyspace;
216217
if (keyspace != null && !Metadata.handleId(keyspace).equals(keyspaceToUse)) {
217-
if (!cluster.manager.protocolVersion().supportsKeyspaceOnQuery()) {
218+
if (!cluster.manager.protocolVersion().supports(ProtocolFeature.KEYSPACE_ON_QUERY)) {
218219
throw new UnsupportedFeatureException(cluster.manager.protocolVersion(), String.format(
219220
"Statement uses keyspace '%s' which is not the same as the" +
220221
" session keyspace '%s'.", Metadata.handleId(keyspace), poolsState.keyspace));

driver-core/src/test/java/com/datastax/driver/core/BatchStatementTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datastax.driver.core;
1717

18+
import com.datastax.driver.core.ProtocolVersion.ProtocolFeature;
1819
import com.datastax.driver.core.exceptions.InvalidQueryException;
1920
import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
2021
import com.datastax.driver.core.utils.CassandraVersion;
@@ -122,7 +123,7 @@ public void should_use_keyspace_if_set_on_batch_statement_same_ks_on_session() {
122123
@Test(groups = "short", expectedExceptions = InvalidQueryException.class)
123124
public void should_not_use_keyspace_if_set_and_protocol_does_not_support() {
124125
ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
125-
while (protocolVersion.supportsKeyspaceOnQuery()) {
126+
while (protocolVersion.supports(ProtocolFeature.KEYSPACE_ON_QUERY)) {
126127
// Downgrade until we hit a protocol version that doesn't support keyspace on query.
127128
protocolVersion = protocolVersion.getLowerSupported();
128129
}

driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.testng.Assert.assertTrue;
3131
import static org.testng.Assert.fail;
3232

33+
import com.datastax.driver.core.ProtocolVersion.ProtocolFeature;
3334
import com.datastax.driver.core.exceptions.InvalidQueryException;
3435
import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
3536
import com.datastax.driver.core.policies.FallthroughRetryPolicy;
@@ -777,7 +778,7 @@ public void should_use_keyspace_if_set_on_origin_statement_same_ks_on_session()
777778
@Test(groups = "short", expectedExceptions = UnsupportedFeatureException.class)
778779
public void should_not_use_keyspace_if_set_and_protocol_does_not_support() {
779780
ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
780-
while (protocolVersion.supportsKeyspaceOnQuery()) {
781+
while (protocolVersion.supports(ProtocolFeature.KEYSPACE_ON_QUERY)) {
781782
// Downgrade until we hit a protocol version that doesn't support keyspace on query.
782783
protocolVersion = protocolVersion.getLowerSupported();
783784
}

driver-core/src/test/java/com/datastax/driver/core/SimpleStatementIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datastax.driver.core;
1717

18+
import com.datastax.driver.core.ProtocolVersion.ProtocolFeature;
1819
import com.datastax.driver.core.exceptions.InvalidQueryException;
1920
import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
2021
import com.datastax.driver.core.utils.CassandraVersion;
@@ -61,7 +62,7 @@ public void should_use_keyspace_if_set_same_ks_on_session() {
6162
@Test(groups = "short", expectedExceptions = InvalidQueryException.class)
6263
public void should_not_use_keyspace_if_set_and_protocol_does_not_support() {
6364
ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
64-
while (protocolVersion.supportsKeyspaceOnQuery()) {
65+
while (protocolVersion.supports(ProtocolFeature.KEYSPACE_ON_QUERY)) {
6566
// Downgrade until we hit a protocol version that doesn't support keyspace on query.
6667
protocolVersion = protocolVersion.getLowerSupported();
6768
}

0 commit comments

Comments
 (0)