@@ -1870,7 +1870,7 @@ internal TdsOperationStatus TryReadStringWithEncoding(int length, System.Text.En
1870
1870
}
1871
1871
byte [ ] buf = null ;
1872
1872
int offset = 0 ;
1873
- ( bool isAvailable , bool isStarting , bool isContinuing ) = GetSnapshotStatuses ( ) ;
1873
+ ( bool canContinue , bool isStarting , bool isContinuing ) = GetSnapshotStatuses ( ) ;
1874
1874
1875
1875
if ( isPlp )
1876
1876
{
@@ -1888,7 +1888,7 @@ internal TdsOperationStatus TryReadStringWithEncoding(int length, System.Text.En
1888
1888
if ( ( ( _inBytesUsed + length ) > _inBytesRead ) || ( _inBytesPacket < length ) )
1889
1889
{
1890
1890
int startOffset = 0 ;
1891
- if ( isAvailable )
1891
+ if ( canContinue )
1892
1892
{
1893
1893
if ( isContinuing || isStarting )
1894
1894
{
@@ -1906,7 +1906,7 @@ internal TdsOperationStatus TryReadStringWithEncoding(int length, System.Text.En
1906
1906
buf = new byte [ length ] ;
1907
1907
}
1908
1908
1909
- TdsOperationStatus result = TryReadByteArray ( buf , length , out _ , startOffset , isAvailable ) ;
1909
+ TdsOperationStatus result = TryReadByteArray ( buf , length , out _ , startOffset , canContinue ) ;
1910
1910
1911
1911
if ( result != TdsOperationStatus . Done )
1912
1912
{
@@ -2036,22 +2036,23 @@ internal int ReadPlpBytesChunk(byte[] buff, int offset, int len)
2036
2036
2037
2037
internal TdsOperationStatus TryReadPlpBytes ( ref byte [ ] buff , int offset , int len , out int totalBytesRead )
2038
2038
{
2039
+ bool canContinue = false ;
2039
2040
bool isStarting = false ;
2040
2041
bool isContinuing = false ;
2041
2042
bool compatibilityMode = LocalAppContextSwitches . UseCompatibilityAsyncBehaviour ;
2042
2043
if ( ! compatibilityMode )
2043
2044
{
2044
- ( _ , isStarting , isContinuing ) = GetSnapshotStatuses ( ) ;
2045
+ ( canContinue , isStarting , isContinuing ) = GetSnapshotStatuses ( ) ;
2045
2046
}
2046
- return TryReadPlpBytes ( ref buff , offset , len , out totalBytesRead , isStarting || isContinuing , compatibilityMode ) ;
2047
+ return TryReadPlpBytes ( ref buff , offset , len , out totalBytesRead , canContinue , canContinue , compatibilityMode ) ;
2047
2048
}
2048
2049
// Reads the requested number of bytes from a plp data stream, or the entire data if
2049
2050
// requested length is -1 or larger than the actual length of data. First call to this method
2050
2051
// should be preceeded by a call to ReadPlpLength or ReadDataLength.
2051
2052
// Returns the actual bytes read.
2052
2053
// NOTE: This method must be retriable WITHOUT replaying a snapshot
2053
2054
// Every time you call this method increment the offset and decrease len by the value of totalBytesRead
2054
- internal TdsOperationStatus TryReadPlpBytes ( ref byte [ ] buff , int offset , int len , out int totalBytesRead , bool writeDataSizeToSnapshot , bool compatibilityMode )
2055
+ internal TdsOperationStatus TryReadPlpBytes ( ref byte [ ] buff , int offset , int len , out int totalBytesRead , bool canContinue , bool writeDataSizeToSnapshot , bool compatibilityMode )
2055
2056
{
2056
2057
totalBytesRead = 0 ;
2057
2058
@@ -2076,9 +2077,16 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
2076
2077
// If total length is known up front, allocate the whole buffer in one shot instead of realloc'ing and copying over each time
2077
2078
if ( buff == null && _longlen != TdsEnums . SQL_PLP_UNKNOWNLEN )
2078
2079
{
2079
- if ( writeDataSizeToSnapshot )
2080
+ if ( compatibilityMode && _snapshot != null && _snapshotStatus != SnapshotStatus . NotActive )
2081
+ {
2082
+ // legacy replay path perf optimization
2083
+ // if there is a snapshot which contains a stored plp buffer take it
2084
+ // and try to use it if it is the right length
2085
+ buff = TryTakeSnapshotStorage ( ) as byte [ ] ;
2086
+ }
2087
+ else if ( writeDataSizeToSnapshot && canContinue && _snapshot != null )
2080
2088
{
2081
- // if there is a snapshot and it contains a stored plp buffer take it
2089
+ // if there is a snapshot which it contains a stored plp buffer take it
2082
2090
// and try to use it if it is the right length
2083
2091
buff = TryTakeSnapshotStorage ( ) as byte [ ] ;
2084
2092
if ( buff != null )
@@ -2087,13 +2095,7 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
2087
2095
totalBytesRead = offset ;
2088
2096
}
2089
2097
}
2090
- else if ( compatibilityMode && _snapshot != null && _snapshotStatus != SnapshotStatus . NotActive )
2091
- {
2092
- // legacy replay path perf optimization
2093
- // if there is a snapshot and it contains a stored plp buffer take it
2094
- // and try to use it if it is the right length
2095
- buff = TryTakeSnapshotStorage ( ) as byte [ ] ;
2096
- }
2098
+
2097
2099
2098
2100
if ( ( ulong ) ( buff ? . Length ?? 0 ) != _longlen )
2099
2101
{
@@ -2145,44 +2147,50 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
2145
2147
_longlenleft -= ( ulong ) bytesRead ;
2146
2148
if ( result != TdsOperationStatus . Done )
2147
2149
{
2148
- if ( writeDataSizeToSnapshot )
2150
+ if ( compatibilityMode && _snapshot != null )
2149
2151
{
2152
+ // legacy replay path perf optimization
2150
2153
// a partial read has happened so store the target buffer in the snapshot
2151
2154
// so it can be re-used when another packet arrives and we read again
2152
2155
SetSnapshotStorage ( buff ) ;
2153
- SetSnapshotDataSize ( bytesRead ) ;
2154
-
2155
2156
}
2156
- else if ( compatibilityMode && _snapshot != null )
2157
+ else if ( canContinue )
2157
2158
{
2158
- // legacy replay path perf optimization
2159
2159
// a partial read has happened so store the target buffer in the snapshot
2160
2160
// so it can be re-used when another packet arrives and we read again
2161
2161
SetSnapshotStorage ( buff ) ;
2162
+ if ( writeDataSizeToSnapshot )
2163
+ {
2164
+ SetSnapshotDataSize ( bytesRead ) ;
2165
+ }
2162
2166
}
2163
2167
return result ;
2164
2168
}
2169
+ if ( writeDataSizeToSnapshot && canContinue )
2170
+ {
2171
+ SetSnapshotDataSize ( bytesRead ) ;
2172
+ }
2165
2173
2166
2174
if ( _longlenleft == 0 )
2167
2175
{
2168
2176
// Read the next chunk or cleanup state if hit the end
2169
2177
result = TryReadPlpLength ( false , out _ ) ;
2170
2178
if ( result != TdsOperationStatus . Done )
2171
2179
{
2172
- if ( writeDataSizeToSnapshot )
2173
- {
2174
- if ( result == TdsOperationStatus . NeedMoreData )
2175
- {
2176
- SetSnapshotStorage ( buff ) ;
2177
- SetSnapshotDataSize ( bytesRead ) ;
2178
- }
2179
- }
2180
- else if ( compatibilityMode && _snapshot != null )
2180
+ if ( compatibilityMode && _snapshot != null )
2181
2181
{
2182
2182
// a partial read has happened so store the target buffer in the snapshot
2183
2183
// so it can be re-used when another packet arrives and we read again
2184
2184
SetSnapshotStorage ( buff ) ;
2185
2185
}
2186
+ else if ( canContinue && result == TdsOperationStatus . NeedMoreData )
2187
+ {
2188
+ SetSnapshotStorage ( buff ) ;
2189
+ if ( writeDataSizeToSnapshot )
2190
+ {
2191
+ SetSnapshotDataSize ( bytesRead ) ;
2192
+ }
2193
+ }
2186
2194
return result ;
2187
2195
}
2188
2196
}
@@ -3454,17 +3462,17 @@ internal bool IsSnapshotContinuing()
3454
3462
_snapshotStatus == TdsParserStateObject . SnapshotStatus . ContinueRunning ;
3455
3463
}
3456
3464
3457
- internal ( bool IsAvailable , bool IsStarting , bool IsContinuing ) GetSnapshotStatuses ( )
3465
+ internal ( bool CanContinue , bool IsStarting , bool IsContinuing ) GetSnapshotStatuses ( )
3458
3466
{
3459
- bool isAvailable = _snapshot != null && _snapshot . ContinueEnabled ;
3467
+ bool canContinue = _snapshot != null && _snapshot . ContinueEnabled && _snapshotStatus != SnapshotStatus . NotActive ;
3460
3468
bool isStarting = false ;
3461
3469
bool isContinuing = false ;
3462
- if ( isAvailable )
3470
+ if ( canContinue )
3463
3471
{
3464
3472
isStarting = _snapshotStatus == SnapshotStatus . ReplayStarting ;
3465
3473
isContinuing = _snapshotStatus == SnapshotStatus . ContinueRunning ;
3466
3474
}
3467
- return ( isAvailable , isStarting , isContinuing ) ;
3475
+ return ( canContinue , isStarting , isContinuing ) ;
3468
3476
}
3469
3477
3470
3478
internal int GetSnapshotStorageLength < T > ( )
0 commit comments