Skip to content

Commit

Permalink
Improve SqliteDataRecord class Efficiency
Browse files Browse the repository at this point in the history
- Update methods GetBytes and GetChars to use internally GetStream.
  • Loading branch information
nmichels authored and bricelam committed Feb 20, 2020
1 parent 4c3bfa7 commit d72f8f9
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Data.Sqlite.Properties;
using SQLitePCL;
using static SQLitePCL.raw;
Expand Down Expand Up @@ -196,25 +197,31 @@ public static Type GetFieldType(string type)

public virtual long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
{
var blob = GetCachedBlob(ordinal);
var blob = GetStream(ordinal);

long bytesToRead = blob.Length - dataOffset;
if (buffer != null)
{
bytesToRead = Math.Min(bytesToRead, length);
Array.Copy(blob, dataOffset, buffer, bufferOffset, bytesToRead);
using (var binaryReader = new BinaryReader(blob))
{
Array.Copy(binaryReader.ReadBytes((int)blob.Length), dataOffset, buffer, bufferOffset, bytesToRead);
}
}

return bytesToRead;
}

public virtual long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
{
var text = GetString(ordinal);
var textStream = GetStream(ordinal);
long charsToRead = textStream.Length - dataOffset;

using (var streamReader = new StreamReader(textStream, Encoding.UTF8))
{
Array.Copy(streamReader.ReadToEnd().ToCharArray(), dataOffset, buffer, bufferOffset, Math.Min(charsToRead, length));
}

int charsToRead = text.Length - (int)dataOffset;
charsToRead = Math.Min(charsToRead, length);
text.CopyTo((int)dataOffset, buffer, bufferOffset, charsToRead);
return charsToRead;
}

Expand Down

0 comments on commit d72f8f9

Please sign in to comment.