From 8ab6ed8bad47dc614d974dc6e04d0394458a594f Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Mon, 14 Dec 2020 01:21:05 -0800 Subject: [PATCH 01/17] Changes to run tests for multihash collections --- .../cosmos/implementation/HttpConstants.java | 2 +- .../implementation/RxDocumentClientImpl.java | 36 +++-- .../implementation/TestConfigurations.java | 2 +- .../routing/PartitionKeyInternalHelper.java | 31 ++++- .../com/azure/cosmos/models/PartitionKey.java | 11 ++ .../azure/cosmos/models/PartitionKind.java | 8 +- .../com/azure/cosmos/CosmosContainerTest.java | 46 +++++-- .../com/azure/cosmos/CosmosMultiHashTest.java | 130 ++++++++++++++++++ .../com/azure/cosmos/rx/TestSuiteBase.java | 4 + 9 files changed, 246 insertions(+), 24 deletions(-) create mode 100644 sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/HttpConstants.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/HttpConstants.java index ff0281744bc4..b0b528e12dcf 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/HttpConstants.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/HttpConstants.java @@ -261,7 +261,7 @@ public static class A_IMHeaderValues { } public static class Versions { - public static final String CURRENT_VERSION = "2018-12-31"; + public static final String CURRENT_VERSION = "2020-07-15"; public static final String QUERY_VERSION = "1.0"; public static final String AZURE_COSMOS_PROPERTIES_FILE_NAME = "azure-cosmos.properties"; diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java index fa68b4c1db91..ddfde689ac74 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java @@ -1222,19 +1222,33 @@ private static PartitionKeyInternal extractPartitionKeyValueFromDocument( InternalObjectNode document, PartitionKeyDefinition partitionKeyDefinition) { if (partitionKeyDefinition != null) { - String path = partitionKeyDefinition.getPaths().iterator().next(); - List parts = PathParser.getPathParts(path); - if (parts.size() >= 1) { - Object value = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, parts); - if (value == null || value.getClass() == ObjectNode.class) { - value = ModelBridgeInternal.getNonePartitionKey(partitionKeyDefinition); - } + switch (partitionKeyDefinition.getKind()) { + case HASH: + + String path = partitionKeyDefinition.getPaths().iterator().next(); + List parts = PathParser.getPathParts(path); + if (parts.size() >= 1) { + Object value = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, parts); + if (value == null || value.getClass() == ObjectNode.class) { + value = ModelBridgeInternal.getNonePartitionKey(partitionKeyDefinition); + } - if (value instanceof PartitionKeyInternal) { - return (PartitionKeyInternal) value; - } else { - return PartitionKeyInternal.fromObjectArray(Collections.singletonList(value), false); + if (value instanceof PartitionKeyInternal) { + return (PartitionKeyInternal) value; + } else { + return PartitionKeyInternal.fromObjectArray(Collections.singletonList(value), false); + } } + break; + case MULTI_HASH: + Object[] partitionKeyValues = new Object[partitionKeyDefinition.getPaths().size()]; + for(int path_iter = 0 ; path_iter < partitionKeyDefinition.getPaths().size(); path_iter++) + { + String partitionPath = partitionKeyDefinition.getPaths().get(path_iter); + List partitionPathParts = PathParser.getPathParts(partitionPath); + partitionKeyValues[path_iter] = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, partitionPathParts); + } + return PartitionKeyInternal.fromObjectArray(partitionKeyValues, false); } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java index 5018c9d5c8f1..0c370b5e392c 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java @@ -33,7 +33,7 @@ public final class TestConfigurations { private static Properties properties = loadProperties(); private final static String COSMOS_EMULATOR_KEY = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; - private final static String COSMOS_EMULATOR_HOST = "https://localhost:8081/"; + private final static String COSMOS_EMULATOR_HOST = "https://localhost:443/"; // REPLACE MASTER_KEY and HOST with values from your Azure Cosmos DB account. // The default values are credentials of the local emulator, which are not used in any production environment. diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java index de4750ece8aa..a9f43a06ef5a 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java @@ -92,6 +92,32 @@ static public String getEffectivePartitionKeyForHashPartitioningV2(PartitionKeyI } } + static String getEffectivePartitionKeyForMultiHashPartitioning(PartitionKeyInternal partitionKeyInternal) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < partitionKeyInternal.components.size(); i++) { + try(ByteBufferOutputStream byteArrayBuffer = new ByteBufferOutputStream()) { + + partitionKeyInternal.components.get(i).writeForHashingV2(byteArrayBuffer); + + + ByteBuffer byteBuffer = byteArrayBuffer.asByteBuffer(); + UInt128 hashAsUnit128 = MurmurHash3_128.hash128(byteBuffer.array(), byteBuffer.limit()); + + byte[] hash = uIntToBytes(hashAsUnit128); + Bytes.reverse(hash); + + // Reset 2 most significant bits, as max exclusive value is 'FF'. + // Plus one more just in case. + hash[0] &= 0x3F; + + stringBuilder.append(HexConvert.bytesToHex(hash)); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + return stringBuilder.toString(); + } + static String getEffectivePartitionKeyForHashPartitioning(PartitionKeyInternal partitionKeyInternal) { IPartitionKeyComponent[] truncatedComponents = new IPartitionKeyComponent[partitionKeyInternal.components.size()]; @@ -138,7 +164,7 @@ public static String getEffectivePartitionKeyString(PartitionKeyInternal partiti return MaximumExclusiveEffectivePartitionKey; } - if (partitionKeyInternal.components.size() < partitionKeyDefinition.getPaths().size()) { + if (partitionKeyInternal.components.size() < partitionKeyDefinition.getPaths().size() && partitionKeyDefinition.getKind() != PartitionKind.MULTI_HASH) { throw new IllegalArgumentException(RMResources.TooFewPartitionKeyComponents); } @@ -161,6 +187,9 @@ public static String getEffectivePartitionKeyString(PartitionKeyInternal partiti return getEffectivePartitionKeyForHashPartitioning(partitionKeyInternal); } + case MULTI_HASH: + return getEffectivePartitionKeyForMultiHashPartitioning(partitionKeyInternal); + default: return toHexEncodedBinaryString(partitionKeyInternal.components); } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java index 9f949b581780..9228208ba400 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java @@ -30,6 +30,17 @@ public PartitionKey(final Object key) { this.internalPartitionKey = PartitionKeyInternal.fromObjectArray(new Object[] {key}, true); } + /** + * Constructor. CREATE a new instance of the PartitionKey object. + * + * @param keys the value of partition keys. + */ + @SuppressWarnings("serial") + public PartitionKey(final Object[] keys) { + this.keyObject = keys; + this.internalPartitionKey = PartitionKeyInternal.fromObjectArray(keys, true); + } + /** * Gets the object used to create partition key * @return the partition key object diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java index e90d0ef54cda..fae534382467 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java @@ -10,7 +10,13 @@ public enum PartitionKind { /** * The Partition of a item is calculated based on the hash value of the PartitionKey. */ - HASH("Hash"); + HASH("Hash"), + + RANGE("Range"), + /** + * The Partition of a item is calculated based on the hash value of multiple PartitionKeys. + */ + MULTI_HASH("MultiHash"); PartitionKind(String overWireValue) { this.overWireValue = overWireValue; diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java index 272614e48a53..4bea50cd9d47 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java @@ -6,16 +6,9 @@ package com.azure.cosmos; +import com.azure.cosmos.implementation.Document; import com.azure.cosmos.implementation.HttpConstants; -import com.azure.cosmos.models.CosmosContainerProperties; -import com.azure.cosmos.models.CosmosContainerRequestOptions; -import com.azure.cosmos.models.CosmosContainerResponse; -import com.azure.cosmos.models.CosmosQueryRequestOptions; -import com.azure.cosmos.models.FeedRange; -import com.azure.cosmos.models.IndexingMode; -import com.azure.cosmos.models.IndexingPolicy; -import com.azure.cosmos.models.SqlQuerySpec; -import com.azure.cosmos.models.ThroughputProperties; +import com.azure.cosmos.models.*; import com.azure.cosmos.rx.TestSuiteBase; import com.azure.cosmos.util.CosmosPagedIterable; import org.testng.annotations.AfterClass; @@ -26,6 +19,7 @@ import java.util.List; import java.util.UUID; +import java.util.ArrayList; import static org.assertj.core.api.Assertions.assertThat; @@ -264,6 +258,40 @@ public void readAllContainers() throws Exception{ assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); } + @Test(groups = { "emulator" }, timeOut = TIMEOUT) + public void crudMultiHashContainerTest() throws Exception{ + String collectionName = UUID.randomUUID().toString(); + + PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); + partitionKeyDefinition.setKind(PartitionKind.MULTI_HASH); + partitionKeyDefinition.setVersion(PartitionKeyDefinitionVersion.V2); + ArrayList paths = new ArrayList<>(); + paths.add("/city"); + paths.add("/zipcode"); + partitionKeyDefinition.setPaths(paths); + + CosmosContainerProperties containerProperties = getCollectionDefinition(collectionName, partitionKeyDefinition); + + //MultiHash collection create + CosmosContainerResponse containerResponse = createdDatabase.createContainer(containerProperties); + validateContainerResponse(containerProperties, containerResponse); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getKind() == PartitionKind.MULTI_HASH); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getPaths().size() == paths.size()); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getPaths().get(0) == paths.get(0)); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getPaths().get(1) == paths.get(1)); + + //MultiHash collection read + CosmosContainer multiHashContainer = createdDatabase.getContainer(collectionName); + containerResponse = multiHashContainer.read(); + validateContainerResponse(containerProperties, containerResponse); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getKind() == PartitionKind.MULTI_HASH); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getPaths().size() == paths.size()); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getPaths().get(0) == paths.get(0)); + assertThat(containerResponse.getProperties().getPartitionKeyDefinition().getPaths().get(1) == paths.get(1)); + + //MultiHash collection delete + CosmosContainerResponse deleteResponse = multiHashContainer.delete(); + } @Test(groups = { "emulator" }, timeOut = TIMEOUT) public void queryContainer() throws Exception{ diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java new file mode 100644 index 000000000000..3d481c7b8424 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + * + */ + +package com.azure.cosmos; + +import com.azure.cosmos.implementation.HttpConstants; +import com.azure.cosmos.implementation.InternalObjectNode; +import com.azure.cosmos.implementation.apachecommons.lang.StringUtils; +import com.azure.cosmos.implementation.routing.PartitionKeyInternal; +import com.azure.cosmos.models.*; +import com.azure.cosmos.rx.TestSuiteBase; +import com.azure.cosmos.util.CosmosPagedIterable; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Factory; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CosmosMultiHashTest extends TestSuiteBase { + + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private String preExistingDatabaseId = CosmosDatabaseForTest.generateId(); + private CosmosClient client; + private CosmosDatabase createdDatabase; + private CosmosContainer createdMultiHashContainer; + + @Factory(dataProvider = "clientBuilders") + public CosmosMultiHashTest(CosmosClientBuilder clientBuilder) { + super(clientBuilder); + } + + @BeforeClass(groups = {"simple"}, timeOut = SETUP_TIMEOUT) + public void before_CosmosItemTest() { + + client = getClientBuilder().buildClient(); + createdDatabase = createSyncDatabase(client, preExistingDatabaseId); + String collectionName = UUID.randomUUID().toString(); + + PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); + partitionKeyDefinition.setKind(PartitionKind.MULTI_HASH); + partitionKeyDefinition.setVersion(PartitionKeyDefinitionVersion.V2); + ArrayList paths = new ArrayList<>(); + paths.add("/city"); + paths.add("/zipcode"); + partitionKeyDefinition.setPaths(paths); + + CosmosContainerProperties containerProperties = getCollectionDefinition(collectionName, partitionKeyDefinition); + + //MultiHash collection create + CosmosContainerResponse containerResponse = createdDatabase.createContainer(containerProperties); + + //MultiHash collection read + createdMultiHashContainer = createdDatabase.getContainer(collectionName); + } + + @AfterClass(groups = {"simple"}, timeOut = SHUTDOWN_TIMEOUT, alwaysRun = true) + public void afterClass() { + logger.info("starting ...."); + //MultiHash Collection delete + createdMultiHashContainer.delete(); + safeDeleteSyncDatabase(createdDatabase); + safeCloseSyncClient(client); + } + + @Test(groups = { "simple" }, timeOut = TIMEOUT) + public void itemCRUDTest() throws Exception { + + List pkIds = new ArrayList<>(); + pkIds.add("Redmond"); + pkIds.add("98052"); + + Object[] pkValue = new Object[2]; + pkValue[0] = pkIds.get(0); + pkValue[1] = pkIds.get(1); + + PartitionKey partitionKey = new PartitionKey(pkValue); + + String documentId = UUID.randomUUID().toString(); + ObjectNode properties = getDocumentDefinition(documentId, pkIds); + createdMultiHashContainer.createItem(properties); + + + CosmosItemResponse readResponse1 = createdMultiHashContainer.readItem( + documentId, partitionKey, ObjectNode.class); + + validateIdOfItemResponse(documentId, readResponse1); + assertThat(readResponse1.getItem().equals(properties)); + } + + private ObjectNode getDocumentDefinition(String documentId, List pkIds) throws JsonProcessingException { + + String json = String.format("{ " + + "\"id\": \"%s\", " + + "\"city\": \"%s\", " + + "\"zipcode\": \"%s\", " + + "\"sgmts\": [[6519456, 1471916863], [2498434, 1455671440]]" + + "}" + , documentId, pkIds.get(0), pkIds.get(1)); + return + OBJECT_MAPPER.readValue(json, ObjectNode.class); + } + + private void validateItemResponse(InternalObjectNode containerProperties, + CosmosItemResponse createResponse) { + // Basic validation + assertThat(BridgeInternal.getProperties(createResponse).getId()).isNotNull(); + assertThat(BridgeInternal.getProperties(createResponse).getId()) + .as("check Resource Id") + .isEqualTo(containerProperties.getId()); + } + + private void validateIdOfItemResponse(String expectedId, CosmosItemResponse createResponse) { + // Basic validation + assertThat(BridgeInternal.getProperties(createResponse).getId()).isNotNull(); + assertThat(BridgeInternal.getProperties(createResponse).getId()) + .as("check Resource Id") + .isEqualTo(expectedId); + } +} diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/rx/TestSuiteBase.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/rx/TestSuiteBase.java index 5a192f2e62b0..9486a1c48d10 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/rx/TestSuiteBase.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/rx/TestSuiteBase.java @@ -530,6 +530,10 @@ static protected CosmosContainerProperties getCollectionDefinition(String collec return collectionDefinition; } + static protected CosmosContainerProperties getCollectionDefinition(String collectionId, PartitionKeyDefinition partitionKeyDefinition) { + return new CosmosContainerProperties(collectionId, partitionKeyDefinition); + } + static protected CosmosContainerProperties getCollectionDefinitionWithRangeRangeIndexWithIdAsPartitionKey() { return getCollectionDefinitionWithRangeRangeIndex(Collections.singletonList("/id")); } From d5d3b472dc7644c9ff09cbaf650f414f87027302 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Thu, 7 Jan 2021 12:47:39 -0800 Subject: [PATCH 02/17] Changes. --- .../com/azure/cosmos/implementation/TestConfigurations.java | 2 +- .../src/test/java/com/azure/cosmos/CosmosMultiHashTest.java | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java index 0c370b5e392c..5018c9d5c8f1 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/TestConfigurations.java @@ -33,7 +33,7 @@ public final class TestConfigurations { private static Properties properties = loadProperties(); private final static String COSMOS_EMULATOR_KEY = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; - private final static String COSMOS_EMULATOR_HOST = "https://localhost:443/"; + private final static String COSMOS_EMULATOR_HOST = "https://localhost:8081/"; // REPLACE MASTER_KEY and HOST with values from your Azure Cosmos DB account. // The default values are credentials of the local emulator, which are not used in any production environment. diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index 3d481c7b8424..ed33b2c6f3ce 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -90,10 +90,8 @@ public void itemCRUDTest() throws Exception { ObjectNode properties = getDocumentDefinition(documentId, pkIds); createdMultiHashContainer.createItem(properties); - CosmosItemResponse readResponse1 = createdMultiHashContainer.readItem( documentId, partitionKey, ObjectNode.class); - validateIdOfItemResponse(documentId, readResponse1); assertThat(readResponse1.getItem().equals(properties)); } From 5459104dbfa4de2c375514799ad8e579c32fc242 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Fri, 15 Jan 2021 14:10:41 -0800 Subject: [PATCH 03/17] Parititionkey builder model as suggested by Mo. --- .../com/azure/cosmos/models/PartitionKey.java | 11 --- .../cosmos/models/PartitionKeyBuilder.java | 90 +++++++++++++++++++ 2 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java index 9228208ba400..9f949b581780 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKey.java @@ -30,17 +30,6 @@ public PartitionKey(final Object key) { this.internalPartitionKey = PartitionKeyInternal.fromObjectArray(new Object[] {key}, true); } - /** - * Constructor. CREATE a new instance of the PartitionKey object. - * - * @param keys the value of partition keys. - */ - @SuppressWarnings("serial") - public PartitionKey(final Object[] keys) { - this.keyObject = keys; - this.internalPartitionKey = PartitionKeyInternal.fromObjectArray(keys, true); - } - /** * Gets the object used to create partition key * @return the partition key object diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java new file mode 100644 index 000000000000..7fc13064c0f9 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java @@ -0,0 +1,90 @@ +package com.azure.cosmos.models; + +import com.azure.cosmos.implementation.Undefined; +import com.azure.cosmos.implementation.routing.PartitionKeyInternal; + +import java.util.ArrayList; +import java.util.List; + +public class PartitionKeyBuilder { + private List partitionKeyValues; + + /** + * Constructor. CREATE a new instance of the PartitionKeyBuilder object. + */ + public PartitionKeyBuilder() + { + this.partitionKeyValues = new ArrayList(); + } + + public PartitionKeyBuilder Add(String value) + { + this.partitionKeyValues.add(value); + return this; + } + + public PartitionKeyBuilder Add(double value) + { + this.partitionKeyValues.add(value); + return this; + } + + public PartitionKeyBuilder Add(boolean value) + { + this.partitionKeyValues.add(value); + return this; + } + + public PartitionKeyBuilder AddNullValue() + { + this.partitionKeyValues.add(null); + return this; + } + + public PartitionKeyBuilder AddNoneValue() + { + this.partitionKeyValues.add(PartitionKey.NONE); + return this; + } + + public PartitionKey Build() + { + // Why these checks? + // These changes are being added for SDK to support multiple paths in a partition key. + // + // Currently, when a resource does not specify a value for the PartitionKey, + // we assign a temporary value `PartitionKey.None` and later discern whether + // it is a PartitionKey.Undefined or PartitionKey.Empty based on the Collection Type. + // We retain this behaviour for single path partition keys. + // + // For collections with multiple path keys, absence of a partition key values is + // always treated as a PartitionKey.Undefined. + if(this.partitionKeyValues.size() == 0) + { + throw new IllegalArgumentException("No partition key value has been specified"); + } + + if(this.partitionKeyValues.size() == 1 && PartitionKey.NONE.equals(this.partitionKeyValues.get(0))) + { + return PartitionKey.NONE; + } + + PartitionKeyInternal partitionKeyInternal; + Object[] valueArray = new Object[this.partitionKeyValues.size()]; + for(int i = 0; i < this.partitionKeyValues.size(); i++) + { + Object val = this.partitionKeyValues.get(i); + if(PartitionKey.NONE.equals(val)) + { + valueArray[i] = Undefined.value(); + } + else + { + valueArray[i] = val; + } + } + + partitionKeyInternal = PartitionKeyInternal.fromObjectArray(valueArray, true); + return new PartitionKey(partitionKeyInternal); + } +} From e1c3001ddb51acc1f22d021ee5da6a00a7e9fbc7 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Fri, 15 Jan 2021 15:08:49 -0800 Subject: [PATCH 04/17] Apply suggestions from code review Co-authored-by: Mohammad Derakhshani --- .../test/java/com/azure/cosmos/CosmosContainerTest.java | 4 ++-- .../test/java/com/azure/cosmos/CosmosMultiHashTest.java | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java index 4bea50cd9d47..99743a1fba7f 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java @@ -259,7 +259,7 @@ public void readAllContainers() throws Exception{ } @Test(groups = { "emulator" }, timeOut = TIMEOUT) - public void crudMultiHashContainerTest() throws Exception{ + public void crudMultiHashContainer() throws Exception { String collectionName = UUID.randomUUID().toString(); PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); @@ -270,7 +270,7 @@ public void crudMultiHashContainerTest() throws Exception{ paths.add("/zipcode"); partitionKeyDefinition.setPaths(paths); - CosmosContainerProperties containerProperties = getCollectionDefinition(collectionName, partitionKeyDefinition); + CosmosContainerProperties containerProperties = getContainerDefinition(collectionName, partitionKeyDefinition); //MultiHash collection create CosmosContainerResponse containerResponse = createdDatabase.createContainer(containerProperties); diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index ed33b2c6f3ce..93d78ce50c71 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -55,7 +55,7 @@ public void before_CosmosItemTest() { paths.add("/zipcode"); partitionKeyDefinition.setPaths(paths); - CosmosContainerProperties containerProperties = getCollectionDefinition(collectionName, partitionKeyDefinition); + CosmosContainerProperties containerProperties = getContainerDefinition(collectionName, partitionKeyDefinition); //MultiHash collection create CosmosContainerResponse containerResponse = createdDatabase.createContainer(containerProperties); @@ -74,7 +74,7 @@ public void afterClass() { } @Test(groups = { "simple" }, timeOut = TIMEOUT) - public void itemCRUDTest() throws Exception { + public void itemCRUD() throws Exception { List pkIds = new ArrayList<>(); pkIds.add("Redmond"); @@ -96,7 +96,8 @@ public void itemCRUDTest() throws Exception { assertThat(readResponse1.getItem().equals(properties)); } - private ObjectNode getDocumentDefinition(String documentId, List pkIds) throws JsonProcessingException { + private ObjectNode getItem(String documentId, List pkIds) throws JsonProcessingException { + String json = String.format("{ " + "\"id\": \"%s\", " From dd77aaff98167b708e45a3e5a0ec7cfa80fb339d Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Tue, 16 Feb 2021 10:18:02 -0800 Subject: [PATCH 05/17] Pushing to remote. Cleanup --- .../implementation/RxDocumentClientImpl.java | 45 ++++++++++--------- .../cosmos/models/PartitionKeyBuilder.java | 5 +-- .../com/azure/cosmos/CosmosContainerTest.java | 10 ++++- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java index 22463ca1f525..05460a4da836 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java @@ -1240,32 +1240,33 @@ private static PartitionKeyInternal extractPartitionKeyValueFromDocument( if (partitionKeyDefinition != null) { switch (partitionKeyDefinition.getKind()) { case HASH: + String path = partitionKeyDefinition.getPaths().iterator().next(); + List parts = PathParser.getPathParts(path); + if (parts.size() >= 1) { + Object value = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, parts); + if (value == null || value.getClass() == ObjectNode.class) { + value = ModelBridgeInternal.getNonePartitionKey(partitionKeyDefinition); + } - String path = partitionKeyDefinition.getPaths().iterator().next(); - List parts = PathParser.getPathParts(path); - if (parts.size() >= 1) { - Object value = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, parts); - if (value == null || value.getClass() == ObjectNode.class) { - value = ModelBridgeInternal.getNonePartitionKey(partitionKeyDefinition); + if (value instanceof PartitionKeyInternal) { + return (PartitionKeyInternal) value; + } else { + return PartitionKeyInternal.fromObjectArray(Collections.singletonList(value), false); + } } + break; + case MULTI_HASH: + Object[] partitionKeyValues = new Object[partitionKeyDefinition.getPaths().size()]; + for(int path_iter = 0 ; path_iter < partitionKeyDefinition.getPaths().size(); path_iter++){ + String partitionPath = partitionKeyDefinition.getPaths().get(path_iter); + List partitionPathParts = PathParser.getPathParts(partitionPath); + partitionKeyValues[path_iter] = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, partitionPathParts); + } + return PartitionKeyInternal.fromObjectArray(partitionKeyValues, false); - if (value instanceof PartitionKeyInternal) { - return (PartitionKeyInternal) value; - } else { - return PartitionKeyInternal.fromObjectArray(Collections.singletonList(value), false); - } + default: + throw new IllegalArgumentException("Unrecognized Partition kind"); } - break; - case MULTI_HASH: - Object[] partitionKeyValues = new Object[partitionKeyDefinition.getPaths().size()]; - for(int path_iter = 0 ; path_iter < partitionKeyDefinition.getPaths().size(); path_iter++) - { - String partitionPath = partitionKeyDefinition.getPaths().get(path_iter); - List partitionPathParts = PathParser.getPathParts(partitionPath); - partitionKeyValues[path_iter] = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, partitionPathParts); - } - return PartitionKeyInternal.fromObjectArray(partitionKeyValues, false); - } } return null; diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java index 7fc13064c0f9..8b045c2a3f84 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java @@ -7,7 +7,7 @@ import java.util.List; public class PartitionKeyBuilder { - private List partitionKeyValues; + private final List partitionKeyValues; /** * Constructor. CREATE a new instance of the PartitionKeyBuilder object. @@ -71,8 +71,7 @@ public PartitionKey Build() PartitionKeyInternal partitionKeyInternal; Object[] valueArray = new Object[this.partitionKeyValues.size()]; - for(int i = 0; i < this.partitionKeyValues.size(); i++) - { + for(int i = 0; i < this.partitionKeyValues.size(); i++) { Object val = this.partitionKeyValues.get(i); if(PartitionKey.NONE.equals(val)) { diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java index 99743a1fba7f..5171880e614c 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java @@ -8,7 +8,15 @@ import com.azure.cosmos.implementation.Document; import com.azure.cosmos.implementation.HttpConstants; -import com.azure.cosmos.models.*; +import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.cosmos.models.CosmosContainerRequestOptions; +import com.azure.cosmos.models.CosmosContainerResponse; +import com.azure.cosmos.models.CosmosQueryRequestOptions; +import com.azure.cosmos.models.FeedRange; +import com.azure.cosmos.models.IndexingMode; +import com.azure.cosmos.models.IndexingPolicy; +import com.azure.cosmos.models.SqlQuerySpec; +import com.azure.cosmos.models.ThroughputProperties; import com.azure.cosmos.rx.TestSuiteBase; import com.azure.cosmos.util.CosmosPagedIterable; import org.testng.annotations.AfterClass; From 58c2746d4500e2e8b1878b7da3816941dd91aaed Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Tue, 6 Apr 2021 15:18:48 -0700 Subject: [PATCH 06/17] commit local changes --- .../implementation/RxDocumentClientImpl.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java index 131ec132e354..d993d1a75e06 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java @@ -1271,17 +1271,17 @@ private static PartitionKeyInternal extractPartitionKeyValueFromDocument( } } break; - case MULTI_HASH: - Object[] partitionKeyValues = new Object[partitionKeyDefinition.getPaths().size()]; - for(int path_iter = 0 ; path_iter < partitionKeyDefinition.getPaths().size(); path_iter++){ - String partitionPath = partitionKeyDefinition.getPaths().get(path_iter); - List partitionPathParts = PathParser.getPathParts(partitionPath); - partitionKeyValues[path_iter] = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, partitionPathParts); - } - return PartitionKeyInternal.fromObjectArray(partitionKeyValues, false); + case MULTI_HASH: + Object[] partitionKeyValues = new Object[partitionKeyDefinition.getPaths().size()]; + for(int path_iter = 0 ; path_iter < partitionKeyDefinition.getPaths().size(); path_iter++){ + String partitionPath = partitionKeyDefinition.getPaths().get(path_iter); + List partitionPathParts = PathParser.getPathParts(partitionPath); + partitionKeyValues[path_iter] = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, partitionPathParts); + } + return PartitionKeyInternal.fromObjectArray(partitionKeyValues, false); - default: - throw new IllegalArgumentException("Unrecognized Partition kind"); + default: + throw new IllegalArgumentException("Unrecognized Partition kind"); } } From 036b7ece633c581115e7475e2733c884616c822b Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Tue, 6 Apr 2021 15:53:25 -0700 Subject: [PATCH 07/17] Address PR comments. --- .../cosmos/models/PartitionKeyBuilder.java | 36 +++++++++---------- .../azure/cosmos/models/PartitionKind.java | 3 ++ .../com/azure/cosmos/CosmosContainerTest.java | 16 ++------- .../com/azure/cosmos/CosmosMultiHashTest.java | 16 +++++---- 4 files changed, 30 insertions(+), 41 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java index 8b045c2a3f84..fa52dbaf87e9 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java @@ -1,11 +1,17 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + package com.azure.cosmos.models; import com.azure.cosmos.implementation.Undefined; import com.azure.cosmos.implementation.routing.PartitionKeyInternal; +import com.azure.cosmos.util.Beta; +import com.azure.cosmos.util.Beta.SinceVersion; import java.util.ArrayList; import java.util.List; +@Beta(value = SinceVersion.V4_14_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public class PartitionKeyBuilder { private final List partitionKeyValues; @@ -17,38 +23,32 @@ public PartitionKeyBuilder() this.partitionKeyValues = new ArrayList(); } - public PartitionKeyBuilder Add(String value) - { + public PartitionKeyBuilder add(String value) { this.partitionKeyValues.add(value); return this; } - public PartitionKeyBuilder Add(double value) - { + public PartitionKeyBuilder add(double value) { this.partitionKeyValues.add(value); return this; } - public PartitionKeyBuilder Add(boolean value) - { + public PartitionKeyBuilder add(boolean value) { this.partitionKeyValues.add(value); return this; } - public PartitionKeyBuilder AddNullValue() - { + public PartitionKeyBuilder addNullValue() { this.partitionKeyValues.add(null); return this; } - public PartitionKeyBuilder AddNoneValue() - { + public PartitionKeyBuilder addNoneValue() { this.partitionKeyValues.add(PartitionKey.NONE); return this; } - public PartitionKey Build() - { + public PartitionKey build() { // Why these checks? // These changes are being added for SDK to support multiple paths in a partition key. // @@ -59,13 +59,11 @@ public PartitionKey Build() // // For collections with multiple path keys, absence of a partition key values is // always treated as a PartitionKey.Undefined. - if(this.partitionKeyValues.size() == 0) - { + if(this.partitionKeyValues.size() == 0) { throw new IllegalArgumentException("No partition key value has been specified"); } - if(this.partitionKeyValues.size() == 1 && PartitionKey.NONE.equals(this.partitionKeyValues.get(0))) - { + if(this.partitionKeyValues.size() == 1 && PartitionKey.NONE.equals(this.partitionKeyValues.get(0))) { return PartitionKey.NONE; } @@ -73,12 +71,10 @@ public PartitionKey Build() Object[] valueArray = new Object[this.partitionKeyValues.size()]; for(int i = 0; i < this.partitionKeyValues.size(); i++) { Object val = this.partitionKeyValues.get(i); - if(PartitionKey.NONE.equals(val)) - { + if(PartitionKey.NONE.equals(val)) { valueArray[i] = Undefined.value(); } - else - { + else { valueArray[i] = val; } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java index fae534382467..eff4daed627a 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java @@ -3,6 +3,9 @@ package com.azure.cosmos.models; +import com.azure.cosmos.util.Beta; +import com.azure.cosmos.util.Beta.SinceVersion; + /** * Specifies the partition scheme for an multiple-partitioned container in the Azure Cosmos DB database service. */ diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java index cd14edc21af8..ef0e97202927 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java @@ -13,19 +13,7 @@ import com.azure.cosmos.implementation.feedranges.FeedRangeInternal; import com.azure.cosmos.implementation.feedranges.FeedRangePartitionKeyRangeImpl; import com.azure.cosmos.implementation.routing.Range; -import com.azure.cosmos.models.ClientEncryptionIncludedPath; -import com.azure.cosmos.models.ClientEncryptionPolicy; -import com.azure.cosmos.models.ChangeFeedPolicy; -import com.azure.cosmos.models.CosmosContainerProperties; -import com.azure.cosmos.models.CosmosContainerRequestOptions; -import com.azure.cosmos.models.CosmosContainerResponse; -import com.azure.cosmos.models.CosmosQueryRequestOptions; -import com.azure.cosmos.models.FeedRange; -import com.azure.cosmos.models.IndexingMode; -import com.azure.cosmos.models.IndexingPolicy; -import com.azure.cosmos.models.PartitionKey; -import com.azure.cosmos.models.SqlQuerySpec; -import com.azure.cosmos.models.ThroughputProperties; +import com.azure.cosmos.models.*; import com.azure.cosmos.rx.TestSuiteBase; import com.azure.cosmos.util.CosmosPagedIterable; import org.testng.annotations.AfterClass; @@ -694,7 +682,7 @@ public void crudMultiHashContainer() throws Exception { paths.add("/zipcode"); partitionKeyDefinition.setPaths(paths); - CosmosContainerProperties containerProperties = getContainerDefinition(collectionName, partitionKeyDefinition); + CosmosContainerProperties containerProperties = getCollectionDefinition(collectionName, partitionKeyDefinition); //MultiHash collection create CosmosContainerResponse containerResponse = createdDatabase.createContainer(containerProperties); diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index 93d78ce50c71..224241c0537b 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -6,13 +6,15 @@ package com.azure.cosmos; -import com.azure.cosmos.implementation.HttpConstants; import com.azure.cosmos.implementation.InternalObjectNode; -import com.azure.cosmos.implementation.apachecommons.lang.StringUtils; -import com.azure.cosmos.implementation.routing.PartitionKeyInternal; -import com.azure.cosmos.models.*; +import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.cosmos.models.CosmosContainerResponse; +import com.azure.cosmos.models.CosmosItemResponse; +import com.azure.cosmos.models.PartitionKey; +import com.azure.cosmos.models.PartitionKeyDefinition; +import com.azure.cosmos.models.PartitionKind; +import com.azure.cosmos.models.PartitionKeyDefinitionVersion; import com.azure.cosmos.rx.TestSuiteBase; -import com.azure.cosmos.util.CosmosPagedIterable; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -55,7 +57,7 @@ public void before_CosmosItemTest() { paths.add("/zipcode"); partitionKeyDefinition.setPaths(paths); - CosmosContainerProperties containerProperties = getContainerDefinition(collectionName, partitionKeyDefinition); + CosmosContainerProperties containerProperties = getCollectionDefinition(collectionName, partitionKeyDefinition); //MultiHash collection create CosmosContainerResponse containerResponse = createdDatabase.createContainer(containerProperties); @@ -87,7 +89,7 @@ public void itemCRUD() throws Exception { PartitionKey partitionKey = new PartitionKey(pkValue); String documentId = UUID.randomUUID().toString(); - ObjectNode properties = getDocumentDefinition(documentId, pkIds); + ObjectNode properties = getItem(documentId, pkIds); createdMultiHashContainer.createItem(properties); CosmosItemResponse readResponse1 = createdMultiHashContainer.readItem( From f2905f4f616ef0e0f2845e87cf764ee8ca3ef513 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Tue, 6 Apr 2021 16:09:59 -0700 Subject: [PATCH 08/17] Address code comments. --- .../routing/PartitionKeyInternalHelper.java | 8 +++++--- .../main/java/com/azure/cosmos/models/PartitionKind.java | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java index 120cfe6ef51c..1237e9598e68 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java @@ -9,6 +9,7 @@ import com.azure.cosmos.implementation.ByteBufferOutputStream; import com.azure.cosmos.implementation.Bytes; import com.azure.cosmos.implementation.RMResources; +import com.azure.cosmos.util.Beta; import java.io.IOException; import java.nio.ByteBuffer; @@ -35,6 +36,8 @@ public class PartitionKeyInternalHelper { (byte) 0x3F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); + private static final Integer HashV2EPKLength = 32; + static byte[] uIntToBytes(UInt128 unit) { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 2); buffer.putLong(unit.low); @@ -93,13 +96,11 @@ static public String getEffectivePartitionKeyForHashPartitioningV2(PartitionKeyI } static String getEffectivePartitionKeyForMultiHashPartitioning(PartitionKeyInternal partitionKeyInternal) { - StringBuilder stringBuilder = new StringBuilder(); + StringBuilder stringBuilder = new StringBuilder(partitionKeyInternal.components.size() * HashV2EPKLength); for (int i = 0; i < partitionKeyInternal.components.size(); i++) { try(ByteBufferOutputStream byteArrayBuffer = new ByteBufferOutputStream()) { - partitionKeyInternal.components.get(i).writeForHashingV2(byteArrayBuffer); - ByteBuffer byteBuffer = byteArrayBuffer.asByteBuffer(); UInt128 hashAsUnit128 = MurmurHash3_128.hash128(byteBuffer.array(), byteBuffer.limit()); @@ -115,6 +116,7 @@ static String getEffectivePartitionKeyForMultiHashPartitioning(PartitionKeyInter throw new IllegalArgumentException(e); } } + return stringBuilder.toString(); } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java index eff4daed627a..bef4e13c37e5 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java @@ -19,6 +19,7 @@ public enum PartitionKind { /** * The Partition of a item is calculated based on the hash value of multiple PartitionKeys. */ + MULTI_HASH("MultiHash"); PartitionKind(String overWireValue) { From 24610d6165ebcf12c953b89a357e973a721804dd Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Tue, 6 Apr 2021 16:44:39 -0700 Subject: [PATCH 09/17] Resolve code comments --- .../java/com/azure/cosmos/models/PartitionKeyBuilder.java | 3 +-- .../main/java/com/azure/cosmos/models/PartitionKind.java | 2 +- .../src/main/java/com/azure/cosmos/util/Beta.java | 7 ++----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java index fa52dbaf87e9..762004633240 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java @@ -18,8 +18,7 @@ public class PartitionKeyBuilder { /** * Constructor. CREATE a new instance of the PartitionKeyBuilder object. */ - public PartitionKeyBuilder() - { + public PartitionKeyBuilder() { this.partitionKeyValues = new ArrayList(); } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java index bef4e13c37e5..3a5831ad3699 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java @@ -19,7 +19,7 @@ public enum PartitionKind { /** * The Partition of a item is calculated based on the hash value of multiple PartitionKeys. */ - + @Beta(value = SinceVersion.V4_14_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) MULTI_HASH("MultiHash"); PartitionKind(String overWireValue) { diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java index a79f2eafcd1c..d50e3445124c 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java @@ -9,14 +9,11 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import static java.lang.annotation.ElementType.CONSTRUCTOR; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.ElementType.PARAMETER; -import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.ElementType.*; @Documented @Retention(RetentionPolicy.CLASS) -@Target({ TYPE, METHOD, PARAMETER, CONSTRUCTOR }) +@Target({ TYPE, METHOD, PARAMETER, CONSTRUCTOR, FIELD }) @Inherited /** * Indicates functionality that is in preview and as such is subject to change in non-backwards compatible ways in future releases, From 5a741c589193532c1f290c09515489e5c6e93dbf Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Wed, 7 Apr 2021 18:07:37 -0700 Subject: [PATCH 10/17] Add tests. --- .../com/azure/cosmos/CosmosMultiHashTest.java | 91 ++++++++++++++++--- 1 file changed, 76 insertions(+), 15 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index 224241c0537b..2ee8a50374e5 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -6,23 +6,21 @@ package com.azure.cosmos; +import com.azure.cosmos.implementation.Document; import com.azure.cosmos.implementation.InternalObjectNode; -import com.azure.cosmos.models.CosmosContainerProperties; -import com.azure.cosmos.models.CosmosContainerResponse; -import com.azure.cosmos.models.CosmosItemResponse; -import com.azure.cosmos.models.PartitionKey; -import com.azure.cosmos.models.PartitionKeyDefinition; -import com.azure.cosmos.models.PartitionKind; -import com.azure.cosmos.models.PartitionKeyDefinitionVersion; +import com.azure.cosmos.models.*; + import com.azure.cosmos.rx.TestSuiteBase; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import org.apache.http.HttpStatus; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Factory; import org.testng.annotations.Test; +import javax.print.Doc; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -82,15 +80,15 @@ public void itemCRUD() throws Exception { pkIds.add("Redmond"); pkIds.add("98052"); - Object[] pkValue = new Object[2]; - pkValue[0] = pkIds.get(0); - pkValue[1] = pkIds.get(1); - - PartitionKey partitionKey = new PartitionKey(pkValue); + PartitionKey partitionKey = + new PartitionKeyBuilder() + .add(pkIds.get(0)) + .add(pkIds.get(1)) + .build(); String documentId = UUID.randomUUID().toString(); ObjectNode properties = getItem(documentId, pkIds); - createdMultiHashContainer.createItem(properties); + createdMultiHashContainer.createItem(properties); CosmosItemResponse readResponse1 = createdMultiHashContainer.readItem( documentId, partitionKey, ObjectNode.class); @@ -104,8 +102,7 @@ private ObjectNode getItem(String documentId, List pkIds) throws JsonPro String json = String.format("{ " + "\"id\": \"%s\", " + "\"city\": \"%s\", " - + "\"zipcode\": \"%s\", " - + "\"sgmts\": [[6519456, 1471916863], [2498434, 1455671440]]" + + "\"zipcode\": \"%s\" " + "}" , documentId, pkIds.get(0), pkIds.get(1)); return @@ -128,4 +125,68 @@ private void validateIdOfItemResponse(String expectedId, CosmosItemResponse docs = new ArrayList(3); + + Document doc = new Document(); + doc.setId(UUID.randomUUID().toString()); + doc.set("city", "Redmond"); + doc.set("zipcode", "98052"); + docs.add(doc); + + Document doc1 = new Document(); + doc1.setId(UUID.randomUUID().toString()); + doc1.set("city", "Pittsburgh"); + doc1.set("zipcode", "15232"); + docs.add(doc1); + + Document doc2 = new Document(); + doc2.setId(UUID.randomUUID().toString()); + doc2.set("city", "Stonybrook"); + doc2.set("zipcode", "11790"); + docs.add(doc2); + + //Document Create + { + createdMultiHashContainer.createItem(doc); + createdMultiHashContainer.createItem(doc1); + createdMultiHashContainer.createItem(doc2); + } + //Document Create - Negative test + { + PartitionKey partitionKey = + new PartitionKeyBuilder() + .add("Redmond") + .build(); + CosmosItemResponse response = + createdMultiHashContainer.createItem(doc, partitionKey, new CosmosItemRequestOptions()); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + + //Document Read + { + for (int i = 0; i < docs.size(); i++) { + Document doc_current = docs.get(i); + PartitionKey partitionKey = new PartitionKeyBuilder() + .add(doc_current.getString("city")) + .add(doc_current.getString("zipcode")) + .build(); + CosmosItemResponse response = createdMultiHashContainer.readItem(doc_current.getId(), partitionKey, Document.class); + assertThat(doc_current.toJson()).isEqualTo(response.getItem().toJson()); + } + } + + + } + catch (Exception e) + { + assertThat(false); + } + + } } From 29bcfc91bb2e0192823b8fbfcbdaeb08b06e4ffe Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Wed, 7 Apr 2021 18:41:11 -0700 Subject: [PATCH 11/17] Well, when does this stop. --- .../cosmos/models/PartitionKeyBuilder.java | 27 +++++++++++++++++++ .../com/azure/cosmos/CosmosMultiHashTest.java | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java index 762004633240..b2f47eb95a90 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java @@ -22,31 +22,58 @@ public PartitionKeyBuilder() { this.partitionKeyValues = new ArrayList(); } + /** + * Adds partition value of type string + * @param value The value of type string to be used as partition key + * @return The current PartitionKeyBuilder object + */ public PartitionKeyBuilder add(String value) { this.partitionKeyValues.add(value); return this; } + /** + * Adds partition value of type double + * @param value The value of type double to be used as partition key + * @return The current PartitionKeyBuilder object + */ public PartitionKeyBuilder add(double value) { this.partitionKeyValues.add(value); return this; } + /** + * Adds partition value of type boolean + * @param value The value of type boolean to be used as partition key + * @return The current PartitionKeyBuilder object + */ public PartitionKeyBuilder add(boolean value) { this.partitionKeyValues.add(value); return this; } + /** + * Adds a null partition key value + * @return The current PartitionKeyBuilder object + */ public PartitionKeyBuilder addNullValue() { this.partitionKeyValues.add(null); return this; } + /** + * Adds a None Partition Key + * @return The current PartitionKeyBuilder object + */ public PartitionKeyBuilder addNoneValue() { this.partitionKeyValues.add(PartitionKey.NONE); return this; } + /** + * Builds a new instance of the type PartitionKey with the specified Partition Key values. + * @return PartitionKey object + */ public PartitionKey build() { // Why these checks? // These changes are being added for SDK to support multiple paths in a partition key. diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index 2ee8a50374e5..d5221bc716af 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -181,7 +181,7 @@ private void validateQueryForMultiHash() throws Exception { } } - + } catch (Exception e) { From 14bd7a25607e7277388c642791ca79b64d1939b1 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Wed, 7 Apr 2021 18:57:04 -0700 Subject: [PATCH 12/17] No more comments. --- .../com/azure/cosmos/CosmosContainerTest.java | 17 ++++++++++++++++- .../com/azure/cosmos/CosmosMultiHashTest.java | 10 +++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java index ef0e97202927..1322f3fcf2b9 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosContainerTest.java @@ -13,7 +13,22 @@ import com.azure.cosmos.implementation.feedranges.FeedRangeInternal; import com.azure.cosmos.implementation.feedranges.FeedRangePartitionKeyRangeImpl; import com.azure.cosmos.implementation.routing.Range; -import com.azure.cosmos.models.*; +import com.azure.cosmos.models.ClientEncryptionIncludedPath; +import com.azure.cosmos.models.ClientEncryptionPolicy; +import com.azure.cosmos.models.ChangeFeedPolicy; +import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.cosmos.models.CosmosContainerRequestOptions; +import com.azure.cosmos.models.CosmosContainerResponse; +import com.azure.cosmos.models.CosmosQueryRequestOptions; +import com.azure.cosmos.models.FeedRange; +import com.azure.cosmos.models.IndexingMode; +import com.azure.cosmos.models.IndexingPolicy; +import com.azure.cosmos.models.PartitionKey; +import com.azure.cosmos.models.PartitionKeyDefinition; +import com.azure.cosmos.models.PartitionKeyDefinitionVersion; +import com.azure.cosmos.models.PartitionKind; +import com.azure.cosmos.models.SqlQuerySpec; +import com.azure.cosmos.models.ThroughputProperties; import com.azure.cosmos.rx.TestSuiteBase; import com.azure.cosmos.util.CosmosPagedIterable; import org.testng.annotations.AfterClass; diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index d5221bc716af..fcf25dc1bc9c 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -8,8 +8,16 @@ import com.azure.cosmos.implementation.Document; import com.azure.cosmos.implementation.InternalObjectNode; -import com.azure.cosmos.models.*; +import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.cosmos.models.CosmosContainerResponse; +import com.azure.cosmos.models.CosmosItemRequestOptions; +import com.azure.cosmos.models.CosmosItemResponse; +import com.azure.cosmos.models.PartitionKey; +import com.azure.cosmos.models.PartitionKeyBuilder; +import com.azure.cosmos.models.PartitionKeyDefinition; +import com.azure.cosmos.models.PartitionKeyDefinitionVersion; +import com.azure.cosmos.models.PartitionKind; import com.azure.cosmos.rx.TestSuiteBase; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; From 8f77d0dd3c8a2c800d43656893e3f065ea882aa7 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Wed, 5 May 2021 19:51:57 -0700 Subject: [PATCH 13/17] Update CosmosMultiHashTest.java Updating a test.. --- .../com/azure/cosmos/CosmosMultiHashTest.java | 69 ++++++++++++++++--- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index fcf25dc1bc9c..a87df3685651 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -13,12 +13,14 @@ import com.azure.cosmos.models.CosmosContainerResponse; import com.azure.cosmos.models.CosmosItemRequestOptions; import com.azure.cosmos.models.CosmosItemResponse; +import com.azure.cosmos.models.CosmosQueryRequestOptions; import com.azure.cosmos.models.PartitionKey; import com.azure.cosmos.models.PartitionKeyBuilder; import com.azure.cosmos.models.PartitionKeyDefinition; import com.azure.cosmos.models.PartitionKeyDefinitionVersion; import com.azure.cosmos.models.PartitionKind; import com.azure.cosmos.rx.TestSuiteBase; +import com.azure.cosmos.util.CosmosPagedIterable; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -74,7 +76,7 @@ public void before_CosmosItemTest() { @AfterClass(groups = {"simple"}, timeOut = SHUTDOWN_TIMEOUT, alwaysRun = true) public void afterClass() { - logger.info("starting ...."); + logger.info("starting cleanup...."); //MultiHash Collection delete createdMultiHashContainer.delete(); safeDeleteSyncDatabase(createdDatabase); @@ -135,7 +137,7 @@ private void validateIdOfItemResponse(String expectedId, CosmosItemResponse docs = new ArrayList(3); @@ -158,11 +160,21 @@ private void validateQueryForMultiHash() throws Exception { doc2.set("zipcode", "11790"); docs.add(doc2); + Document doc3 = new Document(); + doc3.setId(UUID.randomUUID().toString()); + doc3.set("city", "Stonybrook"); + doc3.set("zipcode", "11794"); + docs.add(doc3); + + Document doc4 = new Document(); + doc4.setId(UUID.randomUUID().toString()); + doc4.set("city", "Stonybrook"); + doc4.set("zipcode", "11791"); + docs.add(doc4); + //Document Create - { - createdMultiHashContainer.createItem(doc); - createdMultiHashContainer.createItem(doc1); - createdMultiHashContainer.createItem(doc2); + for (int i = 0; i < docs.size(); i++) { + createdMultiHashContainer.createItem(docs.get(i)); } //Document Create - Negative test { @@ -170,10 +182,13 @@ private void validateQueryForMultiHash() throws Exception { new PartitionKeyBuilder() .add("Redmond") .build(); - CosmosItemResponse response = - createdMultiHashContainer.createItem(doc, partitionKey, new CosmosItemRequestOptions()); - - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + try { + CosmosItemResponse response = + createdMultiHashContainer.createItem(doc, partitionKey, new CosmosItemRequestOptions()); + } + catch (Exception e) { + assertThat(e.getMessage().contains("Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document.\n" )); + } } //Document Read @@ -185,10 +200,42 @@ private void validateQueryForMultiHash() throws Exception { .add(doc_current.getString("zipcode")) .build(); CosmosItemResponse response = createdMultiHashContainer.readItem(doc_current.getId(), partitionKey, Document.class); - assertThat(doc_current.toJson()).isEqualTo(response.getItem().toJson()); } } + // Query Tests. + + for (int i = 0; i < docs.size(); i++) { + Document doc_current = docs.get(i); + //Build the partition key + PartitionKey partitionKey = new PartitionKeyBuilder() + .add(doc_current.getString("city")) + .add(doc_current.getString("zipcode")) + .build(); + + //Build the query request options + CosmosQueryRequestOptions queryRequestOptions = new CosmosQueryRequestOptions(); + queryRequestOptions.setPartitionKey(partitionKey); + + //Run the query. + String query = String.format("SELECT * from c where c.id = '%s'", doc_current.getId()); + + CosmosPagedIterable feedResponseIterator1 = + createdMultiHashContainer.queryItems(query, queryRequestOptions, InternalObjectNode.class); + assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); + + query = String.format("SELECT * from c where c.id = '%s'", doc_current.getId()); + queryRequestOptions = new CosmosQueryRequestOptions(); + feedResponseIterator1 = + createdMultiHashContainer.queryItems(query, queryRequestOptions, InternalObjectNode.class); + assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); + } + + String query = String.format("SELECT * from c where c.city = '%s'", docs.get(2).getString("city")); + CosmosQueryRequestOptions queryRequestOptions = new CosmosQueryRequestOptions(); + CosmosPagedIterable feedResponseIterator1 = + createdMultiHashContainer.queryItems(query, queryRequestOptions, InternalObjectNode.class); + assertThat(feedResponseIterator1.stream().count()).isEqualTo(3); } catch (Exception e) From 0b45b662bc3aec96c04fb61aa685e9a161304450 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Mon, 10 May 2021 14:02:06 -0700 Subject: [PATCH 14/17] Fix test failures --- .../java/com/azure/cosmos/CosmosMultiHashTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index a87df3685651..f60e4d4363f0 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -50,8 +50,8 @@ public CosmosMultiHashTest(CosmosClientBuilder clientBuilder) { super(clientBuilder); } - @BeforeClass(groups = {"simple"}, timeOut = SETUP_TIMEOUT) - public void before_CosmosItemTest() { + @BeforeClass(groups = {"emulator"}, timeOut = SETUP_TIMEOUT) + public void before_CosmosMultiHashTest() { client = getClientBuilder().buildClient(); createdDatabase = createSyncDatabase(client, preExistingDatabaseId); @@ -74,7 +74,7 @@ public void before_CosmosItemTest() { createdMultiHashContainer = createdDatabase.getContainer(collectionName); } - @AfterClass(groups = {"simple"}, timeOut = SHUTDOWN_TIMEOUT, alwaysRun = true) + @AfterClass(groups = {"emulator"}, timeOut = SHUTDOWN_TIMEOUT, alwaysRun = true) public void afterClass() { logger.info("starting cleanup...."); //MultiHash Collection delete @@ -83,7 +83,7 @@ public void afterClass() { safeCloseSyncClient(client); } - @Test(groups = { "simple" }, timeOut = TIMEOUT) + @Test(groups = { "emulator" }, timeOut = TIMEOUT) public void itemCRUD() throws Exception { List pkIds = new ArrayList<>(); @@ -136,7 +136,7 @@ private void validateIdOfItemResponse(String expectedId, CosmosItemResponse Date: Mon, 10 May 2021 20:23:13 -0700 Subject: [PATCH 15/17] Fixing code suggestions --- .../routing/PartitionKeyInternalHelper.java | 5 ++--- .../com/azure/cosmos/models/PartitionKeyBuilder.java | 9 ++++++++- .../main/java/com/azure/cosmos/models/PartitionKind.java | 2 +- .../src/main/java/com/azure/cosmos/util/Beta.java | 4 +++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java index 1237e9598e68..57bbdf8cc984 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java @@ -9,7 +9,6 @@ import com.azure.cosmos.implementation.ByteBufferOutputStream; import com.azure.cosmos.implementation.Bytes; import com.azure.cosmos.implementation.RMResources; -import com.azure.cosmos.util.Beta; import java.io.IOException; import java.nio.ByteBuffer; @@ -36,7 +35,7 @@ public class PartitionKeyInternalHelper { (byte) 0x3F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - private static final Integer HashV2EPKLength = 32; + private static final Integer HASH_V2_EPK_LENGTH = 32; static byte[] uIntToBytes(UInt128 unit) { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 2); @@ -96,7 +95,7 @@ static public String getEffectivePartitionKeyForHashPartitioningV2(PartitionKeyI } static String getEffectivePartitionKeyForMultiHashPartitioning(PartitionKeyInternal partitionKeyInternal) { - StringBuilder stringBuilder = new StringBuilder(partitionKeyInternal.components.size() * HashV2EPKLength); + StringBuilder stringBuilder = new StringBuilder(partitionKeyInternal.components.size() * HASH_V2_EPK_LENGTH); for (int i = 0; i < partitionKeyInternal.components.size(); i++) { try(ByteBufferOutputStream byteArrayBuffer = new ByteBufferOutputStream()) { partitionKeyInternal.components.get(i).writeForHashingV2(byteArrayBuffer); diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java index b2f47eb95a90..3a5d5195c391 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java @@ -11,13 +11,14 @@ import java.util.ArrayList; import java.util.List; -@Beta(value = SinceVersion.V4_14_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) +@Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public class PartitionKeyBuilder { private final List partitionKeyValues; /** * Constructor. CREATE a new instance of the PartitionKeyBuilder object. */ + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public PartitionKeyBuilder() { this.partitionKeyValues = new ArrayList(); } @@ -27,6 +28,7 @@ public PartitionKeyBuilder() { * @param value The value of type string to be used as partition key * @return The current PartitionKeyBuilder object */ + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public PartitionKeyBuilder add(String value) { this.partitionKeyValues.add(value); return this; @@ -37,6 +39,7 @@ public PartitionKeyBuilder add(String value) { * @param value The value of type double to be used as partition key * @return The current PartitionKeyBuilder object */ + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public PartitionKeyBuilder add(double value) { this.partitionKeyValues.add(value); return this; @@ -47,6 +50,7 @@ public PartitionKeyBuilder add(double value) { * @param value The value of type boolean to be used as partition key * @return The current PartitionKeyBuilder object */ + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public PartitionKeyBuilder add(boolean value) { this.partitionKeyValues.add(value); return this; @@ -56,6 +60,7 @@ public PartitionKeyBuilder add(boolean value) { * Adds a null partition key value * @return The current PartitionKeyBuilder object */ + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public PartitionKeyBuilder addNullValue() { this.partitionKeyValues.add(null); return this; @@ -65,6 +70,7 @@ public PartitionKeyBuilder addNullValue() { * Adds a None Partition Key * @return The current PartitionKeyBuilder object */ + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public PartitionKeyBuilder addNoneValue() { this.partitionKeyValues.add(PartitionKey.NONE); return this; @@ -74,6 +80,7 @@ public PartitionKeyBuilder addNoneValue() { * Builds a new instance of the type PartitionKey with the specified Partition Key values. * @return PartitionKey object */ + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) public PartitionKey build() { // Why these checks? // These changes are being added for SDK to support multiple paths in a partition key. diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java index 3a5831ad3699..35f58f5f943d 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKind.java @@ -19,7 +19,7 @@ public enum PartitionKind { /** * The Partition of a item is calculated based on the hash value of multiple PartitionKeys. */ - @Beta(value = SinceVersion.V4_14_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) + @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) MULTI_HASH("MultiHash"); PartitionKind(String overWireValue) { diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java index dbe7975f56a2..d46249766fdd 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java @@ -59,6 +59,8 @@ public enum SinceVersion { /** v4.14.0 */ V4_14_0, /** v4.15.0 */ - V4_15_0; + V4_15_0, + /** v4.16.0 */ + V4_16_0; } } From 00d5f8ea86f160e07d90df7a2c31bff879e4e68d Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Tue, 11 May 2021 23:03:01 -0700 Subject: [PATCH 16/17] Resolve code comments --- .../implementation/RxDocumentClientImpl.java | 8 +- .../routing/PartitionKeyInternalHelper.java | 2 +- .../main/java/com/azure/cosmos/util/Beta.java | 6 +- .../com/azure/cosmos/CosmosMultiHashTest.java | 180 +++++++++--------- 4 files changed, 97 insertions(+), 99 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java index 7c91ed44e75a..4f5bb2509656 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java @@ -1329,15 +1329,15 @@ public static PartitionKeyInternal extractPartitionKeyValueFromDocument( break; case MULTI_HASH: Object[] partitionKeyValues = new Object[partitionKeyDefinition.getPaths().size()]; - for(int path_iter = 0 ; path_iter < partitionKeyDefinition.getPaths().size(); path_iter++){ - String partitionPath = partitionKeyDefinition.getPaths().get(path_iter); + for(int pathIter = 0 ; pathIter < partitionKeyDefinition.getPaths().size(); pathIter++){ + String partitionPath = partitionKeyDefinition.getPaths().get(pathIter); List partitionPathParts = PathParser.getPathParts(partitionPath); - partitionKeyValues[path_iter] = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, partitionPathParts); + partitionKeyValues[pathIter] = ModelBridgeInternal.getObjectByPathFromJsonSerializable(document, partitionPathParts); } return PartitionKeyInternal.fromObjectArray(partitionKeyValues, false); default: - throw new IllegalArgumentException("Unrecognized Partition kind"); + throw new IllegalArgumentException("Unrecognized Partition kind: " + partitionKeyDefinition.getKind()); } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java index 57bbdf8cc984..f7d692650bf4 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/PartitionKeyInternalHelper.java @@ -35,7 +35,7 @@ public class PartitionKeyInternalHelper { (byte) 0x3F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - private static final Integer HASH_V2_EPK_LENGTH = 32; + private static final int HASH_V2_EPK_LENGTH = 32; static byte[] uIntToBytes(UInt128 unit) { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 2); diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java index d46249766fdd..3a38884d82e1 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java @@ -9,7 +9,11 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; @Documented @Retention(RetentionPolicy.CLASS) diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index f60e4d4363f0..b5d7c07a89bd 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -23,7 +23,9 @@ import com.azure.cosmos.util.CosmosPagedIterable; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.node.TextNode; import org.apache.http.HttpStatus; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -40,6 +42,8 @@ public class CosmosMultiHashTest extends TestSuiteBase { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private static final JsonNodeFactory JSON_NODE_FACTORY_INSTANCE = JsonNodeFactory.withExactBigDecimals(true); + private String preExistingDatabaseId = CosmosDatabaseForTest.generateId(); private CosmosClient client; private CosmosDatabase createdDatabase; @@ -77,8 +81,6 @@ public void before_CosmosMultiHashTest() { @AfterClass(groups = {"emulator"}, timeOut = SHUTDOWN_TIMEOUT, alwaysRun = true) public void afterClass() { logger.info("starting cleanup...."); - //MultiHash Collection delete - createdMultiHashContainer.delete(); safeDeleteSyncDatabase(createdDatabase); safeCloseSyncClient(client); } @@ -139,109 +141,101 @@ private void validateIdOfItemResponse(String expectedId, CosmosItemResponse docs = new ArrayList(3); - - Document doc = new Document(); - doc.setId(UUID.randomUUID().toString()); - doc.set("city", "Redmond"); - doc.set("zipcode", "98052"); - docs.add(doc); - - Document doc1 = new Document(); - doc1.setId(UUID.randomUUID().toString()); - doc1.set("city", "Pittsburgh"); - doc1.set("zipcode", "15232"); - docs.add(doc1); - - Document doc2 = new Document(); - doc2.setId(UUID.randomUUID().toString()); - doc2.set("city", "Stonybrook"); - doc2.set("zipcode", "11790"); - docs.add(doc2); - - Document doc3 = new Document(); - doc3.setId(UUID.randomUUID().toString()); - doc3.set("city", "Stonybrook"); - doc3.set("zipcode", "11794"); - docs.add(doc3); - - Document doc4 = new Document(); - doc4.setId(UUID.randomUUID().toString()); - doc4.set("city", "Stonybrook"); - doc4.set("zipcode", "11791"); - docs.add(doc4); - - //Document Create - for (int i = 0; i < docs.size(); i++) { - createdMultiHashContainer.createItem(docs.get(i)); - } - //Document Create - Negative test - { - PartitionKey partitionKey = - new PartitionKeyBuilder() - .add("Redmond") - .build(); - try { - CosmosItemResponse response = - createdMultiHashContainer.createItem(doc, partitionKey, new CosmosItemRequestOptions()); - } - catch (Exception e) { - assertThat(e.getMessage().contains("Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document.\n" )); - } - } - - //Document Read - { - for (int i = 0; i < docs.size(); i++) { - Document doc_current = docs.get(i); - PartitionKey partitionKey = new PartitionKeyBuilder() - .add(doc_current.getString("city")) - .add(doc_current.getString("zipcode")) - .build(); - CosmosItemResponse response = createdMultiHashContainer.readItem(doc_current.getId(), partitionKey, Document.class); - } + ArrayList docs = new ArrayList(3); + + ObjectNode doc = new ObjectNode(JSON_NODE_FACTORY_INSTANCE); + doc.set("id", new TextNode(UUID.randomUUID().toString())); + doc.set("city", new TextNode("Redmond")); + doc.set("zipcode", new TextNode("98052")); + docs.add(doc); + + ObjectNode doc1 = new ObjectNode(JSON_NODE_FACTORY_INSTANCE); + doc1.set("id", new TextNode(UUID.randomUUID().toString())); + doc1.set("city", new TextNode("Pittsburgh")); + doc1.set("zipcode", new TextNode("15232")); + docs.add(doc1); + + ObjectNode doc2 = new ObjectNode(JSON_NODE_FACTORY_INSTANCE); + doc2.set("id", new TextNode(UUID.randomUUID().toString())); + doc2.set("city", new TextNode("Stonybrook")); + doc2.set("zipcode", new TextNode("11790")); + docs.add(doc2); + + ObjectNode doc3 = new ObjectNode(JSON_NODE_FACTORY_INSTANCE); + doc3.set("id", new TextNode(UUID.randomUUID().toString())); + doc3.set("city", new TextNode("Stonybrook")); + doc3.set("zipcode", new TextNode("11794")); + docs.add(doc3); + + ObjectNode doc4 = new ObjectNode(JSON_NODE_FACTORY_INSTANCE); + doc4.set("id", new TextNode(UUID.randomUUID().toString())); + doc4.set("city", new TextNode("Stonybrook")); + doc4.set("zipcode", new TextNode("11791")); + docs.add(doc4); + + //Document Create + for (int i = 0; i < docs.size(); i++) { + createdMultiHashContainer.createItem(docs.get(i)); + } + //Document Create - Negative test + { + PartitionKey partitionKey = + new PartitionKeyBuilder() + .add("Redmond") + .build(); + try { + CosmosItemResponse response = + createdMultiHashContainer.createItem(doc, partitionKey, new CosmosItemRequestOptions()); + } catch (Exception e) { + assertThat(e.getMessage().contains("Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document.\n")); } + } - // Query Tests. - + //Document Read + { for (int i = 0; i < docs.size(); i++) { - Document doc_current = docs.get(i); - //Build the partition key + ObjectNode doc_current = docs.get(i); PartitionKey partitionKey = new PartitionKeyBuilder() - .add(doc_current.getString("city")) - .add(doc_current.getString("zipcode")) + .add(doc_current.get("city").toString()) + .add(doc_current.get("zipcode").toString()) .build(); + CosmosItemResponse response = createdMultiHashContainer.readItem(doc_current.get("id").toString(), partitionKey, ObjectNode.class); + } + } - //Build the query request options - CosmosQueryRequestOptions queryRequestOptions = new CosmosQueryRequestOptions(); - queryRequestOptions.setPartitionKey(partitionKey); + // Query Tests. - //Run the query. - String query = String.format("SELECT * from c where c.id = '%s'", doc_current.getId()); + for (int i = 0; i < docs.size(); i++) { + ObjectNode doc_current = docs.get(i); + //Build the partition key + PartitionKey partitionKey = new PartitionKeyBuilder() + .add(doc_current.get("city").toString()) + .add(doc_current.get("zipcode").toString()) + .build(); - CosmosPagedIterable feedResponseIterator1 = - createdMultiHashContainer.queryItems(query, queryRequestOptions, InternalObjectNode.class); - assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); + //Build the query request options + CosmosQueryRequestOptions queryRequestOptions = new CosmosQueryRequestOptions(); + queryRequestOptions.setPartitionKey(partitionKey); - query = String.format("SELECT * from c where c.id = '%s'", doc_current.getId()); - queryRequestOptions = new CosmosQueryRequestOptions(); - feedResponseIterator1 = - createdMultiHashContainer.queryItems(query, queryRequestOptions, InternalObjectNode.class); - assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); - } + //Run the query. + String query = String.format("SELECT * from c where c.id = '%s'", doc_current.get("id").toString()); - String query = String.format("SELECT * from c where c.city = '%s'", docs.get(2).getString("city")); - CosmosQueryRequestOptions queryRequestOptions = new CosmosQueryRequestOptions(); - CosmosPagedIterable feedResponseIterator1 = - createdMultiHashContainer.queryItems(query, queryRequestOptions, InternalObjectNode.class); - assertThat(feedResponseIterator1.stream().count()).isEqualTo(3); + CosmosPagedIterable feedResponseIterator1 = + createdMultiHashContainer.queryItems(query, queryRequestOptions, ObjectNode.class); + assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); + query = String.format("SELECT * from c where c.id = '%s'", doc_current.get("id").toString()); + queryRequestOptions = new CosmosQueryRequestOptions(); + feedResponseIterator1 = + createdMultiHashContainer.queryItems(query, queryRequestOptions, ObjectNode.class); + assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); } - catch (Exception e) - { - assertThat(false); - } + + String query = String.format("SELECT * from c where c.city = '%s'", docs.get(2).get("city").toString()); + CosmosQueryRequestOptions queryRequestOptions = new CosmosQueryRequestOptions(); + CosmosPagedIterable feedResponseIterator1 = + createdMultiHashContainer.queryItems(query, queryRequestOptions, ObjectNode.class); + assertThat(feedResponseIterator1.stream().count()).isEqualTo(3); } } From b6e452e92231141d6c8c0eac99a15d3729270711 Mon Sep 17 00:00:00 2001 From: Naga Srinikhil Reddy Naravamakula Date: Thu, 10 Jun 2021 02:27:53 -0700 Subject: [PATCH 17/17] Fixing a test --- .../azure/cosmos/models/PartitionKeyBuilder.java | 2 +- .../com/azure/cosmos/CosmosMultiHashTest.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java index 3a5d5195c391..3143b0e79520 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/PartitionKeyBuilder.java @@ -12,7 +12,7 @@ import java.util.List; @Beta(value = SinceVersion.V4_16_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) -public class PartitionKeyBuilder { +public final class PartitionKeyBuilder { private final List partitionKeyValues; /** diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java index b5d7c07a89bd..9f9ead242382 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/CosmosMultiHashTest.java @@ -196,10 +196,10 @@ private void validateDocCRUDandQuery() throws Exception { for (int i = 0; i < docs.size(); i++) { ObjectNode doc_current = docs.get(i); PartitionKey partitionKey = new PartitionKeyBuilder() - .add(doc_current.get("city").toString()) - .add(doc_current.get("zipcode").toString()) + .add(doc_current.get("city").asText()) + .add(doc_current.get("zipcode").asText()) .build(); - CosmosItemResponse response = createdMultiHashContainer.readItem(doc_current.get("id").toString(), partitionKey, ObjectNode.class); + CosmosItemResponse response = createdMultiHashContainer.readItem(doc_current.get("id").asText(), partitionKey, ObjectNode.class); } } @@ -209,8 +209,8 @@ private void validateDocCRUDandQuery() throws Exception { ObjectNode doc_current = docs.get(i); //Build the partition key PartitionKey partitionKey = new PartitionKeyBuilder() - .add(doc_current.get("city").toString()) - .add(doc_current.get("zipcode").toString()) + .add(doc_current.get("city").asText()) + .add(doc_current.get("zipcode").asText()) .build(); //Build the query request options @@ -218,20 +218,20 @@ private void validateDocCRUDandQuery() throws Exception { queryRequestOptions.setPartitionKey(partitionKey); //Run the query. - String query = String.format("SELECT * from c where c.id = '%s'", doc_current.get("id").toString()); + String query = String.format("SELECT * from c where c.id = '%s'", doc_current.get("id").asText()); CosmosPagedIterable feedResponseIterator1 = createdMultiHashContainer.queryItems(query, queryRequestOptions, ObjectNode.class); assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); - query = String.format("SELECT * from c where c.id = '%s'", doc_current.get("id").toString()); + query = String.format("SELECT * from c where c.id = '%s'", doc_current.get("id").asText()); queryRequestOptions = new CosmosQueryRequestOptions(); feedResponseIterator1 = createdMultiHashContainer.queryItems(query, queryRequestOptions, ObjectNode.class); assertThat(feedResponseIterator1.iterator().hasNext()).isTrue(); } - String query = String.format("SELECT * from c where c.city = '%s'", docs.get(2).get("city").toString()); + String query = String.format("SELECT * from c where c.city = '%s'", docs.get(2).get("city").asText()); CosmosQueryRequestOptions queryRequestOptions = new CosmosQueryRequestOptions(); CosmosPagedIterable feedResponseIterator1 = createdMultiHashContainer.queryItems(query, queryRequestOptions, ObjectNode.class);