diff --git a/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/Parallel/EpkRangeComparer.cs b/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/Parallel/EpkRangeComparer.cs deleted file mode 100644 index 57027c610a..0000000000 --- a/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/Parallel/EpkRangeComparer.cs +++ /dev/null @@ -1,43 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// ------------------------------------------------------------ - -namespace Microsoft.Azure.Cosmos.Query.Core.Pipeline.CrossPartition.Parallel -{ - using System; - using System.Collections.Generic; - - internal sealed class EpkRangeComparer : IComparer - { - public static readonly EpkRangeComparer Singleton = new EpkRangeComparer(); - - private EpkRangeComparer() - { - } - - public int Compare(FeedRangeEpk x, FeedRangeEpk y) - { - if (x == null) - { - throw new ArgumentNullException(nameof(x)); - } - - if (y == null) - { - throw new ArgumentNullException(nameof(y)); - } - - if (x.Range.Min.Length == 0) - { - return -1; - } - - if (y.Range.Min.Length == 0) - { - return 1; - } - - return x.Range.Min.CompareTo(y.Range.Min); - } - } -} diff --git a/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/Parallel/ParallelCrossPartitionQueryPipelineStage.cs b/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/Parallel/ParallelCrossPartitionQueryPipelineStage.cs index f165e75760..704e8e9cf8 100644 --- a/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/Parallel/ParallelCrossPartitionQueryPipelineStage.cs +++ b/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/Parallel/ParallelCrossPartitionQueryPipelineStage.cs @@ -83,18 +83,26 @@ public async ValueTask MoveNextAsync(ITrace trace) else { // left most and any non null continuations - List> feedRangeStates = crossPartitionState + IOrderedEnumerable> feedRangeStates = crossPartitionState .Value .ToArray() - .OrderBy(tuple => (FeedRangeEpk)tuple.FeedRange, EpkRangeComparer.Singleton) - .ToList(); + .OrderBy(tuple => ((FeedRangeEpk)tuple.FeedRange).Range.Min); + List activeParallelContinuationTokens = new List(); - for (int i = 0; i < feedRangeStates.Count; i++) + { + FeedRangeState firstState = feedRangeStates.First(); + ParallelContinuationToken firstParallelContinuationToken = new ParallelContinuationToken( + token: firstState.State != null ? ((CosmosString)firstState.State.Value).Value : null, + range: ((FeedRangeEpk)firstState.FeedRange).Range); + + activeParallelContinuationTokens.Add(firstParallelContinuationToken); + } + + foreach (FeedRangeState feedRangeState in feedRangeStates.Skip(1)) { this.cancellationToken.ThrowIfCancellationRequested(); - FeedRangeState feedRangeState = feedRangeStates[i]; - if ((i == 0) || (feedRangeState.State != null)) + if (feedRangeState.State != null) { ParallelContinuationToken parallelContinuationToken = new ParallelContinuationToken( token: feedRangeState.State != null ? ((CosmosString)feedRangeState.State.Value).Value : null,