Skip to content

Commit 1cf50c6

Browse files
Records read by MemoryDataReader can always be cached because the cursor is fully consumed during the read operation.
1 parent ad4ace6 commit 1cf50c6

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

dotnet/src/dotnetframework/GxClasses/Data/GXDataCommon.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,16 +2225,19 @@ public GxDataReader( IGxConnectionManager connManager, GxDataRecord dr, IGxConne
22252225

22262226
public void AddToCache(bool hasNext)
22272227
{
2228-
if (hasNext)
2228+
if (!(reader is MemoryDataReader))
22292229
{
2230-
object[] values = new object[reader.FieldCount];
2231-
m_dr.GetValues(reader, ref values);
2232-
block.Add(values);
2233-
}
2234-
else
2235-
{
2236-
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, pos, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
2237-
Close();
2230+
if (hasNext)
2231+
{
2232+
object[] values = new object[reader.FieldCount];
2233+
m_dr.GetValues(reader, ref values);
2234+
block.Add(values);
2235+
}
2236+
else
2237+
{
2238+
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, pos, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
2239+
Close();
2240+
}
22382241
}
22392242
}
22402243

dotnet/src/dotnetframework/GxClasses/Data/GXDataMemory.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ public MemoryDataReader(IDataReader reader, IGxConnection connection, GxParamete
517517
{
518518
this.key = SqlUtil.GetKeyStmtValues(parameters, stmt, isForFirst);
519519
this.expiration = expiration;
520+
AddToCache(false);
520521
}
521522
}
522523

@@ -602,26 +603,21 @@ public bool Read()
602603
else
603604
this._currentIndex = 0;
604605

605-
if (cached)
606-
{
607-
AddToCache(this._records.Count > 0);
608-
}
609-
610606
return this._records.Count > 0;
611607
}
612608
public void AddToCache(bool hasNext)
613609
{
614-
if (hasNext)
610+
foreach (MemoryDataRecord record in this._records)
615611
{
616612
object[] values = new object[FieldCount];
617-
MemoryDataRecord record = this._records[0];
618613
record.GetValues(values);
614+
foreach(object obj in values)
615+
{
616+
readBytes += MemoryDataHelper.SizeInBytes(obj);
617+
}
619618
block.Add(values);
620619
}
621-
else
622-
{
623-
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, block.Count, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
624-
}
620+
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, block.Count, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
625621
}
626622
public int RecordsAffected
627623
{
@@ -853,6 +849,55 @@ public object this[int i]
853849

854850
class MemoryDataHelper
855851
{
852+
internal static int SizeInBytes(object value)
853+
{
854+
855+
if (value == null || value == DBNull.Value)
856+
return 0;
857+
858+
Type type = value.GetType();
859+
860+
if (type == typeof(bool))
861+
{
862+
return 1;
863+
}
864+
if (type == typeof(byte[]))
865+
{
866+
return ((byte[])value).Length;
867+
}
868+
if (type == typeof(DateTime))
869+
{
870+
return 8;
871+
}
872+
if (type == typeof(Decimal))
873+
{
874+
return 12;
875+
}
876+
if (type == typeof(Double))
877+
{
878+
return 8;
879+
}
880+
if (type == typeof(Guid))
881+
{
882+
return 16;
883+
}
884+
if (type == typeof(Int16))
885+
{
886+
return 2;
887+
}
888+
if (type == typeof(Int32))
889+
{
890+
return 4;
891+
}
892+
if (type == typeof(Int64))
893+
{
894+
return 8;
895+
}
896+
if (type == typeof(string))
897+
return (10 + (2 * ((string)value).Length));
898+
899+
return 0;
900+
}
856901
#region Copy objects
857902

858903
public static object GetCopyFrom(object value)

0 commit comments

Comments
 (0)