AsyncEnumerableQuery.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.Reflection;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. /// <summary>
  12. /// Representation of an asynchronous enumerable sequence using an expression tree.
  13. /// </summary>
  14. internal abstract class AsyncEnumerableQuery
  15. {
  16. /// <summary>
  17. /// Gets the enumerable sequence obtained from evaluating the expression tree.
  18. /// </summary>
  19. internal abstract object Enumerable { get; }
  20. /// <summary>
  21. /// Gets the expression tree representing the asynchronous enumerable sequence.
  22. /// </summary>
  23. internal abstract Expression Expression { get; }
  24. }
  25. /// <summary>
  26. /// Representation of an asynchronous enumerable sequence using an expression tree.
  27. /// </summary>
  28. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  29. internal class AsyncEnumerableQuery<T> : AsyncEnumerableQuery, IOrderedAsyncQueryable<T>, IAsyncQueryProvider
  30. {
  31. private readonly Expression _expression;
  32. private IAsyncEnumerable<T> _enumerable;
  33. /// <summary>
  34. /// Creates a new asynchronous enumerable sequence represented by the specified expression tree.
  35. /// </summary>
  36. /// <param name="expression">The expression tree representing the asynchronous enumerable sequence.</param>
  37. public AsyncEnumerableQuery(Expression expression)
  38. {
  39. _expression = expression;
  40. }
  41. /// <summary>
  42. /// Creates a new asynchronous enumerable sequence by wrapping the specified sequence in an expression tree representation.
  43. /// </summary>
  44. /// <param name="enumerable">The asynchronous enumerable sequence to represent using an expression tree.</param>
  45. public AsyncEnumerableQuery(IAsyncEnumerable<T> enumerable)
  46. {
  47. _enumerable = enumerable;
  48. _expression = Expression.Constant(this);
  49. }
  50. /// <summary>
  51. /// Gets the type of the elements in the sequence.
  52. /// </summary>
  53. Type IAsyncQueryable.ElementType => typeof(T);
  54. /// <summary>
  55. /// Gets the expression representing the sequence.
  56. /// </summary>
  57. Expression IAsyncQueryable.Expression => _expression;
  58. /// <summary>
  59. /// Gets the query provider used to execute the sequence.
  60. /// </summary>
  61. IAsyncQueryProvider IAsyncQueryable.Provider => this;
  62. /// <summary>
  63. /// Gets the enumerable sequence obtained from evaluating the expression tree.
  64. /// </summary>
  65. internal override object Enumerable => _enumerable;
  66. /// <summary>
  67. /// Gets the expression tree representing the asynchronous enumerable sequence.
  68. /// </summary>
  69. internal override Expression Expression => _expression;
  70. /// <summary>
  71. /// Creates a new asynchronous enumerable sequence represented by an expression tree.
  72. /// </summary>
  73. /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam>
  74. /// <param name="expression">The expression tree representing the asynchronous enumerable sequence.</param>
  75. /// <returns>Asynchronous enumerable sequence represented by the specified expression tree.</returns>
  76. IAsyncQueryable<TElement> IAsyncQueryProvider.CreateQuery<TElement>(Expression expression)
  77. {
  78. return new AsyncEnumerableQuery<TElement>(expression);
  79. }
  80. /// <summary>
  81. /// Executes an expression tree representing a computation over asynchronous enumerable sequences.
  82. /// </summary>
  83. /// <typeparam name="TResult">The type of the result of evaluating the expression tree.</typeparam>
  84. /// <param name="expression">The expression tree to evaluate.</param>
  85. /// <param name="token">Cancellation token used to cancel the evaluation.</param>
  86. /// <returns>Task representing the result of evaluating the specified expression tree.</returns>
  87. ValueTask<TResult> IAsyncQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken token)
  88. {
  89. if (expression == null)
  90. {
  91. throw new ArgumentNullException(nameof(expression));
  92. }
  93. if (!typeof(ValueTask<TResult>).IsAssignableFrom(expression.Type))
  94. {
  95. throw new ArgumentException("The specified expression is not assignable to the result type.", nameof(expression));
  96. }
  97. return new AsyncEnumerableExecutor<TResult>(expression).ExecuteAsync(token);
  98. }
  99. /// <summary>
  100. /// Gets an enumerator to enumerate the elements in the sequence.
  101. /// </summary>
  102. /// <param name="token">Cancellation token used to cancel the enumeration.</param>
  103. /// <returns>A new enumerator instance used to enumerate the elements in the sequence.</returns>
  104. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken token)
  105. {
  106. token.ThrowIfCancellationRequested();
  107. if (_enumerable == null)
  108. {
  109. var expression = Expression.Lambda<Func<IAsyncEnumerable<T>>>(new AsyncEnumerableRewriter().Visit(_expression), null);
  110. _enumerable = expression.Compile()();
  111. }
  112. return _enumerable.GetAsyncEnumerator(token);
  113. }
  114. /// <summary>
  115. /// Gets a string representation of the enumerable sequence.
  116. /// </summary>
  117. /// <returns>String representation of the enumerable sequence.</returns>
  118. public override string ToString()
  119. {
  120. if (!(_expression is ConstantExpression ce) || ce.Value != this)
  121. {
  122. return _expression.ToString();
  123. }
  124. if (_enumerable != null)
  125. {
  126. return _enumerable.ToString();
  127. }
  128. return "null";
  129. }
  130. }
  131. }