From 84a8db5603e2ad111a507ac4c9d8bc08398fd500 Mon Sep 17 00:00:00 2001 From: Kushagra Thapar Date: Tue, 19 May 2020 12:05:50 -0700 Subject: [PATCH 1/3] Moved ConsistencyPolicy to implementation --- .../java/com/azure/cosmos/ConnectionMode.java | 4 ---- .../com/azure/cosmos/ConsistencyLevel.java | 2 ++ .../ConsistencyPolicy.java | 4 +--- .../cosmos/implementation/DatabaseAccount.java | 4 +--- .../implementation/JsonSerializable.java | 1 - .../models/ConflictResolutionPolicy.java | 18 ------------------ .../cosmos/models/ModelBridgeInternal.java | 5 +---- 7 files changed, 5 insertions(+), 33 deletions(-) rename sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/{models => implementation}/ConsistencyPolicy.java (97%) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConnectionMode.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConnectionMode.java index ab421059674c..cefe9935cc9f 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConnectionMode.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConnectionMode.java @@ -7,10 +7,6 @@ * Represents the connection mode to be used by the client in the Azure Cosmos DB database service. *

* DIRECT and GATEWAY connectivity modes are supported. DIRECT is the default. - * Refer to <see>http://azure.microsoft.com/documentation/articles/documentdb- - * interactions-with-resources/#connectivity-options</see> for additional - * details. - *

