QueryCheckerVisitor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. bool useStrip;
  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. }
  70. public void Visit<T, TParent> (QueryStreamNode<T, TParent> node)
  71. {
  72. if (node.IsIndexed)
  73. useStrip = true;
  74. Visit<T, TParent> ((QueryChildNode<T, TParent>)node);
  75. }
  76. public void Visit<T> (QueryOrderGuardNode<T> node)
  77. {
  78. if (behindOrderGuard == null) {
  79. if (node.EnsureOrder) {
  80. behindOrderGuard = true;
  81. //useStrip = true;
  82. } else {
  83. behindOrderGuard = false;
  84. }
  85. }
  86. Visit<T, T> ((QueryStreamNode<T, T>)node);
  87. }
  88. public void Visit<TFirst, TSecond, TResult> (QueryMuxNode<TFirst, TSecond, TResult> node)
  89. {
  90. Visit<TResult, TFirst> ((QueryChildNode<TResult, TFirst>)node);
  91. }
  92. #endregion
  93. internal QueryOptions Options {
  94. get {
  95. return new QueryOptions (options, mode, token == null ? CancellationToken.None : token.Value,
  96. useStrip, behindOrderGuard, partitionCount, implementerToken);
  97. }
  98. }
  99. internal bool UseStrip {
  100. get {
  101. return useStrip;
  102. }
  103. }
  104. internal bool BehindOrderGuard {
  105. get {
  106. return behindOrderGuard.Value;
  107. }
  108. }
  109. void MergeOptions (OptionsList list)
  110. {
  111. if (list.Item1 != null) {
  112. if (options == null)
  113. options = list.Item1;
  114. else
  115. Throw ("WithMergeOptions");
  116. }
  117. if (list.Item2 != null) {
  118. if (mode == null)
  119. mode = list.Item2;
  120. else
  121. Throw ("WithExecutionMode");
  122. }
  123. if (list.Item3 != null) {
  124. if (token == null)
  125. token = list.Item3;
  126. else
  127. Throw ("WithCancellationToken");
  128. }
  129. if (list.Item4 != -1) {
  130. if (degreeOfParallelism == null)
  131. degreeOfParallelism = list.Item4;
  132. else
  133. Throw ("WithDegreeOfParallelism");
  134. }
  135. // That one is treated specially
  136. if (list.Item5 != null) {
  137. implementerToken = implementerToken.Chain (list.Item5);
  138. }
  139. }
  140. void Throw (string methName)
  141. {
  142. throw new InvalidOperationException ("You can't have more than one " + methName + " node in a query");
  143. }
  144. }
  145. }
  146. #endif