QueryCheckerVisitor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // QueryCheckerVisitor.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2010 Jérémie "Garuma" Laval
  8. //
  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:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  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
  25. // THE SOFTWARE.
  26. #if NET_4_0
  27. using System;
  28. using System.Threading;
  29. using System.Linq.Parallel.QueryNodes;
  30. namespace System.Linq.Parallel
  31. {
  32. using OptionsList = Tuple<ParallelMergeOptions?, ParallelExecutionMode?, CancellationToken?, int, CancellationTokenSource>;
  33. internal class QueryCheckerVisitor : INodeVisitor
  34. {
  35. const int minSequentialThreshold = 20;
  36. // Information gathering
  37. ParallelMergeOptions? options = null;
  38. ParallelExecutionMode? mode = null;
  39. CancellationToken? token = null;
  40. int? degreeOfParallelism = null;
  41. CancellationToken implementerToken = CancellationToken.None;
  42. int partitionCount;
  43. bool? behindOrderGuard = null;
  44. internal QueryCheckerVisitor (int partitionCount)
  45. {
  46. this.partitionCount = partitionCount;
  47. }
  48. #region INodeVisitor implementation
  49. public void Visit<T> (QueryBaseNode<T> node)
  50. {
  51. // Nothing to do atm. Later we can check if the node is a
  52. // Take or a Skip and set accordingly UseStrip
  53. }
  54. public void Visit<U, V> (QueryChildNode<U, V> node)
  55. {
  56. node.Parent.Visit (this);
  57. }
  58. public void Visit<T> (QueryOptionNode<T> node)
  59. {
  60. MergeOptions (node.GetOptions ());
  61. Visit<T, T> ((QueryChildNode<T, T>)node);
  62. }
  63. public void Visit<T> (QueryStartNode<T> node)
  64. {
  65. if (behindOrderGuard == null)
  66. behindOrderGuard = false;
  67. if (degreeOfParallelism != null)
  68. partitionCount = degreeOfParallelism.Value;
  69. int count;
  70. if ((count = node.Count) != -1 && count < minSequentialThreshold)
  71. ShouldBeSequential = true;
  72. }
  73. public void Visit<T, TParent> (QueryStreamNode<T, TParent> node)
  74. {
  75. if (node.IsIndexed)
  76. UseStrip = true;
  77. Visit<T, TParent> ((QueryChildNode<T, TParent>)node);
  78. }
  79. public void Visit<T> (QueryOrderGuardNode<T> node)
  80. {
  81. if (behindOrderGuard == null) {
  82. if (node.EnsureOrder) {
  83. behindOrderGuard = true;
  84. //UseStrip = true;
  85. } else {
  86. behindOrderGuard = false;
  87. }
  88. }
  89. Visit<T, T> ((QueryStreamNode<T, T>)node);
  90. }
  91. public void Visit<TFirst, TSecond, TResult> (QueryMuxNode<TFirst, TSecond, TResult> node)
  92. {
  93. Visit<TResult, TFirst> ((QueryChildNode<TResult, TFirst>)node);
  94. }
  95. public void Visit<T> (QueryHeadWorkerNode<T> node)
  96. {
  97. // Wouldn't it be better with standard Linq?
  98. if (node.Count.HasValue && node.Count < partitionCount)
  99. ShouldBeSequential = true;
  100. Visit<T, T> ((QueryStreamNode<T, T>)node);
  101. }
  102. #endregion
  103. internal QueryOptions Options {
  104. get {
  105. return new QueryOptions (options, mode, token == null ? CancellationToken.None : token.Value,
  106. UseStrip, behindOrderGuard, partitionCount, implementerToken, ShouldBeSequential);
  107. }
  108. }
  109. internal bool UseStrip {
  110. get;
  111. private set;
  112. }
  113. internal bool BehindOrderGuard {
  114. get {
  115. return behindOrderGuard.Value;
  116. }
  117. }
  118. internal bool ShouldBeSequential {
  119. get;
  120. private set;
  121. }
  122. void MergeOptions (OptionsList list)
  123. {
  124. if (list.Item1 != null) {
  125. if (options == null)
  126. options = list.Item1;
  127. else
  128. Throw ("WithMergeOptions");
  129. }
  130. if (list.Item2 != null) {
  131. if (mode == null)
  132. mode = list.Item2;
  133. else
  134. Throw ("WithExecutionMode");
  135. }
  136. if (list.Item3 != null) {
  137. if (token == null)
  138. token = list.Item3;
  139. else
  140. Throw ("WithCancellationToken");
  141. }
  142. if (list.Item4 != -1) {
  143. if (degreeOfParallelism == null)
  144. degreeOfParallelism = list.Item4;
  145. else
  146. Throw ("WithDegreeOfParallelism");
  147. }
  148. // That one is treated specially
  149. if (list.Item5 != null) {
  150. implementerToken = implementerToken.Chain (list.Item5);
  151. }
  152. }
  153. void Throw (string methName)
  154. {
  155. throw new InvalidOperationException ("You can't have more than one " + methName + " node in a query");
  156. }
  157. }
  158. }
  159. #endif