-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingAnalyticsResult.cs
More file actions
120 lines (104 loc) · 4.26 KB
/
BlockingAnalyticsResult.cs
File metadata and controls
120 lines (104 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#region License
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2025 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion
using System.Runtime.CompilerServices;
using Couchbase.AnalyticsClient.Json;
using Couchbase.AnalyticsClient.Options;
using Couchbase.AnalyticsClient.Query;
using Couchbase.AnalyticsClient.Results;
using Couchbase.AnalyticsClient.Utils;
using Couchbase.Core.Json;
namespace Couchbase.AnalyticsClient.Internal.Results;
/// <summary>
/// A "blocking" result class for Analytics queries.
/// </summary>
/// <remarks>For large result sets use the <see cref="StreamingAnalyticsResult"/> class by setting <see cref="QueryOptions.AsStreaming"/> to true, which is the default.</remarks>
internal class BlockingAnalyticsResult : AnalyticsResultBase
{
private IEnumerable<AnalyticsRow>? _rows;
private bool _enumerated;
public BlockingAnalyticsResult(Stream responseStream, IDeserializer serializer, IDisposable? ownedForCleanup = null)
: base(responseStream, serializer, ownedForCleanup)
{
}
public override IAsyncEnumerator<AnalyticsRow> GetAsyncEnumerator(
CancellationToken cancellationToken = default)
=> EnumerateRows(cancellationToken).GetAsyncEnumerator(cancellationToken);
private async IAsyncEnumerable<AnalyticsRow> EnumerateRows(
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (_rows == null)
{
throw new InvalidOperationException(
$"{nameof(BlockingAnalyticsResult)} has not been initialized, call InitializeAsync first");
}
if (_enumerated)
{
throw new InvalidOperationException("BlockingAnalyticsResult has already been enumerated");
}
_enumerated = true;
foreach (var row in _rows)
{
cancellationToken.ThrowIfCancellationRequested();
yield return row;
}
}
public override async Task InitializeAsync(CancellationToken cancellationToken = default)
{
var reader = Serializer.CreateJsonStreamReader(ResponseStream, cancellationToken);
if (!await reader.InitializeAsync(cancellationToken).ConfigureAwait(false))
{
ThrowHelper.ThrowArgumentNullException("No data received.");
}
MetaData = new QueryMetaData();
var rows = new List<AnalyticsRow>();
while (true)
{
var path = await reader.ReadToNextAttributeAsync(cancellationToken).ConfigureAwait(false);
if (path == null)
{
break;
}
switch (path)
{
case "requestID" when reader.ValueType == typeof(string):
MetaData.RequestId = reader.Value?.ToString();
break;
case "metrics":
MetaData.Metrics = await reader.ReadObjectAsync<QueryMetrics>(cancellationToken).ConfigureAwait(false);
break;
case "results":
{
await foreach (var token in reader.ReadTokensAsync(cancellationToken).ConfigureAwait(false))
{
rows.Add(new AnalyticsRow(token));
}
break;
}
case "errors":
var errors = await reader.ReadObjectAsync<QueryError[]>(cancellationToken).ConfigureAwait(false);
Errors = errors ?? Array.Empty<QueryError>();
break;
}
}
_rows = rows;
Rows = EnumerateRows(cancellationToken);
}
}