AsyncEnumerableQuery.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 => typeof(T);
  53. /// <summary>
  54. /// Gets the expression representing the sequence.
  55. /// </summary>
  56. Expression IAsyncQueryable.Expression => _expression;
  57. /// <summary>
  58. /// Gets the query provider used to execute the sequence.
  59. /// </summary>
  60. IAsyncQueryProvider IAsyncQueryable.Provider => this;
  61. /// <summary>
  62. /// Gets the enumerable sequence obtained from evaluating the expression tree.
  63. /// </summary>
  64. internal override object? Enumerable => _enumerable;
  65. /// <summary>
  66. /// Gets the expression tree representing the asynchronous enumerable sequence.
  67. /// </summary>
  68. internal override Expression Expression => _expression;
  69. /// <summary>
  70. /// Creates a new asynchronous enumerable sequence represented by an expression tree.
  71. /// </summary>
  72. /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam>
  73. /// <param name="expression">The expression tree representing the asynchronous enumerable sequence.</param>
  74. /// <returns>Asynchronous enumerable sequence represented by the specified expression tree.</returns>
  75. IAsyncQueryable<TElement> IAsyncQueryProvider.CreateQuery<TElement>(Expression expression)
  76. {
  77. return new AsyncEnumerableQuery<TElement>(expression);
  78. }
  79. /// <summary>
  80. /// Executes an expression tree representing a computation over asynchronous enumerable sequences.
  81. /// </summary>
  82. /// <typeparam name="TResult">The type of the result of evaluating the expression tree.</typeparam>
  83. /// <param name="expression">The expression tree to evaluate.</param>
  84. /// <param name="token">Cancellation token used to cancel the evaluation.</param>
  85. /// <returns>Task representing the result of evaluating the specified expression tree.</returns>
  86. ValueTask<TResult> IAsyncQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken token)
  87. {
  88. if (expression == null)
  89. {
  90. throw new ArgumentNullException(nameof(expression));
  91. }
  92. if (!typeof(ValueTask<TResult>).IsAssignableFrom(expression.Type))
  93. {
  94. throw new ArgumentException("The specified expression is not assignable to the result type.", nameof(expression));
  95. }
  96. return new AsyncEnumerableExecutor<TResult>(expression).ExecuteAsync(token);
  97. }
  98. /// <summary>
  99. /// Gets an enumerator to enumerate the elements in the sequence.
  100. /// </summary>
  101. /// <param name="token">Cancellation token used to cancel the enumeration.</param>
  102. /// <returns>A new enumerator instance used to enumerate the elements in the sequence.</returns>
  103. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken token)
  104. {
  105. token.ThrowIfCancellationRequested();
  106. if (_enumerable == null)
  107. {
  108. var expression = Expression.Lambda<Func<IAsyncEnumerable<T>>>(new AsyncEnumerableRewriter().Visit(_expression), null);
  109. _enumerable = expression.Compile()();
  110. }
  111. return _enumerable.GetAsyncEnumerator(token);
  112. }
  113. /// <summary>
  114. /// Gets a string representation of the enumerable sequence.
  115. /// </summary>
  116. /// <returns>String representation of the enumerable sequence.</returns>
  117. public override string ToString()
  118. {
  119. if (!(_expression is ConstantExpression ce) || ce.Value != this)
  120. {
  121. return _expression.ToString();
  122. }
  123. if (_enumerable != null)
  124. {
  125. return _enumerable.ToString();
  126. }
  127. return "null";
  128. }
  129. }
  130. }