Table of Contents

Method ToPooledStream

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.Core.dll

ToPooledStream(CancellationToken)

Reads the query in a rolling stream of pooled arrays, once, and yields the elements one by one: it buffers the result in pooled arrays (from an array pool), not a channel of rows.

public IAsyncEnumerable<T> ToPooledStream(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancels the read: a consumer then sees the cancel after what was already delivered.

Returns

IAsyncEnumerable<T>

The lazy, single-use stream of elements.

Remarks

The same pipeline as ToPooledBufferStream(CancellationToken) (lazy, one consumer, a background producer, rolling pooled chunks, a break or a dispose cancels the query), but each chunk is flattened into its elements, so there is no buffer to handle and the stream works with System.Linq.Async. Each element is copied out before it is yielded, so it has no lifetime to mind. The producer is unbounded, as for that stream: a slow consumer lets chunks pile up in memory. A second enumeration throws InvalidOperationException at its first MoveNextAsync() (not at GetAsyncEnumerator(CancellationToken)).

Which one to use: ToPooledBufferStream(CancellationToken) is sequential, read-only access to the elements through the buffers; this method is the same, without buffers; ToPooledBuffer(CancellationToken) is a reusable, potentially indexable list to work with for a while.

Pooled memory: the rows are held in arrays rented from a pool and given back when the call, the buffer or the stream is done. By default that pool is ArrayPool<T>.Shared. To use your own, call .UsePool(pool, clearOnReturn: ...) after Map: the pool is yours, ODX rents from it and gives back to it, and never disposes it. The arrays it hands out may be longer than asked for: ODX uses the rows it wrote and clears only those. A pool that hands out a shorter array than asked for makes the read throw InvalidOperationException. Returning an array must not throw.

Clearing: rows that hold references (a class, a string, a struct that contains either) are always cleared before their array goes back, so a pooled array never keeps a row alive; nothing turns that off. Rows with no references (an int, a plain struct) go back as they are, unless asked: UsePool(pool, clearOnReturn: true) clears them, clearOnReturn: false keeps them as they are whatever the app-wide setting says, and a query that does not call UsePool follows the setting. The app-wide setting is the AppContext switch Open.Database.Extensions.ClearPooledArrays (off by default; read once, so set it before the first query). Set it in the application's project file with <RuntimeHostConfigurationOption Include="Open.Database.Extensions.ClearPooledArrays" Value="true" />, which puts it in runtimeconfig.json, or call AppContext.SetSwitch before the first query.

Here the clearing happens as each chunk goes back, once the consumer is done with its elements.

Three ways to read the rows as they arrive; pick by what the connection and your loop need:

MethodBuffer; threads; connection held until; stall protection
StreamNone: no row is read ahead; your loop's thread (with AsyncReads(), the thread that finishes each read), with no producer task and no channel; until you pull past the last row; yes, on every pull.
StreamBufferedUp to bufferSize rows (default 1,024; it counts rows, not bytes); a producer task and your loop; until the read ends, or, once the buffer is full, until the stall timeout passes without a pull (never with an infinite timeout); yes, once the buffer is full.
ToPooledStreamUnbounded: pooled chunks, the whole result if you are slow; a producer task and your loop; until the read ends, however slowly you read; none.

Which to use: a fast loop, Stream; slow or async work per row, StreamBuffered; to free the connection as soon as possible, ToPooledStream.

All three are lazy: calling one does nothing until the first MoveNextAsync. A slow or hung database read is bounded by the command's CommandTimeout and your cancellation token on every one of them; the stall timeout is only about a slow consumer. A cancel or a dispose stops the read promptly only as far as the provider honors the cancellation.