Skip to content

Commit

Permalink
Requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NaluTripician committed Sep 11, 2023
1 parent 48557c9 commit 4d3b03c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Microsoft.Azure.Cosmos.Query.Core.ExecutionContext
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using global::Azure;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.Pagination;
Expand Down Expand Up @@ -648,25 +648,28 @@ private static async Task<PartitionedQueryExecutionInfo> GetPartitionedQueryExec
else if (TryGetEpkProperty(properties, out string effectivePartitionKeyString))
{
List<Documents.Routing.Range<string>> effectiveRanges = new List<Documents.Routing.Range<string>>
{ new Documents.Routing.Range<string>(effectivePartitionKeyString, effectivePartitionKeyString, true, true) };
{ Documents.Routing.Range<string>.GetPointRange(effectivePartitionKeyString) };

targetRanges = await queryClient.GetTargetPartitionKeyRangesAsync(
resourceLink,
containerQueryProperties.ResourceId,
effectiveRanges,
forceRefresh: false,
trace);
}
else
else if (feedRangeInternal != null)
{
targetRanges = feedRangeInternal != null
? await queryClient.GetTargetPartitionKeyRangeByFeedRangeAsync(
targetRanges = await queryClient.GetTargetPartitionKeyRangeByFeedRangeAsync(
resourceLink,
containerQueryProperties.ResourceId,
containerQueryProperties.PartitionKeyDefinition,
feedRangeInternal,
forceRefresh: false,
trace)
: await queryClient.GetTargetPartitionKeyRangesAsync(
trace);
}
else
{
targetRanges = await queryClient.GetTargetPartitionKeyRangesAsync(
resourceLink,
containerQueryProperties.ResourceId,
partitionedQueryExecutionInfo.QueryRanges,
Expand Down
10 changes: 7 additions & 3 deletions Microsoft.Azure.Cosmos/src/Routing/PartitionKeyHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Microsoft.Azure.Cosmos.Routing
{
using System;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Azure.Documents.Routing;
Expand Down Expand Up @@ -35,7 +35,7 @@ namespace Microsoft.Azure.Cosmos.Routing
/// </example>
internal readonly struct PartitionKeyHash : IComparable<PartitionKeyHash>, IEquatable<PartitionKeyHash>
{
private readonly UInt128[] values;
private readonly IReadOnlyList<UInt128> values;

public PartitionKeyHash(UInt128 value)
: this(new UInt128[] { value })
Expand All @@ -47,6 +47,10 @@ public PartitionKeyHash(UInt128[] values)
StringBuilder stringBuilder = new StringBuilder();
foreach (UInt128 value in values)
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append('-');
}
stringBuilder.Append(value.ToString());
}

Expand All @@ -58,7 +62,7 @@ public PartitionKeyHash(UInt128[] values)

public string Value { get; }

internal readonly UInt128[] HashValues => this.values;
internal readonly IReadOnlyList<UInt128> HashValues => this.values;

public int CompareTo(PartitionKeyHash other)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Results>
<Result>
<Input>
<Description>1 Path List</Description>
<PartitionKeyValue>["/path1"]</PartitionKeyValue>
</Input>
<Output>
<PartitionKeyHashV1>00-00-00-00-00-00-00-00-00-00-00-00-0A-A1-CC-05</PartitionKeyHashV1>
<PartitionKeyHashV2>B6-3D-B6-C4-3A-4F-1B-01-4C-63-B0-C2-E6-28-E8-12</PartitionKeyHashV2>
</Output>
</Result>
<Result>
<Input>
<Description>2 Path List</Description>
<PartitionKeyValue>["/path1","/path2"]</PartitionKeyValue>
</Input>
<Output>
<PartitionKeyHashV1>00-00-00-00-00-00-00-00-00-00-00-00-0A-A1-CC-05-00-00-00-00-00-00-00-00-00-00-00-00-C9-1E-F0-78</PartitionKeyHashV1>
<PartitionKeyHashV2>B6-3D-B6-C4-3A-4F-1B-01-4C-63-B0-C2-E6-28-E8-12A6-0C-6C-BE-5A-2D-38-6E-5D-AE-1A-AC-94-21-6B-6C</PartitionKeyHashV2>
</Output>
</Result>
<Result>
<Input>
<Description>3 Path List</Description>
<PartitionKeyValue>["/path1","/path2","/path3"]</PartitionKeyValue>
</Input>
<Output>
<PartitionKeyHashV1>00-00-00-00-00-00-00-00-00-00-00-00-0A-A1-CC-05-00-00-00-00-00-00-00-00-00-00-00-00-C9-1E-F0-7800-00-00-00-00-00-00-00-00-00-00-00-9A-B4-68-CD</PartitionKeyHashV1>
<PartitionKeyHashV2>B6-3D-B6-C4-3A-4F-1B-01-4C-63-B0-C2-E6-28-E8-12-A6-0C-6C-BE-5A-2D-38-6E-5D-AE-1A-AC-94-21-6B-6C88-A6-18-5D-2D-D5-1C-96-D0-47-75-B7-2E-FA-BE-08</PartitionKeyHashV2>
</Output>
</Result>
</Results>
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ private PartitionKeyHash ComputeMedianSplitPointAmongDocumentsInPKRange(Partitio

// For MultiHash Collection, split at top level to ensure documents for top level key exist across partitions
// after split
if (medianPkHash.HashValues.Length > 1)
if (medianPkHash.HashValues.Count > 1)
{
return new PartitionKeyHash(medianPkHash.HashValues[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Azure.Cosmos.Tests.Routing
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Xml;
using Microsoft.Azure.Cosmos.CosmosElements;
Expand Down Expand Up @@ -126,6 +127,37 @@ public void Numbers()
this.ExecuteTestSuite(inputs);
}

[TestMethod]
public void Lists()
{
List<Input> inputs = new List<Input>()
{
new Input(
description: "1 Path List",
partitionKeyValue: CosmosArray.Create(new List<CosmosElement>()
{
CosmosString.Create("/path1")
})),
new Input(
description: "2 Path List",
partitionKeyValue: CosmosArray.Create(new List<CosmosElement>()
{
CosmosString.Create("/path1"),
CosmosString.Create("/path2")
})),
new Input(
description: "3 Path List",
partitionKeyValue: CosmosArray.Create(new List<CosmosElement>()
{
CosmosString.Create("/path1"),
CosmosString.Create("/path2"),
CosmosString.Create("/path3")
})),
};

this.ExecuteTestSuite(inputs);
}

public override Output ExecuteTest(Input input)
{
CosmosElement value = input.PartitionKeyValue;
Expand Down Expand Up @@ -159,7 +191,38 @@ public override Output ExecuteTest(Input input)
partitionKeyHashV1 = PartitionKeyHash.V1.Hash(Number64.ToDouble(cosmosNumber.Value));
partitionKeyHashV2 = PartitionKeyHash.V2.Hash(Number64.ToDouble(cosmosNumber.Value));
break;
case CosmosArray cosmosArray:
IList<UInt128> partitionKeyHashValuesV1 = new List<UInt128>();
IList<UInt128> partitionKeyHashValuesV2 = new List<UInt128>();

foreach (CosmosElement element in cosmosArray)
{
PartitionKeyHash elementHashV1 = element switch
{
null => PartitionKeyHash.V2.HashUndefined(),
CosmosString stringPartitionKey => PartitionKeyHash.V1.Hash(stringPartitionKey.Value),
CosmosNumber numberPartitionKey => PartitionKeyHash.V1.Hash(Number64.ToDouble(numberPartitionKey.Value)),
CosmosBoolean cosmosBoolean => PartitionKeyHash.V1.Hash(cosmosBoolean.Value),
CosmosNull _ => PartitionKeyHash.V1.HashNull(),
_ => throw new ArgumentOutOfRangeException(),
};
partitionKeyHashValuesV1.Add(elementHashV1.HashValues[0]);

PartitionKeyHash elementHashV2 = element switch
{
null => PartitionKeyHash.V2.HashUndefined(),
CosmosString stringPartitionKey => PartitionKeyHash.V2.Hash(stringPartitionKey.Value),
CosmosNumber numberPartitionKey => PartitionKeyHash.V2.Hash(Number64.ToDouble(numberPartitionKey.Value)),
CosmosBoolean cosmosBoolean => PartitionKeyHash.V2.Hash(cosmosBoolean.Value),
CosmosNull _ => PartitionKeyHash.V2.HashNull(),
_ => throw new ArgumentOutOfRangeException(),
};
partitionKeyHashValuesV2.Add(elementHashV2.HashValues[0]);
}

partitionKeyHashV1 = new PartitionKeyHash(partitionKeyHashValuesV1.ToArray());
partitionKeyHashV2 = new PartitionKeyHash(partitionKeyHashValuesV2.ToArray());
break;
default:
throw new ArgumentOutOfRangeException($"Unknown {nameof(CosmosElement)} type: {value.GetType()}.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static PartitionKeyHashRanges SplitRange(PartitionKeyHashRange partitionK
return splitOutcome switch
{
SplitOutcome.Success => splitRanges,
_ => throw new ArgumentOutOfRangeException($"Unknown {nameof(SplitOutcome)}: {splitOutcome}."),
_ => throw new RangeSplitException($"Splitting range failed because {splitOutcome}"),
};
}

Expand Down Expand Up @@ -144,7 +144,13 @@ private sealed class V2 : PartitionKeyHashRangeSplitterAndMerger

public override PartitionKeyHashRange FullRange => PartitionKeyHashRangeSplitterAndMerger.V2.fullRange;
}

private sealed class RangeSplitException : Exception
{
public RangeSplitException(string message)
: base(message)
{
}
}
public enum SplitOutcome
{
Success,
Expand Down

0 comments on commit 4d3b03c

Please sign in to comment.