Skip to content

Commit cf9fed2

Browse files
committed
JAVA-1738: Make ConsistencyLevel pluggable
1 parent ba71dc1 commit cf9fed2

18 files changed

Lines changed: 231 additions & 107 deletions

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

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,18 @@
1515
*/
1616
package com.datastax.oss.driver.api.core;
1717

18-
import com.datastax.oss.protocol.internal.ProtocolConstants;
19-
import com.google.common.collect.ImmutableMap;
20-
import java.util.Map;
21-
22-
/** The consistency level of a request. */
23-
public enum ConsistencyLevel {
24-
ANY(ProtocolConstants.ConsistencyLevel.ANY),
25-
ONE(ProtocolConstants.ConsistencyLevel.ONE),
26-
TWO(ProtocolConstants.ConsistencyLevel.TWO),
27-
THREE(ProtocolConstants.ConsistencyLevel.THREE),
28-
QUORUM(ProtocolConstants.ConsistencyLevel.QUORUM),
29-
ALL(ProtocolConstants.ConsistencyLevel.ALL),
30-
LOCAL_ONE(ProtocolConstants.ConsistencyLevel.LOCAL_ONE),
31-
LOCAL_QUORUM(ProtocolConstants.ConsistencyLevel.LOCAL_QUORUM),
32-
EACH_QUORUM(ProtocolConstants.ConsistencyLevel.EACH_QUORUM),
33-
34-
SERIAL(ProtocolConstants.ConsistencyLevel.SERIAL),
35-
LOCAL_SERIAL(ProtocolConstants.ConsistencyLevel.LOCAL_SERIAL),
36-
;
37-
38-
private final int protocolCode;
39-
40-
ConsistencyLevel(int protocolCode) {
41-
this.protocolCode = protocolCode;
42-
}
43-
44-
public int getProtocolCode() {
45-
return protocolCode;
46-
}
47-
48-
public static ConsistencyLevel fromCode(int code) {
49-
ConsistencyLevel level = BY_CODE.get(code);
50-
if (level == null) {
51-
throw new IllegalArgumentException("Unknown code: " + code);
52-
}
53-
return level;
54-
}
18+
/**
19+
* The consistency level of a request.
20+
*
21+
* <p>The only reason to model this as an interface (as opposed to an enum type) is to accommodate
22+
* for custom protocol extensions. If you're connecting to a standard Apache Cassandra cluster, all
23+
* {@code ConsistencyLevel}s are {@link CoreConsistencyLevel} instances.
24+
*/
25+
public interface ConsistencyLevel {
5526

56-
private static Map<Integer, ConsistencyLevel> BY_CODE = mapByCode(values());
27+
/** The numerical value that the level is encoded to. */
28+
int getProtocolCode();
5729

58-
private static Map<Integer, ConsistencyLevel> mapByCode(ConsistencyLevel[] levels) {
59-
ImmutableMap.Builder<Integer, ConsistencyLevel> builder = ImmutableMap.builder();
60-
for (ConsistencyLevel level : levels) {
61-
builder.put(level.protocolCode, level);
62-
}
63-
return builder.build();
64-
}
30+
/** The textual representation of the level in configuration files. */
31+
String name();
6532
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.api.core;
17+
18+
import com.datastax.oss.protocol.internal.ProtocolConstants;
19+
import com.google.common.collect.ImmutableMap;
20+
import java.util.Map;
21+
22+
/** A default consistency level supported by the driver out of the box. */
23+
public enum CoreConsistencyLevel implements ConsistencyLevel {
24+
ANY(ProtocolConstants.ConsistencyLevel.ANY),
25+
ONE(ProtocolConstants.ConsistencyLevel.ONE),
26+
TWO(ProtocolConstants.ConsistencyLevel.TWO),
27+
THREE(ProtocolConstants.ConsistencyLevel.THREE),
28+
QUORUM(ProtocolConstants.ConsistencyLevel.QUORUM),
29+
ALL(ProtocolConstants.ConsistencyLevel.ALL),
30+
LOCAL_ONE(ProtocolConstants.ConsistencyLevel.LOCAL_ONE),
31+
LOCAL_QUORUM(ProtocolConstants.ConsistencyLevel.LOCAL_QUORUM),
32+
EACH_QUORUM(ProtocolConstants.ConsistencyLevel.EACH_QUORUM),
33+
34+
SERIAL(ProtocolConstants.ConsistencyLevel.SERIAL),
35+
LOCAL_SERIAL(ProtocolConstants.ConsistencyLevel.LOCAL_SERIAL),
36+
;
37+
38+
private final int protocolCode;
39+
40+
CoreConsistencyLevel(int protocolCode) {
41+
this.protocolCode = protocolCode;
42+
}
43+
44+
@Override
45+
public int getProtocolCode() {
46+
return protocolCode;
47+
}
48+
49+
public static CoreConsistencyLevel fromCode(int code) {
50+
CoreConsistencyLevel level = BY_CODE.get(code);
51+
if (level == null) {
52+
throw new IllegalArgumentException("Unknown code: " + code);
53+
}
54+
return level;
55+
}
56+
57+
private static Map<Integer, CoreConsistencyLevel> BY_CODE = mapByCode(values());
58+
59+
private static Map<Integer, CoreConsistencyLevel> mapByCode(CoreConsistencyLevel[] levels) {
60+
ImmutableMap.Builder<Integer, CoreConsistencyLevel> builder = ImmutableMap.builder();
61+
for (CoreConsistencyLevel level : levels) {
62+
builder.put(level.protocolCode, level);
63+
}
64+
return builder.build();
65+
}
66+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* <p>The only reason to model this as an interface (as opposed to an enum type) is to accommodate
2424
* for custom protocol extensions. If you're connecting to a standard Apache Cassandra cluster, all
25-
* {@code ProtocolVersion}s are {@code CoreProtocolVersion} instances.
25+
* {@code ProtocolVersion}s are {@link CoreProtocolVersion} instances.
2626
*/
2727
public interface ProtocolVersion {
2828
/** The default version used for {@link Detachable detached} objects. */

core/src/main/java/com/datastax/oss/driver/api/core/config/DriverConfigProfile.java

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

18-
import com.datastax.oss.driver.api.core.ConsistencyLevel;
1918
import java.time.Duration;
2019
import java.util.List;
2120

@@ -61,8 +60,4 @@ public interface DriverConfigProfile {
6160
Duration getDuration(DriverOption option);
6261

6362
DriverConfigProfile withDuration(DriverOption option, Duration value);
64-
65-
ConsistencyLevel getConsistencyLevel(DriverOption option);
66-
67-
DriverConfigProfile withConsistencyLevel(DriverOption option, ConsistencyLevel value);
6863
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
import com.datastax.oss.driver.api.core.ConsistencyLevel;
19+
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
20+
21+
/**
22+
* Extension point to plug custom consistency levels.
23+
*
24+
* <p>This is overridable through {@link InternalDriverContext}.
25+
*/
26+
public interface ConsistencyLevelRegistry {
27+
28+
ConsistencyLevel fromCode(int code);
29+
30+
ConsistencyLevel fromName(String name);
31+
32+
/** @return all the values known to this driver instance. */
33+
Iterable<ConsistencyLevel> getValues();
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
import com.datastax.oss.driver.api.core.ConsistencyLevel;
19+
import com.datastax.oss.driver.api.core.CoreConsistencyLevel;
20+
import com.google.common.collect.ImmutableList;
21+
22+
public class DefaultConsistencyLevelRegistry implements ConsistencyLevelRegistry {
23+
24+
private static final ImmutableList<ConsistencyLevel> values =
25+
ImmutableList.<ConsistencyLevel>builder().add(CoreConsistencyLevel.values()).build();
26+
27+
@Override
28+
public ConsistencyLevel fromCode(int code) {
29+
return CoreConsistencyLevel.fromCode(code);
30+
}
31+
32+
@Override
33+
public ConsistencyLevel fromName(String name) {
34+
return CoreConsistencyLevel.valueOf(name);
35+
}
36+
37+
@Override
38+
public Iterable<ConsistencyLevel> getValues() {
39+
return values;
40+
}
41+
}

core/src/main/java/com/datastax/oss/driver/internal/core/config/typesafe/TypesafeDriverConfigProfile.java

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

18-
import com.datastax.oss.driver.api.core.ConsistencyLevel;
1918
import com.datastax.oss.driver.api.core.config.DriverConfigProfile;
2019
import com.datastax.oss.driver.api.core.config.DriverOption;
2120
import com.google.common.collect.MapMaker;
@@ -108,21 +107,6 @@ public DriverConfigProfile withBytes(DriverOption option, long value) {
108107
return with(option, value);
109108
}
110109

111-
@Override
112-
public ConsistencyLevel getConsistencyLevel(DriverOption option) {
113-
return getCached(
114-
option.getPath(),
115-
path -> {
116-
String name = getEffectiveOptions().getString(path);
117-
return ConsistencyLevel.valueOf(name);
118-
});
119-
}
120-
121-
@Override
122-
public DriverConfigProfile withConsistencyLevel(DriverOption option, ConsistencyLevel value) {
123-
return with(option, value.toString());
124-
}
125-
126110
private <T> T getCached(String path, Function<String, T> compute) {
127111
// compute's signature guarantees we get a T, and this is the only place where we mutate the
128112
// entry

core/src/main/java/com/datastax/oss/driver/internal/core/context/DefaultDriverContext.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
3333
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
3434
import com.datastax.oss.driver.internal.core.CassandraProtocolVersionRegistry;
35+
import com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry;
36+
import com.datastax.oss.driver.internal.core.DefaultConsistencyLevelRegistry;
3537
import com.datastax.oss.driver.internal.core.ProtocolVersionRegistry;
3638
import com.datastax.oss.driver.internal.core.channel.ChannelFactory;
3739
import com.datastax.oss.driver.internal.core.channel.WriteCoalescer;
@@ -114,6 +116,9 @@ public class DefaultDriverContext implements InternalDriverContext {
114116
private final LazyReference<ProtocolVersionRegistry> protocolVersionRegistryRef =
115117
new LazyReference<>(
116118
"protocolVersionRegistry", this::buildProtocolVersionRegistry, cycleDetector);
119+
private final LazyReference<ConsistencyLevelRegistry> consistencyLevelRegistryRef =
120+
new LazyReference<>(
121+
"consistencyLevelRegistry", this::buildConsistencyLevelRegistry, cycleDetector);
117122
private final LazyReference<NettyOptions> nettyOptionsRef =
118123
new LazyReference<>("nettyOptions", this::buildNettyOptions, cycleDetector);
119124
private final LazyReference<WriteCoalescer> writeCoalescerRef =
@@ -253,6 +258,10 @@ protected ProtocolVersionRegistry buildProtocolVersionRegistry() {
253258
return new CassandraProtocolVersionRegistry(sessionName());
254259
}
255260

261+
protected ConsistencyLevelRegistry buildConsistencyLevelRegistry() {
262+
return new DefaultConsistencyLevelRegistry();
263+
}
264+
256265
protected NettyOptions buildNettyOptions() {
257266
return new DefaultNettyOptions(this);
258267
}
@@ -405,6 +414,11 @@ public ProtocolVersionRegistry protocolVersionRegistry() {
405414
return protocolVersionRegistryRef.get();
406415
}
407416

417+
@Override
418+
public ConsistencyLevelRegistry consistencyLevelRegistry() {
419+
return consistencyLevelRegistryRef.get();
420+
}
421+
408422
@Override
409423
public NettyOptions nettyOptions() {
410424
return nettyOptionsRef.get();

core/src/main/java/com/datastax/oss/driver/internal/core/context/InternalDriverContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
1919
import com.datastax.oss.driver.api.core.context.DriverContext;
20+
import com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry;
2021
import com.datastax.oss.driver.internal.core.ProtocolVersionRegistry;
2122
import com.datastax.oss.driver.internal.core.channel.ChannelFactory;
2223
import com.datastax.oss.driver.internal.core.channel.WriteCoalescer;
@@ -48,6 +49,8 @@ public interface InternalDriverContext extends DriverContext {
4849

4950
ProtocolVersionRegistry protocolVersionRegistry();
5051

52+
ConsistencyLevelRegistry consistencyLevelRegistry();
53+
5154
NettyOptions nettyOptions();
5255

5356
WriteCoalescer writeCoalescer();

0 commit comments

Comments
 (0)