Table of Contents

Channels and Dataflow

Query results can be read straight into a pipeline instead of a collection. The core package streams a mapped query, and two companion packages put its rows into a channel or a Dataflow block.

Package Entry points Pipeline type
Open.Database.Extensions.Core (the views behind Map<T>()) ToPooledStream(), ToPooledBufferStream() IAsyncEnumerable<T> (elements), IAsyncEnumerable<ReadOnlyMemory<T>> (chunks)
Open.Database.Extensions.Channel ToChannelReader(), ToChannel(), ToChannelHoldingConnection() System.Threading.Channels
Open.Database.Extensions.Dataflow ToTargetBlock(), AsSourceBlock(), ToTargetBlockHoldingConnection() System.Threading.Tasks.Dataflow

Both packages take a mapped query (Map<T>()), so rows are mapped the same way as Map<T>().

Streaming from a mapped query

To read a mapped query while a consumer works, use the pooled streams (ToPooledStream and ToPooledBufferStream) in the core package: the connection is released as soon as the last row is read.

A stream earns its place when a producer and its consumer run at different speeds. After Map<T>(), the query is the producer: it reads the rows on a background task, in chunks, and is finished with the connection as soon as the last row is read. The consumer does the slow work (a web call, a file write, a transform) while the query is still being read. The rows are typed, the later result sets are read through, and rows are read with Read unless you call AsyncReads().

Which one to use:

  • ToPooledStream(cancellationToken) yields the elements one by one, as an IAsyncEnumerable<T> that works with await foreach and System.Linq.Async.
  • ToPooledBufferStream(cancellationToken) yields the chunks (ReadOnlyMemory<T>) without copying the rows out of them. A chunk is valid only until the next step of the loop: copy what you keep.

Both are lazy and single-use, and a break or a dispose cancels the query: it waits for the provider call in progress to return. Always dispose the enumerator (await foreach does): one that is abandoned holds the connection until the read ends. The pool (UsePool after Map), clearing, cancellation and the cost in memory are in Pooled buffers and streams, which also covers ToPooledBuffer.

One producer, one consumer

The query reads ahead into buffers, and the consumer does its slow work row by row:

// The query is the producer: it reads the rows in the background,
// in chunks, and the connection is free as soon as the last row
// is read. Nothing runs until the first read below.
IAsyncEnumerable<Post> posts = connection
    .Command("select * from posts order by id")
    .Map<Post>()
    .ToPooledStream(cancellationToken);

// This is the consumer. Its slow work does not hold the
// query back, because the rows are already buffered.
await foreach (Post post in posts)
{
    enriched.Add(await Enrich(post, cancellationToken));
}

A pipeline

A stage can consume the stream and produce a channel of its own. Here the query feeds a transform, which feeds the final consumer, and each stage runs on its own task.

// Stage 1: the query, as a producer.
IAsyncEnumerable<Post> posts = connection
    .Command("select * from posts order by id")
    .Map<Post>()
    .ToPooledStream(cancellationToken);

// Stage 2: a transform that consumes stage 1 and is the
// producer of a channel.
var formatted = Channel.CreateUnbounded<string>();
Task transform = Task.Run(
    async () =>
    {
        try
        {
            await foreach (Post post in posts)
            {
                string line = await Enrich(post, cancellationToken);
                await formatted.Writer.WriteAsync(
                    line,
                    cancellationToken);
            }

            formatted.Writer.TryComplete();
        }
        catch (Exception ex)
        {
            formatted.Writer.TryComplete(ex);
            throw;
        }
    },
    cancellationToken);

// Stage 3: the final consumer.
var results = formatted.Reader.ReadAllAsync(cancellationToken);
await foreach (string line in results)
    lines.Add(line);

await transform;

When you do not need a stream

If there is one consumer, only a few rows, and nothing else to overlap, you do not need a stream: buffer the rows with ToListAsync(cancellationToken). The connection is released as soon as the list is built, and the slow work happens after that.

// Few rows, one consumer, nothing to overlap: buffer them.
// The connection is released as soon as the list is built,
// and the slow work happens after that.
List<Post> posts = await connection
    .Command("select * from posts order by id")
    .Map<Post>()
    .ToListAsync(cancellationToken);

foreach (Post post in posts)
    lines.Add(await Enrich(post, cancellationToken));

To read rows one at a time and stop early, see Stream: it holds the connection while you pull rows, so it is not the way to do slow work per row.

What it costs

The producer is unbounded. It never waits for the consumer, so the read finishes at the provider's pace and frees the connection, while memory grows with the backlog of a slow consumer. To cap memory by holding the connection, see Channels.

Channels

Open.Database.Extensions.Channel: You write the SQL. ODX writes the other twenty lines.

Import Open.ChannelExtensions alongside Open.Database.Extensions.Channel. The Channel package depends only on Core and System.Threading.Channels, and does one job: it puts the rows of a mapped query into a channel as fast as it can. Everything after the dump (Transform, Batch, Join, ReadAll) is what Open.ChannelExtensions is for.

ToChannelReader() and ToChannel() make an unbounded channel and start reading at the call:

// Returns right away: the rows are read in the background
// and the channel completes when they are all written.
ChannelReader<Post> reader = connectionFactory
    .Command("select * from posts order by id")
    .Map<Post>()
    .ToChannelReader(cancellationToken);

