Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/Core/ChunkSizing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal sealed class ChunkSizes
/// most 2^20. The default is 16.
/// </param>
/// <param name="collectShift">
/// The doublings per chunk of the chain: 1 to 4. The default is 1.
/// The bit shift per chunk of the chain (1 doubles, 2 quadruples), 1 to 4. The default is 1.
/// </param>
/// <param name="collectMaxRows">
/// The rows in the largest chunk of the chain: a power of two, at most 2^26.
Expand Down Expand Up @@ -101,7 +101,7 @@ public ChunkSizes(
/// <summary>Gets the number of rows in the first chunk of the collecting chain.</summary>
public int CollectFirstRows { get; }

/// <summary>Gets the number of doublings per chunk of the chain.</summary>
/// <summary>Gets the bit shift per chunk of the chain (1 doubles, 2 quadruples).</summary>
public int CollectShift { get; }

/// <summary>Gets the number of rows in the largest chunk of the chain.</summary>
Expand Down
33 changes: 1 addition & 32 deletions Source/Core/Core/CancellationTokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static ValueTask<TResult> RunAsync<TState, TResult>(
commandToken,
callToken,
out CancellationTokenSource? linked);
// When no linked source is needed there is no state machine and no allocation; a static lambda with a state keeps it that way for a call with at most one token.
// When no linked source is needed there is no state machine and no allocation.
return linked is null
? start(state, token)
: Linked(linked, token, state, start);
Expand All @@ -78,35 +78,4 @@ static async ValueTask<TResult> Linked(
return await start(state, token).ConfigureAwait(false);
}
}

/// <inheritdoc cref="RunAsync{TState, TResult}(CancellationToken, CancellationToken, TState, Func{TState, CancellationToken, ValueTask{TResult}})"
/// path="/*[not(self::typeparam) and not(self::returns)]"/>
/// <typeparam name="TState">The type of the state.</typeparam>
/// <returns>A task that represents the call.</returns>
[SuppressMessage(
"Design", "CA1068:CancellationToken parameters must come last")]
public static ValueTask RunAsync<TState>(
CancellationToken commandToken,
CancellationToken callToken,
TState state,
Func<TState, CancellationToken, ValueTask> start)
{
CancellationToken token = Combine(
commandToken,
callToken,
out CancellationTokenSource? linked);
return linked is null
? start(state, token)
: Linked(linked, token, state, start);

static async ValueTask Linked(
CancellationTokenSource linked,
CancellationToken token,
TState state,
Func<TState, CancellationToken, ValueTask> start)
{
using (linked)
await start(state, token).ConfigureAwait(false);
}
}
}
7 changes: 0 additions & 7 deletions Source/Core/Core/RowReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,13 +846,6 @@ static ValueTask<bool> ReadRowAsync(DbDataReader reader, bool useReadAsync, Canc
#endregion
}

internal readonly struct ListSink<T>(List<T> list) : IRowSink<T>
{
public List<T> List { get; } = list;
public void Add(T item) => List.Add(item);
public int Count => List.Count;
}

/// <summary>
/// Makes the result of a collecting terminal from the rows in a chain.
/// </summary>
Expand Down
13 changes: 1 addition & 12 deletions Source/Core/DbQueryBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ public static string ResolveDerived(
return last.Real;
}

/// <summary>
/// Gets the names of the members of an object.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="value">The object.</param>
/// <returns>The names of the members.</returns>
public static string[] MemberNames<T>(T value)
=> typeof(T) == typeof(object)
? RuntimeParamSource.Of(value!.GetType()).Names
: ParamSource<T>.Names;

/// <summary>
/// Adds a parameter of a written or derived name.
/// </summary>
Expand Down Expand Up @@ -308,7 +297,7 @@ static bool Has(DbCommand command, string name)
public static bool SameName(string a, string b)
=> Bare(a).Equals(Bare(b), StringComparison.OrdinalIgnoreCase);

static ReadOnlySpan<char> Bare(string name)
internal static ReadOnlySpan<char> Bare(string name)
=> name.Length > 0 && name[0] == '@'
? name.AsSpan(1)
: name.AsSpan();
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/ExpressiveCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ void Apply(
// A member of an object that the procedure does not take is simply not sent; an argument named by the caller must match.
if (p.FromObject) { names[i] = null; continue; } // the pooled slot holds whatever an earlier renter left: write it before it is read

throw NoMatch(expression, scan ? TextNameCache.Names(Command) : real!.Names);
throw NoMatch(expression, scan ? SqlTextParameters.Parse(Command) : real!.Names);
}

