|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.Contracts; |
| 4 | +using System.Threading.Channels; |
| 5 | + |
| 6 | +namespace Open.ChannelExtensions |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A ChannelReader that batches results. |
| 10 | + /// Use the .Batch extension instead of constructing this directly. |
| 11 | + /// </summary> |
| 12 | + public class BatchingChannelReader<T> : BufferingChannelReader<T, List<T>> |
| 13 | + { |
| 14 | + private readonly int _batchSize; |
| 15 | + private List<T>? _current; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Constructs a BatchingChannelReader. |
| 19 | + /// Use the .Batch extension instead of constructing this directly. |
| 20 | + /// </summary> |
| 21 | + public BatchingChannelReader(ChannelReader<T> source, int batchSize, bool singleReader, bool syncCont = false) : base(source, singleReader, syncCont) |
| 22 | + { |
| 23 | + if (batchSize < 1) throw new ArgumentOutOfRangeException(nameof(batchSize), batchSize, "Must be at least 1."); |
| 24 | + Contract.EndContractBlock(); |
| 25 | + |
| 26 | + _batchSize = batchSize; |
| 27 | + _current = source.Completion.IsCompleted ? null : new List<T>(batchSize); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// If no full batch is waiting, will force buffering any batch that has at least one item. |
| 32 | + /// Returns true if anything was added to the buffer. |
| 33 | + /// </summary> |
| 34 | + public bool ForceBatch() |
| 35 | + { |
| 36 | + if (Buffer == null || Buffer.Reader.Completion.IsCompleted) return false; |
| 37 | + if (TryPipeItems()) return true; |
| 38 | + |
| 39 | + lock (Buffer) |
| 40 | + { |
| 41 | + if (Buffer.Reader.Completion.IsCompleted) return false; |
| 42 | + if (TryPipeItems()) return true; |
| 43 | + var c = _current; |
| 44 | + if (c == null || c.Count == 0 || Buffer.Reader.Completion.IsCompleted) |
| 45 | + return false; |
| 46 | + c.TrimExcess(); |
| 47 | + _current = new List<T>(_batchSize); |
| 48 | + Buffer.Writer.TryWrite(c); |
| 49 | + } |
| 50 | + |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + /// <inheritdoc /> |
| 55 | + protected override bool TryPipeItems() |
| 56 | + { |
| 57 | + if (_current == null || Buffer == null || Buffer.Reader.Completion.IsCompleted) |
| 58 | + return false; |
| 59 | + |
| 60 | + lock (Buffer) |
| 61 | + { |
| 62 | + var c = _current; |
| 63 | + if (c == null || Buffer.Reader.Completion.IsCompleted) |
| 64 | + return false; |
| 65 | + |
| 66 | + var source = Source; |
| 67 | + if (source == null || source.Completion.IsCompleted) |
| 68 | + { |
| 69 | + // All finished, release the last batch to the buffer. |
| 70 | + c.TrimExcess(); |
| 71 | + _current = null; |
| 72 | + if (c.Count == 0) |
| 73 | + return false; |
| 74 | + |
| 75 | + Buffer.Writer.TryWrite(c); |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + while (source.TryRead(out T item)) |
| 80 | + { |
| 81 | + if (c.Count == _batchSize) |
| 82 | + { |
| 83 | + _current = new List<T>(_batchSize) { item }; |
| 84 | + Buffer.Writer.TryWrite(c); |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + c.Add(item); |
| 89 | + } |
| 90 | + |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments