ObservableQuery.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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.Globalization;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Reactive.Joins;
  9. using System.Reactive.Linq;
  10. using System.Reflection;
  11. namespace System.Reactive
  12. {
  13. internal class ObservableQueryProvider : IQbservableProvider, IQueryProvider
  14. {
  15. public IQbservable<TResult> CreateQuery<TResult>(Expression expression)
  16. {
  17. if (expression == null)
  18. throw new ArgumentNullException("expression");
  19. if (!typeof(IObservable<TResult>).IsAssignableFrom(expression.Type))
  20. throw new ArgumentException(Strings_Providers.INVALID_TREE_TYPE, "expression");
  21. return new ObservableQuery<TResult>(expression);
  22. }
  23. IQueryable<TElement> IQueryProvider.CreateQuery<TElement>(Expression expression)
  24. {
  25. //
  26. // Here we're on the edge between IQbservable and IQueryable for the local
  27. // execution case. E.g.:
  28. //
  29. // observable.AsQbservable().<operators>.ToQueryable()
  30. //
  31. // This should be turned into a local execution, with the push-to-pull
  32. // adapter in the middle, so we rewrite to:
  33. //
  34. // observable.AsQbservable().<operators>.ToEnumerable().AsQueryable()
  35. //
  36. var call = expression as MethodCallExpression;
  37. if (call == null || call.Method.DeclaringType != typeof(Qbservable) || call.Method.Name != "ToQueryable")
  38. throw new ArgumentException(Strings_Providers.EXPECTED_TOQUERYABLE_METHODCALL, "expression");
  39. //
  40. // This is the IQbservable<T> object corresponding to the lhs. Now wrap
  41. // it in two calls to get the local queryable.
  42. //
  43. var arg0 = call.Arguments[0];
  44. var res =
  45. Expression.Call(
  46. AsQueryable.MakeGenericMethod(typeof(TElement)),
  47. Expression.Call(
  48. typeof(Observable).GetMethod("ToEnumerable").MakeGenericMethod(typeof(TElement)),
  49. arg0
  50. )
  51. );
  52. //
  53. // Queryable operator calls should be taken care of by the provider for
  54. // LINQ to Objects. So we compile and get the resulting IQueryable<T>
  55. // back to hand it out.
  56. //
  57. return Expression.Lambda<Func<IQueryable<TElement>>>(res).Compile()();
  58. }
  59. private static MethodInfo s_AsQueryable;
  60. private static MethodInfo AsQueryable
  61. {
  62. get
  63. {
  64. if (s_AsQueryable == null)
  65. s_AsQueryable = Qbservable.InfoOf<object>(() => Queryable.AsQueryable<object>(null)).GetGenericMethodDefinition();
  66. return s_AsQueryable;
  67. }
  68. }
  69. IQueryable IQueryProvider.CreateQuery(Expression expression)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. TResult IQueryProvider.Execute<TResult>(Expression expression)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. object IQueryProvider.Execute(Expression expression)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. }
  82. internal class ObservableQuery
  83. {
  84. protected object _source;
  85. protected Expression _expression;
  86. public object Source
  87. {
  88. get { return _source; }
  89. }
  90. public Expression Expression
  91. {
  92. get { return _expression; }
  93. }
  94. }
  95. internal class ObservableQuery<TSource> : ObservableQuery, IQbservable<TSource>
  96. {
  97. internal ObservableQuery(IObservable<TSource> source)
  98. {
  99. _source = source;
  100. _expression = Expression.Constant(this);
  101. }
  102. internal ObservableQuery(Expression expression)
  103. {
  104. _expression = expression;
  105. }
  106. public Type ElementType
  107. {
  108. get { return typeof(TSource); }
  109. }
  110. public IQbservableProvider Provider
  111. {
  112. get { return Qbservable.Provider; }
  113. }
  114. public IDisposable Subscribe(IObserver<TSource> observer)
  115. {
  116. if (_source == null)
  117. {
  118. var rewriter = new ObservableRewriter();
  119. var body = rewriter.Visit(_expression);
  120. var f = Expression.Lambda<Func<IObservable<TSource>>>(body);
  121. _source = f.Compile()();
  122. }
  123. //
  124. // [OK] Use of unsafe Subscribe: non-pretentious mapping to IObservable<T> behavior equivalent to the expression tree.
  125. //
  126. return ((IObservable<TSource>)_source).Subscribe/*Unsafe*/(observer);
  127. }
  128. public override string ToString()
  129. {
  130. var c = _expression as ConstantExpression;
  131. if (c != null && c.Value == this)
  132. {
  133. if (_source != null)
  134. return _source.ToString();
  135. return "null";
  136. }
  137. return _expression.ToString();
  138. }
  139. class ObservableRewriter : ExpressionVisitor
  140. {
  141. protected override Expression VisitConstant(ConstantExpression/*!*/ node)
  142. {
  143. var query = node.Value as ObservableQuery;
  144. if (query != null)
  145. {
  146. var source = query.Source;
  147. if (source != null)
  148. {
  149. return Expression.Constant(source);
  150. }
  151. else
  152. {
  153. return Visit(query.Expression);
  154. }
  155. }
  156. return node;
  157. }
  158. protected override Expression VisitMethodCall(MethodCallExpression/*!*/ node)
  159. {
  160. var method = node.Method;
  161. var declaringType = method.DeclaringType;
  162. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  163. var baseType = declaringType.GetTypeInfo().BaseType;
  164. #else
  165. var baseType = declaringType.BaseType;
  166. #endif
  167. if (baseType == typeof(QueryablePattern))
  168. {
  169. if (method.Name == "Then")
  170. {
  171. //
  172. // Retarget Then to the corresponding pattern. Recursive visit of the lhs will rewrite
  173. // the chain of And operators.
  174. //
  175. var pattern = Visit(node.Object);
  176. var arguments = node.Arguments.Select(arg => Unquote(Visit(arg))).ToArray();
  177. var then = Expression.Call(pattern, "Then", method.GetGenericArguments(), arguments);
  178. return then;
  179. }
  180. else if (method.Name == "And")
  181. {
  182. //
  183. // Retarget And to the corresponding pattern.
  184. //
  185. var lhs = Visit(node.Object);
  186. var arguments = node.Arguments.Select(arg => Visit(arg)).ToArray();
  187. var and = Expression.Call(lhs, "And", method.GetGenericArguments(), arguments);
  188. return and;
  189. }
  190. }
  191. else
  192. {
  193. var arguments = node.Arguments.AsEnumerable();
  194. //
  195. // Checking for an IQbservable operator, being either:
  196. // - an extension method on IQbservableProvider
  197. // - an extension method on IQbservable<T>
  198. //
  199. var isOperator = false;
  200. var firstParameter = method.GetParameters().FirstOrDefault();
  201. if (firstParameter != null)
  202. {
  203. var firstParameterType = firstParameter.ParameterType;
  204. //
  205. // Operators like Qbservable.Amb have an n-ary form that take in an IQbservableProvider
  206. // as the first argument. In such a case we need to make sure that the given provider is
  207. // the one targeting regular Observable. If not, we keep the subtree as-is and let that
  208. // provider handle the execution.
  209. //
  210. if (firstParameterType == typeof(IQbservableProvider))
  211. {
  212. isOperator = true;
  213. //
  214. // Since we could be inside a lambda expression where one tries to obtain a query
  215. // provider, or that provider could be stored in an outer variable, we need to
  216. // evaluate the expression to obtain an IQbservableProvider object.
  217. //
  218. var provider = Expression.Lambda<Func<IQbservableProvider>>(Visit(node.Arguments[0])).Compile()();
  219. //
  220. // Let's see whether the ObservableQuery provider is targeted. This one always goes
  221. // to local execution. E.g.:
  222. //
  223. // var xs = Observable.Return(1).AsQbservable();
  224. // var ys = Observable.Return(2).AsQbservable();
  225. // var zs = Observable.Return(3).AsQbservable();
  226. //
  227. // var res = Qbservable.Provider.Amb(xs, ys, zs);
  228. // ^^^^^^^^^^^^^^^^^^^
  229. //
  230. if (provider is ObservableQueryProvider)
  231. {
  232. //
  233. // For further rewrite, simply ignore the query provider argument now to match
  234. // up with the Observable signature. E.g.:
  235. //
  236. // var res = Qbservable.Provider.Amb(xs, ys, zs);
  237. // = Qbservable.Amb(Qbservable.Provider, xs, ys, zs)'
  238. // ^^^^^^^^^^^^^^^^^^^
  239. // ->
  240. // var res = Observable.Amb(xs, ys, zs);
  241. //
  242. arguments = arguments.Skip(1);
  243. }
  244. else
  245. {
  246. //
  247. // We've hit an unknown provider and will defer further execution to it. Upon
  248. // calling Subscribe to the node's output, that provider will take care of it.
  249. //
  250. return node;
  251. }
  252. }
  253. else if (typeof(IQbservable).IsAssignableFrom(firstParameterType))
  254. {
  255. isOperator = true;
  256. }
  257. }
  258. if (isOperator)
  259. {
  260. var args = VisitQbservableOperatorArguments(method, arguments);
  261. return FindObservableMethod(method, args);
  262. }
  263. }
  264. return base.VisitMethodCall(node);
  265. }
  266. #if NO_VISITLAMBDAOFT
  267. protected override Expression VisitLambda(LambdaExpression node)
  268. #else
  269. protected override Expression VisitLambda<T>(Expression<T> node)
  270. #endif
  271. {
  272. return node;
  273. }
  274. private IList<Expression> VisitQbservableOperatorArguments(MethodInfo method, IEnumerable<Expression> arguments)
  275. {
  276. //
  277. // Recognize the Qbservable.When<TResult>(IQbservableProvider, QueryablePlan<TResult>[])
  278. // overload in order to substitute the array with a Plan<TResult>[].
  279. //
  280. if (method.Name == "When")
  281. {
  282. var lastArgument = arguments.Last();
  283. if (lastArgument.NodeType == ExpressionType.NewArrayInit)
  284. {
  285. var paramsArray = (NewArrayExpression)lastArgument;
  286. return new List<Expression>
  287. {
  288. Expression.NewArrayInit(
  289. typeof(Plan<>).MakeGenericType(method.GetGenericArguments()[0]),
  290. paramsArray.Expressions.Select(param => Visit(param))
  291. )
  292. };
  293. }
  294. }
  295. return arguments.Select(arg => Visit(arg)).ToList();
  296. }
  297. class Lazy<T>
  298. {
  299. private readonly Func<T> _factory;
  300. private T _value;
  301. private bool _initialized;
  302. public Lazy(Func<T> factory)
  303. {
  304. _factory = factory;
  305. }
  306. public T Value
  307. {
  308. get
  309. {
  310. lock (_factory)
  311. {
  312. if (!_initialized)
  313. {
  314. _value = _factory();
  315. _initialized = true;
  316. }
  317. }
  318. return _value;
  319. }
  320. }
  321. }
  322. private static Lazy<ILookup<string, MethodInfo>> _observableMethods = new Lazy<ILookup<string, MethodInfo>>(() => GetMethods(typeof(Observable)));
  323. private static MethodCallExpression FindObservableMethod(MethodInfo method, IList<Expression> arguments)
  324. {
  325. //
  326. // Where to look for the matching operator?
  327. //
  328. var targetType = default(Type);
  329. var methods = default(ILookup<string, MethodInfo>);
  330. if (method.DeclaringType == typeof(Qbservable))
  331. {
  332. targetType = typeof(Observable);
  333. methods = _observableMethods.Value;
  334. }
  335. else
  336. {
  337. targetType = method.DeclaringType;
  338. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  339. var typeInfo = targetType.GetTypeInfo();
  340. if (typeInfo.IsDefined(typeof(LocalQueryMethodImplementationTypeAttribute), false))
  341. {
  342. var mapping = (LocalQueryMethodImplementationTypeAttribute)typeInfo.GetCustomAttributes(typeof(LocalQueryMethodImplementationTypeAttribute), false).Single();
  343. targetType = mapping.TargetType;
  344. }
  345. #else
  346. if (targetType.IsDefined(typeof(LocalQueryMethodImplementationTypeAttribute), false))
  347. {
  348. var mapping = (LocalQueryMethodImplementationTypeAttribute)targetType.GetCustomAttributes(typeof(LocalQueryMethodImplementationTypeAttribute), false)[0];
  349. targetType = mapping.TargetType;
  350. }
  351. #endif
  352. methods = GetMethods(targetType);
  353. }
  354. //
  355. // From all the operators with the method's name, find the one that matches all arguments.
  356. //
  357. var typeArgs = method.IsGenericMethod ? method.GetGenericArguments() : null;
  358. var targetMethod = methods[method.Name].FirstOrDefault(candidateMethod => ArgsMatch(candidateMethod, arguments, typeArgs));
  359. if (targetMethod == null)
  360. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Providers.NO_MATCHING_METHOD_FOUND, method.Name, targetType.Name));
  361. //
  362. // Restore generic arguments.
  363. //
  364. if (typeArgs != null)
  365. targetMethod = targetMethod.MakeGenericMethod(typeArgs);
  366. //
  367. // Finally, we need to deal with mismatches on Expression<Func<...>> versus Func<...>.
  368. //
  369. var parameters = targetMethod.GetParameters();
  370. for (int i = 0, n = parameters.Length; i < n; i++)
  371. {
  372. arguments[i] = Unquote(arguments[i]);
  373. }
  374. //
  375. // Emit a new call to the discovered target method.
  376. //
  377. return Expression.Call(null, targetMethod, arguments);
  378. }
  379. private static ILookup<string, MethodInfo> GetMethods(Type type)
  380. {
  381. #if !(CRIPPLED_REFLECTION && HAS_WINRT)
  382. return type.GetMethods(BindingFlags.Static | BindingFlags.Public).ToLookup(m => m.Name);
  383. #else
  384. return type.GetTypeInfo().DeclaredMethods.Where(m => m.IsStatic && m.IsPublic).ToLookup(m => m.Name);
  385. #endif
  386. }
  387. private static bool ArgsMatch(MethodInfo method, IList<Expression> arguments, Type[] typeArgs)
  388. {
  389. //
  390. // Number of parameters should match. Notice we've sanitized IQbservableProvider "this"
  391. // parameters first (see VisitMethodCall).
  392. //
  393. var parameters = method.GetParameters();
  394. if (parameters.Length != arguments.Count)
  395. return false;
  396. //
  397. // Genericity should match too.
  398. //
  399. if (!method.IsGenericMethod && typeArgs != null && typeArgs.Length > 0)
  400. return false;
  401. //
  402. // Reconstruct the generic method if needed.
  403. //
  404. if (method.IsGenericMethodDefinition)
  405. {
  406. if (typeArgs == null)
  407. return false;
  408. if (method.GetGenericArguments().Length != typeArgs.Length)
  409. return false;
  410. var result = method.MakeGenericMethod(typeArgs);
  411. parameters = result.GetParameters();
  412. }
  413. //
  414. // Check compatibility for the parameter types.
  415. //
  416. for (int i = 0, n = arguments.Count; i < n; i++)
  417. {
  418. var parameterType = parameters[i].ParameterType;
  419. var argument = arguments[i];
  420. //
  421. // For operators that take a function (like Where, Select), we'll be faced
  422. // with a quoted argument and a discrepancy between Expression<Func<...>>
  423. // and the underlying Func<...>.
  424. //
  425. if (!parameterType.IsAssignableFrom(argument.Type))
  426. {
  427. argument = Unquote(argument);
  428. if (!parameterType.IsAssignableFrom(argument.Type))
  429. return false;
  430. }
  431. }
  432. return true;
  433. }
  434. private static Expression Unquote(Expression expression)
  435. {
  436. //
  437. // Get rid of all outer quotes around an expression.
  438. //
  439. while (expression.NodeType == ExpressionType.Quote)
  440. expression = ((UnaryExpression)expression).Operand;
  441. return expression;
  442. }
  443. }
  444. }
  445. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  446. static class Helpers
  447. {
  448. public static MethodInfo GetMethod(this Type type, string name)
  449. {
  450. return type.GetTypeInfo().GetDeclaredMethod(name);
  451. }
  452. public static bool IsAssignableFrom(this Type type1, Type type2)
  453. {
  454. return type1.GetTypeInfo().IsAssignableFrom(type2.GetTypeInfo());
  455. }
  456. }
  457. #endif
  458. }