Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding PartitionKeyDefinitionVersion to container builder. #844

Merged
merged 8 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class ContainerDefinition<T>
private int? defaultTimeToLive;
private IndexingPolicy indexingPolicy;
private string timeToLivePropertyPath;
private PartitionKeyDefinitionVersion? partitionKeyDefinitionVersion = null;

/// <summary>
/// Creates an instance for unit-testing
Expand All @@ -33,6 +34,23 @@ internal ContainerDefinition(
this.partitionKeyPath = partitionKeyPath;
}

/// <summary>
/// Set the partition key definition version 2 which can generate a
/// unique hash from large partition keys up to 2 KB.
/// </summary>
/// <remarks>
/// The partition key definition version 1 uses a hash function that computes
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved
/// hash based on the first 100 bytes of the partition key. This can cause
/// conflicts for documents with partition keys greater than 100 bytes.
/// </remarks>
/// <returns>An instance of the current Fluent builder.</returns>
/// <seealso cref="ContainerProperties.PartitionKeyDefinitionVersion"/>
public T WithPartitionKeyDefinitionVersion2()
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved
{
this.partitionKeyDefinitionVersion = PartitionKeyDefinitionVersion.V2;
return (T)this;
}

/// <summary>
/// <see cref="ContainerProperties.DefaultTimeToLive"/> 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.
Expand Down Expand Up @@ -127,6 +145,11 @@ public ContainerProperties Build()
#pragma warning restore 0612
}

if (this.partitionKeyDefinitionVersion.HasValue)
{
containerProperties.PartitionKeyDefinitionVersion = this.partitionKeyDefinitionVersion.Value;
}

containerProperties.ValidateRequiredProperties();

return containerProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ public ContainerProperties(string id, string partitionKeyPath)
}

/// <summary>
/// Gets the Partitioning scheme version used. <see cref="Cosmos.PartitionKeyDefinitionVersion"/>
/// Gets or sets the <see cref="Cosmos.PartitionKeyDefinitionVersion"/>
///
/// 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.
/// </summary>
/// <returns>The Partition Key Definition Version of the container</returns>
[JsonIgnore]
public PartitionKeyDefinitionVersion? PartitionKeyDefinitionVersion
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,11 @@
"Attributes": [],
"MethodInfo": "T WithDefaultTimeToLive(System.TimeSpan)"
},
"T WithPartitionKeyDefinitionVersion2()": {
j82w marked this conversation as resolved.
Show resolved Hide resolved
"Type": "Method",
"Attributes": [],
"MethodInfo": "T WithPartitionKeyDefinitionVersion2()"
},
"T WithTimeToLivePropertyPath(System.String)": {
"Type": "Method",
"Attributes": [],
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

## <a name="3.2.0"/> [3.2.0](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.2.0) - 2019-09-17

Expand Down