Skip to content

Commit

Permalink
Records read by MemoryDataReader can always be cached because the cur…
Browse files Browse the repository at this point in the history
…sor is fully consumed during the read operation. (#1085)
  • Loading branch information
claudiamurialdo authored Nov 27, 2024
1 parent ad4ace6 commit 0bbd3a6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
21 changes: 12 additions & 9 deletions dotnet/src/dotnetframework/GxClasses/Data/GXDataCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2225,16 +2225,19 @@ public GxDataReader( IGxConnectionManager connManager, GxDataRecord dr, IGxConne

public void AddToCache(bool hasNext)
{
if (hasNext)
if (!(reader is MemoryDataReader))
{
object[] values = new object[reader.FieldCount];
m_dr.GetValues(reader, ref values);
block.Add(values);
}
else
{
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, pos, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
Close();
if (hasNext)
{
object[] values = new object[reader.FieldCount];
m_dr.GetValues(reader, ref values);
block.Add(values);
}
else
{
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, pos, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
Close();
}
}
}

Expand Down
67 changes: 56 additions & 11 deletions dotnet/src/dotnetframework/GxClasses/Data/GXDataMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ public MemoryDataReader(IDataReader reader, IGxConnection connection, GxParamete
{
this.key = SqlUtil.GetKeyStmtValues(parameters, stmt, isForFirst);
this.expiration = expiration;
AddToCache(false);
}
}

Expand Down Expand Up @@ -602,26 +603,21 @@ public bool Read()
else
this._currentIndex = 0;

if (cached)
{
AddToCache(this._records.Count > 0);
}

return this._records.Count > 0;
}
public void AddToCache(bool hasNext)
{
if (hasNext)
foreach (MemoryDataRecord record in this._records)
{
object[] values = new object[FieldCount];
MemoryDataRecord record = this._records[0];
record.GetValues(values);
foreach(object obj in values)
{
readBytes += MemoryDataHelper.SizeInBytes(obj);
}
block.Add(values);
}
else
{
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, block.Count, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
}
SqlUtil.AddBlockToCache(key, new CacheItem(block, false, block.Count, readBytes), con, expiration != null ? (int)expiration.ItemSlidingExpiration.TotalMinutes : 0);
}
public int RecordsAffected
{
Expand Down Expand Up @@ -853,6 +849,55 @@ public object this[int i]

class MemoryDataHelper
{
internal static int SizeInBytes(object value)
{

if (value == null || value == DBNull.Value)
return 0;

Type type = value.GetType();

if (type == typeof(bool))
{
return 1;
}
if (type == typeof(byte[]))
{
return ((byte[])value).Length;
}
if (type == typeof(DateTime))
{
return 8;
}
if (type == typeof(Decimal))
{
return 12;
}
if (type == typeof(Double))
{
return 8;
}
if (type == typeof(Guid))
{
return 16;
}
if (type == typeof(Int16))
{
return 2;
}
if (type == typeof(Int32))
{
return 4;
}
if (type == typeof(Int64))
{
return 8;
}
if (type == typeof(string))
return (10 + (2 * ((string)value).Length));

return 0;
}
#region Copy objects

public static object GetCopyFrom(object value)
Expand Down

0 comments on commit 0bbd3a6

Please sign in to comment.