ExecutionPlanBuilder.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright 2015-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Linq.Expressions;
  17. using System.Threading;
  18. namespace MongoDB.Driver.Linq
  19. {
  20. internal static class ExecutionPlanBuilder
  21. {
  22. public static Expression BuildPlan(Expression provider, QueryableTranslation translation)
  23. {
  24. Expression executor = Expression.Call(
  25. provider,
  26. "ExecuteModel",
  27. null,
  28. Expression.Constant(translation.Model, typeof(QueryableExecutionModel)));
  29. executor = Expression.Convert(
  30. executor,
  31. typeof(IAsyncCursor<>).MakeGenericType(translation.Model.OutputType));
  32. // we have an IAsyncCursor at this point... need to change it into an IEnumerable
  33. executor = Expression.Call(
  34. typeof(IAsyncCursorExtensions),
  35. nameof(IAsyncCursorExtensions.ToEnumerable),
  36. new Type[] { translation.Model.OutputType },
  37. executor,
  38. Expression.Constant(CancellationToken.None));
  39. if (translation.ResultTransformer != null)
  40. {
  41. var lambda = translation.ResultTransformer.CreateAggregator(translation.Model.OutputType);
  42. executor = Expression.Invoke(
  43. lambda,
  44. executor);
  45. }
  46. return executor;
  47. }
  48. public static Expression BuildAsyncPlan(Expression provider, QueryableTranslation translation, Expression cancellationToken)
  49. {
  50. Expression executor = Expression.Call(
  51. provider,
  52. "ExecuteModelAsync",
  53. null,
  54. Expression.Constant(translation.Model, typeof(QueryableExecutionModel)),
  55. cancellationToken);
  56. if (translation.ResultTransformer != null)
  57. {
  58. var lambda = translation.ResultTransformer.CreateAsyncAggregator(translation.Model.OutputType);
  59. executor = Expression.Invoke(
  60. lambda,
  61. Expression.Convert(executor, lambda.Parameters[0].Type),
  62. cancellationToken);
  63. }
  64. return executor;
  65. }
  66. }
  67. }