await foreach (Post post in reader.ReadAllAsync(cancellationToken))
    received.Add(post);

The query is read at the provider's pace into pooled chunks, and the connection is closed when the read ends, however slowly the consumer reads. The rows the consumer has not read yet wait in memory. The channel is always completed: after the last row, or with the exception that ended the read, a cancel included. ToChannel(out Task<long> readCount) also returns the number of rows written. ToBoundedChannelReader(capacity) and ToBoundedChannel(capacity) make a bounded channel; the bound limits the channel, not memory.

A channel you own

ToChannel(channel) and ToChannel(writer) only write. They leave the channel open, so passes can share it, and the returned task is the done signal: it completes with the number of rows written, and a failure or a cancel comes back through it. complete: true completes the channel when the pass ends, with the exception that ended it if there was one.

// You own the channel. A pass only writes: it leaves the
// channel open, so passes can share it.
Channel<Post> channel = Channel.CreateUnbounded<Post>();

long first = await connectionFactory
    .Command("select * from posts where id <= 3 order by id")
    .Map<Post>()
    .ToChannel(channel, cancellationToken);

// The last pass completes the channel when it ends.
long second = await connectionFactory
    .Command("select * from posts where id > 3 order by id")
    .Map<Post>()
    .ToChannel(
        channel,
        complete: true,
        cancellationToken);

await foreach (Post post in channel.Reader.ReadAllAsync(cancellationToken))
    received.Add(post);

When the channel is full, the write waits for room, before the command runs and between rows. The rows wait in memory meanwhile, so the channel does not limit memory. If 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.

Capping memory

ToChannelHoldingConnection pulls the rows one at a time from Stream, so a full channel limits memory. Stream says when to use it and what the required stallTimeout does.

// The channel holds at most two rows. The next row is pulled
// only after the last one was written, and the connection is
// held for each row: a slow consumer holds it. To release it as
// soon as the last row is read, use ToChannel (above).
Channel<Post> channel = Channel.CreateBounded<Post>(2);

ValueTask<long> write = connectionFactory
    .Command("select * from posts order by id")
    .Map<Post>()
    .ToChannelHoldingConnection(
        channel,
        TimeSpan.FromSeconds(30),
        complete: true,
        cancellationToken);

await foreach (Post post in channel.Reader.ReadAllAsync(cancellationToken))
    received.Add(post);

long written = await write;

Dataflow

Open.Database.Extensions.Dataflow: You write the SQL. ODX writes the other twenty lines.

The Dataflow package reads a mapped query at the provider's pace into pooled chunks and sends each row to a block, so the blocks, their task schedulers and their priorities stay yours.

ToTargetBlock sends the rows to a block you own and leaves it open, so passes can share it. The returned task is the done signal: it completes with the number of rows the block accepted, and a failure or a cancel comes back through it. complete: true completes the block when the pass ends, or faults it with the exception that ended it, a cancel included (a faulted block drops the rows it holds that were not received yet).

// The block is yours: its scheduler and its priorities are
// whatever you chose. The pass only sends to it.
var stage = new TransformBlock<Post, string>(
    static post => $"#{post.Id} {post.Title}",
    new ExecutionDataflowBlockOptions { BoundedCapacity = 100 });

var sink = new ActionBlock<string>(
    _ => { },
    new ExecutionDataflowBlockOptions { BoundedCapacity = 100 });

stage.LinkTo(
    sink,
    new DataflowLinkOptions { PropagateCompletion = true });

long written = await connectionFactory
    .Command("select * from posts order by id")
    .Map<Post>()
    .ToTargetBlock(stage, complete: true, cancellationToken);

await sink.Completion;

Each row is sent with SendAsync, so a bounded block that is full makes the pass wait and no row is dropped. The rows wait in memory meanwhile, and the connection is closed when the read ends. If the block declines a row for good (completed or faulted), the pass stops without an exception and returns the rows accepted so far.

AsSourceBlock makes the block for you: an unbounded BufferBlock<T>, or one made from the DataflowBlockOptions you pass (its task scheduler, its bound, its name). It starts reading at the call and always finishes the block, completed after the last row or faulted with the exception that ended the read.

// Returns right away: the rows are read in the background
// and the block is completed when they are all sent.
IReceivableSourceBlock<Post> source = connectionFactory
    .Command("select * from posts order by id")
    .Map<Post>()
    .AsSourceBlock(
        new DataflowBlockOptions { BoundedCapacity = 100 },
        cancellationToken);

while (await source.OutputAvailableAsync(cancellationToken))
{
    while (source.TryReceive(out Post? post))
        received.Add(post);
}

ToTargetBlockHoldingConnection pulls the rows one at a time from Stream, so a full block limits memory. Stream says when to use it and what the required stallTimeout does.

Honest limits

  • They cost more than a plain loop for one row (1.61 times for a channel, 2.56 times for a BufferBlock). At 1,000 rows ToChannelReader(true) took 0.32 times as long as a hand-written channel loop and ToTargetBlock 0.72 times; at 100 rows the channel took 0.45 times and the BufferBlock 1.10 times. See Channels and Dataflow against a plain loop.