2
2
// Copyright (c) Microsoft Corporation. All rights reserved.
3
3
// ------------------------------------------------------------
4
4
5
- namespace Microsoft . Azure . Cosmos . Pagination
5
+ namespace Microsoft . Azure . Cosmos . Query . Core . Pipeline . CrossPartition . OrderBy
6
6
{
7
7
using System ;
8
8
using System . Collections ;
9
9
using System . Collections . Generic ;
10
10
using System . Threading ;
11
11
using System . Threading . Tasks ;
12
12
using Microsoft . Azure . Cosmos . CosmosElements ;
13
+ using Microsoft . Azure . Cosmos . Pagination ;
13
14
using Microsoft . Azure . Cosmos . Query . Core . Collections ;
14
15
using Microsoft . Azure . Cosmos . Query . Core . Monads ;
15
- using Microsoft . Azure . Cosmos . Query . Core . Pipeline . CrossPartition . OrderBy ;
16
16
using Microsoft . Azure . Cosmos . Tracing ;
17
17
18
18
internal sealed class OrderByCrossPartitionEnumerator : IEnumerator < OrderByQueryResult >
@@ -25,69 +25,86 @@ internal sealed class OrderByCrossPartitionEnumerator : IEnumerator<OrderByQuery
25
25
26
26
object IEnumerator . Current => this . Current ;
27
27
28
- public OrderByCrossPartitionEnumerator ( PriorityQueue < IEnumerator < OrderByQueryResult > > queue )
28
+ private OrderByCrossPartitionEnumerator ( PriorityQueue < IEnumerator < OrderByQueryResult > > queue )
29
29
{
30
30
this . queue = queue ?? throw new ArgumentNullException ( nameof ( queue ) ) ;
31
31
}
32
32
33
- public static async Task < ( IEnumerator < OrderByQueryResult > orderbyQueryResultEnumerator , double totalRequestCharge ) > CreateAsync (
34
- IEnumerable < OrderByQueryPartitionRangePageAsyncEnumerator > enumerators ,
33
+ public static async Task < BufferedOrderByResults > CreateAsync (
34
+ ITracingAsyncEnumerator < TryCatch < OrderByQueryPage > > enumerator ,
35
35
IComparer < OrderByQueryResult > comparer ,
36
36
int levelSize ,
37
37
ITrace trace ,
38
38
CancellationToken cancellationToken )
39
39
{
40
- if ( enumerators == null )
40
+ if ( enumerator == null )
41
41
{
42
- throw new ArgumentNullException ( nameof ( enumerators ) ) ;
42
+ throw new ArgumentNullException ( nameof ( enumerator ) ) ;
43
43
}
44
44
45
45
if ( comparer == null )
46
46
{
47
47
throw new ArgumentNullException ( nameof ( comparer ) ) ;
48
48
}
49
49
50
+ QueryPageParameters queryPageParameters = null ;
50
51
double totalRequestCharge = 0 ;
52
+ int bufferedItemCount = 0 ;
51
53
EnumeratorComparer enumeratorComparer = new EnumeratorComparer ( comparer ) ;
52
54
PriorityQueue < IEnumerator < OrderByQueryResult > > queue = new PriorityQueue < IEnumerator < OrderByQueryResult > > ( enumeratorComparer ) ;
53
- foreach ( ITracingAsyncEnumerator < TryCatch < OrderByQueryPage > > enumerator in enumerators )
55
+ while ( await enumerator . MoveNextAsync ( trace , cancellationToken ) )
54
56
{
55
- while ( await enumerator . MoveNextAsync ( trace , cancellationToken ) )
57
+ TryCatch < OrderByQueryPage > currentPage = enumerator . Current ;
58
+ if ( currentPage . Failed )
56
59
{
57
- TryCatch < OrderByQueryPage > currentPage = enumerator . Current ;
58
- if ( currentPage . Failed )
59
- {
60
- throw currentPage . Exception ;
61
- }
60
+ throw currentPage . Exception ;
61
+ }
62
62
63
- totalRequestCharge += currentPage . Result . RequestCharge ;
64
- IReadOnlyList < CosmosElement > page = currentPage . Result . Page . Documents ;
63
+ if ( queryPageParameters == null )
64
+ {
65
+ queryPageParameters = new QueryPageParameters (
66
+ activityId : currentPage . Result . ActivityId ,
67
+ cosmosQueryExecutionInfo : currentPage . Result . Page . CosmosQueryExecutionInfo ,
68
+ distributionPlanSpec : currentPage . Result . Page . DistributionPlanSpec ,
69
+ additionalHeaders : currentPage . Result . AdditionalHeaders ) ;
70
+ }
65
71
66
- if ( page . Count > 0 )
67
- {
68
- PageEnumerator pageEnumerator = new PageEnumerator ( page ) ;
69
- pageEnumerator . MoveNext ( ) ;
72
+ totalRequestCharge += currentPage . Result . RequestCharge ;
73
+ IReadOnlyList < CosmosElement > page = currentPage . Result . Page . Documents ;
74
+ bufferedItemCount += page . Count ;
75
+
76
+ if ( page . Count > 0 )
77
+ {
78
+ PageEnumerator pageEnumerator = new PageEnumerator ( page ) ;
79
+ pageEnumerator . MoveNext ( ) ;
70
80
71
- queue . Enqueue ( pageEnumerator ) ;
81
+ queue . Enqueue ( pageEnumerator ) ;
72
82
73
- if ( queue . Count >= levelSize )
74
- {
75
- OrderByCrossPartitionEnumerator newEnumerator = new OrderByCrossPartitionEnumerator ( queue ) ;
76
- newEnumerator . MoveNext ( ) ;
83
+ if ( queue . Count >= levelSize )
84
+ {
85
+ OrderByCrossPartitionEnumerator newEnumerator = new OrderByCrossPartitionEnumerator ( queue ) ;
86
+ newEnumerator . MoveNext ( ) ;
77
87
78
- queue = new PriorityQueue < IEnumerator < OrderByQueryResult > > ( enumeratorComparer ) ;
79
- queue . Enqueue ( newEnumerator ) ;
80
- }
88
+ queue = new PriorityQueue < IEnumerator < OrderByQueryResult > > ( enumeratorComparer ) ;
89
+ queue . Enqueue ( newEnumerator ) ;
81
90
}
82
91
}
83
92
}
84
93
85
94
if ( queue . Count == 0 )
86
95
{
87
- return ( EmptyEnumerator . Instance , totalRequestCharge ) ;
96
+ return new BufferedOrderByResults (
97
+ EmptyEnumerator . Instance ,
98
+ itemCount : 0 ,
99
+ totalRequestCharge ,
100
+ queryPageParameters ) ;
88
101
}
89
102
90
- return ( new OrderByCrossPartitionEnumerator ( queue ) , totalRequestCharge ) ;
103
+ return new BufferedOrderByResults (
104
+ new OrderByCrossPartitionEnumerator ( queue ) ,
105
+ bufferedItemCount ,
106
+ totalRequestCharge ,
107
+ queryPageParameters ) ;
91
108
}
92
109
93
110
public bool MoveNext ( )
0 commit comments