Skip to content

Commit 3e8eb21

Browse files
committed
address asserts and refine TryReadPlpBytes
1 parent dad696f commit 3e8eb21

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4413,7 +4413,12 @@ private TdsOperationStatus TryResetBlobState()
44134413
#if DEBUG
44144414
else
44154415
{
4416-
Debug.Assert((_sharedState._columnDataBytesRemaining == 0 || _sharedState._columnDataBytesRemaining == -1) && _stateObj._longlen == 0, "Haven't read header yet, but column is partially read?");
4416+
Debug.Assert(
4417+
(_sharedState._columnDataBytesRemaining == 0 || _sharedState._columnDataBytesRemaining == -1)
4418+
&&
4419+
(_stateObj._longlen == 0 || _stateObj.IsSnapshotContinuing()),
4420+
"Haven't read header yet, but column is partially read?"
4421+
);
44174422
}
44184423
#endif
44194424

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,8 @@ internal int ReadPlpBytesChunk(byte[] buff, int offset, int len)
18601860

18611861
internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len, out int totalBytesRead)
18621862
{
1863-
return TryReadPlpBytes(ref buff, offset, len, out totalBytesRead, IsSnapshotAvailable());
1863+
( _, bool isStarting, bool isContinuing) = GetSnapshotStatuses();
1864+
return TryReadPlpBytes(ref buff, offset, len, out totalBytesRead, isStarting || isContinuing);
18641865
}
18651866
// Reads the requested number of bytes from a plp data stream, or the entire data if
18661867
// requested length is -1 or larger than the actual length of data. First call to this method
@@ -1904,6 +1905,11 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19041905
totalBytesRead = offset;
19051906
}
19061907
}
1908+
else if (_snapshot != null)
1909+
{
1910+
// legacy replay path perf optimization
1911+
buff = (byte[])TryTakeSnapshotStorage();
1912+
}
19071913

19081914
if ((ulong)(buff?.Length ?? 0) != _longlen)
19091915
{
@@ -1935,7 +1941,6 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19351941

19361942
while (bytesLeft > 0)
19371943
{
1938-
bool stored = false;
19391944
int bytesToRead = (int)Math.Min(_longlenleft, (ulong)bytesLeft);
19401945
if (buff.Length < (offset + bytesToRead))
19411946
{
@@ -1961,20 +1966,15 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19611966
// a partial read has happened so store the target buffer in the snapshot
19621967
// so it can be re-used when another packet arrives and we read again
19631968
SetSnapshotStorage(buff);
1969+
SetSnapshotDataSize(bytesRead);
1970+
19641971
}
1965-
return result;
1966-
}
1967-
else
1968-
{
1969-
if (writeDataSizeToSnapshot)
1972+
else if (_snapshot != null)
19701973
{
1974+
// legacy replay path perf optimization
19711975
SetSnapshotStorage(buff);
1972-
if (_snapshot.ContinueEnabled)
1973-
{
1974-
SetSnapshotDataSize(bytesRead);
1975-
stored = true;
1976-
}
19771976
}
1977+
return result;
19781978
}
19791979

19801980
if (_longlenleft == 0)
@@ -1983,9 +1983,18 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19831983
result = TryReadPlpLength(false, out _);
19841984
if (result != TdsOperationStatus.Done)
19851985
{
1986-
if (result == TdsOperationStatus.NeedMoreData && writeDataSizeToSnapshot && !stored)
1986+
if (result == TdsOperationStatus.NeedMoreData)
19871987
{
1988-
SetSnapshotDataSize(bytesRead);
1988+
if (writeDataSizeToSnapshot)
1989+
{
1990+
SetSnapshotStorage(buff);
1991+
SetSnapshotDataSize(bytesRead);
1992+
}
1993+
else if (_snapshot != null)
1994+
{
1995+
// legacy replay path perf optimization
1996+
SetSnapshotStorage(buff);
1997+
}
19891998
}
19901999
return result;
19912000
}
@@ -2767,7 +2776,7 @@ internal int GetSnapshotStorageLength<T>()
27672776

27682777
internal object TryTakeSnapshotStorage()
27692778
{
2770-
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "should not access snapshot accessor functions without first checking that the snapshot is present");
2779+
Debug.Assert(_snapshot != null, "should not access snapshot accessor functions without first checking that the snapshot is present");
27712780
object buffer = null;
27722781
if (_snapshot != null)
27732782
{
@@ -2779,7 +2788,7 @@ internal object TryTakeSnapshotStorage()
27792788

27802789
internal void SetSnapshotStorage(object buffer)
27812790
{
2782-
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "should not access snapshot accessor functions without first checking that the snapshot is available");
2791+
Debug.Assert(_snapshot != null, "should not access snapshot accessor functions without first checking that the snapshot is available");
27832792
Debug.Assert(_snapshot._storage == null, "should not overwrite snapshot stored buffer");
27842793
if (_snapshot != null)
27852794
{

0 commit comments

Comments
 (0)