Path: redb-route/redb.Route.Sql/Batch/BatchItemReader.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Sql/Batch/BatchItemReader.cs ---- using System.Collections; using System.Runtime.CompilerServices; namespace redb.Route.Sql.Batch; /// /// Reads the items of a batch source one at a time, whatever the source is — a list, a lazy sequence, an asynchronous stream. /// Nothing is read ahead of what the writer asks for, except the first item, which the processor reads before it opens a /// connection so that an empty source costs none. A failure of the source itself (not of an item's statement) ends the batch: /// it is reported with the number of items read before it. /// internal sealed class BatchItemReader : IAsyncDisposable { private readonly IAsyncEnumerator _enumerator; private readonly CancellationToken _ct; private bool _pending; private BatchItemReader(IAsyncEnumerator enumerator, CancellationToken ct) { _enumerator = enumerator; _ct = ct; } /// Items read from the source so far. internal int ItemsRead { get; private set; } /// Zero-based index of . internal int Index => ItemsRead - 1; /// The item last read. internal object? Current => _enumerator.Current; /// A reader over an asynchronous source. internal static BatchItemReader Over(IAsyncEnumerable source, CancellationToken ct) => new(source.GetAsyncEnumerator(ct), ct); /// A reader over a synchronous source. internal static BatchItemReader Over(IEnumerable source, CancellationToken ct) => new(Enumerate(source, ct).GetAsyncEnumerator(ct), ct); /// Reads the first item and keeps it for the first ; false for an empty source. /// The source failed. internal async ValueTask HasItemsAsync() { _pending = await MoveAsync().ConfigureAwait(false); return _pending; } /// Advances to the next item; false when the source is exhausted. /// The source failed. internal async ValueTask ReadAsync() { if (_pending) { _pending = false; return true; } return await MoveAsync().ConfigureAwait(false); } /// public ValueTask DisposeAsync() => _enumerator.DisposeAsync(); private async ValueTask MoveAsync() { bool moved; try { moved = await _enumerator.MoveNextAsync().ConfigureAwait(false); } catch (Exception ex) when (ex is not OperationCanceledException && !_ct.IsCancellationRequested) { // The source's own failure: the batch ends at the item that could not be read. Under a cancellation the source's // exception (a driver answering the stop in its own way) goes up as is, as the writers' do. throw new BatchItemFailedException(ItemsRead, ex); } if (moved) ItemsRead++; return moved; } private static async IAsyncEnumerable Enumerate(IEnumerable source, [EnumeratorCancellation] CancellationToken ct) { foreach (var item in source) { ct.ThrowIfCancellationRequested(); yield return item; } await Task.CompletedTask.ConfigureAwait(false); } }