Table of Contents

Method ToPooledBuffer

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

ToPooledBuffer(CancellationToken)

Reads every row into pooled arrays and keeps them until the returned buffer is disposed.

public PooledBuffer<T> ToPooledBuffer(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

PooledBuffer<T>

The buffer, already reading.

Remarks

The call returns at once and starts the read on a background producer, once, even if nobody reads the buffer yet (it may finish before any read happens). Await Items for the complete result. The buffer is not readable while it fills: to read while the query is still running, use ToPooledBufferStream(CancellationToken) or ToPooledStream(CancellationToken).

The rows stay valid until the buffer is disposed: dispose it (await using) when you are done. Disposing it while the read is still running cancels the query; after the read finished it only releases the arrays. A buffer that is never disposed holds its arrays until the garbage collector takes it (there is no finalizer). The connection ODX opened is closed when the read ends, however slowly the buffer is read. The producer is unbounded: it never waits for a reader, so a slow reader lets rows pile up in memory (the read finishes at full speed and frees the connection, while memory grows with the backlog), as with an unbounded channel.

Use it for a reusable, potentially indexable list to work with for a while. ToPooledBufferStream(CancellationToken) reads the elements once, through the buffers, without copying them twice; ToPooledStream(CancellationToken) is the same without buffers.

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 when the buffer, or the PooledArray<T> view of it, is disposed.