Skip to content

Commit a52d33e

Browse files
committed
[fixup] rename options builder class
1 parent a7d36d5 commit a52d33e

File tree

7 files changed

+301
-212
lines changed

7 files changed

+301
-212
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/session/SessionBuilder.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public abstract class SessionBuilder<SelfT extends SessionBuilder, SessionT> {
7272
private ImmutableMap.Builder<String, Predicate<Node>> nodeFilters = ImmutableMap.builder();
7373
protected CqlIdentifier keyspace;
7474
private ClassLoader classLoader = null;
75-
protected Map<String, String> startupOptions = new HashMap<>();
75+
protected Map<String, String> additionalStartupOptions = new HashMap<>();
7676

7777
/**
7878
* Sets the configuration loader to use.
@@ -259,6 +259,20 @@ public SelfT withClassLoader(@Nullable ClassLoader classLoader) {
259259
return self;
260260
}
261261

262+
/**
263+
* A {@link Map} of custom options to send in the Startup message.
264+
*
265+
* <p>This is used when a client wants/needs to send additional options in the Startup message
266+
* that the driver doesn't already provide. The driver should already provide CQL_VERSION,
267+
* COMPRESSION, DRIVER_NAME and DRIVER_VERSION options, so those should NOT be explicitly set via
268+
* this method.
269+
*/
270+
@NonNull
271+
public SelfT withAdditionalStartupOptions(@Nullable Map<String, String> options) {
272+
this.additionalStartupOptions = options;
273+
return self;
274+
}
275+
262276
/**
263277
* Creates the session with the options set by this builder.
264278
*
@@ -343,16 +357,10 @@ protected DriverContext buildContext(
343357
requestTracker,
344358
nodeFilters,
345359
classLoader,
346-
startupOptions);
360+
additionalStartupOptions);
347361
}
348362

349363
private static <T> T buildIfNull(T value, Supplier<T> builder) {
350364
return (value == null) ? builder.get() : value;
351365
}
352-
353-
@NonNull
354-
public SelfT withStartupOptions(@Nullable Map<String, String> options) {
355-
this.startupOptions = options;
356-
return self;
357-
}
358366
}

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
import com.datastax.oss.driver.internal.core.util.concurrent.LazyReference;
7474
import com.datastax.oss.protocol.internal.Compressor;
7575
import com.datastax.oss.protocol.internal.FrameCodec;
76-
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
7776
import edu.umd.cs.findbugs.annotations.NonNull;
7877
import edu.umd.cs.findbugs.annotations.Nullable;
7978
import io.netty.buffer.ByteBuf;
@@ -182,6 +181,7 @@ public class DefaultDriverContext implements InternalDriverContext {
182181
private final LazyReference<NodeStateListener> nodeStateListenerRef;
183182
private final LazyReference<SchemaChangeListener> schemaChangeListenerRef;
184183
private final LazyReference<RequestTracker> requestTrackerRef;
184+
private final LazyReference<StartupOptionsBuilder> startupOptionsBuilderRef;
185185

186186
private final DriverConfig config;
187187
private final DriverConfigLoader configLoader;
@@ -193,7 +193,6 @@ public class DefaultDriverContext implements InternalDriverContext {
193193
private final RequestTracker requestTrackerFromBuilder;
194194
private final Map<String, Predicate<Node>> nodeFiltersFromBuilder;
195195
private final ClassLoader classLoader;
196-
private final Map<String, String> startupOptions;
197196

198197
public DefaultDriverContext(
199198
DriverConfigLoader configLoader,
@@ -203,7 +202,7 @@ public DefaultDriverContext(
203202
RequestTracker requestTracker,
204203
Map<String, Predicate<Node>> nodeFilters,
205204
ClassLoader classLoader,
206-
Map<String, String> extraStartupOptions) {
205+
Map<String, String> additionalStartupOptions) {
207206
this.config = configLoader.getInitialConfig();
208207
this.configLoader = configLoader;
209208
DriverExecutionProfile defaultProfile = config.getDefaultProfile();
@@ -231,20 +230,23 @@ public DefaultDriverContext(
231230
"requestTracker", () -> buildRequestTracker(requestTrackerFromBuilder), cycleDetector);
232231
this.nodeFiltersFromBuilder = nodeFilters;
233232
this.classLoader = classLoader;
234-
this.startupOptions = buildStartupOptions(extraStartupOptions);
233+
this.startupOptionsBuilderRef =
234+
new LazyReference<>(
235+
"startupOptionsBuilder",
236+
() -> buildStartupOptions(additionalStartupOptions),
237+
cycleDetector);
235238
}
236239

237240
/**
238-
* Builds a map of options to use when sending a Startup message. The options argument passed in
239-
* will append to, or overwrite, the Internal default options sent by the driver.
241+
* Builds a map of options to send in a Startup message. The map will contain any internal options
242+
* built into the driver (i.e. CQL_VERSION, COMPRESSION, DRIVER_NAME and DRIVER_VERSION), as well
243+
* as any custom options passed in. This method will typically be called during the session
244+
* building process, with the options passed here being set by {@link
245+
* com.datastax.oss.driver.api.core.session.SessionBuilder#withCustomStartupOptions(java.util.Map)}
240246
*/
241-
protected Map<String, String> buildStartupOptions(Map<String, String> options) {
242-
NullAllowingImmutableMap.Builder<String, String> builder = NullAllowingImmutableMap.builder();
243-
builder.putAll(new InternalStartupOptions(this).getOptions());
244-
if (options != null) {
245-
builder.putAll(options);
246-
}
247-
return builder.build();
247+
protected StartupOptionsBuilder buildStartupOptions(
248+
Map<String, String> additionalStartupOptions) {
249+
return new StartupOptionsBuilder(this).withAdditionalOptions(additionalStartupOptions);
248250
}
249251

250252
protected Map<String, LoadBalancingPolicy> buildLoadBalancingPolicies() {
@@ -752,6 +754,6 @@ public ProtocolVersion getProtocolVersion() {
752754
@NonNull
753755
@Override
754756
public Map<String, String> getStartupOptions() {
755-
return startupOptions;
757+
return startupOptionsBuilderRef.get().build();
756758
}
757759
}

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

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.context;
17+
18+
import com.datastax.oss.driver.api.core.MavenCoordinates;
19+
import com.datastax.oss.driver.internal.core.DefaultMavenCoordinates;
20+
import com.datastax.oss.protocol.internal.request.Startup;
21+
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import net.jcip.annotations.Immutable;
25+
26+
@Immutable
27+
public class StartupOptionsBuilder {
28+
29+
public static final String DRIVER_NAME_KEY = "DRIVER_NAME";
30+
public static final String DRIVER_VERSION_KEY = "DRIVER_VERSION";
31+
32+
private static final MavenCoordinates MAVEN_COORDINATES =
33+
DefaultMavenCoordinates.buildFromResource(
34+
StartupOptionsBuilder.class.getResource("/com/datastax/oss/driver/Driver.properties"));
35+
36+
private final InternalDriverContext context;
37+
private final Map<String, String> additionalOptions = new HashMap<>();
38+
39+
public StartupOptionsBuilder(InternalDriverContext context) {
40+
this.context = context;
41+
}
42+
43+
/**
44+
* Adds additional startup options to the builder.
45+
*
46+
* <p>The additional options set here should NOT include DRIVER_NAME, DRIVER_VERSION, CQL_VERSION
47+
* or COMPRESSION as those are derived from the driver itself.
48+
*
49+
* @param additionalOptions Extra options to send in a Startup message.
50+
*/
51+
public StartupOptionsBuilder withAdditionalOptions(Map<String, String> additionalOptions) {
52+
if (additionalOptions != null) {
53+
additionalOptions
54+
.entrySet()
55+
.forEach(
56+
(entry) -> {
57+
String additionalOptionKey = entry.getKey();
58+
String additionalOptionValue = entry.getValue();
59+
// only add non-internal options
60+
if (!DRIVER_NAME_KEY.equals(additionalOptionKey)
61+
&& !DRIVER_VERSION_KEY.equals(additionalOptionKey)
62+
&& !Startup.COMPRESSION_KEY.equals(additionalOptionKey)
63+
&& !Startup.CQL_VERSION_KEY.equals(additionalOptionKey)) {
64+
this.additionalOptions.put(additionalOptionKey, additionalOptionValue);
65+
}
66+
});
67+
}
68+
return this;
69+
}
70+
71+
/**
72+
* Builds a map of options to send in a Startup message.
73+
*
74+
* <p>The default set of options are built here and include {@link
75+
* com.datastax.oss.protocol.internal.request.Startup#COMPRESSION_KEY} (if the context passed in
76+
* has a compressor/algorithm set), and the driver's {@link #DRIVER_NAME_KEY} and {@link
77+
* #DRIVER_VERSION_KEY}. The {@link com.datastax.oss.protocol.internal.request.Startup}
78+
* constructor will add {@link
79+
* com.datastax.oss.protocol.internal.request.Startup#CQL_VERSION_KEY}.
80+
*
81+
* <p>Additional options can be set via {@link #withAdditionalOptions(java.util.Map)}.
82+
*
83+
* @return Map of Startup Options.
84+
*/
85+
public Map<String, String> build() {
86+
NullAllowingImmutableMap.Builder<String, String> builder =
87+
NullAllowingImmutableMap.builder(3 + additionalOptions.size());
88+
// add compression option
89+
String compressionAlgorithm = context.getCompressor().algorithm();
90+
if (compressionAlgorithm != null && !compressionAlgorithm.trim().isEmpty()) {
91+
builder.put(Startup.COMPRESSION_KEY, compressionAlgorithm.trim());
92+
}
93+
// add driver name and version
94+
builder
95+
.put(DRIVER_NAME_KEY, MAVEN_COORDINATES.getName())
96+
.put(DRIVER_VERSION_KEY, MAVEN_COORDINATES.getVersion().toString());
97+
// add any additonal options
98+
return builder.putAll(additionalOptions).build();
99+
}
100+
}

0 commit comments

Comments
 (0)