names[i] = name;
Expand Down Expand Up @@ -1078,7 +1078,7 @@ void Apply(
for (int j = i + 1; j < count; j++)
{
if (names[j] is { } second && DbQueryBinding.SameName(first, second))
throw Collision(first, list[i], list[j], scan ? TextNameCache.Names(Command) : real?.Names ?? default);
throw Collision(first, list[i], list[j], scan ? SqlTextParameters.Parse(Command) : real?.Names ?? default);
}
}

Expand Down Expand Up @@ -1312,7 +1312,7 @@ static ValueTask<ConnectionScope<TConnection>> EnsureOpenScopeUnlessOpenAsync(
CancellationToken cancellationToken)
{
ConnectionState state = connection.State;
if ((state & (ConnectionState.Open | ConnectionState.Broken)) == ConnectionState.Open)
if (!ConnectionExtensions.NeedsOpen(state))
{
return new ValueTask<ConnectionScope<TConnection>>(
pool is null
Expand Down
35 changes: 17 additions & 18 deletions Source/Core/Extensions/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@ public static IEnumerable<T> Select<T>(

static async IAsyncEnumerable<object[]> AsAsyncEnumerableCore(DbDataReader reader, [EnumeratorCancellation] CancellationToken cancellationToken)
{
Contract.EndContractBlock();

if (cancellationToken.IsCancellationRequested
|| !await reader.ReadAsync(cancellationToken).CancelledAsFalse().ConfigureAwait(false))
{
Expand All @@ -329,8 +327,11 @@ static async IAsyncEnumerable<object[]> AsAsyncEnumerableCore(DbDataReader reade

static async IAsyncEnumerable<object[]> AsAsyncEnumerableCore(DbDataReader reader, ArrayPool<object> arrayPool, [EnumeratorCancellation] CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested || !await reader.ReadAsync().ConfigureAwait(false))
if (cancellationToken.IsCancellationRequested
|| !await reader.ReadAsync(cancellationToken).CancelledAsFalse().ConfigureAwait(false))
{
yield break;
}

int fieldCount = reader.FieldCount;
do
Expand All @@ -339,7 +340,8 @@ static async IAsyncEnumerable<object[]> AsAsyncEnumerableCore(DbDataReader reade
reader.GetValues(row);
yield return row;
}
while (!cancellationToken.IsCancellationRequested && await reader.ReadAsync().ConfigureAwait(false));
while (!cancellationToken.IsCancellationRequested
&& await reader.ReadAsync(cancellationToken).CancelledAsFalse().ConfigureAwait(false));
}

/// <include file="Readers.docs.xml" path="docs/members[@name='reader-enumerate']/*" />
Expand Down Expand Up @@ -385,20 +387,19 @@ public static IAsyncEnumerable<object[]> AsAsyncEnumerable(
}

static IAsyncEnumerable<object[]> AsAsyncEnumerableInternal(
this DbDataReader reader,
DbDataReader reader,
IEnumerable<int> ordinals,
bool readStarted,
CancellationToken cancellationToken)
{
return reader is null
? throw new ArgumentNullException(nameof(reader))
: ordinals is null
? throw new ArgumentNullException(nameof(ordinals))
: AsAsyncEnumerableInternalCore(reader, ordinals, readStarted, cancellationToken);
: AsAsyncEnumerableInternalCore(reader, ordinals, cancellationToken);

static async IAsyncEnumerable<object[]> AsAsyncEnumerableInternalCore(DbDataReader reader, IEnumerable<int> ordinals, bool readStarted, [EnumeratorCancellation] CancellationToken cancellationToken)
static async IAsyncEnumerable<object[]> AsAsyncEnumerableInternalCore(DbDataReader reader, IEnumerable<int> ordinals, [EnumeratorCancellation] CancellationToken cancellationToken)
{
if (!readStarted && (cancellationToken.IsCancellationRequested || !await reader.ReadAsync(cancellationToken).CancelledAsFalse().ConfigureAwait(false)))
if (cancellationToken.IsCancellationRequested || !await reader.ReadAsync(cancellationToken).CancelledAsFalse().ConfigureAwait(false))
yield break;

IList<int> o = ordinals as IList<int> ?? ordinals.ToArray();
Expand Down Expand Up @@ -428,9 +429,8 @@ static async IAsyncEnumerable<object[]> AsAsyncEnumerableInternalCore(DbDataRead
}

static IAsyncEnumerable<object[]> AsAsyncEnumerableInternal(
this DbDataReader reader,
DbDataReader reader,
IEnumerable<int> ordinals,
bool readStarted,
ArrayPool<object> arrayPool,
CancellationToken cancellationToken)
{
Expand All @@ -439,17 +439,16 @@ static IAsyncEnumerable<object[]> AsAsyncEnumerableInternal(
Debug.Assert(arrayPool is not null);
Contract.EndContractBlock();

return AsAsyncEnumerableInternalCore(reader, ordinals, readStarted, arrayPool, cancellationToken);
return AsAsyncEnumerableInternalCore(reader, ordinals, arrayPool, cancellationToken);

static async IAsyncEnumerable<object[]> AsAsyncEnumerableInternalCore(
DbDataReader reader,
IEnumerable<int> ordinals,
bool readStarted,
ArrayPool<object> arrayPool,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
if (!readStarted && (cancellationToken.IsCancellationRequested
|| !await reader.ReadAsync(cancellationToken).CancelledAsFalse().ConfigureAwait(false)))
if (cancellationToken.IsCancellationRequested
|| !await reader.ReadAsync(cancellationToken).CancelledAsFalse().ConfigureAwait(false))
{
yield break;
}
Expand Down Expand Up @@ -484,7 +483,7 @@ static async IAsyncEnumerable<object[]> AsAsyncEnumerableInternalCore(
/// <include file="Readers.docs.xml" path="docs/members[@name='ordinals-limited']/*" />
/// <include file="Readers.docs.xml" path="docs/members[@name='cancellationToken']/*" />
public static IAsyncEnumerable<object[]> AsAsyncEnumerable(this DbDataReader reader, IEnumerable<int> ordinals, CancellationToken cancellationToken = default)
=> AsAsyncEnumerableInternal(reader, ordinals, false, cancellationToken);
=> AsAsyncEnumerableInternal(reader, ordinals, cancellationToken);

/// <summary>
/// Asynchronously enumerates the remaining records of the current result set of a <see cref="DbDataReader"/> as arrays of values;
Expand Down Expand Up @@ -515,8 +514,8 @@ public static IAsyncEnumerable<object[]> AsAsyncEnumerable(this DbDataReader rea
/// </exception>
public static IAsyncEnumerable<object[]> AsAsyncEnumerable(this DbDataReader reader, IEnumerable<int> ordinals, ArrayPool<object>? arrayPool, CancellationToken cancellationToken = default)
=> arrayPool is null
? AsAsyncEnumerableInternal(reader, ordinals, false, cancellationToken)
: AsAsyncEnumerableInternal(reader, ordinals, false, arrayPool, cancellationToken);
? AsAsyncEnumerableInternal(reader, ordinals, cancellationToken)
: AsAsyncEnumerableInternal(reader, ordinals, arrayPool, cancellationToken);

/// <summary>
/// Asynchronously and lazily transforms each remaining record of a <see cref="DbDataReader"/> through a function.
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Extensions/DataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// <remarks>
/// These methods read the record's current row and result-set metadata; they never advance the reader. Methods that return a lazy <see cref="IEnumerable{T}"/> read from the record while they are enumerated, so enumerate them before the reader advances or is disposed. Methods that return an array, <see cref="ImmutableArray{T}"/>, or dictionary read everything before they return. Null database values are represented as <see cref="DBNull"/>, except where noted.
/// </remarks>
public static partial class DataRecordExtensions
public static class DataRecordExtensions
{
/// <summary>
/// Lazily enumerates the column names of the current result set in ordinal order.
Expand Down
20 changes: 0 additions & 20 deletions Source/Core/Extensions/_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@
public static partial class CoreExtensions
{

internal static ValueTask<bool> CancelledAsFalse(this ValueTask<bool> task)
{
if (task.IsCompletedSuccessfully)
return task;

return CancelledAsFalseCore(task);

static async ValueTask<bool> CancelledAsFalseCore(ValueTask<bool> task)
{
try
{
return await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
return false;
}
}
}

internal static async ValueTask<bool> CancelledAsFalse(this Task<bool> task)
{
try
Expand Down
17 changes: 0 additions & 17 deletions Source/Core/MappedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,15 +1268,6 @@ public readonly record struct MappedQuery<T>
public MappedQuery<T> UsePool(ArrayPool<T> pool, bool clearOnReturn)
=> new(_core.WithPool(pool, clearOnReturn));

/// <summary>
/// Returns the query with a whole configuration
/// (pool, clear, chunk sizes, bin), for a test or a benchmark.
/// </summary>
/// <param name="config">The configuration to use.</param>
/// <returns>A copy of the query that uses <paramref name="config"/>.</returns>
internal MappedQuery<T> WithConfig(ChunkConfig<T> config)
=> new(_core.WithConfig(config));

/// <summary>Gets the query as a call when it was made from a <see cref="DbQuery"/> definition.</summary>
internal bool TryGetCall(out DbQueryCall call)
=> _core.TryGetCall(out call);
Expand Down Expand Up @@ -1781,10 +1772,6 @@ public readonly record struct MappedQueryWithSkip<T>
public MappedQueryWithSkip<T> UsePool(ArrayPool<T> pool, bool clearOnReturn)
=> new(_core.WithPool(pool, clearOnReturn));

/// <inheritdoc cref="MappedQuery{T}.WithConfig(ChunkConfig{T})"/>
internal MappedQueryWithSkip<T> WithConfig(ChunkConfig<T> config)
=> new(_core.WithConfig(config));

internal List<T> ToList() => _core.ToList();
/// <inheritdoc cref="MappedQuery{T}.ToListAsync"/>
public ValueTask<List<T>> ToListAsync(CancellationToken cancellationToken = default) => _core.ToListAsync(cancellationToken);
Expand Down Expand Up @@ -1924,10 +1911,6 @@ public readonly record struct MappedQueryWithTake<T>
public MappedQueryWithTake<T> UsePool(ArrayPool<T> pool, bool clearOnReturn)
=> new(_core.WithPool(pool, clearOnReturn));

/// <inheritdoc cref="MappedQuery{T}.WithConfig(ChunkConfig{T})"/>
internal MappedQueryWithTake<T> WithConfig(ChunkConfig<T> config)
=> new(_core.WithConfig(config));

internal List<T> ToList() => _core.ToList();
/// <inheritdoc cref="MappedQuery{T}.ToListAsync"/>
public ValueTask<List<T>> ToListAsync(CancellationToken cancellationToken = default) => _core.ToListAsync(cancellationToken);
Expand Down
7 changes: 6 additions & 1 deletion Source/Core/MappedViews.cs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,12 @@ public MappedCommand<T> SequentialAccess()
public MappedCommand<T> UsePool(ArrayPool<T> pool, bool clearOnReturn)
=> new(_core.WithPool(pool, clearOnReturn));

/// <inheritdoc cref="MappedQuery{T}.WithConfig(ChunkConfig{T})"/>
/// <summary>
/// Returns the query with a whole configuration
/// (pool, clear, chunk sizes, bin), for a test or a benchmark.
/// </summary>
/// <param name="config">The configuration to use.</param>
/// <returns>A copy of the query that uses <paramref name="config"/>.</returns>
internal MappedCommand<T> WithConfig(ChunkConfig<T> config)
=> new(_core.WithConfig(config));

Expand Down
7 changes: 2 additions & 5 deletions Source/Core/NameSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,15 @@ sealed class BareComparer : IEqualityComparer<string>
{
internal static readonly BareComparer Instance = new();

static ReadOnlySpan<char> Bare(string name)
=> name.Length > 0 && name[0] == '@' ? name.AsSpan(1) : name.AsSpan();

public bool Equals(string? x, string? y)
=> Bare(x!).Equals(Bare(y!), StringComparison.OrdinalIgnoreCase);
=> DbQueryBinding.Bare(x!).Equals(DbQueryBinding.Bare(y!), StringComparison.OrdinalIgnoreCase);

public int GetHashCode(string obj)
{
unchecked
{
int h = 17;
foreach (char c in Bare(obj)) h = h * 31 + char.ToUpperInvariant(c);
foreach (char c in DbQueryBinding.Bare(obj)) h = h * 31 + char.ToUpperInvariant(c);
return h;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/PooledBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ static PooledBuffer<T> Create(ReadOnlySpan<T> rows, ChunkConfig<T> config)
{
System.Diagnostics.Debug.Assert(rows.Length <= MaxFromRows);

if (rows.IsEmpty) return new([], 0, 1);
if (rows.IsEmpty) return CreateEmptyOwner();

// One array that is one first chunk: row i is at i, so the indexer always lands in chunk 0.
int firstRows = 1;
Expand Down
5 changes: 0 additions & 5 deletions Source/Core/TextNameCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ internal static bool HasMarkers(string text)
return SqlTextParameters.NextMarker(text, ref position, out _, out _);
}

/// <summary>Gets the distinct markers of the text, for a message.</summary>
/// <param name="text">The SQL text.</param>
/// <returns>The markers, each with its <c>@</c>.</returns>
internal static ImmutableArray<string> Names(string text) => SqlTextParameters.Parse(text);

/// <summary>
/// Gets the marker (with its <c>@</c>) that the argument expression stands for, as the text spells it, or <see langword="null"/> when none fits.
/// </summary>
Expand Down
Loading