Skip to content

Commit

Permalink
Adding PartitionKeyDefinitionVersion to container builder. (#844)
Browse files Browse the repository at this point in the history
* Adding PartitionKeyDefinitionVersion to container builder.

* Updated changelog

* Update contract changes

* Updated summary

* Updates the summary of containe properties

* Fixed test and updated to take the version in as a parameter

* Updating contract
  • Loading branch information
j82w authored and kirankumarkolli committed Sep 27, 2019
1 parent b240f84 commit c5cc95a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
25 changes: 25 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerDefinition.cs
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,25 @@ internal ContainerDefinition(
this.partitionKeyPath = partitionKeyPath;
}

/// <summary>
/// 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>
/// <param name="partitionKeyDefinitionVersion">The partition key definition version</param>
/// <returns>An instance of the current Fluent builder.</returns>
/// <seealso cref="ContainerProperties.PartitionKeyDefinitionVersion"/>
public T WithPartitionKeyDefinitionVersion(PartitionKeyDefinitionVersion partitionKeyDefinitionVersion)
{
this.partitionKeyDefinitionVersion = partitionKeyDefinitionVersion;
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 +147,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 @@ -91,23 +91,47 @@ 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();
this.ValidateCreateContainerResponseContract(response);
}

[TestMethod]
public async Task ContainerBuilderPartitionKeyDefinitionContractTest()
{
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.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")
.WithPartitionKeyDefinitionVersion(Cosmos.PartitionKeyDefinitionVersion.V2)
.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 @@ -2330,6 +2330,11 @@
"Attributes": [],
"MethodInfo": "T WithDefaultTimeToLive(System.TimeSpan)"
},
"T WithPartitionKeyDefinitionVersion(Microsoft.Azure.Cosmos.PartitionKeyDefinitionVersion)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "T WithPartitionKeyDefinitionVersion(Microsoft.Azure.Cosmos.PartitionKeyDefinitionVersion)"
},
"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 @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [#814](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/814) Ability to limit to configured endpoint only
- [#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

### Fixed

Expand Down

0 comments on commit c5cc95a

Please sign in to comment.