2 // ParallelEnumerator.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 using System
.Threading
;
29 using System
.Collections
;
30 using System
.Collections
.Generic
;
31 using System
.Collections
.Concurrent
;
37 internal class ParallelQueryEnumerator
<T
> : IEnumerator
<T
>
39 readonly int DefaultBufferSize
= ParallelExecuter
.GetBestWorkerNumber () * 50;
41 BlockingCollection
<T
> buffer
;
42 IEnumerator
<T
> loader
;
44 OrderingEnumerator
<T
> ordEnumerator
;
50 internal ParallelQueryEnumerator (QueryBaseNode
<T
> node
)
52 this.options
= ParallelExecuter
.CheckQuery (node
);
55 // Launch adding to the buffer asynchronously via Tasks
56 if (options
.BehindOrderGuard
.Value
) {
57 waitAction
= ParallelExecuter
.ProcessAndCallback (node
,
58 (e
, i
) => ordEnumerator
.Add (e
),
59 (i
) => ordEnumerator
.EndParticipation (),
63 waitAction
= ParallelExecuter
.ProcessAndCallback (node
,
65 buffer
.CompleteAdding
,
69 if (options
.Options
.HasValue
&& options
.Options
.Value
== ParallelMergeOptions
.FullyBuffered
)
75 if (!options
.BehindOrderGuard
.Value
) {
76 if (options
.Options
.HasValue
&& (options
.Options
.Value
== ParallelMergeOptions
.NotBuffered
77 || options
.Options
.Value
== ParallelMergeOptions
.FullyBuffered
)) {
78 buffer
= new BlockingCollection
<T
> ();
80 buffer
= new BlockingCollection
<T
> (DefaultBufferSize
);
83 IEnumerable
<T
> source
= buffer
.GetConsumingEnumerable (options
.Token
);
85 loader
= source
.GetEnumerator ();
87 loader
= ordEnumerator
= new OrderingEnumerator
<T
> (options
.PartitionCount
);
91 public void Dispose ()
98 throw new NotSupportedException ();
101 public bool MoveNext ()
103 // If there are no stuff in the buffer
104 // but CompleteAdding hasn't been called,
105 // MoveNext blocks until further results are produced
106 if (!loader
.MoveNext ())
109 current
= loader
.Current
;
119 object IEnumerator
.Current
{