*/ public enum ConnectionMode { diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConsistencyLevel.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConsistencyLevel.java index b88edb4f706d..4131d2df7f3c 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConsistencyLevel.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/ConsistencyLevel.java @@ -11,6 +11,8 @@ *

* The requested ConsistencyLevel must match or be weaker than that provisioned for the database account. Consistency * levels by order of strength are STRONG, BOUNDED_STALENESS, SESSION and EVENTUAL. + * + * Refer to consistency level documentation for additional details: https://docs.microsoft.com/en-us/azure/cosmos-db/consistency-levels */ public enum ConsistencyLevel { diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConsistencyPolicy.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java similarity index 97% rename from sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConsistencyPolicy.java rename to sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java index 7f41b43f0a35..011835faffc8 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConsistencyPolicy.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java @@ -1,12 +1,10 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -package com.azure.cosmos.models; +package com.azure.cosmos.implementation; import com.azure.cosmos.ConsistencyLevel; -import com.azure.cosmos.implementation.Constants; -import com.azure.cosmos.implementation.JsonSerializable; import com.fasterxml.jackson.databind.node.ObjectNode; import com.azure.cosmos.implementation.guava25.base.CaseFormat; diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DatabaseAccount.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DatabaseAccount.java index f4fbc676efc8..bd8357e5edde 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DatabaseAccount.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DatabaseAccount.java @@ -4,9 +4,7 @@ package com.azure.cosmos.implementation; import com.azure.cosmos.BridgeInternal; -import com.azure.cosmos.models.ConsistencyPolicy; import com.azure.cosmos.models.DatabaseAccountLocation; -import com.azure.cosmos.models.ModelBridgeInternal; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.node.ObjectNode; import com.azure.cosmos.implementation.apachecommons.lang.ObjectUtils; @@ -273,7 +271,7 @@ public void setEnableMultipleWriteLocations(boolean value) { public void populatePropertyBag() { super.populatePropertyBag(); if (this.consistencyPolicy != null) { - ModelBridgeInternal.populatePropertyBag(this.consistencyPolicy); + this.consistencyPolicy.populatePropertyBag(); BridgeInternal.setProperty(this, Constants.Properties.USER_CONSISTENCY_POLICY, this.consistencyPolicy); } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java index c90bdcca9c5c..8f54befa71f5 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java @@ -5,7 +5,6 @@ import com.azure.cosmos.models.CompositePath; import com.azure.cosmos.models.ConflictResolutionPolicy; -import com.azure.cosmos.models.ConsistencyPolicy; import com.azure.cosmos.models.CosmosError; import com.azure.cosmos.models.DatabaseAccountLocation; import com.azure.cosmos.models.ExcludedPath; diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java index d068c8d799d8..3257a512580b 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java @@ -147,24 +147,6 @@ public static ConflictResolutionPolicy createCustomPolicy() { this.jsonSerializable = new JsonSerializable(); } - /** - * Instantiates a new Conflict resolution policy. - * - * @param jsonString the json string - */ - public ConflictResolutionPolicy(String jsonString) { - this.jsonSerializable = new JsonSerializable(jsonString); - } - - /** - * Instantiates a new Conflict resolution policy. - * - * @param objectNode the object node. - */ - ConflictResolutionPolicy(ObjectNode objectNode) { - this.jsonSerializable = new JsonSerializable(objectNode); - } - /** * Gets the {@link ConflictResolutionMode} in the Azure Cosmos DB service. * By default it is {@link ConflictResolutionMode#LAST_WRITER_WINS}. diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java index ac3e3e4c206b..fc2cd34bce7a 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java @@ -13,6 +13,7 @@ import com.azure.cosmos.CosmosTrigger; import com.azure.cosmos.CosmosUserDefinedFunction; import com.azure.cosmos.implementation.Conflict; +import com.azure.cosmos.implementation.ConsistencyPolicy; import com.azure.cosmos.implementation.CosmosItemProperties; import com.azure.cosmos.implementation.CosmosResourceType; import com.azure.cosmos.implementation.Database; @@ -615,8 +616,6 @@ public static void populatePropertyBag(T t) { ((CompositePath) t).populatePropertyBag(); } else if (t instanceof ConflictResolutionPolicy) { ((ConflictResolutionPolicy) t).populatePropertyBag(); - } else if (t instanceof ConsistencyPolicy) { - ((ConsistencyPolicy) t).populatePropertyBag(); } else if (t instanceof DatabaseAccountLocation) { ((DatabaseAccountLocation) t).populatePropertyBag(); } else if (t instanceof ExcludedPath) { @@ -652,8 +651,6 @@ public static JsonSerializable getJsonSerializable(T t) { return ((CompositePath) t).getJsonSerializable(); } else if (t instanceof ConflictResolutionPolicy) { return ((ConflictResolutionPolicy) t).getJsonSerializable(); - } else if (t instanceof ConsistencyPolicy) { - return ((ConsistencyPolicy) t).getJsonSerializable(); } else if (t instanceof DatabaseAccountLocation) { return ((DatabaseAccountLocation) t).getJsonSerializable(); } else if (t instanceof ExcludedPath) { From 258b7e835a4438bf84af495942acfcdaa2bdc30d Mon Sep 17 00:00:00 2001 From: Kushagra Thapar Date: Tue, 19 May 2020 13:41:17 -0700 Subject: [PATCH 2/3] Fixed public constructor of ConsistencyPolicy. Added package-private constructors for ConflictResolutionPolicy for Json serializable ModelBridgeInternal APIs --- .../implementation/ConsistencyPolicy.java | 4 ++-- .../models/ConflictResolutionPolicy.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java index 011835faffc8..02047eb5896b 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java @@ -34,7 +34,7 @@ public ConsistencyPolicy() { * @param objectNode the {@link ObjectNode} that represent the * {@link JsonSerializable} */ - ConsistencyPolicy(ObjectNode objectNode) { + public ConsistencyPolicy(ObjectNode objectNode) { this.jsonSerializable = new JsonSerializable(objectNode); } @@ -43,7 +43,7 @@ public ConsistencyPolicy() { * * @param jsonString the json string that represents the consistency policy. */ - ConsistencyPolicy(String jsonString) { + public ConsistencyPolicy(String jsonString) { this.jsonSerializable = new JsonSerializable(jsonString); } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java index 3257a512580b..5ff66715907d 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ConflictResolutionPolicy.java @@ -147,6 +147,24 @@ public static ConflictResolutionPolicy createCustomPolicy() { this.jsonSerializable = new JsonSerializable(); } + /** + * Instantiates a new Conflict resolution policy. + * + * @param jsonString the json string + */ + ConflictResolutionPolicy(String jsonString) { + this.jsonSerializable = new JsonSerializable(jsonString); + } + + /** + * Instantiates a new Conflict resolution policy. + * + * @param objectNode the object node. + */ + ConflictResolutionPolicy(ObjectNode objectNode) { + this.jsonSerializable = new JsonSerializable(objectNode); + } + /** * Gets the {@link ConflictResolutionMode} in the Azure Cosmos DB service. * By default it is {@link ConflictResolutionMode#LAST_WRITER_WINS}. From 0b026e8de71716e7734e3054ca89ae9fd60a22a5 Mon Sep 17 00:00:00 2001 From: Kushagra Thapar Date: Tue, 19 May 2020 13:57:47 -0700 Subject: [PATCH 3/3] Re-extending JsonSerializable since ConsistencyPolicy is internal now --- .../implementation/ConsistencyPolicy.java | 28 +++++++------------ .../implementation/JsonSerializable.java | 1 - 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java index 02047eb5896b..366e892c8f30 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConsistencyPolicy.java @@ -13,19 +13,17 @@ /** * Encapsulates the properties for consistency policy in the Azure Cosmos DB database service. */ -public final class ConsistencyPolicy { +public final class ConsistencyPolicy extends JsonSerializable { private static final ConsistencyLevel DEFAULT_DEFAULT_CONSISTENCY_LEVEL = ConsistencyLevel.SESSION; private static final int DEFAULT_MAX_STALENESS_INTERVAL = 5; private static final int DEFAULT_MAX_STALENESS_PREFIX = 100; - private JsonSerializable jsonSerializable; /** * Constructor. */ public ConsistencyPolicy() { - this.jsonSerializable = new JsonSerializable(); } /** @@ -35,7 +33,7 @@ public ConsistencyPolicy() { * {@link JsonSerializable} */ public ConsistencyPolicy(ObjectNode objectNode) { - this.jsonSerializable = new JsonSerializable(objectNode); + super(objectNode); } /** @@ -44,7 +42,7 @@ public ConsistencyPolicy(ObjectNode objectNode) { * @param jsonString the json string that represents the consistency policy. */ public ConsistencyPolicy(String jsonString) { - this.jsonSerializable = new JsonSerializable(jsonString); + super(jsonString); } /** @@ -55,13 +53,13 @@ public ConsistencyPolicy(String jsonString) { public ConsistencyLevel getDefaultConsistencyLevel() { ConsistencyLevel result = ConsistencyPolicy.DEFAULT_DEFAULT_CONSISTENCY_LEVEL; - String consistencyLevelString = this.jsonSerializable.getString(Constants.Properties.DEFAULT_CONSISTENCY_LEVEL); + String consistencyLevelString = super.getString(Constants.Properties.DEFAULT_CONSISTENCY_LEVEL); try { result = ConsistencyLevel .valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, consistencyLevelString)); } catch (IllegalArgumentException e) { // ignore the exception and return the default - this.jsonSerializable.getLogger().warn("Unknown consistency level {}, value ignored.", consistencyLevelString); + this.getLogger().warn("Unknown consistency level {}, value ignored.", consistencyLevelString); } return result; } @@ -73,7 +71,7 @@ public ConsistencyLevel getDefaultConsistencyLevel() { * @return the ConsistencyPolicy. */ public ConsistencyPolicy setDefaultConsistencyLevel(ConsistencyLevel level) { - this.jsonSerializable.set(Constants.Properties.DEFAULT_CONSISTENCY_LEVEL, level.toString()); + super.set(Constants.Properties.DEFAULT_CONSISTENCY_LEVEL, level.toString()); return this; } @@ -84,7 +82,7 @@ public ConsistencyPolicy setDefaultConsistencyLevel(ConsistencyLevel level) { * @return the max staleness prefix. */ public int getMaxStalenessPrefix() { - Integer value = this.jsonSerializable.getInt(Constants.Properties.MAX_STALENESS_PREFIX); + Integer value = super.getInt(Constants.Properties.MAX_STALENESS_PREFIX); if (value == null) { return ConsistencyPolicy.DEFAULT_MAX_STALENESS_PREFIX; } @@ -99,7 +97,7 @@ public int getMaxStalenessPrefix() { * @return the ConsistencyPolicy. */ public ConsistencyPolicy setMaxStalenessPrefix(int maxStalenessPrefix) { - this.jsonSerializable.set(Constants.Properties.MAX_STALENESS_PREFIX, maxStalenessPrefix); + super.set(Constants.Properties.MAX_STALENESS_PREFIX, maxStalenessPrefix); return this; } @@ -110,7 +108,7 @@ public ConsistencyPolicy setMaxStalenessPrefix(int maxStalenessPrefix) { * @return the max staleness prefix. */ public Duration getMaxStalenessInterval() { - Integer value = this.jsonSerializable.getInt(Constants.Properties.MAX_STALENESS_INTERVAL_IN_SECONDS); + Integer value = super.getInt(Constants.Properties.MAX_STALENESS_INTERVAL_IN_SECONDS); if (value == null) { return Duration.ofSeconds(ConsistencyPolicy.DEFAULT_MAX_STALENESS_INTERVAL); } @@ -128,13 +126,7 @@ public ConsistencyPolicy setMaxStalenessInterval(Duration maxStalenessInterval) if (maxStalenessInterval == null) { throw new IllegalArgumentException("maxStalenessInterval should not be null"); } - this.jsonSerializable.set(Constants.Properties.MAX_STALENESS_INTERVAL_IN_SECONDS, maxStalenessInterval.getSeconds()); + super.set(Constants.Properties.MAX_STALENESS_INTERVAL_IN_SECONDS, maxStalenessInterval.getSeconds()); return this; } - - void populatePropertyBag() { - this.jsonSerializable.populatePropertyBag(); - } - - JsonSerializable getJsonSerializable() { return this.jsonSerializable; } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java index 8f54befa71f5..98eda9509534 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java @@ -687,7 +687,6 @@ public ObjectNode getPropertyBag() { boolean containsJsonSerializable(Class c) { return CompositePath.class.equals(c) || ConflictResolutionPolicy.class.equals(c) - || ConsistencyPolicy.class.equals(c) || DatabaseAccountLocation.class.equals(c) || ExcludedPath.class.equals(c) || IncludedPath.class.equals(c)