Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reusable chunk to reduce memory consumption
- Loading branch information
Showing
15 changed files
with
530 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
namespace Snowflake.Data.Tests | ||
{ | ||
using NUnit.Framework; | ||
using System.IO; | ||
using System.Text; | ||
using Snowflake.Data.Core; | ||
|
||
[TestFixture] | ||
class SFReusableChunkTest | ||
{ | ||
[Test] | ||
public void TestSimpleChunk() | ||
{ | ||
string data = "[ [\"1\", \"1.234\", \"abcde\"], [\"2\", \"5.678\", \"fghi\"] ]"; | ||
byte[] bytes = Encoding.UTF8.GetBytes(data); | ||
Stream stream = new MemoryStream(bytes); | ||
IChunkParser parser = new ReusableChunkParser(stream); | ||
|
||
ExecResponseChunk chunkInfo = new ExecResponseChunk() | ||
{ | ||
url = "fake", | ||
uncompressedSize = 100, | ||
rowCount = 2 | ||
}; | ||
|
||
SFReusableChunk chunk = new SFReusableChunk(3); | ||
chunk.Reset(chunkInfo, 0); | ||
|
||
parser.ParseChunk(chunk); | ||
|
||
Assert.AreEqual("1", chunk.ExtractCell(0, 0)); | ||
Assert.AreEqual("1.234", chunk.ExtractCell(0, 1)); | ||
Assert.AreEqual("abcde", chunk.ExtractCell(0, 2)); | ||
Assert.AreEqual("2", chunk.ExtractCell(1, 0)); | ||
Assert.AreEqual("5.678", chunk.ExtractCell(1, 1)); | ||
Assert.AreEqual("fghi", chunk.ExtractCell(1, 2)); | ||
} | ||
|
||
[Test] | ||
public void TestChunkWithNull() | ||
{ | ||
string data = "[ [null, \"1.234\", null], [\"2\", null, \"fghi\"] ]"; | ||
byte[] bytes = Encoding.UTF8.GetBytes(data); | ||
Stream stream = new MemoryStream(bytes); | ||
IChunkParser parser = new ReusableChunkParser(stream); | ||
|
||
ExecResponseChunk chunkInfo = new ExecResponseChunk() | ||
{ | ||
url = "fake", | ||
uncompressedSize = 100, | ||
rowCount = 2 | ||
}; | ||
|
||
SFReusableChunk chunk = new SFReusableChunk(3); | ||
chunk.Reset(chunkInfo, 0); | ||
|
||
parser.ParseChunk(chunk); | ||
|
||
Assert.AreEqual(null, chunk.ExtractCell(0, 0)); | ||
Assert.AreEqual("1.234", chunk.ExtractCell(0, 1)); | ||
Assert.AreEqual(null, chunk.ExtractCell(0, 2)); | ||
Assert.AreEqual("2", chunk.ExtractCell(1, 0)); | ||
Assert.AreEqual(null, chunk.ExtractCell(1, 1)); | ||
Assert.AreEqual("fghi", chunk.ExtractCell(1, 2)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
namespace Snowflake.Data.Core | ||
{ | ||
using Snowflake.Data.Client; | ||
|
||
class ReusableChunkParser : IChunkParser | ||
{ | ||
private readonly Stream stream; | ||
|
||
internal ReusableChunkParser(Stream stream) | ||
{ | ||
this.stream = stream; | ||
} | ||
|
||
public void ParseChunk(IResultChunk chunk) | ||
{ | ||
SFReusableChunk rc = (SFReusableChunk)chunk; | ||
// parse results row by row | ||
using (StreamReader sr = new StreamReader(stream)) | ||
using (JsonTextReader jr = new JsonTextReader(sr)) | ||
{ | ||
while (jr.Read()) | ||
{ | ||
switch (jr.TokenType) | ||
{ | ||
case JsonToken.StartArray: | ||
case JsonToken.None: | ||
case JsonToken.EndArray: | ||
break; | ||
|
||
case JsonToken.Null: | ||
rc.AddCell(null); | ||
break; | ||
|
||
case JsonToken.String: | ||
rc.AddCell((string)jr.Value); | ||
break; | ||
|
||
default: | ||
throw new SnowflakeDbException(SFError.INTERNAL_ERROR, $"Unexpected token type: {jr.TokenType}"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.