-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TabularReader`1.cs
274 lines (236 loc) · 9.99 KB
/
TabularReader`1.cs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// (c) Oleksandr Kozlenko. Licensed under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Addax.Formats.Tabular;
/// <summary>Provides forward-only, read-only access to tabular data records. This class cannot be inherited.</summary>
/// <typeparam name="T">The type of an object that represents a record.</typeparam>
public sealed class TabularReader<T> : IDisposable, IAsyncDisposable
where T : notnull
{
private readonly TabularReader _fieldReader;
private readonly TabularHandler<T> _recordHandler;
private TabularRecord<T> _currentRecord;
/// <summary>Initializes a new instance of the <see cref="TabularReader{T}" /> class for the specified stream using the provided dialect, options, and record handler.</summary>
/// <param name="stream">The stream to read from.</param>
/// <param name="dialect">The dialect to use for reading.</param>
/// <param name="options">The options to control the behavior during reading.</param>
/// <param name="handler">The handler to read a <typeparamref name="T" /> instance from a record.</param>
/// <exception cref="ArgumentException"><paramref name="stream" /> does not support reading.</exception>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> or <paramref name="dialect" /> is <see langword="null" />.</exception>
/// <exception cref="InvalidOperationException">The record handler is not specified and cannot be found in the registry.</exception>
public TabularReader(Stream stream, TabularDialect dialect, TabularOptions? options = null, TabularHandler<T>? handler = null)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentNullException.ThrowIfNull(dialect);
_fieldReader = new(stream, dialect, options);
_recordHandler = handler ?? TabularRegistry.SelectHandler<T>();
}
/// <summary>Releases the resources used by the current instance of the <see cref="TabularReader{T}" /> class.</summary>
public void Dispose()
{
_currentRecord = default;
_fieldReader.Dispose();
}
/// <summary>Asynchronously releases the resources used by the current instance of the <see cref="TabularReader{T}" /> class.</summary>
/// <returns>A task object.</returns>
public ValueTask DisposeAsync()
{
_currentRecord = default;
return _fieldReader.DisposeAsync();
}
/// <summary>Tries to advance the reader to the next record.</summary>
/// <returns><see langword="true" /> if the reader was successfully advanced; otherwise, <see langword="false" />.</returns>
/// <exception cref="TabularContentException">An unexpected character or end of stream was encountered.</exception>
public bool TrySkipRecord()
{
if (_fieldReader.TryPickRecord())
{
_currentRecord = default;
while (_fieldReader.TrySkipField())
{
}
return true;
}
return false;
}
/// <summary>Asynchronously tries to advance the reader to the next record.</summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object that, when awaited, produces <see langword="true" /> if the reader was successfully advanced; otherwise, <see langword="false" />.</returns>
/// <exception cref="OperationCanceledException">The cancellation token was canceled. This exception is stored into the returned task.</exception>
/// <exception cref="TabularContentException">An unexpected character or end of stream was encountered. This exception is stored into the returned task.</exception>
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
public async ValueTask<bool> TrySkipRecordAsync(CancellationToken cancellationToken = default)
{
if (_fieldReader.TryPickRecord())
{
_currentRecord = default;
while (await _fieldReader.TrySkipFieldAsync(cancellationToken).ConfigureAwait(false))
{
}
return true;
}
return false;
}
/// <summary>Tries to read the next record that can be represented as <typeparamref name="T" />.</summary>
/// <returns><see langword="true" /> if the record was successfully read; otherwise, <see langword="false" />.</returns>
/// <exception cref="TabularContentException">An unexpected character or end of stream was encountered.</exception>
/// <remarks>If the record handler defines a header, the first record will be automatically skipped.</remarks>
public bool TryReadRecord()
{
while (true)
{
if (_fieldReader.CurrentPositionType == TabularPositionType.StartOfStream)
{
if (!TryReadHeader())
{
return false;
}
}
if (_fieldReader.TryPickRecord())
{
_currentRecord = default;
_currentRecord = _recordHandler.Read(_fieldReader);
if (_fieldReader.CurrentPositionType == TabularPositionType.Delimiter)
{
while (_fieldReader.TrySkipField())
{
}
}
if (_currentRecord.HasValue)
{
return true;
}
}
else
{
return false;
}
}
}
/// <summary>Asynchronously reads the next record that can be represented as <typeparamref name="T" />.</summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object that, when awaited, produces <see langword="true" /> if the record was successfully read; otherwise, <see langword="false" />.</returns>
/// <exception cref="OperationCanceledException">The cancellation token was canceled. This exception is stored into the returned task.</exception>
/// <exception cref="TabularContentException">An unexpected character or end of stream was encountered. This exception is stored into the returned task.</exception>
/// <remarks>If the record handler defines a header, the first record will be automatically skipped.</remarks>
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
public async ValueTask<bool> TryReadRecordAsync(CancellationToken cancellationToken = default)
{
while (true)
{
if (_fieldReader.CurrentPositionType == TabularPositionType.StartOfStream)
{
if (!await TryReadHeaderAsync(cancellationToken).ConfigureAwait(false))
{
return false;
}
}
if (_fieldReader.TryPickRecord())
{
_currentRecord = default;
_currentRecord = await _recordHandler.ReadAsync(_fieldReader, cancellationToken).ConfigureAwait(false);
if (_fieldReader.CurrentPositionType == TabularPositionType.Delimiter)
{
while (await _fieldReader.TrySkipFieldAsync(cancellationToken).ConfigureAwait(false))
{
}
}
if (_currentRecord.HasValue)
{
return true;
}
}
else
{
return false;
}
}
}
private bool TryReadHeader()
{
if (_recordHandler.Header is not null)
{
if (_fieldReader.TryPickRecord())
{
_currentRecord = default;
while (_fieldReader.TrySkipField())
{
}
}
else
{
return false;
}
}
return true;
}
private async ValueTask<bool> TryReadHeaderAsync(CancellationToken cancellationToken)
{
if (_recordHandler.Header is not null)
{
if (_fieldReader.TryPickRecord())
{
_currentRecord = default;
while (await _fieldReader.TrySkipFieldAsync(cancellationToken).ConfigureAwait(false))
{
}
}
else
{
return false;
}
}
return true;
}
[DoesNotReturn]
[StackTraceHidden]
private static void ThrowRecordNotSetException()
{
throw new InvalidOperationException("The current record is not set.");
}
/// <summary>Gets the last read record that is represented as <typeparamref name="T" />.</summary>
/// <value>A <typeparamref name="T" /> instance. An exception is thrown if the current record is not set.</value>
/// <exception cref="InvalidOperationException">The current record is not set.</exception>
public T CurrentRecord
{
get
{
if (!_currentRecord.HasValue)
{
ThrowRecordNotSetException();
}
return _currentRecord.Value!;
}
}
/// <summary>Gets a reference to the last read record that is represented as <typeparamref name="T" />.</summary>
/// <value>A read-only reference to a <typeparamref name="T" /> instance. An exception is thrown if the current record is not set.</value>
/// <exception cref="InvalidOperationException">The current record is not set.</exception>
public ref readonly T CurrentRecordRef
{
get
{
if (!_currentRecord.HasValue)
{
ThrowRecordNotSetException();
}
return ref _currentRecord.Value!;
}
}
/// <inheritdoc cref="TabularReader.BytesConsumed" />
public long BytesConsumed
{
get
{
return _fieldReader.BytesConsumed;
}
}
/// <inheritdoc cref="TabularReader.RecordsRead" />
public long RecordsRead
{
get
{
return _fieldReader.RecordsRead;
}
}
}