QueryOptionNode.cs 4.1 KB

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