QueryCheckerVisitor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Information gathering
  36. ParallelMergeOptions? options = null;
  37. ParallelExecutionMode? mode = null;
  38. CancellationToken? token = null;
  39. int? degreeOfParallelism = null;
  40. CancellationToken implementerToken = CancellationToken.None;
  41. int partitionCount;
  42. bool? behindOrderGuard = null;
  43. internal QueryCheckerVisitor (int partitionCount)
  44. {
  45. this.partitionCount = partitionCount;
  46. }
  47. #region INodeVisitor implementation
  48. public void Visit<T> (QueryBaseNode<T> node)
  49. {
  50. // Nothing to do atm. Later we can check if the node is a
  51. // Take or a Skip and set accordingly UseStrip
  52. }
  53. public void Visit<U, V> (QueryChildNode<U, V> node)
  54. {
  55. node.Parent.Visit (this);
  56. }
  57. public void Visit<T> (QueryOptionNode<T> node)
  58. {
  59. MergeOptions (node.GetOptions ());
  60. Visit<T, T> ((QueryChildNode<T, T>)node);
  61. }
  62. public void Visit<T> (QueryStartNode<T> node)
  63. {
  64. if (behindOrderGuard == null)
  65. behindOrderGuard = false;
  66. if (degreeOfParallelism != null)
  67. partitionCount = degreeOfParallelism.Value;
  68. }
  69. public void Visit<T, TParent> (QueryStreamNode<T, TParent> node)
  70. {
  71. if (node.IsIndexed)
  72. UseStrip = true;
  73. Visit<T, TParent> ((QueryChildNode<T, TParent>)node);
  74. }
  75. public void Visit<T> (QueryOrderGuardNode<T> node)
  76. {
  77. if (behindOrderGuard == null) {
  78. if (node.EnsureOrder) {
  79. behindOrderGuard = true;
  80. //UseStrip = true;
  81. } else {
  82. behindOrderGuard = false;
  83. }
  84. }
  85. Visit<T, T> ((QueryStreamNode<T, T>)node);
  86. }
  87. public void Visit<TFirst, TSecond, TResult> (QueryMuxNode<TFirst, TSecond, TResult> node)
  88. {
  89. Visit<TResult, TFirst> ((QueryChildNode<TResult, TFirst>)node);
  90. }
  91. public void Visit<T> (QueryHeadWorkerNode<T> node)
  92. {
  93. // Wouldn't it be better with standard Linq?
  94. if (node.Count.HasValue && node.Count < partitionCount)
  95. ShouldBeSequential = true;
  96. Visit<T, T> ((QueryStreamNode<T, T>)node);
  97. }
  98. #endregion
  99. internal QueryOptions Options {
  100. get {
  101. return new QueryOptions (options, mode, token == null ? CancellationToken.None : token.Value,
  102. UseStrip, behindOrderGuard, partitionCount, implementerToken);
  103. }
  104. }
  105. internal bool UseStrip {
  106. get;
  107. private set;
  108. }
  109. internal bool BehindOrderGuard {
  110. get {
  111. return behindOrderGuard.Value;
  112. }
  113. }
  114. internal bool ShouldBeSequential {
  115. get;
  116. private set;
  117. }
  118. void MergeOptions (OptionsList list)
  119. {
  120. if (list.Item1 != null) {
  121. if (options == null)
  122. options = list.Item1;
  123. else
  124. Throw ("WithMergeOptions");
  125. }
  126. if (list.Item2 != null) {
  127. if (mode == null)
  128. mode = list.Item2;
  129. else
  130. Throw ("WithExecutionMode");
  131. }
  132. if (list.Item3 != null) {
  133. if (token == null)
  134. token = list.Item3;
  135. else
  136. Throw ("WithCancellationToken");
  137. }
  138. if (list.Item4 != -1) {
  139. if (degreeOfParallelism == null)
  140. degreeOfParallelism = list.Item4;
  141. else
  142. Throw ("WithDegreeOfParallelism");
  143. }
  144. // That one is treated specially
  145. if (list.Item5 != null) {
  146. implementerToken = implementerToken.Chain (list.Item5);
  147. }
  148. }
  149. void Throw (string methName)
  150. {
  151. throw new InvalidOperationException ("You can't have more than one " + methName + " node in a query");
  152. }
  153. }
  154. }
  155. #endif