-
Notifications
You must be signed in to change notification settings - Fork 818
/
PgWriter.cs
571 lines (478 loc) · 18.7 KB
/
PgWriter.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Npgsql.Internal.Postgres;
namespace Npgsql.Internal;
enum FlushMode
{
None,
Blocking,
NonBlocking
}
// A streaming alternative to a System.IO.Stream, instead based on the preferable IBufferWriter.
interface IStreamingWriter<T>: IBufferWriter<T>
{
void Flush(TimeSpan timeout = default);
ValueTask FlushAsync(CancellationToken cancellationToken = default);
}
sealed class NpgsqlBufferWriter : IStreamingWriter<byte>
{
readonly NpgsqlWriteBuffer _buffer;
int? _lastBufferSize;
public NpgsqlBufferWriter(NpgsqlWriteBuffer buffer) => _buffer = buffer;
public void Advance(int count)
{
if (_lastBufferSize < count || _buffer.WriteSpaceLeft < count)
throw new InvalidOperationException("Cannot advance past the end of the current buffer.");
_lastBufferSize = null;
_buffer.WritePosition += count;
}
public Memory<byte> GetMemory(int sizeHint = 0)
{
if (sizeHint > _buffer.WriteSpaceLeft)
throw new OutOfMemoryException("Not enough space left in buffer.");
var bufferSize = _buffer.WriteSpaceLeft;
_lastBufferSize = bufferSize;
return _buffer.Buffer.AsMemory(_buffer.WritePosition, bufferSize);
}
public Span<byte> GetSpan(int sizeHint = 0)
{
if (sizeHint > _buffer.WriteSpaceLeft)
throw new OutOfMemoryException("Not enough space left in buffer.");
var bufferSize = _buffer.WriteSpaceLeft;
_lastBufferSize = bufferSize;
return _buffer.Buffer.AsSpan(_buffer.WritePosition, bufferSize);
}
public void Flush(TimeSpan timeout = default)
{
if (timeout == TimeSpan.Zero)
_buffer.Flush();
else
{
TimeSpan? originalTimeout = null;
try
{
if (timeout != TimeSpan.Zero)
{
originalTimeout = _buffer.Timeout;
_buffer.Timeout = timeout;
}
_buffer.Flush();
}
finally
{
if (originalTimeout is { } value)
_buffer.Timeout = value;
}
}
}
public ValueTask FlushAsync(CancellationToken cancellationToken = default)
=> new(_buffer.Flush(async: true, cancellationToken));
}
public sealed class PgWriter
{
readonly IBufferWriter<byte> _writer;
byte[]? _buffer;
int _offset;
int _pos;
int _length;
int _totalBytesWritten;
ValueMetadata _current;
NpgsqlDatabaseInfo? _typeCatalog;
internal PgWriter(IBufferWriter<byte> writer) => _writer = writer;
internal PgWriter Init(NpgsqlDatabaseInfo typeCatalog)
{
if (_typeCatalog is not null)
throw new InvalidOperationException("Invalid concurrent use or PgWriter was not reset properly.");
_typeCatalog = typeCatalog;
return this;
}
internal void Reset()
{
if (_pos != _offset)
throw new InvalidOperationException("PgWriter still has uncommitted bytes.");
_typeCatalog = null;
FlushMode = FlushMode.None;
_totalBytesWritten = 0;
ResetBuffer();
}
void ResetBuffer()
{
_buffer = null;
_pos = 0;
_offset = 0;
_length = 0;
}
internal FlushMode FlushMode { get; private set; }
internal PgWriter Refresh()
{
if (_buffer is not null)
ResetBuffer();
return this;
}
internal PgWriter WithFlushMode(FlushMode mode)
{
FlushMode = mode;
return this;
}
// TODO if we're working on a normal buffer writer we should use normal Ensure (so commit and get another buffer) semantics.
void Ensure(int count = 1)
{
if (_buffer is null)
SetBuffer();
if (count > _length - _pos)
ThrowOutOfRange();
void ThrowOutOfRange() => throw new ArgumentOutOfRangeException(nameof(count), "Coud not ensure enough space in buffer.");
[MethodImpl(MethodImplOptions.NoInlining)]
void SetBuffer()
{
// GetMemory will check whether count is larger than the max buffer size.
var mem = _writer.GetMemory(count);
if (!MemoryMarshal.TryGetArray<byte>(mem, out var segment))
throw new NotSupportedException("Only array backed writers are supported.");
_buffer = segment.Array!;
_offset = segment.Offset;
_pos = segment.Offset;
_length = segment.Offset + segment.Count;
}
}
Span<byte> Span => _buffer.AsSpan(_pos, _length - _pos);
int Remaining
{
get
{
if (_buffer is null)
Ensure(count: 0);
return _length - _pos;
}
}
void Advance(int count) => _pos += count;
internal void Commit(int? expectedByteCount = null)
{
_totalBytesWritten += _pos - _offset;
_writer.Advance(_pos - _offset);
_offset = _pos;
if (expectedByteCount is not null)
{
var totalBytesWritten = _totalBytesWritten;
_totalBytesWritten = 0;
if (totalBytesWritten != expectedByteCount)
throw new InvalidOperationException($"Bytes written ({totalBytesWritten}) and expected byte count ({expectedByteCount}) don't match.");
}
}
internal ValueTask BeginWrite(bool async, ValueMetadata current, CancellationToken cancellationToken)
{
_current = current;
if (ShouldFlush(current.BufferRequirement))
return Flush(async, cancellationToken);
return new();
}
public ValueMetadata Current => _current;
internal Size CurrentBufferRequirement => _current.BufferRequirement;
// When we don't know the size during writing we're using the writer buffer as a sizing mechanism.
internal bool BufferingWrite => Current.Size.Kind is SizeKind.Unknown;
// This method lives here to remove the chances oids will be cached on converters inadvertently when data type names should be used.
// Such a mapping (for instance for array element oids) should be done per operation to ensure it is done in the context of a specific backend.
public void WriteAsOid(PgTypeId pgTypeId)
{
var oid = _typeCatalog!.GetOid(pgTypeId);
WriteUInt32((uint)oid);
}
public void WriteByte(byte value)
{
Ensure(sizeof(byte));
Span[0] = value;
Advance(sizeof(byte));
}
public void WriteInt16(short value)
{
Ensure(sizeof(short));
BinaryPrimitives.WriteInt16BigEndian(Span, value);
Advance(sizeof(short));
}
public void WriteInt32(int value)
{
Ensure(sizeof(int));
BinaryPrimitives.WriteInt32BigEndian(Span, value);
Advance(sizeof(int));
}
public void WriteInt64(long value)
{
Ensure(sizeof(long));
BinaryPrimitives.WriteInt64BigEndian(Span, value);
Advance(sizeof(long));
}
public void WriteUInt16(ushort value)
{
Ensure(sizeof(ushort));
BinaryPrimitives.WriteUInt16BigEndian(Span, value);
Advance(sizeof(ushort));
}
public void WriteUInt32(uint value)
{
Ensure(sizeof(uint));
BinaryPrimitives.WriteUInt32BigEndian(Span, value);
Advance(sizeof(uint));
}
public void WriteUInt64(ulong value)
{
Ensure(sizeof(ulong));
BinaryPrimitives.WriteUInt64BigEndian(Span, value);
Advance(sizeof(ulong));
}
public void WriteFloat(float value)
{
#if NET5_0_OR_GREATER
Ensure(sizeof(float));
BinaryPrimitives.WriteSingleBigEndian(Span, value);
Advance(sizeof(float));
#else
WriteUInt32(Unsafe.As<float, uint>(ref value));
#endif
}
public void WriteDouble(double value)
{
#if NET5_0_OR_GREATER
Ensure(sizeof(double));
BinaryPrimitives.WriteDoubleBigEndian(Span, value);
Advance(sizeof(double));
#else
WriteUInt64(Unsafe.As<double, ulong>(ref value));
#endif
}
public void WriteChars(ReadOnlySpan<char> data, Encoding encoding)
{
// If we have more chars than bytes remaining we can immediately go to the slow path.
if (data.Length <= Remaining)
{
// If not, it's worth a shot to see if we can convert in one go.
var encodedLength = encoding.GetByteCount(data);
if (!ShouldFlush(encodedLength))
{
var count = encoding.GetBytes(data, Span);
Advance(count);
return;
}
}
Core(data, encoding);
void Core(ReadOnlySpan<char> data, Encoding encoding)
{
var encoder = encoding.GetEncoder();
var minBufferSize = encoding.GetMaxByteCount(1);
bool completed;
do
{
if (ShouldFlush(minBufferSize))
Flush();
Ensure(minBufferSize);
encoder.Convert(data, Span, flush: data.Length <= Span.Length, out var charsUsed, out var bytesUsed, out completed);
data = data.Slice(charsUsed);
Advance(bytesUsed);
} while (!completed);
}
}
public ValueTask WriteCharsAsync(ReadOnlyMemory<char> data, Encoding encoding, CancellationToken cancellationToken = default)
{
var dataSpan = data.Span;
// If we have more chars than bytes remaining we can immediately go to the slow path.
if (data.Length <= Remaining)
{
// If not, it's worth a shot to see if we can convert in one go.
var encodedLength = encoding.GetByteCount(dataSpan);
if (!ShouldFlush(encodedLength))
{
var count = encoding.GetBytes(dataSpan, Span);
Advance(count);
return new();
}
}
return Core(data, encoding, cancellationToken);
async ValueTask Core(ReadOnlyMemory<char> data, Encoding encoding, CancellationToken cancellationToken)
{
var encoder = encoding.GetEncoder();
var minBufferSize = encoding.GetMaxByteCount(1);
bool completed;
do
{
if (ShouldFlush(minBufferSize))
await FlushAsync(cancellationToken).ConfigureAwait(false);
Ensure(minBufferSize);
encoder.Convert(data.Span, Span, flush: data.Length <= Span.Length, out var charsUsed, out var bytesUsed, out completed);
data = data.Slice(charsUsed);
Advance(bytesUsed);
} while (!completed);
}
}
public void WriteBytes(ReadOnlySpan<byte> buffer)
{
while (!buffer.IsEmpty)
{
var write = Math.Min(buffer.Length, Remaining);
buffer.Slice(0, write).CopyTo(Span);
Advance(write);
buffer = buffer.Slice(write);
if (Remaining is 0)
Flush();
}
}
public ValueTask WriteBytesAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (buffer.Length <= Remaining)
{
buffer.Span.CopyTo(Span);
Advance(buffer.Length);
return new();
}
return Core(buffer, cancellationToken);
async ValueTask Core(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
{
while (!buffer.IsEmpty)
{
var write = Math.Min(buffer.Length, Remaining);
buffer.Span.Slice(0, write).CopyTo(Span);
Advance(write);
buffer = buffer.Slice(write);
if (Remaining is 0)
await FlushAsync(cancellationToken).ConfigureAwait(false);
}
}
}
public Stream GetStream()
=> new PgWriterStream(this);
public bool ShouldFlush(Size bufferRequirement)
=> ShouldFlush(bufferRequirement is { Kind: SizeKind.UpperBound }
? Math.Min(Current.Size.Value, bufferRequirement.Value)
: bufferRequirement.GetValueOrDefault());
public bool ShouldFlush(int byteCount) => Remaining < byteCount && FlushMode is not FlushMode.None;
public void Flush(TimeSpan timeout = default)
{
switch (FlushMode)
{
case FlushMode.None:
return;
case FlushMode.NonBlocking:
throw new NotSupportedException($"Cannot call {nameof(Flush)} on a non-blocking {nameof(PgWriter)}, you might need to override {nameof(PgConverter<object>.WriteAsync)} on {nameof(PgConverter)} if you want to call flush.");
}
if (_writer is not IStreamingWriter<byte> writer)
throw new NotSupportedException($"Cannot call {nameof(Flush)} on a buffered {nameof(PgWriter)}, {nameof(FlushMode)}.{nameof(FlushMode.None)} should be used to prevent this.");
Commit();
ResetBuffer();
writer.Flush(timeout);
}
public ValueTask FlushAsync(CancellationToken cancellationToken = default)
{
switch (FlushMode)
{
case FlushMode.None:
return new();
case FlushMode.Blocking:
throw new NotSupportedException($"Cannot call {nameof(FlushAsync)} on a blocking {nameof(PgWriter)}, call Flush instead.");
}
if (_writer is not IStreamingWriter<byte> writer)
throw new NotSupportedException($"Cannot call {nameof(FlushAsync)} on a buffered {nameof(PgWriter)}, {nameof(FlushMode)}.{nameof(FlushMode.None)} should be used to prevent this.");
Commit();
ResetBuffer();
return writer.FlushAsync(cancellationToken);
}
internal ValueTask Flush(bool async, CancellationToken cancellationToken = default)
{
if (async)
return FlushAsync(cancellationToken);
Flush();
return new();
}
internal ValueTask<NestedWriteScope> BeginNestedWrite(bool async, Size bufferRequirement, int byteCount, object? state, CancellationToken cancellationToken)
{
Debug.Assert(bufferRequirement != -1);
if (ShouldFlush(bufferRequirement))
return Core(async, bufferRequirement, byteCount, state, cancellationToken);
_current = new() { Format = _current.Format, Size = byteCount, BufferRequirement = bufferRequirement, WriteState = state };
return new(new NestedWriteScope());
#if NET6_0_OR_GREATER
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
#endif
async ValueTask<NestedWriteScope> Core(bool async, Size bufferRequirement, int byteCount, object? state, CancellationToken cancellationToken)
{
await Flush(async, cancellationToken).ConfigureAwait(false);
_current = new() { Format = _current.Format, Size = byteCount, BufferRequirement = bufferRequirement, WriteState = state };
return new();
}
}
public NestedWriteScope BeginNestedWrite(Size bufferRequirement, int byteCount, object? state)
=> BeginNestedWrite(async: false, bufferRequirement, byteCount, state, CancellationToken.None).GetAwaiter().GetResult();
public ValueTask<NestedWriteScope> BeginNestedWriteAsync(Size bufferRequirement, int byteCount, object? state, CancellationToken cancellationToken = default)
=> BeginNestedWrite(async: true, bufferRequirement, byteCount, state, cancellationToken);
sealed class PgWriterStream : Stream
{
readonly PgWriter _writer;
internal PgWriterStream(PgWriter writer)
=> _writer = writer;
public override void Write(byte[] buffer, int offset, int count)
=> Write(async: false, buffer: buffer, offset: offset, count: count, CancellationToken.None).GetAwaiter().GetResult();
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> Write(async: true, buffer: buffer, offset: offset, count: count, cancellationToken: cancellationToken);
Task Write(bool async, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (buffer is null)
throw new ArgumentNullException(nameof(buffer));
if (offset < 0)
throw new ArgumentNullException(nameof(offset));
if (count < 0)
throw new ArgumentNullException(nameof(count));
if (buffer.Length - offset < count)
throw new ArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
if (async)
{
if (cancellationToken.IsCancellationRequested)
return Task.FromCanceled(cancellationToken);
return _writer.WriteBytesAsync(buffer, cancellationToken).AsTask();
}
_writer.WriteBytes(new Span<byte>(buffer, offset, count));
return Task.CompletedTask;
}
#if !NETSTANDARD2_0
public override void Write(ReadOnlySpan<byte> buffer) => _writer.WriteBytes(buffer);
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
return new(Task.FromCanceled(cancellationToken));
return _writer.WriteBytesAsync(buffer, cancellationToken);
}
#endif
public override void Flush()
=> _writer.Flush();
public override Task FlushAsync(CancellationToken cancellationToken)
=> _writer.FlushAsync(cancellationToken).AsTask();
public override bool CanRead => false;
public override bool CanWrite => true;
public override bool CanSeek => false;
public override int Read(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> throw new NotSupportedException();
public override long Length => throw new NotSupportedException();
public override void SetLength(long value)
=> throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();
}
}
// No-op for now.
public struct NestedWriteScope : IDisposable
{
public void Dispose()
{
}
}