Skip to content

Commit 941f5f0

Browse files
adutraolim7t
authored andcommitted
Make ProtocolFeature an interface
Motivation: ProtocolFeature is currently an enum and as such is not extensible; if custom protocol extensions need to support proprietary features, such features cannot be modeled as a ProtocolFeature instance. Modification: Make ProtocolFeature an interface, create enum DefaultProtocolFeature. Result: ProtocolFeature can now be implemented by other classes.
1 parent 529b173 commit 941f5f0

File tree

7 files changed

+69
-35
lines changed

7 files changed

+69
-35
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/ProtocolVersion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public interface ProtocolVersion {
4343
/**
4444
* Whether the protocol version is in a beta status.
4545
*
46-
* <p>Beta versions are intended for Cassandra development. They should be used in a regular
47-
* application, beta features may break at any point.
46+
* <p>Beta versions are intended for Cassandra development. They should not be used in a regular
47+
* application, as beta features may break at any point.
4848
*/
4949
boolean isBeta();
5050
}

core/src/main/java/com/datastax/oss/driver/internal/core/CassandraProtocolVersionRegistry.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,12 @@ public ProtocolVersion highestCommon(Collection<Node> nodes) {
170170

171171
@Override
172172
public boolean supports(ProtocolVersion version, ProtocolFeature feature) {
173-
switch (feature) {
174-
case UNSET_BOUND_VALUES:
175-
return version.getCode() >= 4;
176-
case PER_REQUEST_KEYSPACE:
177-
return version.getCode() >= 5;
178-
default:
179-
throw new IllegalArgumentException("Unhandled protocol feature: " + feature);
173+
if (DefaultProtocolFeature.UNSET_BOUND_VALUES.equals(feature)) {
174+
return version.getCode() >= 4;
175+
} else if (DefaultProtocolFeature.PER_REQUEST_KEYSPACE.equals(feature)) {
176+
return version.getCode() >= 5;
177+
} else {
178+
throw new IllegalArgumentException("Unhandled protocol feature: " + feature);
180179
}
181180
}
182181

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core;
17+
18+
/**
19+
* Features that are commonly supported by most Apache Cassandra protocol versions.
20+
*
21+
* @see com.datastax.oss.driver.api.core.DefaultProtocolVersion
22+
*/
23+
public enum DefaultProtocolFeature implements ProtocolFeature {
24+
25+
/**
26+
* The ability to leave variables unset in prepared statements.
27+
*
28+
* @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-7304">CASSANDRA-7304</a>
29+
*/
30+
UNSET_BOUND_VALUES,
31+
32+
/**
33+
* The ability to override the keyspace on a per-request basis.
34+
*
35+
* @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-10145">CASSANDRA-10145</a>
36+
*/
37+
PER_REQUEST_KEYSPACE,
38+
;
39+
}

core/src/main/java/com/datastax/oss/driver/internal/core/ProtocolFeature.java

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

18-
/** Features of the native protocol that are only supported in specific versions. */
19-
public enum ProtocolFeature {
18+
import com.datastax.oss.driver.api.core.ProtocolVersion;
2019

21-
/**
22-
* The ability to leave variables unset in prepared statements.
23-
*
24-
* @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-7304">CASSANDRA-7304</a>
25-
*/
26-
UNSET_BOUND_VALUES,
27-
28-
/**
29-
* The ability to override the keyspace on a per-request basis.
30-
*
31-
* @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-10145">CASSANDRA-10145</a>
32-
*/
33-
PER_REQUEST_KEYSPACE,
34-
;
35-
}
20+
/**
21+
* A marker interface for features of the native protocol that are only supported by specific
22+
* {@linkplain ProtocolVersion versions}.
23+
*
24+
* <p>The only reason to model this as an interface (as opposed to an enum type) is to accommodate
25+
* for custom protocol extensions. If you're connecting to a standard Apache Cassandra cluster, all
26+
* {@code ProtocolFeature}s are {@link DefaultProtocolFeature} instances.
27+
*
28+
* @see ProtocolVersionRegistry#supports(ProtocolVersion, ProtocolFeature)
29+
* @see DefaultProtocolFeature
30+
*/
31+
public interface ProtocolFeature {}

core/src/main/java/com/datastax/oss/driver/internal/core/cql/Conversions.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import com.datastax.oss.driver.api.core.servererrors.WriteTimeoutException;
5454
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
5555
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
56-
import com.datastax.oss.driver.internal.core.ProtocolFeature;
56+
import com.datastax.oss.driver.internal.core.DefaultProtocolFeature;
5757
import com.datastax.oss.driver.internal.core.ProtocolVersionRegistry;
5858
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
5959
import com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken;
@@ -128,7 +128,7 @@ public static Message toMessage(
128128
"Can't have both positional and named values in a statement.");
129129
}
130130
if (keyspace != null
131-
&& !registry.supports(protocolVersion, ProtocolFeature.PER_REQUEST_KEYSPACE)) {
131+
&& !registry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
132132
throw new IllegalArgumentException(
133133
"Can't use per-request keyspace with protocol " + protocolVersion);
134134
}
@@ -146,7 +146,7 @@ public static Message toMessage(
146146
return new Query(simpleStatement.getQuery(), queryOptions);
147147
} else if (statement instanceof BoundStatement) {
148148
BoundStatement boundStatement = (BoundStatement) statement;
149-
if (!registry.supports(protocolVersion, ProtocolFeature.UNSET_BOUND_VALUES)) {
149+
if (!registry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
150150
ensureAllSet(boundStatement);
151151
}
152152
boolean skipMetadata =
@@ -171,11 +171,11 @@ public static Message toMessage(
171171
queryOptions);
172172
} else if (statement instanceof BatchStatement) {
173173
BatchStatement batchStatement = (BatchStatement) statement;
174-
if (!registry.supports(protocolVersion, ProtocolFeature.UNSET_BOUND_VALUES)) {
174+
if (!registry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
175175
ensureAllSet(batchStatement);
176176
}
177177
if (keyspace != null
178-
&& !registry.supports(protocolVersion, ProtocolFeature.PER_REQUEST_KEYSPACE)) {
178+
&& !registry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
179179
throw new IllegalArgumentException(
180180
"Can't use per-request keyspace with protocol " + protocolVersion);
181181
}

core/src/main/java/com/datastax/oss/driver/internal/core/cql/CqlPrepareHandlerBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import com.datastax.oss.driver.api.core.servererrors.QueryValidationException;
3737
import com.datastax.oss.driver.api.core.session.throttling.RequestThrottler;
3838
import com.datastax.oss.driver.api.core.session.throttling.Throttled;
39-
import com.datastax.oss.driver.internal.core.ProtocolFeature;
39+
import com.datastax.oss.driver.internal.core.DefaultProtocolFeature;
4040
import com.datastax.oss.driver.internal.core.ProtocolVersionRegistry;
4141
import com.datastax.oss.driver.internal.core.adminrequest.ThrottledAdminRequestHandler;
4242
import com.datastax.oss.driver.internal.core.channel.DriverChannel;
@@ -149,7 +149,7 @@ protected CqlPrepareHandlerBase(
149149
ProtocolVersionRegistry registry = context.getProtocolVersionRegistry();
150150
CqlIdentifier keyspace = request.getKeyspace();
151151
if (keyspace != null
152-
&& !registry.supports(protocolVersion, ProtocolFeature.PER_REQUEST_KEYSPACE)) {
152+
&& !registry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
153153
throw new IllegalArgumentException(
154154
"Can't use per-request keyspace with protocol " + protocolVersion);
155155
}

integration-tests/src/test/java/com/datastax/oss/driver/api/core/cql/BoundStatementIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import com.datastax.oss.driver.api.testinfra.session.SessionUtils;
3737
import com.datastax.oss.driver.api.testinfra.simulacron.SimulacronRule;
3838
import com.datastax.oss.driver.categories.ParallelizableTests;
39-
import com.datastax.oss.driver.internal.core.ProtocolFeature;
39+
import com.datastax.oss.driver.internal.core.DefaultProtocolFeature;
4040
import com.datastax.oss.driver.internal.core.ProtocolVersionRegistry;
4141
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
4242
import com.datastax.oss.driver.internal.core.type.codec.CqlIntToStringCodec;
@@ -499,6 +499,6 @@ private boolean supportsPerRequestKeyspace(CqlSession session) {
499499
InternalDriverContext context = (InternalDriverContext) session.getContext();
500500
ProtocolVersionRegistry protocolVersionRegistry = context.getProtocolVersionRegistry();
501501
return protocolVersionRegistry.supports(
502-
context.getProtocolVersion(), ProtocolFeature.PER_REQUEST_KEYSPACE);
502+
context.getProtocolVersion(), DefaultProtocolFeature.PER_REQUEST_KEYSPACE);
503503
}
504504
}

0 commit comments

Comments
 (0)