1 // BatchedJoinBlock.cs
3 // Copyright (c) 2012 Petr Onderka
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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
25 using System.Collections.Generic;
27 namespace System.Threading.Tasks.Dataflow {
28 public sealed class BatchedJoinBlock<T1, T2, T3> :
29 IReceivableSourceBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>> {
30 GroupingDataflowBlockOptions options;
32 CompletionHelper completionHelper = CompletionHelper.GetNew ();
33 readonly MessageOutgoingQueue<Tuple<IList<T1>, IList<T2>, IList<T3>>> outgoing;
35 readonly MessageVault<Tuple<IList<T1>, IList<T2>, IList<T3>>> vault =
36 new MessageVault<Tuple<IList<T1>, IList<T2>, IList<T3>>> ();
38 readonly TargetBuffer<Tuple<IList<T1>, IList<T2>, IList<T3>>> targets =
39 new TargetBuffer<Tuple<IList<T1>, IList<T2>, IList<T3>>> ();
41 DataflowMessageHeader headers;
44 readonly JoinTarget<T1> target1;
45 readonly JoinTarget<T2> target2;
46 readonly JoinTarget<T3> target3;
50 public BatchedJoinBlock (int batchSize)
51 : this (batchSize, GroupingDataflowBlockOptions.Default)
55 public BatchedJoinBlock (int batchSize,
56 GroupingDataflowBlockOptions dataflowBlockOptions)
59 throw new ArgumentOutOfRangeException (
60 "batchSize", batchSize, "The batchSize must be positive.");
61 if (dataflowBlockOptions == null)
62 throw new ArgumentNullException ("dataflowBlockOptions");
64 BatchSize = batchSize;
65 options = dataflowBlockOptions;
67 target1 = new JoinTarget<T1> (
68 this, SignalTarget, completionHelper, () => outgoing.IsCompleted);
69 target2 = new JoinTarget<T2> (
70 this, SignalTarget, completionHelper, () => outgoing.IsCompleted);
71 target3 = new JoinTarget<T3>(
72 this, SignalTarget, completionHelper, () => outgoing.IsCompleted);
74 outgoing = new MessageOutgoingQueue<Tuple<IList<T1>, IList<T2>, IList<T3>>> (
76 () => target1.Buffer.IsCompleted || target2.Buffer.IsCompleted || target3.Buffer.IsCompleted);
79 public int BatchSize { get; private set; }
81 public ITargetBlock<T1> Target1 {
82 get { return target1; }
85 public ITargetBlock<T2> Target2 {
86 get { return target2; }
89 public ITargetBlock<T3> Target3 {
90 get { return target3; }
95 int current = Interlocked.Increment (ref batchCount);
97 if (current % BatchSize != 0)
100 Interlocked.Add (ref batchCount, -current);
102 MakeBatch (BatchSize);
105 void MakeBatch (int batchSize)
107 var list1 = new List<T1> ();
108 var list2 = new List<T2> ();
109 var list3 = new List<T3> ();
111 // lock is necessary here to make sure items are in the correct order
114 batchLock.Enter (ref taken);
119 while (i < batchSize && target1.Buffer.TryTake (out item1)) {
125 while (i < batchSize && target2.Buffer.TryTake (out item2)) {
131 while (i < batchSize && target3.Buffer.TryTake (out item3)) {
137 throw new InvalidOperationException ("Unexpected count of items.");
143 var batch = Tuple.Create<IList<T1>, IList<T2>, IList<T3>> (list1, list2,
146 var target = targets.Current;
148 outgoing.AddData (batch);
150 target.OfferMessage (headers.Increment (), batch, this, false);
152 if (!outgoing.IsEmpty && targets.Current != null)
153 outgoing.ProcessForTarget (targets.Current, this, false, ref headers);
156 public Task Completion
158 get { return completionHelper.Completion; }
161 public void Complete ()
163 outgoing.Complete ();
166 void IDataflowBlock.Fault (Exception exception)
168 completionHelper.Fault (exception);
171 Tuple<IList<T1>, IList<T2>, IList<T3>>
172 ISourceBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>>.ConsumeMessage (
173 DataflowMessageHeader messageHeader,
174 ITargetBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>> target,
175 out bool messageConsumed)
177 return vault.ConsumeMessage (messageHeader, target, out messageConsumed);
180 public IDisposable LinkTo (
181 ITargetBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>> target,
184 var result = targets.AddTarget (target, unlinkAfterOne);
185 outgoing.ProcessForTarget (target, this, false, ref headers);
189 void ISourceBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>>.ReleaseReservation (
190 DataflowMessageHeader messageHeader,
191 ITargetBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>> target)
193 vault.ReleaseReservation (messageHeader, target);
196 bool ISourceBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>>.ReserveMessage (
197 DataflowMessageHeader messageHeader,
198 ITargetBlock<Tuple<IList<T1>, IList<T2>, IList<T3>>> target)
200 return vault.ReserveMessage (messageHeader, target);
203 public bool TryReceive (
204 Predicate<Tuple<IList<T1>, IList<T2>, IList<T3>>> filter,
205 out Tuple<IList<T1>, IList<T2>, IList<T3>> item)
207 return outgoing.TryReceive (filter, out item);
210 public bool TryReceiveAll (
211 out IList<Tuple<IList<T1>, IList<T2>, IList<T3>>> items)
213 return outgoing.TryReceiveAll (out items);