forked from Emill/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlBinaryImporter.cs
More file actions
executable file
·324 lines (279 loc) · 10.8 KB
/
NpgsqlBinaryImporter.cs
File metadata and controls
executable file
·324 lines (279 loc) · 10.8 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
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using Npgsql.BackendMessages;
using Npgsql.FrontendMessages;
using NpgsqlTypes;
namespace Npgsql
{
/// <summary>
/// Provides an API for a binary COPY FROM operation, a high-performance data import mechanism to
/// a PostgreSQL table. Initiated by <see cref="NpgsqlConnection.BeginBinaryImport"/>
/// </summary>
/// <remarks>
/// See http://www.postgresql.org/docs/current/static/sql-copy.html.
/// </remarks>
public class NpgsqlBinaryImporter : IDisposable
{
#region Fields and Properties
NpgsqlConnector _connector;
NpgsqlBuffer _buf;
TypeHandlerRegistry _registry;
LengthCache _lengthCache;
bool _isDisposed;
bool _writingDataMsg;
/// <summary>
/// The number of columns in the current (not-yet-written) row.
/// </summary>
short _column;
/// <summary>
/// The number of columns, as returned from the backend in the CopyInResponse.
/// </summary>
internal int NumColumns { get; private set; }
#endregion
#region Construction / Initialization
internal NpgsqlBinaryImporter(NpgsqlConnector connector, string copyFromCommand)
{
_connector = connector;
_buf = connector.Buffer;
_registry = connector.TypeHandlerRegistry;
_lengthCache = new LengthCache();
_column = -1;
try
{
_connector.SendSingleMessage(new QueryMessage(copyFromCommand));
// TODO: Failure will break the connection (e.g. if we get CopyOutResponse), handle more gracefully
var copyInResponse = _connector.ReadExpecting<CopyInResponseMessage>();
if (!copyInResponse.IsBinary)
{
throw new ArgumentException("copyFromCommand triggered a text transfer, only binary is allowed", "copyFromCommand");
}
NumColumns = copyInResponse.NumColumns;
WriteHeader();
}
catch
{
_connector.Break();
throw;
}
}
void WriteHeader()
{
EnsureDataMessage();
_buf.WriteBytesSimple(NpgsqlRawCopyStream.BinarySignature);
_buf.WriteInt32(0); // Flags field. OID inclusion not supported at the moment.
_buf.WriteInt32(0); // Header extension area length
}
#endregion
#region Write
/// <summary>
/// Starts writing a single row, must be invoked before writing any columns.
/// </summary>
public void StartRow()
{
if (_column != -1 && _column != NumColumns) {
throw new InvalidOperationException("Row has already been started and must be finished");
}
if (_buf.WriteSpaceLeft < 2) { FlushAndStartDataMessage(); }
_buf.WriteInt16(NumColumns);
_column = 0;
}
/// <summary>
/// Writes a single column in the current row.
/// </summary>
/// <param name="value">The value to be written</param>
/// <typeparam name="T">
/// The type of the column to be written. This must correspond to the actual type or data
/// corruption will occur. If in doubt, use <see cref="Write{T}(T, NpgsqlDbType)"/> to manually
/// specify the type.
/// </typeparam>
public void Write<T>(T value)
{
CheckDisposed();
if (_column == -1) {
throw new InvalidOperationException("A row hasn't been started");
}
var handler = _registry[value];
DoWrite(handler, value);
}
/// <summary>
/// Writes a single column in the current row as type <paramref name="type"/>.
/// </summary>
/// <param name="value">The value to be written</param>
/// <param name="type">
/// In some cases <typeparamref name="T"/> isn't enough to infer the data type to be written to
/// the database. This parameter and 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 written.</typeparam>
public void Write<T>(T value, NpgsqlDbType type)
{
CheckDisposed();
if (_column == -1) {
throw new InvalidOperationException("A row hasn't been started");
}
var handler = _registry[type];
DoWrite(handler, value);
}
void DoWrite<T>(TypeHandler handler, T value)
{
if (_buf.WriteSpaceLeft < 4) { Flush(); }
EnsureDataMessage();
var asObject = (object)value; // TODO: Implement boxless writing in the future
if (asObject == null) {
_buf.WriteInt32(-1);
_column++;
return;
}
var asSimple = handler as ISimpleTypeWriter;
if (asSimple != null) {
var len = asSimple.ValidateAndGetLength(asObject);
_buf.WriteInt32(len);
if (_buf.WriteSpaceLeft < len) {
Contract.Assume(_buf.Size >= len);
FlushAndStartDataMessage();
}
asSimple.Write(asObject, _buf);
_column++;
return;
}
var asChunking = handler as IChunkingTypeWriter;
if (asChunking != null) {
_lengthCache.Clear();
var len = asChunking.ValidateAndGetLength(asObject, ref _lengthCache);
_buf.WriteInt32(len);
_lengthCache.Rewind();
_lengthCache.Get(); // Hack
asChunking.PrepareWrite(asObject, _buf, _lengthCache);
var directBuf = new DirectBuffer();
while (!asChunking.Write(ref directBuf)) {
FlushAndStartDataMessage();
// The following is an optimization hack for writing large byte arrays without passing
// through our buffer
if (directBuf.Buffer != null) {
len = directBuf.Size == 0 ? directBuf.Buffer.Length : directBuf.Size;
_buf.WriteInt32(len);
Flush();
_buf.Underlying.Write(directBuf.Buffer, directBuf.Offset, len);
directBuf.Buffer = null;
directBuf.Size = 0;
}
}
_column++;
return;
}
throw PGUtil.ThrowIfReached();
}
/// <summary>
/// Writes a single null column value.
/// </summary>
public void WriteNull()
{
CheckDisposed();
if (_column == -1) {
throw new InvalidOperationException("A row hasn't been started");
}
if (_buf.WriteSpaceLeft < 4) { Flush(); }
EnsureDataMessage();
_buf.WriteInt32(-1);
_column++;
}
/// <summary>
/// Writes an entire row of columns.
/// Equivalent to calling <see cref="StartRow"/>, followed by multiple <see cref="Write{T}(T)"/>
/// on each value.
/// </summary>
/// <param name="values">An array of column values to be written as a single row</param>
public void WriteRow(params object[] values)
{
StartRow();
foreach (var value in values) {
Write(value);
}
}
void FlushAndStartDataMessage()
{
Flush();
EnsureDataMessage();
}
void Flush()
{
if (!_writingDataMsg) { return; }
// Need to update the length for the CopyData about to be sent
var pos = _buf.WritePosition;
_buf.WritePosition = 1;
_buf.WriteInt32(pos - 1);
_buf.WritePosition = pos;
_buf.Flush();
_writingDataMsg = false;
}
void EnsureDataMessage()
{
if (_writingDataMsg) { return; }
Contract.Assert(_buf.WritePosition == 0);
_buf.WriteByte((byte)BackendMessageCode.CopyData);
// Leave space for the message length
_buf.WriteInt32(0);
_writingDataMsg = true;
}
#endregion
#region Cancel / Close / Dispose
/// <summary>
/// Cancels and terminates an ongoing import. Any data already written will be discarded.
/// </summary>
public void Cancel()
{
_isDisposed = true;
_buf.Clear();
_connector.SendSingleMessage(new CopyFailMessage());
try {
var msg = _connector.ReadSingleMessage();
// The CopyFail should immediately trigger an exception from the read above.
_connector.Break();
throw new Exception("Expected ErrorResponse when cancelling COPY but got: " + msg.Code);
} catch (NpgsqlException e) {
if (e.Code == "57014") { return; }
throw;
}
}
/// <summary>
/// Completes that binary import and sets the connection back to idle state
/// </summary>
public void Dispose() { Close(); }
/// <summary>
/// Completes the import process and signals to the database to write everything.
/// </summary>
public void Close()
{
if (_isDisposed) { return; }
if (_column != -1 && _column != NumColumns) {
throw new InvalidOperationException("Can't close writer, a row is still in progress, end it first");
}
WriteTrailer();
_connector.SendSingleMessage(CopyDoneMessage.Instance);
_connector.ReadExpecting<CommandCompleteMessage>();
_connector.ReadExpecting<ReadyForQueryMessage>();
_connector.EndUserAction();
_connector = null;
_registry = null;
_buf = null;
_isDisposed = true;
}
void WriteTrailer()
{
if (_buf.WriteSpaceLeft < 2) { FlushAndStartDataMessage(); }
_buf.WriteInt16(-1);
Flush();
}
void CheckDisposed()
{
if (_isDisposed) {
throw new ObjectDisposedException(GetType().FullName, "The COPY operation has already ended.");
}
}
#endregion
}
}