-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProtocol.Types.Array.cs
937 lines (857 loc) · 37.3 KB
/
Protocol.Types.Array.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/*
* Copyright 2017 Stanislav Muhametsin. All rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using CBAM.Abstractions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UtilPack;
namespace CBAM.SQL.PostgreSQL.Implementation
{
using TextSizeAdditionalInfo = ValueTuple<BackendSizeInfo[], ValueTuple<Int32[], Int32[], Int32>>;
internal sealed class PgSQLTypeFunctionalityForArrays : PgSQLTypeFunctionality
{
internal const Byte ARRAY_START = (Byte) '{';
internal const Byte ARRAY_END = (Byte) '}';
internal const Char ESCAPE = '\\';
internal const Char QUOTE = '"';
internal const Byte ARRAY_LOBO_START = (Byte) '[';
internal const Byte ARRAY_LOBO_END = (Byte) ']';
internal const Byte ARRAY_LOBO_SPEC_SEPARATOR = (Byte) '=';
internal const Char DIM_SEPARATOR = ':';
internal const Int32 NULL_CHAR_COUNT = 4;
internal const String NULL_STRING = "NULL";
private readonly Array _emptyArray;
private readonly Type _arrayElementType;
private readonly Lazy<TypeFunctionalityInformation> _elementTypeInfo;
public PgSQLTypeFunctionalityForArrays(
TypeRegistry protocol,
ref Type arrayElementType,
Int32 elementTypeID
)
{
this._elementTypeInfo = new Lazy<TypeFunctionalityInformation>( () => protocol.TryGetTypeInfo( elementTypeID ), LazyThreadSafetyMode.PublicationOnly );
if ( arrayElementType.GetTypeInfo().IsValueType && !arrayElementType.IsNullable() )
{
// Allow nulls
arrayElementType = typeof( Nullable<> ).MakeGenericType( arrayElementType );
}
// TODO maybe make CLRType getter throw exception? Since the actual type may be X[], X[,], X[,,], etc...?
this._arrayElementType = arrayElementType;
this._emptyArray = Array.CreateInstance( arrayElementType, 0 );
}
public Boolean SupportsReadingBinaryFormat => this._elementTypeInfo.Value.Functionality.SupportsReadingBinaryFormat;
public Boolean SupportsWritingBinaryFormat => this._elementTypeInfo.Value.Functionality.SupportsWritingBinaryFormat;
public Object ChangeTypeFrameworkToPgSQL( PgSQLTypeDatabaseData dbData, Object obj )
{
// We will enter here for multidimensional arrays, from BindMessage
var objType = obj.GetType();
return objType.IsArray
&& EqualsIgnoreNullability( objType.GetElementType(), this._arrayElementType ) ? // The WriteArrayText and WriteArrayBinary methods work for both X[] and X?[] array types.
obj :
throw new InvalidCastException( $"The object to cast must be single- or multidimensionsal array with element type of {this._arrayElementType.FullName}." );
}
private static Boolean EqualsIgnoreNullability( Type x, Type y )
{
return Equals( x, y )
|| Equals( GetActualIfNullable( x ), GetActualIfNullable( y ) );
}
private static Type GetActualIfNullable( Type type )
{
return type.IsNullable( out var actual ) ? actual : type;
}
public Object ChangeTypePgSQLToFramework( PgSQLTypeDatabaseData dbData, Object obj, Type typeTo )
{
// TODO cast all elements of array...?
throw new InvalidCastException();
}
public BackendSizeInfo GetBackendSize( DataFormat dataFormat, PgSQLTypeDatabaseData boundData, BackendABIHelper helper, Object value, Boolean isArrayElement )
{
switch ( dataFormat )
{
case DataFormat.Text:
return this.GetBackendTextSize( boundData, helper, value );
case DataFormat.Binary:
return this.GetBackendBinarySize( boundData, helper, value );
default:
throw new NotSupportedException( $"Data format {dataFormat} is not recognized." );
}
}
private BackendSizeInfo GetBackendBinarySize( PgSQLTypeDatabaseData boundData, BackendABIHelper helper, Object value )
{
var array = (Array) value;
BackendSizeInfo retVal;
var arrayLength = array.Length;
// The header size is three integers (rank, null map, element type id), and then two integers for each rank
var size = sizeof( Int32 ) * 3;
BackendSizeInfo[] elementSizes;
if ( arrayLength > 0 )
{
size += array.Rank * 2 * sizeof( Int32 );
elementSizes = new BackendSizeInfo[arrayLength];
var i = 0;
var elementInfo = this._elementTypeInfo.Value;
foreach ( var elem in array )
{
var sizeInfo = elementInfo.Functionality.GetBackendSizeCheckNull( DataFormat.Binary, elementInfo.DatabaseData, helper, elem, true );
elementSizes[i++] = sizeInfo;
size += sizeof( Int32 );
if ( sizeInfo.ByteCount > 0 )
{
size += sizeInfo.ByteCount;
}
}
}
else
{
elementSizes = null;
}
retVal = new BackendSizeInfo( size, elementSizes );
return retVal;
}
private BackendSizeInfo GetBackendTextSize( PgSQLTypeDatabaseData boundData, BackendABIHelper helper, Object value )
{
var array = (Array) value;
var helperEncoding = helper.Encoding;
var encoding = helperEncoding.Encoding;
var asciiSize = helperEncoding.BytesPerASCIICharacter;
var length = array.Length;
BackendSizeInfo retVal;
if ( length <= 0 )
{
retVal = new BackendSizeInfo( 2 * asciiSize );
}
else
{
var rank = array.Rank;
var bracesCount = 0; // Amount of array start/end braces
var innermostLength = array.GetLength( rank - 1 );
var delimitersCount = Math.Max( innermostLength - 1, 0 ); // Amount of delimiter characters
// Iterate from second-innermost dimension towards outermost dimension
for ( var i = rank - 2; i >= 0; --i )
{
var curLen = array.GetLength( i );
bracesCount = bracesCount * curLen + 2 * curLen;
delimitersCount = curLen - 1 + delimitersCount * curLen;
}
// Remember outermost braces
bracesCount += 2;
var elementSizes = new BackendSizeInfo[length];
var elementInfo = this._elementTypeInfo.Value;
var j = 0;
foreach ( var elem in array )
{
elementSizes[j++] = elementInfo.Functionality.GetBackendSizeCheckNull( DataFormat.Text, elementInfo.DatabaseData, helper, elem, true );
}
// All the space taken by array structure information
var sizeForArrayInfra = encoding.GetByteCount( boundData.ArrayDelimiter ) * delimitersCount + bracesCount * asciiSize;
var lobos = array.GetLowerBounds();
var loboSpecByteCount = 0;
Int32[] upbos = null;
if ( lobos != null )
{
// Bounds specification: "[lobo1:upbo1][lobo2:upbo2]...="
loboSpecByteCount = rank * 3 * asciiSize + asciiSize; // Amount of '[', ']', ':', and '='
upbos = new Int32[rank];
for ( var i = 0; i < rank; ++i )
{
// Remember that (Pg)SQL array indexing starts from 1 by default
upbos[i] = array.GetUpperBound( i ) + 1;
++lobos[i];
// Increment spec count
loboSpecByteCount += helperEncoding.GetTextualIntegerRepresentationSize( lobos[i] ) + helperEncoding.GetTextualIntegerRepresentationSize( upbos[i] );
}
sizeForArrayInfra += loboSpecByteCount;
}
var nullSize = NULL_CHAR_COUNT * asciiSize;
retVal = new BackendSizeInfo(
sizeForArrayInfra + elementSizes.Aggregate( 0, ( cur, item ) => cur + ( item.ByteCount >= 0 ? item.ByteCount : nullSize ) ), // All the space taken by actual values
(elementSizes, (lobos, upbos, loboSpecByteCount))
);
}
return retVal;
}
public async ValueTask<Object> ReadBackendValueAsync(
DataFormat dataFormat,
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamReaderWithResizableBufferAndLimitedSize stream
)
{
Array retVal;
switch ( dataFormat )
{
case DataFormat.Text:
if ( stream.TotalByteCount > 2 * helper.Encoding.BytesPerASCIICharacter )
{
retVal = await this.ReadArrayText( boundData, helper, stream );
}
else
{
// Empty array
retVal = this._emptyArray;
}
break;
case DataFormat.Binary:
retVal = await this.ReadArrayBinary( boundData, helper, stream );
break;
default:
throw new NotSupportedException( $"Data format {dataFormat} is not recognized." );
}
return retVal;
}
public async Task WriteBackendValueAsync(
DataFormat dataFormat,
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamWriterWithResizableBufferAndLimitedSize stream,
Object value,
BackendSizeInfo sizeInfo,
Boolean isArrayElement
)
{
switch ( dataFormat )
{
case DataFormat.Text:
await this.WriteArrayText( boundData, helper, stream, (Array) value, sizeInfo );
break;
case DataFormat.Binary:
await this.WriteArrayBinary( boundData, helper, stream, (Array) value, sizeInfo );
break;
default:
throw new NotSupportedException( $"Data format {dataFormat} is not recognized." );
}
}
private async Task<Array> ReadArrayText(
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamReaderWithResizableBufferAndLimitedSize stream
)
{
(var rank, var lobos, var lengths, var retVal) = await this.ReadArrayTextHeader( boundData, helper, stream );
// Use exponentially expanding array instead of list. That way we can use Array.Copy right away when creating rank-1 array.
var useTempArray = retVal == null;
var totalCount = 0;
// We only need temporary array if we are creating array without dimension specification prefix
Int32[] retValIndices;
ResizableArray<Object> tempArray;
Int32 arrayDelimiterByteCount;
if ( useTempArray )
{
tempArray = new ResizableArray<Object>( initialSize: 2, exponentialResize: true );
lengths = new Int32[rank];
retValIndices = null;
arrayDelimiterByteCount = -1;
}
else
{
retValIndices = new Int32[rank];
Array.Copy( lobos, retValIndices, rank );
tempArray = null;
arrayDelimiterByteCount = helper.Encoding.Encoding.GetByteCount( boundData.ArrayDelimiter );
}
// We start with innermost array
var innermostArrayIndex = rank - 1;
var lowestEncounteredArrayEnd = innermostArrayIndex;
var asciiSize = helper.Encoding.BytesPerASCIICharacter;
Boolean hasMore = true;
while ( hasMore )
{
(var value, var ending) = await this.ReadArrayElementText( boundData, helper, stream );
if ( useTempArray )
{
tempArray.CurrentMaxCapacity = totalCount + 1;
tempArray.Array[totalCount++] = value;
if ( lowestEncounteredArrayEnd == innermostArrayIndex )
{
++lengths[innermostArrayIndex];
}
// If array end encountered, we must find start of next element, if possible
if ( ending == ElementEndingWay.ArrayEnd )
{
(lowestEncounteredArrayEnd, lengths, hasMore) = await this.ReadArrayTextDimensionEnd(
boundData,
helper,
stream,
lowestEncounteredArrayEnd,
lengths,
rank
);
}
}
else
{
retVal.SetValue( value, retValIndices );
var dimsEnded = MoveNextMultiDimensionalIndex( lengths, retValIndices, lobos );
// At this point, the ReadArrayElementText method has already read either complete array delimiter, or one array end character
// So we only need to skip thru bytes if we have read array end character (dimsEnded > 0)
if ( dimsEnded > 0 )
{
hasMore = dimsEnded < rank;
if ( hasMore )
{
// Skip thru array end, array delimiter, and array start characters
await stream.ReadMoreOrThrow( ( dimsEnded * 2 - 1 ) * asciiSize + arrayDelimiterByteCount );
}
}
}
stream.EraseReadBytesFromBuffer();
}
// Now, construct the actual array to return, if needed
// If array to be returned is null at this stage, this means that no lower bound specifications were given.
if ( retVal == null )
{
if ( rank == 1 )
{
// Create normal one-dimensional array (we always need to create it, since we must return X[] instead of Object[])
retVal = Array.CreateInstance( this._arrayElementType, totalCount );
// Populate it
Array.Copy( tempArray.Array, retVal, totalCount );
}
else
{
// Create multi-dimensional array
retVal = Array.CreateInstance( this._arrayElementType, lengths );
// Populate it
var curIndices = new Int32[lengths.Length];
var idx = 0;
var actualArray = tempArray.Array;
do
{
var elem = actualArray[idx++];
if ( elem != null )
{
retVal.SetValue( elem, curIndices );
}
MoveNextMultiDimensionalIndex( lengths, curIndices );
} while ( idx < totalCount );
}
}
return retVal;
}
private async ValueTask<(Int32 Rank, Int32[] Lobos, Int32[] Lengths, Array CreatedArray)> ReadArrayTextHeader(
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamReaderWithResizableBufferAndLimitedSize stream
)
{
stream.EraseReadBytesFromBuffer();
Char curChar;
// Read the optional "[lobo1:upbo1][lobo2:upbo2]...=" dimension specification into buffer.
var rank = 0;
var charReader = helper.CharacterReader;
do
{
curChar = await charReader.ReadNextAsync( stream );
if ( curChar == DIM_SEPARATOR )
{
++rank;
}
} while ( curChar != ARRAY_START );
Int32[] lobos = null;
Int32[] lengths = null;
Array retVal = null;
if ( rank > 0 )
{
// We encountered the explicit dimension specification. Backend should issue this only when there are 'special' lower bounds.
// As a bonus, we will know the array dimensions before array elements start, and we can create the array to be returned right away,
// instead of reading elements into temporary array
lobos = new Int32[rank];
lengths = new Int32[rank];
var encoding = helper.Encoding;
var asciiSize = encoding.BytesPerASCIICharacter;
var byteArray = stream.Buffer;
var idx = asciiSize; // Skip first '['
for ( var i = 0; i < rank; ++i )
{
// In (Pg)SQL, lower bounds normally start at 1. So 1 translates to 0 in CLR, 0 to -1, etc.
lobos[i] = encoding.ParseInt32Textual( byteArray, ref idx ) - 1;
idx += asciiSize; // Skip ':'
lengths[i] = encoding.ParseInt32Textual( byteArray, ref idx ) - lobos[i];
idx += asciiSize * 2; // Skip ']' and next '[' or '='
}
retVal = Array.CreateInstance( this._arrayElementType, lengths, lobos );
}
// Read amount of starting '{' characters. That will be the array rank (unless we already learned about the rank in the dimension specification header).
rank = 0;
Int32 prevIdx;
do
{
++rank;
prevIdx = stream.ReadBytesCount;
curChar = await charReader.ReadNextAsync( stream );
} while ( curChar == ARRAY_START );
if ( retVal != null && rank != retVal.Rank )
{
throw new PgSQLException( "Backend array lower-bound specification had different rank than actual array specifciation." );
}
// Back one character (the one we read, that wasn't array start character)
stream.UnreadBytes( stream.ReadBytesCount - prevIdx );
// Remember to get rid of array start characters currently in buffer (ReadArrayElementText expects clean buffer start)
stream.EraseReadBytesFromBuffer();
return (rank, lobos, lengths, retVal);
}
private async ValueTask<(Int32 LowestEncounteredArrayEnd, Int32[] Lengths, Boolean HasMore)> ReadArrayTextDimensionEnd(
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamReaderWithResizableBufferAndLimitedSize stream,
Int32 lowestEncounteredArrayEnd,
Int32[] lengths,
Int32 rank
)
{
stream.EraseReadBytesFromBuffer();
var wasArrayEnd = true;
var innermostArrayIndex = rank - 1;
var curArrayIndex = innermostArrayIndex;
Char curChar;
var charReader = helper.CharacterReader;
while ( wasArrayEnd && --curArrayIndex >= 0 )
{
// End current array block
lowestEncounteredArrayEnd = Math.Min( lowestEncounteredArrayEnd, curArrayIndex );
if ( curArrayIndex <= lowestEncounteredArrayEnd )
{
lengths[curArrayIndex]++;
}
// Read next character
curChar = await charReader.ReadNextAsync( stream );
wasArrayEnd = curChar == ARRAY_END;
}
var hasMore = curArrayIndex >= 0;
if ( hasMore )
{
// More arrays follow
// Read until we are at innermost array level again
while ( curArrayIndex < innermostArrayIndex )
{
curChar = await charReader.ReadNextAsync( stream );
if ( curChar == ARRAY_START )
{
++curArrayIndex;
}
}
}
stream.EraseReadBytesFromBuffer();
return (lowestEncounteredArrayEnd, lengths, hasMore);
}
private static Int32 MoveNextMultiDimensionalIndex(
Int32[] lengths,
Int32[] indices,
Int32[] loBos = null
)
{
var i = indices.Length - 1;
if ( loBos == null )
{
for ( ; i >= 0 && ++indices[i] == lengths[i]; --i )
{
indices[i] = 0;
}
}
else
{
for ( ; i >= 0 && ++indices[i] == lengths[i] + loBos[i]; --i )
{
indices[i] = loBos[i];
}
}
return lengths.Length - i - 1;
}
// We never encounter empty arrays when calling this, since inner empty arrays are not possible, and whole empty array string is handled separately
private async ValueTask<(Object Value, ElementEndingWay Ending)> ReadArrayElementText(
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamReaderWithResizableBufferAndLimitedSize stream
)
{
// Scan for element end
stream.EraseReadBytesFromBuffer();
var insideQuote = false;
var delimLength = boundData.ArrayDelimiter.Length;
var delim0 = boundData.ArrayDelimiter[0];
var delim1 = delimLength > 1 ? boundData.ArrayDelimiter[1] : '\0';
Int32 prevIdx;
Char curChar;
Char curChar2 = '\0';
Boolean wasArrayDelimiter = false;
var prevWasEscape = false;
var asciiSize = helper.Encoding.BytesPerASCIICharacter;
var charReader = helper.CharacterReader;
Boolean continueReading;
do
{
prevIdx = stream.ReadBytesCount;
var charNullable = await charReader.TryReadNextAsync( stream );
continueReading = charNullable.HasValue;
curChar = charNullable.GetValueOrDefault();
if ( continueReading )
{
if ( prevWasEscape )
{
prevWasEscape = false;
}
else
{
if ( delimLength > 1 && Char.IsHighSurrogate( curChar ) )
{
curChar2 = await charReader.TryReadNextAsync( stream ) ?? '\0';
}
switch ( curChar )
{
case ESCAPE:
case QUOTE:
if ( curChar == QUOTE )
{
insideQuote = !insideQuote;
}
else
{
prevWasEscape = true;
}
// "Shift" the array
stream.EraseReadBufferSegment( prevIdx, asciiSize );
break;
default:
wasArrayDelimiter = !insideQuote
&& (
( delimLength == 1 && curChar == delim0 )
|| ( delimLength > 1 && curChar == delim0 && curChar2 == delim1 )
);
continueReading = !wasArrayDelimiter && curChar != ARRAY_END;
break;
}
}
}
} while ( continueReading );
var elementAndSeparatorSize = stream.ReadBytesCount;
var ending = wasArrayDelimiter ? ElementEndingWay.ArrayDelimiter : ( curChar == ARRAY_END ? ElementEndingWay.ArrayEnd : ElementEndingWay.Abnormal );
Object arrayElement;
if ( ending == ElementEndingWay.Abnormal || IsNullArrayElement( helper.Encoding, stream.Buffer, prevIdx ) )
{
arrayElement = null;
}
else
{
stream.UnreadBytes();
var elementTypeInfo = this._elementTypeInfo.Value;
using ( var elementStream = stream.CreateWithLimitedSizeAndSharedBuffer( prevIdx ) )
{
arrayElement = await elementTypeInfo.Functionality.ReadBackendValueAsync(
DataFormat.Text,
elementTypeInfo.DatabaseData,
helper,
elementStream
);
}
// Re-read separator
await stream.ReadMoreOrThrow( elementAndSeparatorSize - prevIdx );
}
// Erase all previous read data (element + separator)
stream.EraseReadBytesFromBuffer();
return (
arrayElement,
ending
);
}
private const Int32 CHUNK_SIZE = 1024;
private enum ElementEndingWay
{
ArrayDelimiter,
ArrayEnd,
Abnormal
}
private static Boolean IsNullArrayElement(
IEncodingInfo encoding,
Byte[] array,
Int32 elementByteCount
)
{
Int32 idx = 0;
Byte lastASCIIByte;
return elementByteCount == encoding.BytesPerASCIICharacter * NULL_CHAR_COUNT
// Poor man's case insensitive matching
&& ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'N' || lastASCIIByte == 'n' )
&& ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'U' || lastASCIIByte == 'u' )
&& ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'L' || lastASCIIByte == 'l' )
&& ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'L' || lastASCIIByte == 'l' )
;
}
private async Task<Array> ReadArrayBinary(
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamReaderWithResizableBufferAndLimitedSize stream
)
{
await stream.ReadOrThrow( sizeof( Int32 ) );
var idx = 0;
var rank = stream.Buffer.ReadPgInt32( ref idx );
Array retVal;
if ( rank < 0 )
{
throw new PgSQLException( "Array rank must be zero or more." );
}
else if ( rank == 0 )
{
// Empty array
retVal = this._emptyArray;
}
else
{
// Read the rest of the header (null-map (Int32), element type id (Int32), and rank infos (2 integers per rank)
await stream.ReadMoreOrThrow( ( 2 + 2 * rank ) * sizeof( Int32 ) );
// Skip null-map and element type id.
idx = sizeof( Int32 ) * 3;
// Read lengths and lower bounds for dimensions
var lengths = new Int32[rank];
Int32[] loBos = null;
for ( var i = 0; i < rank; ++i )
{
var curNumber = stream.Buffer.ReadPgInt32( ref idx );
lengths[i] = curNumber;
curNumber = stream.Buffer.ReadPgInt32( ref idx );
if ( curNumber != 1 ) // In SQL, default min lo bo is 1. In C#, the default is 0.
{
if ( loBos == null )
{
loBos = new Int32[rank];
}
loBos[i] = curNumber - 1;
}
}
// Create & populate array instance
stream.EraseReadBytesFromBuffer();
var elemInfo = this._elementTypeInfo.Value;
if ( rank == 1 && loBos == null )
{
// Can just use normal array
var len = lengths[0];
retVal = Array.CreateInstance( this._arrayElementType, len );
for ( var i = 0; i < len; ++i )
{
var curInfo = await elemInfo.Functionality.ReadBackendValueCheckNull( DataFormat.Binary, elemInfo.DatabaseData, helper, stream );
retVal.SetValue( curInfo.Value, i );
}
}
else
{
// Have to create multi-dimensional array
retVal = loBos == null ? Array.CreateInstance( this._arrayElementType, lengths ) : Array.CreateInstance( this._arrayElementType, lengths, loBos );
var indices = new Int32[rank];
if ( loBos != null )
{
Array.Copy( loBos, indices, rank );
}
do
{
var curInfo = await elemInfo.Functionality.ReadBackendValueCheckNull( DataFormat.Binary, elemInfo.DatabaseData, helper, stream );
retVal.SetValue( curInfo.Value, indices );
} while ( MoveNextMultiDimensionalIndex( lengths, indices, loBos ) < rank );
}
}
return retVal;
}
private async Task WriteArrayText(
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamWriterWithResizableBufferAndLimitedSize stream,
Array value,
BackendSizeInfo sizeInfo
)
{
var encoding = helper.Encoding;
var length = value.Length;
if ( length <= 0 )
{
// Just write '{}'
stream.AppendToBytes( 2 * encoding.BytesPerASCIICharacter, ( bArray, idx, count ) => encoding.WriteASCIIByte( bArray, ref idx, ARRAY_START ).WriteASCIIByte( bArray, ref idx, ARRAY_END ) );
}
else
{
var additionalSizeInfo = (TextSizeAdditionalInfo) sizeInfo.CustomInformation;
var loboInfo = additionalSizeInfo.Item2;
var rank = value.Rank;
var lobos = loboInfo.Item1;
var asciiSize = encoding.BytesPerASCIICharacter;
if ( lobos != null )
{
// We must write the lower bound specification: '[lobo1:upbo1][lobo2:upbo2]...='
var upbos = loboInfo.Item2;
stream.AppendToBytes( loboInfo.Item3, ( array, idx, count ) =>
{
for ( var i = 0; i < rank; ++i )
{
// Both lobos and upbos have correct values from GetBackendBinarySize method
encoding
.WriteASCIIByte( array, ref idx, ARRAY_LOBO_START )
.WriteIntegerTextual( array, ref idx, lobos[i] )
.WriteASCIIByte( array, ref idx, (Byte) DIM_SEPARATOR )
.WriteIntegerTextual( array, ref idx, upbos[i] )
.WriteASCIIByte( array, ref idx, ARRAY_LOBO_END );
}
// Write final '='
encoding.WriteASCIIByte( array, ref idx, ARRAY_LOBO_SPEC_SEPARATOR );
} );
}
Int32[] indices = null;
Int32[] lengths = null;
if ( rank > 1 )
{
// We have to use indices and lengths in order to properly emit '}' and '{' in between array elements
// N.B.! One-ranked array with lobo-specification still doesn't need to use indices and lengths, as it won't have '}' and '{' in between any elements.
// We also don't need to initialize indices with lower bounds, as we only need to know when dimensions end.
indices = new Int32[rank];
lengths = value.GetLengths();
}
// Write '{'s
var rankASCIISize = rank * asciiSize;
stream.AppendToBytes( rankASCIISize, ( bArray, idx, count ) =>
{
for ( var i = 0; i < rank; ++i )
{
encoding.WriteASCIIByte( bArray, ref idx, ARRAY_START );
}
} );
// Send prefix, then send elements
await stream.FlushAsync();
var elementSizeInfos = additionalSizeInfo.Item1;
var eIdx = 0;
var elementTypeInfo = this._elementTypeInfo.Value;
var delimByteCount = encoding.Encoding.GetByteCount( boundData.ArrayDelimiter );
var curIdx = 0;
foreach ( var element in value )
{
var elementSizeInfo = elementSizeInfos[eIdx++];
if ( element == null )
{
// Write 'NULL'
stream.AppendToBytes( asciiSize * NULL_CHAR_COUNT, ( bArray, idx, count ) => encoding.Encoding.GetBytes( NULL_STRING, 0, NULL_CHAR_COUNT, bArray, idx ) );
await stream.FlushAsync();
}
else
{
using ( var elementStream = await stream.CreateWithLimitedSizeAndSharedBuffer( elementSizeInfo.ByteCount ) )
{
await elementTypeInfo.Functionality.WriteBackendValueAsync(
DataFormat.Text,
elementTypeInfo.DatabaseData,
helper,
elementStream,
element,
elementSizeInfo,
true
);
await elementStream.FlushAsync();
}
}
if ( curIdx++ < length - 1 )
{
if ( indices == null )
{
stream.AppendToBytes( delimByteCount, ( bArray, idx, count ) => encoding.Encoding.GetBytes( boundData.ArrayDelimiter, 0, boundData.ArrayDelimiter.Length, bArray, idx ) );
}
else
{
// We might need to write '}'s
var amountOfDimensionsEnded = MoveNextMultiDimensionalIndex( lengths, indices );
// Have to write '}'s, followed by array separator, followed by equally many '{'s.
stream.AppendToBytes( amountOfDimensionsEnded * 2 * asciiSize + delimByteCount, ( bArray, idx, count ) =>
{
for ( var i = 0; i < amountOfDimensionsEnded; ++i )
{
encoding.WriteASCIIByte( bArray, ref idx, ARRAY_END );
}
idx += encoding.Encoding.GetBytes( boundData.ArrayDelimiter, 0, boundData.ArrayDelimiter.Length, bArray, idx );
for ( var i = 0; i < amountOfDimensionsEnded; ++i )
{
encoding.WriteASCIIByte( bArray, ref idx, ARRAY_START );
}
} );
}
await stream.FlushAsync();
}
}
// Write final '}'s
stream.AppendToBytes( rankASCIISize, ( bArray, idx, count ) =>
{
for ( var i = 0; i < rank; ++i )
{
encoding.WriteASCIIByte( bArray, ref idx, ARRAY_END );
}
} );
}
await stream.FlushAsync();
}
private async Task WriteArrayBinary(
PgSQLTypeDatabaseData boundData,
BackendABIHelper helper,
StreamWriterWithResizableBufferAndLimitedSize stream,
Array value,
BackendSizeInfo sizeInfo
)
{
// Write header (3 integers + 2 integers per rank)
var elemInfo = this._elementTypeInfo.Value;
var arrayLength = value.Length;
var rank = arrayLength <= 0 ? 0 : value.Rank;
stream.AppendToBytes( ( 3 + 2 * rank ) * sizeof( Int32 ), ( bArray, idx, count ) =>
{
bArray
.WritePgInt32( ref idx, rank )
.WritePgInt32( ref idx, 1 ) // null map, always zero in our case
.WritePgInt32( ref idx, elemInfo.DatabaseData.TypeID );
for ( var i = 0; i < rank; ++i )
{
bArray
.WritePgInt32( ref idx, value.GetLength( i ) )
.WritePgInt32( ref idx, value.GetLowerBound( i ) + 1 ); // SQL lower bounds for arrays are 1 by default
}
} );
// Send header
await stream.FlushAsync();
if ( arrayLength > 0 )
{
// Send elements
var additionalSizeInfo = (BackendSizeInfo[]) sizeInfo.CustomInformation;
var j = 0;
foreach ( var element in value )
{
var elementSizeInfo = additionalSizeInfo[j++];
using ( var elementStream = await stream.CreateWithLimitedSizeAndSharedBuffer(
Math.Max( 0, elementSizeInfo.ByteCount ) + sizeof( Int32 )
) )
{
await elemInfo.Functionality.WriteBackendValueCheckNull(
DataFormat.Binary,
elemInfo.DatabaseData,
helper,
elementStream,
element,
elementSizeInfo,
true
);
}
}
}
}
}
}