TransformManyBlock.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // TransformManyBlock.cs
  2. //
  3. // Copyright (c) 2011 Jérémie "garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using System.Threading.Tasks;
  26. using System.Collections.Generic;
  27. using System.Collections.Concurrent;
  28. namespace System.Threading.Tasks.Dataflow
  29. {
  30. public sealed class TransformManyBlock<TInput, TOutput> :
  31. IPropagatorBlock<TInput, TOutput>, ITargetBlock<TInput>, IDataflowBlock, ISourceBlock<TOutput>, IReceivableSourceBlock<TOutput>
  32. {
  33. static readonly ExecutionDataflowBlockOptions defaultOptions = new ExecutionDataflowBlockOptions ();
  34. CompletionHelper compHelper;
  35. BlockingCollection<TInput> messageQueue = new BlockingCollection<TInput> ();
  36. MessageBox<TInput> messageBox;
  37. MessageVault<TOutput> vault;
  38. ExecutionDataflowBlockOptions dataflowBlockOptions;
  39. readonly Func<TInput, IEnumerable<TOutput>> transformer;
  40. MessageOutgoingQueue<TOutput> outgoing;
  41. TargetBuffer<TOutput> targets = new TargetBuffer<TOutput> ();
  42. DataflowMessageHeader headers = DataflowMessageHeader.NewValid ();
  43. public TransformManyBlock (Func<TInput, IEnumerable<TOutput>> transformer) : this (transformer, defaultOptions)
  44. {
  45. }
  46. public TransformManyBlock (Func<TInput, IEnumerable<TOutput>> transformer, ExecutionDataflowBlockOptions dataflowBlockOptions)
  47. {
  48. if (dataflowBlockOptions == null)
  49. throw new ArgumentNullException ("dataflowBlockOptions");
  50. this.transformer = transformer;
  51. this.dataflowBlockOptions = dataflowBlockOptions;
  52. this.compHelper = CompletionHelper.GetNew (dataflowBlockOptions);
  53. this.messageBox = new ExecutingMessageBox<TInput> (
  54. messageQueue, compHelper,
  55. () => outgoing.IsCompleted, TransformProcess, () => outgoing.Complete (),
  56. dataflowBlockOptions);
  57. this.outgoing = new MessageOutgoingQueue<TOutput> (compHelper, () => messageQueue.IsCompleted);
  58. this.vault = new MessageVault<TOutput> ();
  59. }
  60. public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
  61. TInput messageValue,
  62. ISourceBlock<TInput> source,
  63. bool consumeToAccept)
  64. {
  65. return messageBox.OfferMessage (this, messageHeader, messageValue, source, consumeToAccept);
  66. }
  67. public IDisposable LinkTo (ITargetBlock<TOutput> target, bool unlinkAfterOne)
  68. {
  69. var result = targets.AddTarget (target, unlinkAfterOne);
  70. outgoing.ProcessForTarget (target, this, false, ref headers);
  71. return result;
  72. }
  73. public TOutput ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<TOutput> target, out bool messageConsumed)
  74. {
  75. return vault.ConsumeMessage (messageHeader, target, out messageConsumed);
  76. }
  77. public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<TOutput> target)
  78. {
  79. vault.ReleaseReservation (messageHeader, target);
  80. }
  81. public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<TOutput> target)
  82. {
  83. return vault.ReserveMessage (messageHeader, target);
  84. }
  85. public bool TryReceive (Predicate<TOutput> filter, out TOutput item)
  86. {
  87. return outgoing.TryReceive (filter, out item);
  88. }
  89. public bool TryReceiveAll (out IList<TOutput> items)
  90. {
  91. return outgoing.TryReceiveAll (out items);
  92. }
  93. bool TransformProcess ()
  94. {
  95. ITargetBlock<TOutput> target;
  96. TInput input;
  97. var dequeued = messageQueue.TryTake (out input);
  98. if (dequeued) {
  99. var result = transformer (input);
  100. if (result != null) {
  101. foreach (var item in result) {
  102. if ((target = targets.Current) != null)
  103. target.OfferMessage (headers.Increment (), item, this, false);
  104. else
  105. outgoing.AddData (item);
  106. }
  107. }
  108. }
  109. if (!outgoing.IsEmpty && (target = targets.Current) != null)
  110. outgoing.ProcessForTarget (target, this, false, ref headers);
  111. return dequeued;
  112. }
  113. public void Complete ()
  114. {
  115. messageBox.Complete ();
  116. }
  117. public void Fault (Exception ex)
  118. {
  119. compHelper.RequestFault (ex);
  120. }
  121. public Task Completion {
  122. get {
  123. return compHelper.Completion;
  124. }
  125. }
  126. public int OutputCount {
  127. get {
  128. return outgoing.Count;
  129. }
  130. }
  131. public int InputCount {
  132. get {
  133. return messageQueue.Count;
  134. }
  135. }
  136. public override string ToString ()
  137. {
  138. return NameHelper.GetName (this, dataflowBlockOptions);
  139. }
  140. }
  141. }