| | | 1 | | using CounterpointCollective.Dataflow.Encapsulation; |
| | | 2 | | using CounterpointCollective.Dataflow.Fluent; |
| | | 3 | | using CounterpointCollective.Dataflow.Internal; |
| | | 4 | | using System.Threading.Tasks.Dataflow; |
| | | 5 | | |
| | | 6 | | namespace CounterpointCollective.Dataflow |
| | | 7 | | { |
| | | 8 | | public class GroupAdjacentBlock<T, K, V> : AbstractEncapsulatedPropagatorBlock<T, IGrouping<K, V>> |
| | | 9 | | { |
| | 316 | 10 | | private sealed class Grouping(K key, IEnumerable<V> values) : IGrouping<K, V> |
| | | 11 | | { |
| | 316 | 12 | | private readonly K _key = key; |
| | 316 | 13 | | private readonly IEnumerable<V> _values = [..values]; |
| | | 14 | | |
| | 677 | 15 | | public K Key => _key; |
| | 628 | 16 | | public IEnumerator<V> GetEnumerator() => _values.GetEnumerator(); |
| | 0 | 17 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | private readonly BoundedPropagatorBlock<T, IGrouping<K, V>> _boundedPropagatorBlock; |
| | | 21 | | |
| | | 22 | | /// <exclude/> |
| | 381 | 23 | | protected override ITargetBlock<T> TargetSide => _boundedPropagatorBlock; |
| | | 24 | | |
| | | 25 | | /// <exclude/> |
| | 404 | 26 | | protected override ISourceBlock<IGrouping<K, V>> SourceSide => _boundedPropagatorBlock; |
| | | 27 | | |
| | 1 | 28 | | public int Count => _boundedPropagatorBlock.Count; |
| | | 29 | | |
| | 14 | 30 | | public GroupAdjacentBlock( |
| | 14 | 31 | | Func<T, K> keySelector, |
| | 14 | 32 | | Func<T, V> valueSelector, |
| | 14 | 33 | | ExecutionDataflowBlockOptions options, |
| | 14 | 34 | | bool flushOnIdle = false |
| | 14 | 35 | | ) |
| | | 36 | | { |
| | 14 | 37 | | var inputCount = 0; |
| | | 38 | | |
| | 14 | 39 | | K currentKey = default!; |
| | 14 | 40 | | List<V> currentGroup = []; |
| | | 41 | | |
| | 14 | 42 | | var outputBlock = new BufferBlock<IGrouping<K, V>>(new() { CancellationToken = options.CancellationToken }); |
| | | 43 | | |
| | | 44 | | void EmitCurrentGrouping() |
| | | 45 | | { |
| | | 46 | | var g = new Grouping(currentKey, currentGroup); |
| | | 47 | | outputBlock.PostAsserted(g); |
| | | 48 | | currentGroup.Clear(); |
| | | 49 | | } |
| | | 50 | | |
| | 14 | 51 | | var a = |
| | 14 | 52 | | new ActionBlock<T>(e => |
| | 14 | 53 | | { |
| | 369 | 54 | | var k = keySelector(e); |
| | 369 | 55 | | var v = valueSelector(e); |
| | 369 | 56 | | if (currentGroup.Count > 0 && !EqualityComparer<K>.Default.Equals(currentKey, k)) |
| | 14 | 57 | | { |
| | 135 | 58 | | EmitCurrentGrouping(); |
| | 14 | 59 | | } |
| | 369 | 60 | | currentKey = k; |
| | 369 | 61 | | currentGroup.Add(v); |
| | 369 | 62 | | if (flushOnIdle && Interlocked.Decrement(ref inputCount) == 0) |
| | 14 | 63 | | { |
| | 180 | 64 | | EmitCurrentGrouping(); |
| | 14 | 65 | | } |
| | 383 | 66 | | }, new() { CancellationToken = options.CancellationToken, SingleProducerConstrained = options.SingleProd |
| | | 67 | | |
| | 14 | 68 | | a.Completion.ContinueWith(t => |
| | 14 | 69 | | { |
| | 13 | 70 | | if (t.IsCompletedSuccessfully) |
| | 14 | 71 | | { |
| | 7 | 72 | | if (currentGroup.Count > 0) |
| | 14 | 73 | | { |
| | 1 | 74 | | EmitCurrentGrouping(); |
| | 14 | 75 | | } |
| | 7 | 76 | | outputBlock.Complete(); |
| | 14 | 77 | | } |
| | 14 | 78 | | else |
| | 14 | 79 | | { |
| | 6 | 80 | | currentGroup.Clear(); |
| | 6 | 81 | | if (t.IsFaulted) |
| | 14 | 82 | | { |
| | 4 | 83 | | ((IDataflowBlock)outputBlock).Fault(t.Exception!); |
| | 14 | 84 | | } |
| | 14 | 85 | | } |
| | 20 | 86 | | }); |
| | | 87 | | |
| | 14 | 88 | | _boundedPropagatorBlock = new BoundedPropagatorBlock<T, IGrouping<K, V>>( |
| | 14 | 89 | | a, |
| | 14 | 90 | | outputBlock, |
| | 14 | 91 | | options.BoundedCapacity, |
| | 369 | 92 | | onEntering: () => Interlocked.Increment(ref inputCount) |
| | 14 | 93 | | ); |
| | 14 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |