kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | db6140bb093e4a8269cc0681e4c6a4d5f16607846d3be42f6e54e498ecaab107 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-06-19 20:04 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-22 10:33 +02:00 |
| Parents | 392aefac701d |
3 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +8 −8
| @@ -1156,14 +1156,14 @@ | ||
| 1156 | 1156 | */ |
| 1157 | 1157 | @Override |
| 1158 | 1158 | public Configuration getConfiguration() { |
| 1159 | - Policies policies = new Policies( | |
| 1160 | - loadBalancingPolicy == null ? Policies.defaultLoadBalancingPolicy() : loadBalancingPolicy, | |
| 1161 | - Objects.firstNonNull(reconnectionPolicy, Policies.defaultReconnectionPolicy()), | |
| 1162 | - Objects.firstNonNull(retryPolicy, Policies.defaultRetryPolicy()), | |
| 1163 | - Objects.firstNonNull(addressTranslater, Policies.defaultAddressTranslater()), | |
| 1164 | - Objects.firstNonNull(timestampGenerator, Policies.defaultTimestampGenerator()), | |
| 1165 | - Objects.firstNonNull(speculativeExecutionPolicy, Policies.defaultSpeculativeExecutionPolicy()) | |
| 1166 | - ); | |
| 1159 | + Policies policies = Policies.builder() | |
| 1160 | + .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 1161 | + .withReconnectionPolicy(reconnectionPolicy) | |
| 1162 | + .withRetryPolicy(retryPolicy) | |
| 1163 | + .withAddressTranslater(addressTranslater) | |
| 1164 | + .withTimestampGenerator(timestampGenerator) | |
| 1165 | + .withSpeculativeExecutionPolicy(speculativeExecutionPolicy) | |
| 1166 | + .build(); | |
| 1167 | 1167 | return new Configuration(policies, |
| 1168 | 1168 | new ProtocolOptions(port, protocolVersion, maxSchemaAgreementWaitSeconds, sslOptions, authProvider).setCompression(compression), |
| 1169 | 1169 | poolingOptions == null ? new PoolingOptions() : poolingOptions, |
modified driver-core/src/main/java/com/datastax/driver/core/Configuration.java +1 −1
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | * Creates a configuration object. |
| 47 | 47 | */ |
| 48 | 48 | public Configuration() { |
| 49 | - this(new Policies(), | |
| 49 | + this(Policies.builder().build(), | |
| 50 | 50 | new ProtocolOptions(), |
| 51 | 51 | new PoolingOptions(), |
| 52 | 52 | new SocketOptions(), |
modified driver-core/src/main/java/com/datastax/driver/core/policies/Policies.java +172 −50
| @@ -15,6 +15,8 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | +import com.google.common.base.Objects; | |
| 19 | + | |
| 18 | 20 | import com.datastax.driver.core.ServerSideTimestampGenerator; |
| 19 | 21 | import com.datastax.driver.core.TimestampGenerator; |
| 20 | 22 | |
| @@ -23,6 +25,15 @@ | ||
| 23 | 25 | */ |
| 24 | 26 | public class Policies { |
| 25 | 27 | |
| 28 | + /** | |
| 29 | + * Returns a builder to create a new {@code Policies} object. | |
| 30 | + * | |
| 31 | + * @return the builder. | |
| 32 | + */ | |
| 33 | + public static Builder builder() { | |
| 34 | + return new Builder(); | |
| 35 | + } | |
| 36 | + | |
| 26 | 37 | private static final ReconnectionPolicy DEFAULT_RECONNECTION_POLICY = new ExponentialReconnectionPolicy(1000, 10 * 60 * 1000); |
| 27 | 38 | private static final RetryPolicy DEFAULT_RETRY_POLICY = DefaultRetryPolicy.INSTANCE; |
| 28 | 39 | private static final AddressTranslater DEFAULT_ADDRESS_TRANSLATER = new IdentityTranslater(); |
| @@ -35,57 +46,15 @@ | ||
| 35 | 46 | private final TimestampGenerator timestampGenerator; |
| 36 | 47 | private final SpeculativeExecutionPolicy speculativeExecutionPolicy; |
| 37 | 48 | |
| 38 | - public Policies() { | |
| 39 | - this(defaultLoadBalancingPolicy(), defaultReconnectionPolicy(), defaultRetryPolicy(), defaultAddressTranslater(), defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy()); | |
| 40 | - } | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Creates a new {@code Policies} object using the provided policies. | |
| 44 | - * <p> | |
| 45 | - * This constructor use the default {@link IdentityTranslater} and {@link TimestampGenerator}. | |
| 46 | - * | |
| 47 | - * @param loadBalancingPolicy the load balancing policy to use. | |
| 48 | - * @param reconnectionPolicy the reconnection policy to use. | |
| 49 | - * @param retryPolicy the retry policy to use. | |
| 50 | - */ | |
| 51 | - public Policies(LoadBalancingPolicy loadBalancingPolicy, | |
| 52 | - ReconnectionPolicy reconnectionPolicy, | |
| 53 | - RetryPolicy retryPolicy, | |
| 54 | - SpeculativeExecutionPolicy speculativeExecutionPolicy) { | |
| 55 | - this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, DEFAULT_ADDRESS_TRANSLATER, speculativeExecutionPolicy); | |
| 56 | - } | |
| 57 | - | |
| 58 | 49 | /** |
| 59 | - * Creates a new {@code Policies} object using the provided policies. | |
| 60 | - * | |
| 61 | - * @param loadBalancingPolicy the load balancing policy to use. | |
| 62 | - * @param reconnectionPolicy the reconnection policy to use. | |
| 63 | - * @param retryPolicy the retry policy to use. | |
| 64 | - * @param addressTranslater the address translater to use. | |
| 50 | + * @deprecated this constructor is exposed for backward compatibility. | |
| 51 | + * Use {@link #builder()} instead. | |
| 65 | 52 | */ |
| 66 | - public Policies(LoadBalancingPolicy loadBalancingPolicy, | |
| 67 | - ReconnectionPolicy reconnectionPolicy, | |
| 68 | - RetryPolicy retryPolicy, | |
| 69 | - AddressTranslater addressTranslater, | |
| 70 | - SpeculativeExecutionPolicy speculativeExecutionPolicy) { | |
| 71 | - // NB: this constructor is provided for backward compatibility with 2.1.0 | |
| 72 | - this.loadBalancingPolicy = loadBalancingPolicy; | |
| 73 | - this.reconnectionPolicy = reconnectionPolicy; | |
| 74 | - this.retryPolicy = retryPolicy; | |
| 75 | - this.addressTranslater = addressTranslater; | |
| 76 | - this.speculativeExecutionPolicy = speculativeExecutionPolicy; | |
| 77 | - this.timestampGenerator = defaultTimestampGenerator(); | |
| 78 | - } | |
| 79 | - | |
| 80 | - /** | |
| 81 | - * Creates a new {@code Policies} object using the provided policies. | |
| 82 | - * | |
| 83 | - * @param loadBalancingPolicy the load balancing policy to use. | |
| 84 | - * @param reconnectionPolicy the reconnection policy to use. | |
| 85 | - * @param retryPolicy the retry policy to use. | |
| 86 | - * @param addressTranslater the address translater to use. | |
| 87 | - * @param timestampGenerator the timestamp generator to use. | |
| 88 | - * @param speculativeExecutionPolicy the speculative execution policy to use. | |
| 53 | + @Deprecated | |
| 54 | + /* | |
| 55 | + * This constructor should be private and called only by the builder. | |
| 56 | + * If a new field gets added, add it to this constructor and make it private, then expose the current signature as a deprecated method | |
| 57 | + * (see end of class). | |
| 89 | 58 | */ |
| 90 | 59 | public Policies(LoadBalancingPolicy loadBalancingPolicy, |
| 91 | 60 | ReconnectionPolicy reconnectionPolicy, |
| @@ -108,7 +77,7 @@ | ||
| 108 | 77 | * awareness (so {@code new TokenAwarePolicy(new DCAwareRoundRobinPolicy())}). |
| 109 | 78 | * <p> |
| 110 | 79 | * Note that this policy shuffles the replicas when token awareness is used, see |
| 111 | - * {@link TokenAwarePolicy#TokenAwarePolicy(LoadBalancingPolicy,boolean)} for an | |
| 80 | + * {@link TokenAwarePolicy#TokenAwarePolicy(LoadBalancingPolicy, boolean)} for an | |
| 112 | 81 | * explanation of the tradeoffs. |
| 113 | 82 | * |
| 114 | 83 | * @return the default load balancing policy. |
| @@ -235,4 +204,157 @@ | ||
| 235 | 204 | public SpeculativeExecutionPolicy getSpeculativeExecutionPolicy() { |
| 236 | 205 | return speculativeExecutionPolicy; |
| 237 | 206 | } |
| 207 | + | |
| 208 | + /** | |
| 209 | + * A builder to create a new {@code Policies} object. | |
| 210 | + */ | |
| 211 | + public static class Builder { | |
| 212 | + private LoadBalancingPolicy loadBalancingPolicy; | |
| 213 | + private ReconnectionPolicy reconnectionPolicy; | |
| 214 | + private RetryPolicy retryPolicy; | |
| 215 | + private AddressTranslater addressTranslater; | |
| 216 | + private TimestampGenerator timestampGenerator; | |
| 217 | + private SpeculativeExecutionPolicy speculativeExecutionPolicy; | |
| 218 | + | |
| 219 | + /** | |
| 220 | + * Sets the load balancing policy. | |
| 221 | + * | |
| 222 | + * @param loadBalancingPolicy see {@link #getLoadBalancingPolicy()}. | |
| 223 | + * @return this builder. | |
| 224 | + */ | |
| 225 | + public Builder withLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) { | |
| 226 | + this.loadBalancingPolicy = loadBalancingPolicy; | |
| 227 | + return this; | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * Sets the reconnection policy. | |
| 232 | + * | |
| 233 | + * @param reconnectionPolicy see {@link #getReconnectionPolicy()}. | |
| 234 | + * @return this builder. | |
| 235 | + */ | |
| 236 | + public Builder withReconnectionPolicy(ReconnectionPolicy reconnectionPolicy) { | |
| 237 | + this.reconnectionPolicy = reconnectionPolicy; | |
| 238 | + return this; | |
| 239 | + } | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Sets the retry policy. | |
| 243 | + * | |
| 244 | + * @param retryPolicy see {@link #getRetryPolicy()}. | |
| 245 | + * @return this builder. | |
| 246 | + */ | |
| 247 | + public Builder withRetryPolicy(RetryPolicy retryPolicy) { | |
| 248 | + this.retryPolicy = retryPolicy; | |
| 249 | + return this; | |
| 250 | + } | |
| 251 | + | |
| 252 | + /** | |
| 253 | + * Sets the address translator. | |
| 254 | + * | |
| 255 | + * @param addressTranslater see {@link #getAddressTranslater()}. | |
| 256 | + * @return this builder. | |
| 257 | + */ | |
| 258 | + public Builder withAddressTranslater(AddressTranslater addressTranslater) { | |
| 259 | + this.addressTranslater = addressTranslater; | |
| 260 | + return this; | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * Sets the timestamp generator. | |
| 265 | + * | |
| 266 | + * @param timestampGenerator see {@link #getTimestampGenerator()}. | |
| 267 | + * @return this builder. | |
| 268 | + */ | |
| 269 | + public Builder withTimestampGenerator(TimestampGenerator timestampGenerator) { | |
| 270 | + this.timestampGenerator = timestampGenerator; | |
| 271 | + return this; | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 275 | + * Sets the speculative execution policy. | |
| 276 | + * | |
| 277 | + * @param speculativeExecutionPolicy see {@link #getSpeculativeExecutionPolicy()}. | |
| 278 | + * @return this builder. | |
| 279 | + */ | |
| 280 | + public Builder withSpeculativeExecutionPolicy(SpeculativeExecutionPolicy speculativeExecutionPolicy) { | |
| 281 | + this.speculativeExecutionPolicy = speculativeExecutionPolicy; | |
| 282 | + return this; | |
| 283 | + } | |
| 284 | + | |
| 285 | + /** | |
| 286 | + * Builds the final object from this builder. | |
| 287 | + * <p> | |
| 288 | + * Any field that hasn't been set explicitly will get its default value. | |
| 289 | + * | |
| 290 | + * @return the object. | |
| 291 | + */ | |
| 292 | + public Policies build() { | |
| 293 | + return new Policies( | |
| 294 | + loadBalancingPolicy == null ? Policies.defaultLoadBalancingPolicy() : loadBalancingPolicy, | |
| 295 | + Objects.firstNonNull(reconnectionPolicy, Policies.defaultReconnectionPolicy()), | |
| 296 | + Objects.firstNonNull(retryPolicy, Policies.defaultRetryPolicy()), | |
| 297 | + Objects.firstNonNull(addressTranslater, Policies.defaultAddressTranslater()), | |
| 298 | + Objects.firstNonNull(timestampGenerator, Policies.defaultTimestampGenerator()), | |
| 299 | + Objects.firstNonNull(speculativeExecutionPolicy, Policies.defaultSpeculativeExecutionPolicy())); | |
| 300 | + } | |
| 301 | + } | |
| 302 | + | |
| 303 | + /* | |
| 304 | + * The public constructors of this class were mistakenly changed over time (this is a breaking change). | |
| 305 | + * | |
| 306 | + * The code below provides backward compatibility with every signature that was ever exposed in a released version. | |
| 307 | + * This is ugly but the safest way to avoid issues for clients upgrading from a previous version. | |
| 308 | + * | |
| 309 | + * In the future, new fields will only be added to the builder, which avoids this problem. | |
| 310 | + */ | |
| 311 | + | |
| 312 | + /** | |
| 313 | + * @deprecated this constructor is provided for backward compatibility. | |
| 314 | + * Use {@link #builder()} instead. | |
| 315 | + */ | |
| 316 | + @Deprecated | |
| 317 | + public Policies() { | |
| 318 | + this(defaultLoadBalancingPolicy(), defaultReconnectionPolicy(), defaultRetryPolicy(), defaultAddressTranslater(), defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy()); | |
| 319 | + } | |
| 320 | + | |
| 321 | + /** | |
| 322 | + * @deprecated this constructor is provided for backward compatibility. | |
| 323 | + * Use {@link #builder()} instead. | |
| 324 | + */ | |
| 325 | + public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy) { | |
| 326 | + this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, defaultAddressTranslater(), defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy()); | |
| 327 | + } | |
| 328 | + | |
| 329 | + /** | |
| 330 | + * @deprecated this constructor is provided for backward compatibility. | |
| 331 | + * Use {@link #builder()} instead. | |
| 332 | + */ | |
| 333 | + public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, AddressTranslater addressTranslater) { | |
| 334 | + this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, addressTranslater, defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy()); | |
| 335 | + } | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * @deprecated this constructor is provided for backward compatibility. | |
| 339 | + * Use {@link #builder()} instead. | |
| 340 | + */ | |
| 341 | + public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, SpeculativeExecutionPolicy speculativeExecutionPolicy) { | |
| 342 | + this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, defaultAddressTranslater(), defaultTimestampGenerator(), speculativeExecutionPolicy); | |
| 343 | + } | |
| 344 | + | |
| 345 | + /** | |
| 346 | + * @deprecated this constructor is provided for backward compatibility. | |
| 347 | + * Use {@link #builder()} instead. | |
| 348 | + */ | |
| 349 | + public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, AddressTranslater addressTranslater, TimestampGenerator timestampGenerator) { | |
| 350 | + this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, addressTranslater, timestampGenerator, defaultSpeculativeExecutionPolicy()); | |
| 351 | + } | |
| 352 | + | |
| 353 | + /** | |
| 354 | + * @deprecated this constructor is provided for backward compatibility. | |
| 355 | + * Use {@link #builder()} instead. | |
| 356 | + */ | |
| 357 | + public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, AddressTranslater addressTranslater, SpeculativeExecutionPolicy speculativeExecutionPolicy) { | |
| 358 | + this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, addressTranslater, defaultTimestampGenerator(), speculativeExecutionPolicy); | |
| 359 | + } | |
| 238 | 360 | } |