QueryOptionNode.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // QueryOptionNode.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.Collections.Generic;
  30. namespace System.Linq.Parallel.QueryNodes
  31. {
  32. // The first four elements correspond to the public operator With*
  33. // Last CancellationToken parameter is used internally for ImplementerToken
  34. using OptionsList = Tuple<ParallelMergeOptions?, ParallelExecutionMode?, CancellationToken?, int, CancellationTokenSource>;
  35. internal class QueryOptionNode<T> : QueryChildNode<T, T>
  36. {
  37. public QueryOptionNode (QueryBaseNode<T> parent)
  38. : base (parent)
  39. {
  40. }
  41. internal virtual OptionsList GetOptions ()
  42. {
  43. return new OptionsList (null, null, null, -1, null);
  44. }
  45. internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
  46. {
  47. return Parent.GetEnumerables (options);
  48. }
  49. internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
  50. {
  51. return Parent.GetOrderedEnumerables (options);
  52. }
  53. internal override IEnumerable<T> GetSequential ()
  54. {
  55. return Parent.GetSequential ();
  56. }
  57. public override void Visit (INodeVisitor visitor)
  58. {
  59. visitor.Visit<T> (this);
  60. }
  61. }
  62. internal class ParallelExecutionModeNode<T> : QueryOptionNode<T>
  63. {
  64. ParallelExecutionMode mode;
  65. internal ParallelExecutionModeNode (ParallelExecutionMode mode, QueryBaseNode<T> parent)
  66. : base (parent)
  67. {
  68. this.mode = mode;
  69. }
  70. internal override OptionsList GetOptions ()
  71. {
  72. return new OptionsList (null, mode, null, -1, null);
  73. }
  74. }
  75. internal class ParallelMergeOptionsNode<T> : QueryOptionNode<T>
  76. {
  77. ParallelMergeOptions opts;
  78. internal ParallelMergeOptionsNode (ParallelMergeOptions opts, QueryBaseNode<T> parent)
  79. : base (parent)
  80. {
  81. this.opts = opts;
  82. }
  83. internal override OptionsList GetOptions ()
  84. {
  85. return new OptionsList (opts, null, null, -1, null);
  86. }
  87. }
  88. internal class CancellationTokenNode<T> : QueryOptionNode<T>
  89. {
  90. CancellationToken token;
  91. internal CancellationTokenNode (CancellationToken token, QueryBaseNode<T> parent)
  92. : base (parent)
  93. {
  94. this.token = token;
  95. }
  96. internal override OptionsList GetOptions ()
  97. {
  98. return new OptionsList (null, null, token, -1, null);
  99. }
  100. }
  101. internal class DegreeOfParallelismNode<T> : QueryOptionNode<T>
  102. {
  103. int degreeParallelism;
  104. internal DegreeOfParallelismNode (int degreeParallelism, QueryBaseNode<T> parent)
  105. : base (parent)
  106. {
  107. this.degreeParallelism = degreeParallelism;
  108. }
  109. internal override OptionsList GetOptions ()
  110. {
  111. return new OptionsList (null, null, null, degreeParallelism, null);
  112. }
  113. }
  114. internal class ImplementerTokenNode<T> : QueryOptionNode<T>
  115. {
  116. CancellationTokenSource source;
  117. internal ImplementerTokenNode (CancellationTokenSource token, QueryBaseNode<T> parent)
  118. : base (parent)
  119. {
  120. this.source = token;
  121. }
  122. internal override OptionsList GetOptions ()
  123. {
  124. return new OptionsList (null, null, null, -1, source);
  125. }
  126. }
  127. }
  128. #endif