QueryCheckerVisitor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. using System;
  27. using System.Threading;
  28. using System.Linq.Parallel.QueryNodes;
  29. namespace System.Linq.Parallel
  30. {
  31. using OptionsList = Tuple<ParallelMergeOptions?, ParallelExecutionMode?, CancellationToken?, int, CancellationTokenSource>;
  32. internal class QueryCheckerVisitor : INodeVisitor
  33. {
  34. const int minSequentialThreshold = 20;
  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 (QueryBaseNode 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 (QueryChildNode node)
  54. {
  55. node.Parent.Visit (this);
  56. }
  57. public void Visit (QueryOptionNode node)
  58. {
  59. MergeOptions (node.GetOptions ());
  60. Visit ((QueryChildNode)node);
  61. }
  62. public void Visit (QueryStartNode node)
  63. {
  64. if (behindOrderGuard == null)
  65. behindOrderGuard = false;
  66. if (degreeOfParallelism != null)
  67. partitionCount = degreeOfParallelism.Value;
  68. int count;
  69. if ((count = node.Count) != -1 && count < minSequentialThreshold)
  70. ShouldBeSequential = true;
  71. }
  72. public void Visit (QueryStreamNode node)
  73. {
  74. if (node.IsIndexed)
  75. UseStrip = true;
  76. Visit ((QueryChildNode)node);
  77. }
  78. public void Visit (QueryOrderGuardNode node)
  79. {
  80. if (behindOrderGuard == null) {
  81. if (node.EnsureOrder) {
  82. behindOrderGuard = true;
  83. //UseStrip = true;
  84. } else {
  85. behindOrderGuard = false;
  86. }
  87. }
  88. Visit ((QueryStreamNode)node);
  89. }
  90. public void Visit (QueryMuxNode node)
  91. {
  92. Visit ((QueryChildNode)node);
  93. }
  94. public void Visit (QueryHeadWorkerNode node)
  95. {
  96. // Wouldn't it be better with standard Linq?
  97. if (node.Count.HasValue && node.Count < partitionCount)
  98. ShouldBeSequential = true;
  99. Visit ((QueryStreamNode)node);
  100. }
  101. #endregion
  102. internal QueryOptions Options {
  103. get {
  104. return new QueryOptions (options, mode, token == null ? CancellationToken.None : token.Value,
  105. UseStrip, behindOrderGuard, partitionCount, implementerToken, ShouldBeSequential);
  106. }
  107. }
  108. internal bool UseStrip {
  109. get;
  110. private set;
  111. }
  112. internal bool BehindOrderGuard {
  113. get {
  114. return behindOrderGuard.Value;
  115. }
  116. }
  117. internal bool ShouldBeSequential {
  118. get;
  119. private set;
  120. }
  121. void MergeOptions (OptionsList list)
  122. {
  123. if (list.Item1 != null) {
  124. if (options == null)
  125. options = list.Item1;
  126. else
  127. Throw ("WithMergeOptions");
  128. }
  129. if (list.Item2 != null) {
  130. if (mode == null)
  131. mode = list.Item2;
  132. else
  133. Throw ("WithExecutionMode");
  134. }
  135. if (list.Item3 != null) {
  136. if (token == null)
  137. token = list.Item3;
  138. else
  139. Throw ("WithCancellationToken");
  140. }
  141. if (list.Item4 != -1) {
  142. if (degreeOfParallelism == null)
  143. degreeOfParallelism = list.Item4;
  144. else
  145. Throw ("WithDegreeOfParallelism");
  146. }
  147. // That one is treated specially
  148. if (list.Item5 != null) {
  149. implementerToken = implementerToken.Chain (list.Item5);
  150. }
  151. }
  152. void Throw (string methName)
  153. {
  154. throw new InvalidOperationException ("You can't have more than one " + methName + " node in a query");
  155. }
  156. }
  157. }