QueryOptionNode.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. interface QueryOptionNode : IVisitableNode {
  36. OptionsList GetOptions ();
  37. }
  38. internal class QueryOptionNode<T> : QueryChildNode<T, T>, QueryOptionNode
  39. {
  40. public QueryOptionNode (QueryBaseNode<T> parent)
  41. : base (parent)
  42. {
  43. }
  44. public virtual OptionsList GetOptions ()
  45. {
  46. return new OptionsList (null, null, null, -1, null);
  47. }
  48. internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
  49. {
  50. return Parent.GetEnumerables (options);
  51. }
  52. internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
  53. {
  54. return Parent.GetOrderedEnumerables (options);
  55. }
  56. internal override IEnumerable<T> GetSequential ()
  57. {
  58. return Parent.GetSequential ();
  59. }
  60. public override void Visit (INodeVisitor visitor)
  61. {
  62. visitor.Visit ((QueryOptionNode)this);
  63. }
  64. }
  65. internal class ParallelExecutionModeNode<T> : QueryOptionNode<T>
  66. {
  67. ParallelExecutionMode mode;
  68. internal ParallelExecutionModeNode (ParallelExecutionMode mode, QueryBaseNode<T> parent)
  69. : base (parent)
  70. {
  71. this.mode = mode;
  72. }
  73. public override OptionsList GetOptions ()
  74. {
  75. return new OptionsList (null, mode, null, -1, null);
  76. }
  77. }
  78. internal class ParallelMergeOptionsNode<T> : QueryOptionNode<T>
  79. {
  80. ParallelMergeOptions opts;
  81. internal ParallelMergeOptionsNode (ParallelMergeOptions opts, QueryBaseNode<T> parent)
  82. : base (parent)
  83. {
  84. this.opts = opts;
  85. }
  86. public override OptionsList GetOptions ()
  87. {
  88. return new OptionsList (opts, null, null, -1, null);
  89. }
  90. }
  91. internal class CancellationTokenNode<T> : QueryOptionNode<T>
  92. {
  93. CancellationToken token;
  94. internal CancellationTokenNode (CancellationToken token, QueryBaseNode<T> parent)
  95. : base (parent)
  96. {
  97. this.token = token;
  98. }
  99. public override OptionsList GetOptions ()
  100. {
  101. return new OptionsList (null, null, token, -1, null);
  102. }
  103. }
  104. internal class DegreeOfParallelismNode<T> : QueryOptionNode<T>
  105. {
  106. int degreeParallelism;
  107. internal DegreeOfParallelismNode (int degreeParallelism, QueryBaseNode<T> parent)
  108. : base (parent)
  109. {
  110. this.degreeParallelism = degreeParallelism;
  111. }
  112. public override OptionsList GetOptions ()
  113. {
  114. return new OptionsList (null, null, null, degreeParallelism, null);
  115. }
  116. }
  117. internal class ImplementerTokenNode<T> : QueryOptionNode<T>
  118. {
  119. CancellationTokenSource source;
  120. internal ImplementerTokenNode (CancellationTokenSource token, QueryBaseNode<T> parent)
  121. : base (parent)
  122. {
  123. this.source = token;
  124. }
  125. public override OptionsList GetOptions ()
  126. {
  127. return new OptionsList (null, null, null, -1, source);
  128. }
  129. }
  130. }
  131. #endif