|
| 1 | +namespace Open.Database.Extensions; |
| 2 | + |
| 3 | +// What MappedQueryExtensions.Select returns: a lazy projection of another |
| 4 | +// query. It holds the source and the selector and runs nothing itself. |
| 5 | +// Each terminal hands the handler a stream of the source's rows, projected one |
| 6 | +// at a time as the handler enumerates it, so a projection of a projection |
| 7 | +// still reads through one reader. |
| 8 | +// A caller sees only IMappedQuery<T>, so this type stays internal. |
| 9 | +sealed class ProjectedQuery<TSource, T>( |
| 10 | + IMappedQuery<TSource> source, |
| 11 | + Func<TSource, T> selector) |
| 12 | + : IMappedQuery<T> |
| 13 | +{ |
| 14 | + public void Read(Action<IEnumerable<T>> handler) |
| 15 | + { |
| 16 | + if (handler is null) throw new ArgumentNullException(nameof(handler)); |
| 17 | + Contract.EndContractBlock(); |
| 18 | + |
| 19 | + source.Read(rows => handler(Project(rows, selector))); |
| 20 | + } |
| 21 | + |
| 22 | + public TResult Read<TResult>(Func<IEnumerable<T>, TResult> handler) |
| 23 | + { |
| 24 | + if (handler is null) throw new ArgumentNullException(nameof(handler)); |
| 25 | + Contract.EndContractBlock(); |
| 26 | + |
| 27 | + return source.Read(rows => handler(Project(rows, selector))); |
| 28 | + } |
| 29 | + |
| 30 | + public ValueTask ReadAsync( |
| 31 | + Func<IAsyncEnumerable<T>, CancellationToken, ValueTask> handler, |
| 32 | + CancellationToken cancellationToken) |
| 33 | + { |
| 34 | + if (handler is null) throw new ArgumentNullException(nameof(handler)); |
| 35 | + Contract.EndContractBlock(); |
| 36 | + |
| 37 | + return source.ReadAsync( |
| 38 | + (rows, token) => handler(ProjectAsync(rows, selector, token), token), |
| 39 | + cancellationToken); |
| 40 | + } |
| 41 | + |
| 42 | + public ValueTask<TResult> ReadAsync<TResult>( |
| 43 | + Func<IAsyncEnumerable<T>, CancellationToken, ValueTask<TResult>> handler, |
| 44 | + CancellationToken cancellationToken) |
| 45 | + { |
| 46 | + if (handler is null) throw new ArgumentNullException(nameof(handler)); |
| 47 | + Contract.EndContractBlock(); |
| 48 | + |
| 49 | + return source.ReadAsync( |
| 50 | + (rows, token) => handler(ProjectAsync(rows, selector, token), token), |
| 51 | + cancellationToken); |
| 52 | + } |
| 53 | + |
| 54 | + // The foreach disposes the source's enumerator when this one is disposed, |
| 55 | + // so a handler that stops early still releases the reader. |
| 56 | + static IEnumerable<T> Project( |
| 57 | + IEnumerable<TSource> rows, |
| 58 | + Func<TSource, T> selector) |
| 59 | + { |
| 60 | + foreach (TSource row in rows) |
| 61 | + { |
| 62 | + yield return selector(row); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static async IAsyncEnumerable<T> ProjectAsync( |
| 67 | + IAsyncEnumerable<TSource> rows, |
| 68 | + Func<TSource, T> selector, |
| 69 | + [EnumeratorCancellation] CancellationToken cancellationToken) |
| 70 | + { |
| 71 | + await foreach (TSource row in rows |
| 72 | + .WithCancellation(cancellationToken) |
| 73 | + .ConfigureAwait(false)) |
| 74 | + { |
| 75 | + yield return selector(row); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments