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
5 changes: 3 additions & 2 deletions Source/Channel/ToChannel.Produce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static partial class ChannelDbExtensions
/// The channel is always completed:
/// without an error after the last row, or with the exception that
/// ended the read, a cancel included.
/// <see href="https://open-net-libraries.github.io/Open.Database.Extensions/articles/channels-and-dataflow.html#channels">Channels</see>.
/// </para>
/// </remarks>
/// <typeparam name="T">The type of each row.</typeparam>
Expand Down Expand Up @@ -140,8 +141,8 @@ public static Channel<T> ToChannel<T>(
/// </summary>
/// <remarks>
/// The bound limits the channel, not memory:
/// the query is still read at full speed, and the rows the channel has
/// no room for wait in an internal queue.
/// the query is still read at full speed, and the rows that do not fit
/// wait in memory.
/// </remarks>
/// <inheritdoc cref="ToChannelReader{T}(IMappedQuery{T}, CancellationToken)" path="/typeparam"/>
/// <param name="source">The mapped query to read.</param>
Expand Down
20 changes: 12 additions & 8 deletions Source/Channel/ToChannel.Streaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ public static partial class ChannelDbExtensions
/// </summary>
/// <remarks>
/// Holds the reader and the connection open for each row: while the channel is full,
/// the next row is not pulled and the connection stays open until
/// the read waits and the connection stays open until
/// <paramref name="stallTimeout"/> expires (never, with <see cref="Timeout.InfiniteTimeSpan"/>).
/// Use <see cref="ToChannel{T}(IMappedQuery{T}, Channel{T}, CancellationToken)"/>,
/// which frees the connection first, unless the rows must be pulled one at a time;
/// otherwise it behaves as that member.
/// See <see cref="IMappedQuery{T}.StreamBuffered(TimeSpan, int, CancellationToken)"/> (with a buffer of one row) for the stall rules.
/// otherwise it behaves the same, apart from the stall timeout.
/// See <see href="https://open-net-libraries.github.io/Open.Database.Extensions/articles/mapping.html#terminals">Terminals</see> for the stall rules.
/// </remarks>
/// <typeparam name="T">The type of each row.</typeparam>
/// <param name="source">The mapped query to read.</param>
/// <param name="channel">The channel to write to.</param>
/// <param name="stallTimeout">
/// The longest the channel may make no room while the reader is held. Required; passed to <c>Stream</c>.
/// The longest the channel may make no room while the reader is held. Required; passed to <see cref="IMappedQuery{T}.StreamBuffered(TimeSpan, int, CancellationToken)"/> with a buffer of one row.
/// The wait for room before the command runs is not timed.
/// </param>
/// <include file="../Core/Mapped.docs.xml" path="docs/members[@name='token-pass']/*" />
Expand All @@ -42,7 +42,11 @@ public static ValueTask<long> ToChannelHoldingConnection<T>(
false,
cancellationToken);

/// <inheritdoc cref="ToChannelHoldingConnection{T}(IMappedQuery{T}, Channel{T}, TimeSpan, CancellationToken)" path="/summary"/>
/// <summary>
/// Reads the query one row at a time and writes each row to a channel
/// you own, completing the channel when the pass ends if
/// <paramref name="complete"/> is <see langword="true"/>.
/// </summary>
/// <inheritdoc cref="ToChannelHoldingConnection{T}(IMappedQuery{T}, Channel{T}, TimeSpan, CancellationToken)" path="/remarks"/>
/// <inheritdoc cref="ToChannelHoldingConnection{T}(IMappedQuery{T}, Channel{T}, TimeSpan, CancellationToken)" path="/typeparam"/>
/// <inheritdoc cref="ToChannelHoldingConnection{T}(IMappedQuery{T}, Channel{T}, TimeSpan, CancellationToken)" path="/returns"/>
Expand All @@ -53,7 +57,7 @@ public static ValueTask<long> ToChannelHoldingConnection<T>(
/// </param>
/// <param name="source">The mapped query to read.</param>
/// <param name="channel">The channel to write to.</param>
/// <param name="stallTimeout">The longest the channel may make no room while the reader is held. Required; passed to <c>Stream</c>.
/// <param name="stallTimeout">The longest the channel may make no room while the reader is held. Required; passed to <see cref="IMappedQuery{T}.StreamBuffered(TimeSpan, int, CancellationToken)"/> with a buffer of one row.
/// The wait for room before the command runs is not timed.</param>
/// <include file="../Core/Mapped.docs.xml" path="docs/members[@name='token-pass']/*" />
public static ValueTask<long> ToChannelHoldingConnection<T>(
Expand Down Expand Up @@ -85,7 +89,7 @@ public static ValueTask<long> ToChannelHoldingConnection<T>(
/// <inheritdoc cref="ToChannelHoldingConnection{T}(IMappedQuery{T}, Channel{T}, TimeSpan, CancellationToken)" path="/exception"/>
/// <param name="writer">The writer to write to.</param>
/// <param name="source">The mapped query to read.</param>
/// <param name="stallTimeout">The longest the writer may make no room while the reader is held. Required; passed to <c>Stream</c>.
/// <param name="stallTimeout">The longest the writer may make no room while the reader is held. Required; passed to <see cref="IMappedQuery{T}.StreamBuffered(TimeSpan, int, CancellationToken)"/> with a buffer of one row.
/// The wait for room before the command runs is not timed.</param>
/// <include file="../Core/Mapped.docs.xml" path="docs/members[@name='token-pass']/*" />
public static ValueTask<long> ToChannelHoldingConnection<T>(
Expand All @@ -107,7 +111,7 @@ public static ValueTask<long> ToChannelHoldingConnection<T>(
/// <inheritdoc cref="ToChannelHoldingConnection{T}(IMappedQuery{T}, Channel{T}, TimeSpan, bool, CancellationToken)" path="/exception"/>
/// <param name="writer">The writer to write to.</param>
/// <param name="source">The mapped query to read.</param>
/// <param name="stallTimeout">The longest the writer may make no room while the reader is held. Required; passed to <c>Stream</c>.
/// <param name="stallTimeout">The longest the writer may make no room while the reader is held. Required; passed to <see cref="IMappedQuery{T}.StreamBuffered(TimeSpan, int, CancellationToken)"/> with a buffer of one row.
/// The wait for room before the command runs is not timed.</param>
/// <param name="complete">
/// <see langword="true"/> to complete the writer when the pass ends,
Expand Down
34 changes: 21 additions & 13 deletions Source/Channel/ToChannel.Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ public static partial class ChannelDbExtensions
/// </summary>
/// <remarks>
/// <para>
/// The task is the done signal: it completes with the number of rows
/// handed to the channel after the last result set was read through.
/// A failure or a cancel comes back through the task, and the channel
/// The returned <c>ValueTask&lt;long&gt;</c> completes with the number of
/// rows written after the last result set was read through.
/// A failure or a cancel is thrown by the await, and the channel
/// is left as it was.
/// </para>
/// <para>
/// The channel stays open, so several passes can share one.
/// Pass <c>complete: true</c> to complete it when the pass ends.
/// </para>
/// <para>
/// The query is read at full speed and the connection is closed when the
/// read ends. When the channel is full, the write waits for room,
/// before the command runs and then between rows, while the rows wait in
/// an internal queue: the connection is free, but your channel does not
/// limit memory.
/// When the channel is closed, the pass stops without an exception and
/// returns the rows written so far.
/// A channel completed with an error throws that error.
/// The connection is closed when the read ends.
/// A full channel makes the write wait, before the command runs and then
/// between rows; the rows wait in memory meanwhile, so the channel does
/// not limit memory.
/// A closed channel ends the pass without an exception and returns the
/// rows written so far; a channel completed with an error throws that error.
/// <see href="https://open-net-libraries.github.io/Open.Database.Extensions/articles/channels-and-dataflow.html#a-channel-you-own">A channel you own</see>.
/// </para>
/// </remarks>
/// <typeparam name="T">The type of each row.</typeparam>
Expand All @@ -47,8 +46,17 @@ public static ValueTask<long> ToChannel<T>(
false,
cancellationToken);

/// <inheritdoc cref="ToChannel{T}(IMappedQuery{T}, Channel{T}, CancellationToken)" path="/summary"/>
/// <inheritdoc cref="ToChannel{T}(IMappedQuery{T}, Channel{T}, CancellationToken)" path="/remarks"/>
/// <summary>
/// Reads the query and writes each row to a channel you own,
/// completing the channel when the pass ends if <paramref name="complete"/> is
/// <see langword="true"/>.
/// </summary>
/// <remarks>
/// With <c>complete: false</c> it is
/// <see cref="ToChannel{T}(IMappedQuery{T}, Channel{T}, CancellationToken)"/>.
/// With <c>complete: true</c> the channel is completed when the pass ends,
/// or completed with the exception that ended it, a cancel included.
/// </remarks>
/// <inheritdoc cref="ToChannel{T}(IMappedQuery{T}, Channel{T}, CancellationToken)" path="/typeparam"/>
/// <inheritdoc cref="ToChannel{T}(IMappedQuery{T}, Channel{T}, CancellationToken)" path="/returns"/>
/// <inheritdoc cref="ToChannel{T}(IMappedQuery{T}, Channel{T}, CancellationToken)" path="/exception"/>
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/Commands.docs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<param name="connectionPool">
The pool to take connections from.
Each execution takes one and gives it back when finished.
The pool must return a connection; <see langword="null"/> is not supported.
</param>
<exception cref="ArgumentNullException">
<paramref name="connectionPool"/> or <paramref name="command"/> is
Expand All @@ -82,6 +83,7 @@
The factory that creates the connections.
Each execution creates a connection, opens it if needed, and disposes it
when finished.
The factory must return a connection; <see langword="null"/> is not supported.
</param>
<exception cref="ArgumentNullException">
<paramref name="connFactory"/> or <paramref name="command"/> is
Expand All @@ -94,6 +96,7 @@
The function that creates the connections.
Each execution invokes it, opens the connection if needed, and disposes
it when finished.
The function must return a connection; <see langword="null"/> is not supported.
</param>
<exception cref="ArgumentNullException">
<paramref name="connFactory"/> or <paramref name="command"/> is
Expand Down
77 changes: 9 additions & 68 deletions Source/Core/ConnectionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,69 +27,15 @@ enum ScopeEnd : byte
/// to do with it when the scope is disposed.
/// </summary>
/// <remarks>
/// <para>
/// One type for every source of a connection: the scope carries how it ends. What
/// disposing does depends on what made it:
/// </para>
/// <list type="table">
/// <listheader>
/// <term>Made by</term>
/// <description>The connection came from; disposing the scope</description>
/// </listheader>
/// <item>
/// <term><see cref="ConnectionExtensions.EnsureOpenScope{TConnection}(TConnection)"/> /
/// <see cref="ConnectionExtensions.EnsureOpenScopeAsync{TConnection}(TConnection, CancellationToken)"/></term>
/// <description>You already hold it; closes it if the scope opened it, and leaves it open if it was already open.
/// Never disposes it.</description>
/// </item>
/// <item>
/// <term><see cref="ConnectionExtensions.EnsureOpenScope(IDbConnectionFactory)"/> /
/// <see cref="ConnectionExtensions.EnsureOpenScopeAsync(IDbConnectionFactory, CancellationToken)"/> (on a factory)</term>
/// <description>The factory created it, so it is yours; disposes it: <c>Dispose</c>, as
/// <c>factory.Open(...)</c> does, and <c>DisposeAsync</c> from <see cref="DisposeAsync"/> when the connection has one.</description>
/// </item>
/// <item>
/// <term><see cref="ConnectionExtensions.EnsureOpenScope(IDbConnectionPool)"/> /
/// <see cref="ConnectionExtensions.EnsureOpenScopeAsync(IDbConnectionPool, CancellationToken)"/> (on a pool)</term>
/// <description>The pool leased it; gives it back with <see cref="IDbConnectionPool.Give(IDbConnection)"/>, as
/// <c>pool.Open(...)</c> does. What giving back means (close it, keep it, dispose it) is the pool's.</description>
/// </item>
/// What disposing the scope does depends on where the connection came from:
/// <list type="bullet">
/// <item><description>From <c>EnsureOpenScope</c> on a connection: closes it if the scope opened it; never disposes it.</description></item>
/// <item><description>From <c>EnsureOpenScope</c> on a factory: disposes it (<c>DisposeAsync</c> when it has one).</description></item>
/// <item><description>From <c>EnsureOpenScope</c> on a pool: gives it back with <see cref="IDbConnectionPool.Give(IDbConnection)"/>; what that means is the pool's.</description></item>
/// </list>
/// <para>
/// When you already hold the connection the scope is usually only there to be disposed:
/// </para>
/// <code language="csharp">
/// await using var _ = await connection.EnsureOpenScopeAsync(cancellationToken);
/// // ... use the connection you already hold.
/// </code>
/// <para>
/// When the connection came from a factory or a pool the scope is how you get it:
/// </para>
/// <code language="csharp">
/// await using var scope = await factory.EnsureOpenScopeAsync(cancellationToken);
/// var connection = scope.Connection;
/// </code>
/// <para>
/// The scope keeps the type of the connection (a <c>SqlConnection</c> stays a
/// <c>SqlConnection</c>), and deconstructs into the connection and the state it was found in:
/// <c>var (sql, was) = scope;</c>.
/// </para>
/// <para>
/// A connection that was broken is closed and opened again, so a scope that closes it closes it
/// again afterwards; the broken state is not restored.
/// </para>
/// <para>
/// Dispose the value you were given exactly once, and do not copy it: a second dispose of a copy
/// disposes a factory's connection again, gives a pool's connection back again, or closes a connection
/// that someone opened in between. Deconstruct a scope you hold, never the call itself:
/// <c>var (c, s) = connection.EnsureOpenScope();</c> drops the scope, and the connection it opened
/// stays open.
/// </para>
/// <para>
/// A <see langword="default"/> scope holds nothing: disposing it does nothing, and
/// reading <see cref="Connection"/> (or deconstructing it) throws
/// <see cref="InvalidOperationException"/>.
/// </para>
/// Dispose the value exactly once; a copy disposes again.
/// Deconstruct a scope you hold, never the call itself, which drops the scope and leaves a connection it opened open.
/// <see href="https://open-net-libraries.github.io/Open.Database.Extensions/articles/foundation.html#connection-scopes">Connection scopes</see>.
/// </remarks>
/// <typeparam name="TConnection">The type of the connection, as the caller holds it.</typeparam>
public readonly struct ConnectionScope<TConnection> : IDisposable, IAsyncDisposable
Expand Down Expand Up @@ -169,7 +115,7 @@ public void Deconstruct(out TConnection connection, out ConnectionState priorSta
/// <summary>
/// Lets go of the connection the way its source calls for: closes it if the scope opened it
/// (a connection the caller held), disposes it (a factory's), or gives it back to the pool
/// (a pool's). See the table in the remarks of the type.
/// (a pool's). See the remarks of the type.
/// </summary>
public void Dispose()
{
Expand Down Expand Up @@ -264,11 +210,6 @@ public CommandBehavior ReaderBehavior(CommandBehavior behavior = CommandBehavior
/// Configures how <see langword="await"/> <see langword="using"/> awaits the disposal of this scope.
/// </summary>
/// <remarks>
/// Library code takes the scope first, then hands it to <see langword="await"/> <see langword="using"/> on its own line:
/// <code language="csharp">
/// var scope = await connection.EnsureOpenScopeAsync(cancellationToken).ConfigureAwait(false);
/// await using var disposal = scope.ConfigureAwait(false);
/// </code>
/// The library never returns to a caller's context; whether to is the caller's choice at their own call site,
/// as with <see cref="Task.ConfigureAwait(bool)"/>.
/// </remarks>
Expand Down
20 changes: 5 additions & 15 deletions Source/Core/Core/NullPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum NullAction
/// <remarks>
/// The same as no policy. On a member it overrides the type's policy, so one hot member can opt out.
/// A NULL into a nullable member is <see langword="null"/>;
/// into a non-nullable one it surfaces as the provider's own exception inside a <see cref="ValueMappingException"/> that names the model type and, when the row is known, the row (the member and the column are not known).
/// into a non-nullable one it throws <see cref="ValueMappingException"/> naming the model type and the row, not the member or the column.
/// To find which member and column, use <see cref="Throw"/>.
/// </remarks>
/// <seealso cref="Throw"/>
Expand Down Expand Up @@ -50,21 +50,11 @@ public enum NullAction
/// Declares what a NULL does to a member when a row is mapped: on the model type for every member, or on one member.
/// </summary>
/// <remarks>
/// <para>
/// A policy always applies, nullable members included.
/// On the type it covers every member, and a member's own policy overrides it (<see cref="NullAction.None"/> included).
/// A <see cref="NullValueAttribute"/> on a member overrides any policy, the member's own included.
/// A positional record's parameter takes the policy on the parameter or on its property.
/// A pair or a converter of a <see cref="RowMap{T}"/> that names the member is explicit and overrides the member's own declarations in that map (the type's policy still covers it);
/// a converter whose column type is a <see cref="Nullable{T}"/> receives the NULL itself, and no policy applies.
/// A converter whose column type is a reference type (<see cref="string"/>) is not called for a NULL; the member then follows the NULL rules and the type's policy.
/// A policy on a base type covers a derived model's members. The nearest declaration wins: the derived type's own policy, then its base type's;
/// a member's own policy beats both, and a <see cref="NullValueAttribute"/> beats all.
/// The declarations are read once per type, when its slots are built.
/// </para>
/// <para>
/// Every check costs one NULL test per covered member per row. A member with no policy is not checked (<see cref="NullAction.None"/>).
/// </para>
/// On the type it covers every member, and a policy on a base type covers a derived model's members.
/// A member's own policy overrides the type's, and a <see cref="NullValueAttribute"/> on a member overrides any policy.
/// A member with no policy is not checked (<see cref="NullAction.None"/>).
/// See <see href="https://open-net-libraries.github.io/Open.Database.Extensions/articles/mapping.html#null-policy">Null policy</see>.
/// </remarks>
/// <example>
/// <code>
Expand Down
Loading