| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using System.Threading.Tasks.Dataflow; |
| | | 6 | | |
| | | 7 | | namespace CounterpointCollective.Dataflow |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Block means not to accept messages if the predicate does not hold. |
| | | 11 | | /// Drop means the messages are accepted (and disappear) if the predicate does not hold. |
| | | 12 | | /// </summary> |
| | | 13 | | public enum FilterMode |
| | | 14 | | { |
| | | 15 | | Block, |
| | | 16 | | Drop |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | public sealed class SynchronousFilterBlock<T> : IReceivableSourceBlock<T> |
| | | 20 | | { |
| | | 21 | | |
| | | 22 | | private readonly ISourceBlock<T> _sourceBlock; |
| | 152647 | 23 | | private Predicate<T> Predicate { get; } |
| | | 24 | | private readonly DummyTarget _target; |
| | 4214 | 25 | | private object OutgoingLock { get; } = new(); |
| | | 26 | | private ITargetBlock<T>? reservationTarget; |
| | 52311 | 27 | | public FilterMode Mode { get; set; } |
| | | 28 | | |
| | 36 | 29 | | public SynchronousFilterBlock(ISourceBlock<T> sourceBlock, Predicate<T> predicate, FilterMode mode = FilterMode. |
| | | 30 | | { |
| | 36 | 31 | | Mode = mode; |
| | 36 | 32 | | _sourceBlock = sourceBlock; |
| | 36 | 33 | | _target = new(this); |
| | 36 | 34 | | Predicate = predicate; |
| | 36 | 35 | | } |
| | | 36 | | |
| | | 37 | | public IDisposable LinkTo(ITargetBlock<T> target, DataflowLinkOptions linkOptions) |
| | | 38 | | { |
| | 31 | 39 | | var t = new LinkTarget(this, target, linkOptions.MaxMessages); |
| | 31 | 40 | | return _sourceBlock.LinkTo(t, new() { PropagateCompletion = linkOptions.PropagateCompletion, Append = linkOp |
| | | 41 | | } |
| | | 42 | | |
| | 5 | 43 | | public Task Completion => _sourceBlock.Completion; |
| | | 44 | | |
| | 0 | 45 | | public void Complete() => _sourceBlock.Complete(); |
| | | 46 | | |
| | 0 | 47 | | public void Fault(Exception exception) => _sourceBlock.Fault(exception); |
| | | 48 | | |
| | | 49 | | public T? ConsumeMessage( |
| | | 50 | | DataflowMessageHeader messageHeader, |
| | | 51 | | ITargetBlock<T> target, |
| | | 52 | | out bool messageConsumed |
| | | 53 | | ) |
| | | 54 | | { |
| | 4171 | 55 | | messageConsumed = false; |
| | 4171 | 56 | | T? ret = default; |
| | 4171 | 57 | | lock (OutgoingLock) |
| | | 58 | | { |
| | 4171 | 59 | | if (reservationTarget == null || reservationTarget == target) |
| | | 60 | | { |
| | 4171 | 61 | | ret = _sourceBlock.ConsumeMessage(messageHeader, _target, out messageConsumed); |
| | 4171 | 62 | | reservationTarget = null; |
| | | 63 | | } |
| | 4171 | 64 | | } |
| | 4171 | 65 | | return ret; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public void ReleaseReservation( |
| | | 69 | | DataflowMessageHeader messageHeader, |
| | | 70 | | ITargetBlock<T> target |
| | | 71 | | ) |
| | | 72 | | { |
| | 1 | 73 | | lock (OutgoingLock) |
| | | 74 | | { |
| | 1 | 75 | | if (reservationTarget == target) |
| | | 76 | | { |
| | 1 | 77 | | _sourceBlock.ReleaseReservation(messageHeader, _target); |
| | 1 | 78 | | reservationTarget = null; |
| | | 79 | | } |
| | 1 | 80 | | } |
| | 1 | 81 | | } |
| | | 82 | | |
| | | 83 | | public bool ReserveMessage( |
| | | 84 | | DataflowMessageHeader messageHeader, |
| | | 85 | | ITargetBlock<T> target |
| | | 86 | | ) |
| | | 87 | | { |
| | 6 | 88 | | lock (OutgoingLock) |
| | | 89 | | { |
| | 6 | 90 | | if (reservationTarget == null || reservationTarget == target) |
| | | 91 | | { |
| | 5 | 92 | | var res = _sourceBlock.ReserveMessage(messageHeader, _target); |
| | 5 | 93 | | if (res) |
| | | 94 | | { |
| | 5 | 95 | | reservationTarget = target; |
| | | 96 | | } |
| | 5 | 97 | | return res; |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | 1 | 101 | | return false; //other target already holds a reservation. |
| | | 102 | | } |
| | | 103 | | } |
| | 6 | 104 | | } |
| | | 105 | | |
| | | 106 | | public bool TryReceive(Predicate<T>? filter, [MaybeNullWhen(false)] out T item) |
| | | 107 | | { |
| | 8 | 108 | | if (_sourceBlock is IReceivableSourceBlock<T> receivableSource && |
| | 0 | 109 | | receivableSource.TryReceive((filter == null) ? Predicate : i => Predicate(i) && filter(i), out item) |
| | 8 | 110 | | ) |
| | | 111 | | { |
| | 3 | 112 | | return true; |
| | | 113 | | } |
| | | 114 | | else |
| | | 115 | | { |
| | 5 | 116 | | item = default; |
| | 5 | 117 | | return false; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public bool TryReceiveAll([NotNullWhen(true)] out IList<T>? items) |
| | | 122 | | { |
| | 4 | 123 | | List<T> l = []; |
| | 6 | 124 | | while(TryReceive(null, out var item)) |
| | | 125 | | { |
| | 2 | 126 | | l.Add(item); |
| | 2 | 127 | | } |
| | | 128 | | |
| | 4 | 129 | | if (l.Count > 0) |
| | | 130 | | { |
| | 2 | 131 | | items = l; |
| | 2 | 132 | | return true; |
| | | 133 | | } |
| | | 134 | | else |
| | | 135 | | { |
| | 2 | 136 | | items = null; |
| | 2 | 137 | | return false; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | 31 | 141 | | private sealed class LinkTarget(SynchronousFilterBlock<T> outer, ITargetBlock<T> target, int maxMessages) : ITar |
| | | 142 | | { |
| | 0 | 143 | | public Task Completion => target.Completion; |
| | | 144 | | |
| | 17 | 145 | | public void Complete() => target.Complete(); |
| | 8 | 146 | | public void Fault(Exception exception) => target.Fault(exception); |
| | | 147 | | public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, T messageValue, ISourceBlock< |
| | | 148 | | { |
| | 152639 | 149 | | if (maxMessages == 0) |
| | | 150 | | { |
| | 0 | 151 | | return DataflowMessageStatus.DecliningPermanently; |
| | 152639 | 152 | | } else if (!outer.Predicate(messageValue)) |
| | | 153 | | { |
| | 52275 | 154 | | return outer.Mode == FilterMode.Drop ? DataflowMessageStatus.Accepted : DataflowMessageStatus.Declin |
| | | 155 | | } |
| | | 156 | | else |
| | | 157 | | { |
| | 100364 | 158 | | var ret = target.OfferMessage(messageHeader, messageValue, outer, consumeToAccept); |
| | 100364 | 159 | | if (ret == DataflowMessageStatus.Accepted && maxMessages > 0) |
| | | 160 | | { |
| | 0 | 161 | | maxMessages--; |
| | | 162 | | } |
| | 100364 | 163 | | return ret; |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 36 | 168 | | private sealed class DummyTarget(SynchronousFilterBlock<T> outer) : ITargetBlock<T> |
| | | 169 | | { |
| | 0 | 170 | | public Task Completion => outer.Completion; |
| | 0 | 171 | | public void Complete() => outer.Complete(); |
| | 0 | 172 | | public void Fault(Exception exception) => outer.Fault(exception); |
| | | 173 | | public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, T messageValue, ISourceBlock< |
| | 0 | 174 | | => DataflowMessageStatus.Postponed; |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | } |