AsyncEnumerableQuery.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Linq.Expressions;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. /// <summary>
  11. /// Representation of an asynchronous enumerable sequence using an expression tree.
  12. /// </summary>
  13. internal abstract class AsyncEnumerableQuery
  14. {
  15. /// <summary>
  16. /// Gets the enumerable sequence obtained from evaluating the expression tree.
  17. /// </summary>
  18. internal abstract object Enumerable { get; }
  19. /// <summary>
  20. /// Gets the expression tree representing the asynchronous enumerable sequence.
  21. /// </summary>
  22. internal abstract Expression Expression { get; }
  23. }
  24. /// <summary>
  25. /// Representation of an asynchronous enumerable sequence using an expression tree.
  26. /// </summary>
  27. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  28. internal class AsyncEnumerableQuery<T> : AsyncEnumerableQuery, IOrderedAsyncQueryable<T>, IAsyncQueryProvider
  29. {
  30. private readonly Expression _expression;
  31. private IAsyncEnumerable<T> _enumerable;
  32. /// <summary>
  33. /// Creates a new asynchronous enumerable sequence represented by the specified expression tree.
  34. /// </summary>
  35. /// <param name="expression">The expression tree representing the asynchronous enumerable sequence.</param>
  36. public AsyncEnumerableQuery(Expression expression)
  37. {
  38. _expression = expression;
  39. }
  40. /// <summary>
  41. /// Creates a new asynchronous enumerable sequence by wrapping the specified sequence in an expression tree representation.
  42. /// </summary>
  43. /// <param name="enumerable">The asynchronous enumerable sequence to represent using an expression tree.</param>
  44. public AsyncEnumerableQuery(IAsyncEnumerable<T> enumerable)
  45. {
  46. _enumerable = enumerable;
  47. _expression = Expression.Constant(this);
  48. }
  49. /// <summary>
  50. /// Gets the type of the elements in the sequence.
  51. /// </summary>
  52. Type IAsyncQueryable.ElementType
  53. {
  54. get
  55. {
  56. return typeof(T);
  57. }
  58. }
  59. /// <summary>
  60. /// Gets the expression representing the sequence.
  61. /// </summary>
  62. Expression IAsyncQueryable.Expression
  63. {
  64. get
  65. {
  66. return _expression;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets the query provider used to execute the sequence.
  71. /// </summary>
  72. IAsyncQueryProvider IAsyncQueryable.Provider
  73. {
  74. get
  75. {
  76. return this;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets the enumerable sequence obtained from evaluating the expression tree.
  81. /// </summary>
  82. internal override object Enumerable
  83. {
  84. get
  85. {
  86. return _enumerable;
  87. }
  88. }
  89. /// <summary>
  90. /// Gets the expression tree representing the asynchronous enumerable sequence.
  91. /// </summary>
  92. internal override Expression Expression
  93. {
  94. get
  95. {
  96. return _expression;
  97. }
  98. }
  99. /// <summary>
  100. /// Creates a new asynchronous enumerable sequence represented by an expression tree.
  101. /// </summary>
  102. /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam>
  103. /// <param name="expression">The expression tree representing the asynchronous enumerable sequence.</param>
  104. /// <returns>Asynchronous enumerable sequence represented by the specified expression tree.</returns>
  105. IAsyncQueryable<TElement> IAsyncQueryProvider.CreateQuery<TElement>(Expression expression)
  106. {
  107. return new AsyncEnumerableQuery<TElement>(expression);
  108. }
  109. /// <summary>
  110. /// Executes an expression tree representing a computation over asynchronous enumerable sequences.
  111. /// </summary>
  112. /// <typeparam name="TResult">The type of the result of evaluating the expression tree.</typeparam>
  113. /// <param name="expression">The expression tree to evaluate.</param>
  114. /// <param name="token">Cancellation token used to cancel the evaluation.</param>
  115. /// <returns>Task representing the result of evaluating the specified expression tree.</returns>
  116. Task<TResult> IAsyncQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken token)
  117. {
  118. if (expression == null)
  119. {
  120. throw new ArgumentNullException(nameof(expression));
  121. }
  122. if (!typeof(Task<TResult>).IsAssignableFrom(expression.Type))
  123. {
  124. throw new ArgumentException("The specified expression is not assignable to the result type.", nameof(expression));
  125. }
  126. return new AsyncEnumerableExecutor<TResult>(expression).ExecuteAsync(token);
  127. }
  128. /// <summary>
  129. /// Gets an enumerator to enumerate the elements in the sequence.
  130. /// </summary>
  131. /// <returns>A new enumerator instance used to enumerate the elements in the sequence.</returns>
  132. public IAsyncEnumerator<T> GetAsyncEnumerator()
  133. {
  134. if (_enumerable == null)
  135. {
  136. var expression = Expression.Lambda<Func<IAsyncEnumerable<T>>>(new AsyncEnumerableRewriter().Visit(_expression), null);
  137. _enumerable = expression.Compile()();
  138. }
  139. return _enumerable.GetAsyncEnumerator();
  140. }
  141. /// <summary>
  142. /// Gets a string representation of the enumerable sequence.
  143. /// </summary>
  144. /// <returns>String representation of the enumerable sequence.</returns>
  145. public override string ToString()
  146. {
  147. var ce = _expression as ConstantExpression;
  148. if (ce == null || ce.Value != this)
  149. {
  150. return _expression.ToString();
  151. }
  152. if (_enumerable != null)
  153. {
  154. return _enumerable.ToString();
  155. }
  156. return "null";
  157. }
  158. }
  159. }