forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNpgsqlBinaryExporter.cs
More file actions
507 lines (428 loc) · 19.4 KB
/
NpgsqlBinaryExporter.cs
File metadata and controls
507 lines (428 loc) · 19.4 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Npgsql.BackendMessages;
using Npgsql.Internal;
using Npgsql.Internal.Postgres;
using NpgsqlTypes;
using static Npgsql.Util.Statics;
namespace Npgsql;
/// <summary>
/// Provides an API for a binary COPY TO operation, a high-performance data export mechanism from
/// a PostgreSQL table. Initiated by <see cref="NpgsqlConnection.BeginBinaryExport(string)"/>
/// </summary>
public sealed class NpgsqlBinaryExporter : ICancelable
{
const int BeforeRow = -2;
const int BeforeColumn = -1;
#region Fields and Properties
NpgsqlConnector _connector;
NpgsqlReadBuffer _buf;
bool _isConsumed, _isDisposed;
long _endOfMessagePos;
short _column;
ulong _rowsExported;
PgReader PgReader => _buf.PgReader;
/// <summary>
/// The number of columns, as returned from the backend in the CopyInResponse.
/// </summary>
internal int NumColumns { get; private set; }
PgConverterInfo[] _columnInfoCache;
readonly ILogger _copyLogger;
/// <summary>
/// Current timeout
/// </summary>
public TimeSpan Timeout
{
set
{
_buf.Timeout = value;
// While calling Complete(), we're using the connector, which overwrites the buffer's timeout with it's own
_connector.UserTimeout = (int)value.TotalMilliseconds;
}
}
#endregion
#region Construction / Initialization
internal NpgsqlBinaryExporter(NpgsqlConnector connector)
{
_connector = connector;
_buf = connector.ReadBuffer;
_column = BeforeRow;
_columnInfoCache = null!;
_copyLogger = connector.LoggingConfiguration.CopyLogger;
}
internal async Task Init(string copyToCommand, bool async, CancellationToken cancellationToken = default)
{
await _connector.WriteQuery(copyToCommand, async, cancellationToken).ConfigureAwait(false);
await _connector.Flush(async, cancellationToken).ConfigureAwait(false);
using var registration = _connector.StartNestedCancellableOperation(cancellationToken, attemptPgCancellation: false);
CopyOutResponseMessage copyOutResponse;
var msg = await _connector.ReadMessage(async).ConfigureAwait(false);
switch (msg.Code)
{
case BackendMessageCode.CopyOutResponse:
copyOutResponse = (CopyOutResponseMessage)msg;
if (!copyOutResponse.IsBinary)
{
throw _connector.Break(
new ArgumentException("copyToCommand triggered a text transfer, only binary is allowed",
nameof(copyToCommand)));
}
break;
case BackendMessageCode.CommandComplete:
throw new InvalidOperationException(
"This API only supports import/export from the client, i.e. COPY commands containing TO/FROM STDIN. " +
"To import/export with files on your PostgreSQL machine, simply execute the command with ExecuteNonQuery. " +
"Note that your data has been successfully imported/exported.");
default:
throw _connector.UnexpectedMessageReceived(msg.Code);
}
NumColumns = copyOutResponse.NumColumns;
_columnInfoCache = new PgConverterInfo[NumColumns];
_rowsExported = 0;
_endOfMessagePos = _buf.CumulativeReadPosition;
await ReadHeader(async).ConfigureAwait(false);
}
async Task ReadHeader(bool async)
{
var msg = await _connector.ReadMessage(async).ConfigureAwait(false);
_endOfMessagePos = _buf.CumulativeReadPosition + Expect<CopyDataMessage>(msg, _connector).Length;
var headerLen = NpgsqlRawCopyStream.BinarySignature.Length + 4 + 4;
await _buf.Ensure(headerLen, async).ConfigureAwait(false);
foreach (var t in NpgsqlRawCopyStream.BinarySignature)
if (_buf.ReadByte() != t)
throw new NpgsqlException("Invalid COPY binary signature at beginning!");
var flags = _buf.ReadInt32();
if (flags != 0)
throw new NotSupportedException("Unsupported flags in COPY operation (OID inclusion?)");
_buf.ReadInt32(); // Header extensions, currently unused
}
#endregion
#region Read
/// <summary>
/// Starts reading a single row, must be invoked before reading any columns.
/// </summary>
/// <returns>
/// The number of columns in the row. -1 if there are no further rows.
/// Note: This will currently be the same value for all rows, but this may change in the future.
/// </returns>
public int StartRow() => StartRow(false).GetAwaiter().GetResult();
/// <summary>
/// Starts reading a single row, must be invoked before reading any columns.
/// </summary>
/// <returns>
/// The number of columns in the row. -1 if there are no further rows.
/// Note: This will currently be the same value for all rows, but this may change in the future.
/// </returns>
public ValueTask<int> StartRowAsync(CancellationToken cancellationToken = default) => StartRow(true, cancellationToken);
async ValueTask<int> StartRow(bool async, CancellationToken cancellationToken = default)
{
CheckDisposed();
if (_isConsumed)
return -1;
using var registration = _connector.StartNestedCancellableOperation(cancellationToken);
// Consume and advance any active column.
if (_column >= 0)
await Commit(async, resumableOp: false).ConfigureAwait(false);
// The very first row (i.e. _column == -1) is included in the header's CopyData message.
// Otherwise we need to read in a new CopyData row (the docs specify that there's a CopyData
// message per row).
if (_column == NumColumns)
{
var msg = Expect<CopyDataMessage>(await _connector.ReadMessage(async).ConfigureAwait(false), _connector);
_endOfMessagePos = _buf.CumulativeReadPosition + msg.Length;
}
else if (_column != BeforeRow)
ThrowHelper.ThrowInvalidOperationException("Already in the middle of a row");
await _buf.Ensure(2, async).ConfigureAwait(false);
var numColumns = _buf.ReadInt16();
if (numColumns == -1)
{
Expect<CopyDoneMessage>(await _connector.ReadMessage(async).ConfigureAwait(false), _connector);
Expect<CommandCompleteMessage>(await _connector.ReadMessage(async).ConfigureAwait(false), _connector);
Expect<ReadyForQueryMessage>(await _connector.ReadMessage(async).ConfigureAwait(false), _connector);
_column = BeforeRow;
_isConsumed = true;
return -1;
}
Debug.Assert(numColumns == NumColumns);
_column = BeforeColumn;
_rowsExported++;
return NumColumns;
}
/// <summary>
/// Reads the current column, returns its value and moves ahead to the next column.
/// If the column is null an exception is thrown.
/// </summary>
/// <typeparam name="T">
/// The type of the column to be read. This must correspond to the actual type or data
/// corruption will occur. If in doubt, use <see cref="Read{T}(NpgsqlDbType)"/> to manually
/// specify the type.
/// </typeparam>
/// <returns>The value of the column</returns>
public T Read<T>() => Read<T>(async: false).GetAwaiter().GetResult();
/// <summary>
/// Reads the current column, returns its value and moves ahead to the next column.
/// If the column is null an exception is thrown.
/// </summary>
/// <typeparam name="T">
/// The type of the column to be read. This must correspond to the actual type or data
/// corruption will occur. If in doubt, use <see cref="Read{T}(NpgsqlDbType)"/> to manually
/// specify the type.
/// </typeparam>
/// <returns>The value of the column</returns>
public ValueTask<T> ReadAsync<T>(CancellationToken cancellationToken = default)
=> Read<T>(async: true, cancellationToken);
ValueTask<T> Read<T>(bool async, CancellationToken cancellationToken = default)
=> Read<T>(async, null, cancellationToken);
PgConverterInfo CreateConverterInfo(Type type, NpgsqlDbType? npgsqlDbType = null)
{
var options = _connector.SerializerOptions;
PgTypeId? pgTypeId = null;
if (npgsqlDbType.HasValue)
{
pgTypeId = npgsqlDbType.Value.ToDataTypeName() is { } name
? options.GetCanonicalTypeId(name)
// Handle plugin types via lookup.
: GetRepresentationalOrDefault(npgsqlDbType.Value.ToUnqualifiedDataTypeNameOrThrow());
}
var info = options.GetTypeInfo(type, pgTypeId)
?? throw new NotSupportedException($"Reading is not supported for type '{type}'{(npgsqlDbType is null ? "" : $" and NpgsqlDbType '{npgsqlDbType}'")}");
// Binary export has no type info so we only do caller-directed interpretation of data.
return info.Bind(new Field("?", info.PgTypeId!.Value, -1), DataFormat.Binary);
PgTypeId GetRepresentationalOrDefault(string dataTypeName)
{
var type = options.DatabaseInfo.GetPostgresType(dataTypeName);
return options.ToCanonicalTypeId(type.GetRepresentationalType());
}
}
/// <summary>
/// Reads the current column, returns its value according to <paramref name="type"/> and
/// moves ahead to the next column.
/// If the column is null an exception is thrown.
/// </summary>
/// <param name="type">
/// In some cases <typeparamref name="T"/> isn't enough to infer the data type coming in from the
/// database. This parameter can be used to unambiguously specify the type. An example is the JSONB
/// type, for which <typeparamref name="T"/> will be a simple string but for which
/// <paramref name="type"/> must be specified as <see cref="NpgsqlDbType.Jsonb"/>.
/// </param>
/// <typeparam name="T">The .NET type of the column to be read.</typeparam>
/// <returns>The value of the column</returns>
public T Read<T>(NpgsqlDbType type) => Read<T>(async: false, type, CancellationToken.None).GetAwaiter().GetResult();
/// <summary>
/// Reads the current column, returns its value according to <paramref name="type"/> and
/// moves ahead to the next column.
/// If the column is null an exception is thrown.
/// </summary>
/// <param name="type">
/// In some cases <typeparamref name="T"/> isn't enough to infer the data type coming in from the
/// database. This parameter can be used to unambiguously specify the type. An example is the JSONB
/// type, for which <typeparamref name="T"/> will be a simple string but for which
/// <paramref name="type"/> must be specified as <see cref="NpgsqlDbType.Jsonb"/>.
/// </param>
/// <param name="cancellationToken">
/// An optional token to cancel the asynchronous operation. The default value is <see cref="CancellationToken.None"/>.
/// </param>
/// <typeparam name="T">The .NET type of the column to be read.</typeparam>
/// <returns>The value of the column</returns>
public ValueTask<T> ReadAsync<T>(NpgsqlDbType type, CancellationToken cancellationToken = default)
=> Read<T>(async: true, type, cancellationToken);
async ValueTask<T> Read<T>(bool async, NpgsqlDbType? type, CancellationToken cancellationToken)
{
CheckDisposed();
if (_column is BeforeRow)
ThrowHelper.ThrowInvalidOperationException("Not reading a row");
using var registration = _connector.StartNestedCancellableOperation(cancellationToken, attemptPgCancellation: false);
// Allow one more read if the field is a db null.
// We cannot allow endless rereads otherwise it becomes quite unclear when a column advance happens.
if (PgReader is { Initialized: true, Resumable: true, FieldSize: -1 })
{
await Commit(async, resumableOp: false).ConfigureAwait(false);
return DbNullOrThrow();
}
// We must commit the current column before reading the next one unless it was an IsNull call.
PgConverterInfo info;
bool asObject;
if (!PgReader.Initialized || !PgReader.Resumable || PgReader.CurrentRemaining != PgReader.FieldSize)
{
await Commit(async, resumableOp: false).ConfigureAwait(false);
info = GetInfo(out asObject);
// We need to get info after potential I/O as we don't know beforehand at what column we're at.
var columnLen = await ReadColumnLenIfNeeded(async, resumableOp: false).ConfigureAwait(false);
if (_column == NumColumns)
ThrowHelper.ThrowInvalidOperationException("No more columns left in the current row");
if (columnLen is -1)
return DbNullOrThrow();
}
else
info = GetInfo(out asObject);
T result;
if (async)
{
await PgReader.StartReadAsync(info.BufferRequirement, cancellationToken).ConfigureAwait(false);
result = asObject
? (T)await info.Converter.ReadAsObjectAsync(PgReader, cancellationToken).ConfigureAwait(false)
: await info.GetConverter<T>().ReadAsync(PgReader, cancellationToken).ConfigureAwait(false);
await PgReader.EndReadAsync().ConfigureAwait(false);
}
else
{
PgReader.StartRead(info.BufferRequirement);
result = asObject
? (T)info.Converter.ReadAsObject(PgReader)
: info.GetConverter<T>().Read(PgReader);
PgReader.EndRead();
}
return result;
PgConverterInfo GetInfo(out bool asObject)
{
ref var cachedInfo = ref _columnInfoCache[_column];
var converterInfo = cachedInfo.IsDefault ? cachedInfo = CreateConverterInfo(typeof(T), type) : cachedInfo;
asObject = converterInfo.IsBoxingConverter;
return converterInfo;
}
T DbNullOrThrow()
{
// When T is a Nullable<T>, we support returning null
if (default(T) is null && typeof(T).IsValueType)
return default!;
throw new InvalidCastException("Column is null");
}
}
/// <summary>
/// Returns whether the current column is null.
/// </summary>
public bool IsNull
{
get
{
Commit(async: false, resumableOp: true);
return ReadColumnLenIfNeeded(async: false, resumableOp: true).GetAwaiter().GetResult() is -1;
}
}
/// <summary>
/// Skips the current column without interpreting its value.
/// </summary>
public void Skip() => Skip(async: false).GetAwaiter().GetResult();
/// <summary>
/// Skips the current column without interpreting its value.
/// </summary>
public Task SkipAsync(CancellationToken cancellationToken = default)
=> Skip(true, cancellationToken);
async Task Skip(bool async, CancellationToken cancellationToken = default)
{
CheckDisposed();
using var registration = _connector.StartNestedCancellableOperation(cancellationToken);
// We allow IsNull to have been called before skip.
if (PgReader.Initialized && PgReader is not { Resumable: true, FieldSize: -1 })
await Commit(async, resumableOp: false).ConfigureAwait(false);
await ReadColumnLenIfNeeded(async, resumableOp: false).ConfigureAwait(false);
await PgReader.Consume(async, cancellationToken: cancellationToken).ConfigureAwait(false);
}
#endregion
#region Utilities
ValueTask Commit(bool async, bool resumableOp)
{
var resuming = PgReader is { Initialized: true, Resumable: true } && resumableOp;
if (!resuming)
_column++;
if (async)
return PgReader.CommitAsync(resuming);
PgReader.Commit(resuming);
return new();
}
async ValueTask<int> ReadColumnLenIfNeeded(bool async, bool resumableOp)
{
if (PgReader is { Initialized: true, Resumable: true, FieldSize: -1 })
return -1;
await _buf.Ensure(4, async).ConfigureAwait(false);
var columnLen = _buf.ReadInt32();
PgReader.Init(columnLen, DataFormat.Binary, resumableOp);
return PgReader.FieldSize;
}
void CheckDisposed()
{
if (_isDisposed)
ThrowHelper.ThrowObjectDisposedException(nameof(NpgsqlBinaryExporter), "The COPY operation has already ended.");
}
#endregion
#region Cancel / Close / Dispose
/// <summary>
/// Cancels an ongoing export.
/// </summary>
public void Cancel() => _connector.PerformUserCancellation();
/// <summary>
/// Async cancels an ongoing export.
/// </summary>
public Task CancelAsync()
{
Cancel();
return Task.CompletedTask;
}
/// <summary>
/// Completes that binary export and sets the connection back to idle state
/// </summary>
public void Dispose() => DisposeAsync(async: false).GetAwaiter().GetResult();
/// <summary>
/// Async completes that binary export and sets the connection back to idle state
/// </summary>
/// <returns></returns>
public ValueTask DisposeAsync() => DisposeAsync(async: true);
async ValueTask DisposeAsync(bool async)
{
if (_isDisposed)
return;
if (_isConsumed)
{
LogMessages.BinaryCopyOperationCompleted(_copyLogger, _rowsExported, _connector.Id);
}
else if (!_connector.IsBroken)
{
try
{
using var registration = _connector.StartNestedCancellableOperation(attemptPgCancellation: false);
// Be sure to commit the reader.
if (async)
await PgReader.CommitAsync(resuming: false).ConfigureAwait(false);
else
PgReader.Commit(resuming: false);
// Finish the current CopyData message
await _buf.Skip(checked((int)(_endOfMessagePos - _buf.CumulativeReadPosition)), async).ConfigureAwait(false);
// Read to the end
_connector.SkipUntil(BackendMessageCode.CopyDone);
// We intentionally do not pass a CancellationToken since we don't want to cancel cleanup
Expect<CommandCompleteMessage>(await _connector.ReadMessage(async).ConfigureAwait(false), _connector);
Expect<ReadyForQueryMessage>(await _connector.ReadMessage(async).ConfigureAwait(false), _connector);
}
catch (OperationCanceledException e) when (e.InnerException is PostgresException pg && pg.SqlState == PostgresErrorCodes.QueryCanceled)
{
LogMessages.CopyOperationCancelled(_copyLogger, _connector.Id);
}
catch (Exception e)
{
LogMessages.ExceptionWhenDisposingCopyOperation(_copyLogger, _connector.Id, e);
}
}
_connector.EndUserAction();
Cleanup();
}
#pragma warning disable CS8625
void Cleanup()
{
Debug.Assert(!_isDisposed);
var connector = _connector;
if (connector != null)
{
connector.CurrentCopyOperation = null;
_connector.Connection?.EndBindingScope(ConnectorBindingScope.Copy);
_connector = null;
}
_buf = null;
_isDisposed = true;
}
#pragma warning restore CS8625
#endregion
}