2 // QueryCheckerVisitor.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
29 using System.Threading;
30 using System.Linq.Parallel.QueryNodes;
32 namespace System.Linq.Parallel
34 using OptionsList = Tuple<ParallelMergeOptions?, ParallelExecutionMode?, CancellationToken?, int, CancellationTokenSource>;
36 internal class QueryCheckerVisitor : INodeVisitor
38 const int minSequentialThreshold = 20;
40 // Information gathering
41 ParallelMergeOptions? options = null;
42 ParallelExecutionMode? mode = null;
43 CancellationToken? token = null;
44 int? degreeOfParallelism = null;
45 CancellationToken implementerToken = CancellationToken.None;
48 bool? behindOrderGuard = null;
50 internal QueryCheckerVisitor (int partitionCount)
52 this.partitionCount = partitionCount;
55 #region INodeVisitor implementation
56 public void Visit (QueryBaseNode node)
58 // Nothing to do atm. Later we can check if the node is a
59 // Take or a Skip and set accordingly UseStrip
62 public void Visit (QueryChildNode node)
64 node.Parent.Visit (this);
67 public void Visit (QueryOptionNode node)
69 MergeOptions (node.GetOptions ());
71 Visit ((QueryChildNode)node);
74 public void Visit (QueryStartNode node)
76 if (behindOrderGuard == null)
77 behindOrderGuard = false;
78 if (degreeOfParallelism != null)
79 partitionCount = degreeOfParallelism.Value;
82 if ((count = node.Count) != -1 && count < minSequentialThreshold)
83 ShouldBeSequential = true;
86 public void Visit (QueryStreamNode node)
91 Visit ((QueryChildNode)node);
94 public void Visit (QueryOrderGuardNode node)
96 if (behindOrderGuard == null) {
97 if (node.EnsureOrder) {
98 behindOrderGuard = true;
101 behindOrderGuard = false;
105 Visit ((QueryStreamNode)node);
108 public void Visit (QueryMuxNode node)
110 Visit ((QueryChildNode)node);
113 public void Visit (QueryHeadWorkerNode node)
115 // Wouldn't it be better with standard Linq?
116 if (node.Count.HasValue && node.Count < partitionCount)
117 ShouldBeSequential = true;
119 Visit ((QueryStreamNode)node);
123 internal QueryOptions Options {
125 return new QueryOptions (options, mode, token == null ? CancellationToken.None : token.Value,
126 UseStrip, behindOrderGuard, partitionCount, implementerToken, ShouldBeSequential);
130 internal bool UseStrip {
135 internal bool BehindOrderGuard {
137 return behindOrderGuard.Value;
141 internal bool ShouldBeSequential {
146 void MergeOptions (OptionsList list)
148 if (list.Item1 != null) {
150 options = list.Item1;
152 Throw ("WithMergeOptions");
155 if (list.Item2 != null) {
159 Throw ("WithExecutionMode");
162 if (list.Item3 != null) {
166 Throw ("WithCancellationToken");
169 if (list.Item4 != -1) {
170 if (degreeOfParallelism == null)
171 degreeOfParallelism = list.Item4;
173 Throw ("WithDegreeOfParallelism");
176 // That one is treated specially
177 if (list.Item5 != null) {
178 implementerToken = implementerToken.Chain (list.Item5);
182 void Throw (string methName)
184 throw new InvalidOperationException ("You can't have more than one " + methName + " node in a query");