forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlSequentialDataReader.cs
More file actions
444 lines (384 loc) · 16.2 KB
/
NpgsqlSequentialDataReader.cs
File metadata and controls
444 lines (384 loc) · 16.2 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Npgsql.BackendMessages;
using Npgsql.TypeHandling;
namespace Npgsql
{
/// <summary>
/// A sequential reader, which does not buffer rows in memory, and requires columns to be
/// read in-order only. Returned when <see cref="CommandBehavior.SequentialAccess"/> is passed
/// to <see cref="NpgsqlCommand.ExecuteReader()"/>.
/// </summary>
/// <remarks>
/// This reader is suitable in scenarios where a single row is very large, and holding
/// it in memory is undesirable.
/// </remarks>
#pragma warning disable CA1010
sealed class NpgsqlSequentialDataReader : NpgsqlDataReader
#pragma warning restore CA1010
{
/// <summary>
/// The number of columns in the current row
/// </summary>
int _numColumns;
/// <summary>
/// The index of the column that we're on, i.e. that has already been parsed, is
/// is memory and can be retrieved. Initialized to -1
/// </summary>
int _column;
/// <summary>
/// A stream that has been opened on this colun, and needs to be disposed of when the column is consumed.
/// </summary>
[CanBeNull]
SequentialByteaStream _stream;
internal NpgsqlSequentialDataReader(NpgsqlCommand command, CommandBehavior behavior, List<NpgsqlStatement> statements, Task sendTask)
: base(command, behavior, statements, sendTask)
{
Debug.Assert(!command.Parameters.HasOutputParameters);
Buffer = Connector.ReadBuffer;
}
internal override ValueTask<IBackendMessage> ReadMessage(bool async)
=> Connector.ReadMessage(async, DataRowLoadingMode.Sequential);
internal override void ProcessDataMessage(DataRowMessage dataMsg)
{
_column = -1;
ColumnLen = -1;
PosInColumn = 0;
}
public override Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken)
=> SynchronizationContextSwitcher.NoContext(async () => await GetFieldValue<T>(ordinal, true));
public override T GetFieldValue<T>(int column)
=> GetFieldValue<T>(column, false).GetAwaiter().GetResult();
async ValueTask<T> GetFieldValue<T>(int column, bool async)
{
CheckRowAndOrdinal(column);
await SeekToColumn(column, async);
CheckColumnStart();
if (ColumnLen == -1)
throw new InvalidCastException("Column is null");
var fieldDescription = RowDescription[column];
try
{
if (typeof(T) == typeof(object))
{
return ColumnLen <= Buffer.ReadBytesLeft
? (T)fieldDescription.Handler.ReadAsObject(Buffer, ColumnLen, fieldDescription)
: (T)await fieldDescription.Handler.ReadAsObject(Buffer, ColumnLen, async, fieldDescription);
}
else
{
return ColumnLen <= Buffer.ReadBytesLeft
? fieldDescription.Handler.Read<T>(Buffer, ColumnLen, fieldDescription)
: await fieldDescription.Handler.Read<T>(Buffer, ColumnLen, async, fieldDescription);
}
}
catch (NpgsqlSafeReadException e)
{
throw e.InnerException;
}
catch
{
Connector.Break();
throw;
}
finally
{
// Important in case a NpgsqlSafeReadException was thrown, position must still be updated
PosInColumn += ColumnLen;
}
}
/// <summary>
/// Gets the value of the specified column as an instance of <see cref="object"/>.
/// </summary>
/// <param name="column">The zero-based column ordinal.</param>
/// <returns>The value of the specified column.</returns>
public override object GetValue(int column)
{
CheckRowAndOrdinal(column);
SeekToColumn(column, false).GetAwaiter().GetResult();
CheckColumnStart();
if (ColumnLen == -1)
return DBNull.Value;
var fieldDescription = RowDescription[column];
object result;
try
{
result = fieldDescription.Handler.ReadAsObject(Buffer, ColumnLen, false, fieldDescription)
.GetAwaiter().GetResult();
}
catch (NpgsqlSafeReadException e)
{
throw e.InnerException;
}
catch
{
Connector.Break();
throw;
}
finally
{
// Important in case a NpgsqlSafeReadException was thrown, position must still be updated
PosInColumn += ColumnLen;
}
// Used for Entity Framework <= 6 compability
if (Command.ObjectResultTypes?[column] != null)
{
var type = Command.ObjectResultTypes[column];
result = type == typeof(DateTimeOffset)
? new DateTimeOffset((DateTime)result)
: Convert.ChangeType(result, type);
}
return result;
}
/// <summary>
/// Gets the value of the specified column as an instance of <see cref="object"/>.
/// </summary>
/// <param name="column">The zero-based column ordinal.</param>
/// <returns>The value of the specified column.</returns>
public override object GetProviderSpecificValue(int column)
{
CheckRowAndOrdinal(column);
SeekToColumn(column, false).GetAwaiter().GetResult();
CheckColumnStart();
if (ColumnLen == -1)
return DBNull.Value;
var fieldDescription = RowDescription[column];
try
{
return fieldDescription.Handler.ReadPsvAsObject(Buffer, ColumnLen, false, fieldDescription)
.GetAwaiter().GetResult();
}
catch (NpgsqlSafeReadException e)
{
throw e.InnerException;
}
catch
{
Connector.Break();
throw;
}
finally
{
// Important in case a NpgsqlSafeReadException was thrown, position must still be updated
PosInColumn += ColumnLen;
}
}
/// <summary>
/// Seeks to the given column. The 4-byte length is read and stored in <see cref="NpgsqlDataReader.ColumnLen"/>.
/// </summary>
internal override async Task SeekToColumn(int column, bool async)
{
if (_column == -1)
{
await Buffer.Ensure(2, async);
_numColumns = Buffer.ReadInt16();
}
if (column < 0 || column >= _numColumns)
throw new IndexOutOfRangeException("Column index out of range");
if (column < _column)
throw new InvalidOperationException($"Invalid attempt to read from column ordinal '{column}'. With CommandBehavior.SequentialAccess, you may only read from column ordinal '{_column}' or greater.");
if (column > _column)
{
// Skip to end of column if needed
// TODO: Simplify by better initializing _columnLen/_posInColumn
var remainingInColumn = ColumnLen == -1 ? 0 : ColumnLen - PosInColumn;
if (remainingInColumn > 0)
await Buffer.Skip(remainingInColumn, async);
// Shut down any streaming going on on the column
if (_stream != null)
{
_stream.Dispose();
_stream = null;
}
// Skip over unwanted fields
for (; _column < column - 1; _column++)
{
await Buffer.Ensure(4, async);
var len = Buffer.ReadInt32();
if (len != -1)
await Buffer.Skip(len, async);
}
await Buffer.Ensure(4, async);
ColumnLen = Buffer.ReadInt32();
PosInColumn = 0;
_column = column;
}
}
internal override async Task SeekInColumn(int posInColumn, bool async)
{
Debug.Assert(_column > -1);
if (posInColumn < PosInColumn)
throw new InvalidOperationException("Attempt to read a position in the column which has already been read");
if (posInColumn > ColumnLen)
posInColumn = ColumnLen;
if (posInColumn > PosInColumn)
{
await Buffer.Skip(posInColumn - PosInColumn, async);
PosInColumn = posInColumn;
}
}
internal override async Task ConsumeRow(bool async)
{
if (_column == -1)
{
await Buffer.Ensure(2, async);
_numColumns = Buffer.ReadInt16();
}
if (_stream != null)
{
_stream.Dispose();
_stream = null;
}
// TODO: Potential for code-sharing with ReadColumn above, which also skips
// Skip to end of column if needed
var remainingInColumn = ColumnLen == -1 ? 0 : ColumnLen - PosInColumn;
if (remainingInColumn > 0)
await Buffer.Skip(remainingInColumn, async);
// Skip over the remaining columns in the row
for (; _column < _numColumns - 1; _column++)
{
await Buffer.Ensure(4, async);
var len = Buffer.ReadInt32();
if (len != -1)
await Buffer.Skip(len, async);
}
}
#region IsDBNull
/// <summary>
/// Gets a value that indicates whether the column contains nonexistent or missing values.
/// </summary>
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <returns><b>true</b> if the specified column is equivalent to <see cref="DBNull"/>; otherwise <b>false</b>.</returns>
public override bool IsDBNull(int ordinal) => IsDBNull(ordinal, false).GetAwaiter().GetResult();
/// <summary>
/// An asynchronous version of <see cref="IsDBNull(int)"/>, which gets a value that indicates whether the column contains non-existent or missing values.
/// The <paramref name="cancellationToken"/> parameter is currently ignored.
/// </summary>
/// <param name="ordinal">The zero-based column to be retrieved.</param>
/// <param name="cancellationToken">Currently ignored.</param>
/// <returns><b>true</b> if the specified column value is equivalent to <see cref="DBNull"/> otherwise <b>false</b>.</returns>
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
=> SynchronizationContextSwitcher.NoContext(async () => await IsDBNull(ordinal, true));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once InconsistentNaming
async Task<bool> IsDBNull(int ordinal, bool async)
{
CheckRowAndOrdinal(ordinal);
await SeekToColumn(ordinal, async);
return ColumnLen == -1;
}
/// <summary>
/// Reads a stream of bytes from the specified column, starting at location indicated by dataOffset, into the buffer, starting at the location indicated by bufferOffset.
/// </summary>
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <param name="dataOffset">The index within the row from which to begin the read operation.</param>
/// <param name="buffer">The buffer into which to copy the data.</param>
/// <param name="bufferOffset">The index with the buffer to which the data will be copied.</param>
/// <param name="length">The maximum number of characters to read.</param>
/// <returns>The actual number of bytes read.</returns>
public override long GetBytes(int ordinal, long dataOffset, [CanBeNull] byte[] buffer, int bufferOffset, int length)
{
var read = base.GetBytes(ordinal, dataOffset, buffer, bufferOffset, length);
if (buffer != null) // If buffer is null we're just getting the length, no change in position
PosInColumn += (int)read;
return read;
}
#endregion
internal override async ValueTask<Stream> GetStreamInternal(int column, bool async)
{
CheckRowAndOrdinal(column);
if (_stream != null)
throw new InvalidOperationException("Attempt to read a position in the column which has already been read");
await SeekToColumn(column, false);
if (ColumnLen == -1)
throw new InvalidCastException("Column is null");
return _stream = new SequentialByteaStream(this);
}
void CheckColumnStart()
{
if (PosInColumn != 0)
throw new InvalidOperationException("Attempt to read a position in the column which has already been read");
}
class SequentialByteaStream : Stream
{
readonly NpgsqlSequentialDataReader _reader;
bool _disposed;
internal SequentialByteaStream(NpgsqlSequentialDataReader reader)
{
_reader = reader;
}
public override int Read(byte[] buffer, int offset, int count)
{
CheckDisposed();
count = Math.Min(count, _reader.ColumnLen - _reader.PosInColumn);
var read = _reader.Buffer.ReadAllBytes(buffer, offset, count, true, false).Result;
_reader.PosInColumn += read;
return read;
}
public override Task<int> ReadAsync([NotNull] byte[] buffer, int offset, int count, CancellationToken token)
{
CheckDisposed();
return SynchronizationContextSwitcher.NoContext(async () =>
{
count = Math.Min(count, _reader.ColumnLen - _reader.PosInColumn);
var read = await _reader.Buffer.ReadAllBytes(buffer, offset, count, true, true);
_reader.PosInColumn += read;
return read;
});
}
public override long Length
{
get
{
CheckDisposed();
return _reader.ColumnLen;
}
}
public override long Position
{
get
{
CheckDisposed();
return _reader.PosInColumn;
}
set => throw new NotSupportedException();
}
protected override void Dispose(bool disposing) => _disposed = true;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
void CheckDisposed()
{
if (_disposed)
throw new ObjectDisposedException(null);
}
#region Not Supported
public override void Flush()
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
#endregion
}
}
}