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 all 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,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
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.
///
/// 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