From 2b9c7ec844df20d31df3fb06a9d5882ea11fec4c Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Tue, 24 Sep 2019 09:06:58 -0700 Subject: [PATCH 1/7] Adding PartitionKeyDefinitionVersion to container builder. --- .../Fluent/Settings/ContainerDefinition.cs | 17 +++++++++++++++++ .../CosmosContainerTests.cs | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs b/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs index 97f0004cb2..6d6b0aef27 100644 --- a/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs +++ b/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs @@ -17,6 +17,7 @@ public abstract class ContainerDefinition private int? defaultTimeToLive; private IndexingPolicy indexingPolicy; private string timeToLivePropertyPath; + private PartitionKeyDefinitionVersion? partitionKeyDefinitionVersion = null; /// /// Creates an instance for unit-testing @@ -33,6 +34,17 @@ internal ContainerDefinition( this.partitionKeyPath = partitionKeyPath; } + /// + /// Set the partition key definition version for the container. + /// + /// An instance of the current Fluent builder. + /// + public T WithPartitionKeyDefinitionVersion2() + { + this.partitionKeyDefinitionVersion = PartitionKeyDefinitionVersion.V2; + return (T)this; + } + /// /// will be applied to all the items in the container as the default time-to-live policy. /// The individual item could override the default time-to-live policy by setting its time to live. @@ -127,6 +139,11 @@ public ContainerProperties Build() #pragma warning restore 0612 } + if (this.partitionKeyDefinitionVersion.HasValue) + { + containerProperties.PartitionKeyDefinitionVersion = this.partitionKeyDefinitionVersion.Value; + } + containerProperties.ValidateRequiredProperties(); return containerProperties; diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs index 535f3d15f2..e0a961d4bd 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs @@ -108,6 +108,25 @@ public async Task ContainerBuilderContractTest() this.ValidateCreateContainerResponseContract(response); } + [TestMethod] + public async Task ContainerBuilderPartitionKeyDefinitionContractTest() + { + ContainerResponse response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id") + .WithPartitionKeyDefinitionVersion2() + .CreateAsync(); + + this.ValidateCreateContainerResponseContract(response); + Assert.AreEqual(response.Resource.PartitionKeyDefinitionVersion, Cosmos.PartitionKeyDefinitionVersion.V2); + + response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id").CreateIfNotExistsAsync(); + this.ValidateCreateContainerResponseContract(response); + Assert.AreEqual(response.Resource.PartitionKeyDefinitionVersion, Cosmos.PartitionKeyDefinitionVersion.V2); + + response = await this.cosmosDatabase.DefineContainer(response.Container.Id, "/id").CreateIfNotExistsAsync(); + this.ValidateCreateContainerResponseContract(response); + Assert.AreEqual(response.Resource.PartitionKeyDefinitionVersion, Cosmos.PartitionKeyDefinitionVersion.V2); + } + [TestMethod] public async Task PartitionedCRUDTest() { From 31a553cc89190d78e21aecc622698183cd7e2317 Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Tue, 24 Sep 2019 09:10:13 -0700 Subject: [PATCH 2/7] Updated changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index da00a4695b..8d481d3dc8 100644 --- a/changelog.md +++ b/changelog.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [#822](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/822) GROUP BY query support. +- [#844](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/844) Added PartitionKeyDefinitionVersion to container builder ## [3.2.0](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.2.0) - 2019-09-17 From 8b9862d6360a88edea1c209405691e5151db47fe Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Tue, 24 Sep 2019 09:36:46 -0700 Subject: [PATCH 3/7] Update contract changes --- .../tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json index 1e587f8600..163bcca236 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json @@ -2311,6 +2311,11 @@ "Attributes": [], "MethodInfo": "T WithDefaultTimeToLive(System.TimeSpan)" }, + "T WithPartitionKeyDefinitionVersion2()": { + "Type": "Method", + "Attributes": [], + "MethodInfo": "T WithPartitionKeyDefinitionVersion2()" + }, "T WithTimeToLivePropertyPath(System.String)": { "Type": "Method", "Attributes": [], From a1fca1de2865965b96449c1e6cc05ed9bb1b4746 Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Tue, 24 Sep 2019 09:54:36 -0700 Subject: [PATCH 4/7] Updated summary --- .../src/Fluent/Settings/ContainerDefinition.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs b/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs index 6d6b0aef27..5faebb8a0b 100644 --- a/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs +++ b/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs @@ -35,8 +35,14 @@ internal ContainerDefinition( } /// - /// Set the partition key definition version for the container. + /// Set the partition key definition version 2 which can generate a + /// unique hash from large partition keys up to 2 KB. /// + /// + /// The partition key definition version 1 uses a hash function that computes + /// hash based on the first 100 bytes of the partition key. This can cause + /// conflicts for documents with partition keys greater than 100 bytes. + /// /// An instance of the current Fluent builder. /// public T WithPartitionKeyDefinitionVersion2() From c120c49497ac59b9dcd20a493e5e0c48687d6658 Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Tue, 24 Sep 2019 09:57:55 -0700 Subject: [PATCH 5/7] Updates the summary of containe properties --- .../src/Resource/Settings/ContainerProperties.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs b/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs index 40f7ff888d..0bfb634916 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs @@ -90,8 +90,16 @@ public ContainerProperties(string id, string partitionKeyPath) } /// - /// Gets the Partitioning scheme version used. + /// Gets or sets the + /// + /// The partition key definition version 1 uses a hash function that computes + /// hash based on the first 100 bytes of the partition key. This can cause + /// conflicts for documents with partition keys greater than 100 bytes. + /// + /// The partition key definition version 2 uses a hash function that computes + /// hash based on the first 2 KB of the partition key. /// + /// The Partition Key Definition Version of the container [JsonIgnore] public PartitionKeyDefinitionVersion? PartitionKeyDefinitionVersion { From 0c05713fec7979798a84091125354ea286bbb9d2 Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Tue, 24 Sep 2019 14:40:44 -0700 Subject: [PATCH 6/7] Fixed test and updated to take the version in as a parameter --- .../Fluent/Settings/ContainerDefinition.cs | 16 +++++++++------- .../CosmosContainerTests.cs | 19 ++++++++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs b/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs index 5faebb8a0b..759059affc 100644 --- a/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs +++ b/Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs @@ -35,19 +35,21 @@ internal ContainerDefinition( } /// - /// Set the partition key definition version 2 which can generate a - /// unique hash from large partition keys up to 2 KB. - /// - /// + /// Sets the + /// /// The partition key definition version 1 uses a hash function that computes /// hash based on the first 100 bytes of the partition key. This can cause /// conflicts for documents with partition keys greater than 100 bytes. - /// + /// + /// The partition key definition version 2 uses a hash function that computes + /// hash based on the first 2 KB of the partition key. + /// + /// The partition key definition version /// An instance of the current Fluent builder. /// - public T WithPartitionKeyDefinitionVersion2() + public T WithPartitionKeyDefinitionVersion(PartitionKeyDefinitionVersion partitionKeyDefinitionVersion) { - this.partitionKeyDefinitionVersion = PartitionKeyDefinitionVersion.V2; + this.partitionKeyDefinitionVersion = partitionKeyDefinitionVersion; return (T)this; } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs index e0a961d4bd..64783d10bb 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs @@ -91,17 +91,17 @@ public async Task ReIndexingTest() [TestMethod] public async Task ContainerContractTest() { - ContainerResponse response = await this.cosmosDatabase.CreateContainerAsync(new Guid().ToString(), "/id"); + ContainerResponse response = await this.cosmosDatabase.CreateContainerAsync(Guid.NewGuid().ToString(), "/id"); this.ValidateCreateContainerResponseContract(response); } [TestMethod] public async Task ContainerBuilderContractTest() { - ContainerResponse response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id").CreateAsync(); + ContainerResponse response = await this.cosmosDatabase.DefineContainer(Guid.NewGuid().ToString(), "/id").CreateAsync(); this.ValidateCreateContainerResponseContract(response); - response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id").CreateIfNotExistsAsync(); + response = await this.cosmosDatabase.DefineContainer(Guid.NewGuid().ToString(), "/id").CreateIfNotExistsAsync(); this.ValidateCreateContainerResponseContract(response); response = await this.cosmosDatabase.DefineContainer(response.Container.Id, "/id").CreateIfNotExistsAsync(); @@ -111,18 +111,23 @@ public async Task ContainerBuilderContractTest() [TestMethod] public async Task ContainerBuilderPartitionKeyDefinitionContractTest() { - ContainerResponse response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id") - .WithPartitionKeyDefinitionVersion2() + ContainerResponse response = await this.cosmosDatabase.DefineContainer(Guid.NewGuid().ToString(), "/id") + .WithPartitionKeyDefinitionVersion(Cosmos.PartitionKeyDefinitionVersion.V2) .CreateAsync(); this.ValidateCreateContainerResponseContract(response); Assert.AreEqual(response.Resource.PartitionKeyDefinitionVersion, Cosmos.PartitionKeyDefinitionVersion.V2); - response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id").CreateIfNotExistsAsync(); + //response = await this.cosmosDatabase.CreateContainerAsync(new ContainerProperties(new)) + response = await this.cosmosDatabase.DefineContainer(Guid.NewGuid().ToString(), "/id") + .WithPartitionKeyDefinitionVersion(Cosmos.PartitionKeyDefinitionVersion.V2) + .CreateIfNotExistsAsync(); this.ValidateCreateContainerResponseContract(response); Assert.AreEqual(response.Resource.PartitionKeyDefinitionVersion, Cosmos.PartitionKeyDefinitionVersion.V2); - response = await this.cosmosDatabase.DefineContainer(response.Container.Id, "/id").CreateIfNotExistsAsync(); + response = await this.cosmosDatabase.DefineContainer(response.Container.Id, "/id") + .WithPartitionKeyDefinitionVersion(Cosmos.PartitionKeyDefinitionVersion.V2) + .CreateIfNotExistsAsync(); this.ValidateCreateContainerResponseContract(response); Assert.AreEqual(response.Resource.PartitionKeyDefinitionVersion, Cosmos.PartitionKeyDefinitionVersion.V2); } From ea344e0e3f03991208c0b05ca31ca69d9536c8ed Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Thu, 26 Sep 2019 08:08:47 -0700 Subject: [PATCH 7/7] Updating contract --- .../tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json index f19112835c..be96ace12a 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/DotNetSDKAPI.json @@ -2330,10 +2330,10 @@ "Attributes": [], "MethodInfo": "T WithDefaultTimeToLive(System.TimeSpan)" }, - "T WithPartitionKeyDefinitionVersion2()": { + "T WithPartitionKeyDefinitionVersion(Microsoft.Azure.Cosmos.PartitionKeyDefinitionVersion)": { "Type": "Method", "Attributes": [], - "MethodInfo": "T WithPartitionKeyDefinitionVersion2()" + "MethodInfo": "T WithPartitionKeyDefinitionVersion(Microsoft.Azure.Cosmos.PartitionKeyDefinitionVersion)" }, "T WithTimeToLivePropertyPath(System.String)": { "Type": "Method",