Skip to content

Commit

Permalink
SepReaderExtensions: Add FileOptions.Asynchronous to FromFileAsync (#246
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nietras authored Jan 28, 2025
1 parent 4dc2039 commit 8c4a1ac
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
24 changes: 23 additions & 1 deletion src/Sep/SepReaderExtensions.IO.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ namespace nietras.SeparatedValues;

public static partial class SepReaderExtensions
{
#if SYNC
static readonly FileStreamOptions s_streamReaderOptionsSync = new()
{
Access = FileAccess.Read,
Mode = FileMode.Open,
Options = FileOptions.SequentialScan,
// Consider whether to define larger BufferSize
};
#else
static readonly FileStreamOptions s_streamReaderOptionsAsync = new()
{
Access = FileAccess.Read,
Mode = FileMode.Open,
Options = FileOptions.SequentialScan | FileOptions.Asynchronous,
// Consider whether to define larger BufferSize
};
#endif

#if SYNC
public static SepReader FromText(this in SepReaderOptions options, string text)
#else
Expand All @@ -33,7 +51,11 @@ public static SepReader FromFile(this in SepReaderOptions options, string filePa
public static async ValueTask<SepReader> FromFileAsync(this SepReaderOptions options, string filePath, CancellationToken cancellationToken = default)
#endif
{
var reader = new StreamReader(filePath, s_streamReaderOptions);
#if SYNC
var reader = new StreamReader(filePath, s_streamReaderOptionsSync);
#else
var reader = new StreamReader(filePath, s_streamReaderOptionsAsync);
#endif
Func<SepReader.Info, string> display = static info => $"File='{info.Source}'";
#if SYNC
return FromWithInfo(new(filePath, display), options, reader);
Expand Down
24 changes: 23 additions & 1 deletion src/Sep/SepReaderExtensions.IO.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ namespace nietras.SeparatedValues;

public static partial class SepReaderExtensions
{
#if SYNC
static readonly FileStreamOptions s_streamReaderOptionsSync = new()
{
Access = FileAccess.Read,
Mode = FileMode.Open,
Options = FileOptions.SequentialScan,
// Consider whether to define larger BufferSize
};
#else
static readonly FileStreamOptions s_streamReaderOptionsAsync = new()
{
Access = FileAccess.Read,
Mode = FileMode.Open,
Options = FileOptions.SequentialScan | FileOptions.Asynchronous,
// Consider whether to define larger BufferSize
};
#endif

#if SYNC
public static SepReader FromText(this in SepReaderOptions options, string text)
#else
Expand All @@ -33,7 +51,11 @@ public static SepReader FromFile(this in SepReaderOptions options, string filePa
public static async ValueTask<SepReader> FromFileAsync(this SepReaderOptions options, string filePath, CancellationToken cancellationToken = default)
#endif
{
var reader = new StreamReader(filePath, s_streamReaderOptions);
#if SYNC
var reader = new StreamReader(filePath, s_streamReaderOptionsSync);
#else
var reader = new StreamReader(filePath, s_streamReaderOptionsAsync);
#endif
Func<SepReader.Info, string> display = static info => $"File='{info.Source}'";
#if SYNC
return FromWithInfo(new(filePath, display), options, reader);
Expand Down
9 changes: 0 additions & 9 deletions src/Sep/SepReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace nietras.SeparatedValues;

public static partial class SepReaderExtensions
{
static readonly FileStreamOptions s_streamReaderOptions = new()
{
Access = FileAccess.Read,
Mode = FileMode.Open,
Options = FileOptions.SequentialScan,
// Consider whether to define larger BufferSize
};

public static SepReaderOptions Reader(this Sep sep) => new(sep);
public static SepReaderOptions Reader(this Sep? sep) => new(sep);
public static SepReaderOptions Reader(this SepSpec spec) =>
Expand Down

0 comments on commit 8c4a1ac

Please sign in to comment.