MultiQuery.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using InABox.Clients;
  6. namespace InABox.Core
  7. {
  8. public class MultiQuery
  9. {
  10. private readonly List<InternalQueryDef> _queries = new List<InternalQueryDef>();
  11. public int Count => _queries.Count;
  12. public Dictionary<object, IQueryDef> Definitions()
  13. {
  14. Dictionary<object, IQueryDef> result = new Dictionary<object, IQueryDef>();
  15. foreach (var query in _queries)
  16. result[query.Key] = query.Query;
  17. return result;
  18. }
  19. public Dictionary<object, CoreTable> Results()
  20. {
  21. Dictionary<object, CoreTable> result = new Dictionary<object, CoreTable>();
  22. foreach (var query in _queries)
  23. result[query.Key] = query.Result;
  24. return result;
  25. }
  26. public void Clear()
  27. {
  28. _queries.Clear();
  29. }
  30. public void Add(IQueryDef query, object key)
  31. {
  32. if (key == null)
  33. throw new Exception("MultiQuery Key many not be null");
  34. if (_queries.Any(x => Equals(x.Key, key)))
  35. throw new Exception("Key already exists");
  36. _queries.Add(new InternalQueryDef
  37. {
  38. Key = key,
  39. Query = query
  40. });
  41. }
  42. public void Query(Action<MultiQuery> callback = null)
  43. {
  44. var tasks = new Dictionary<Task<CoreTable>, object>();
  45. foreach (var query in _queries)
  46. {
  47. var task = Task.Run(
  48. () =>
  49. {
  50. var client = ClientFactory.CreateClient(query.Query.Type);
  51. return client.Query(query.Query.Filter, query.Query.Columns, query.Query.SortOrder);
  52. }
  53. );
  54. tasks[task] = query.Key;
  55. }
  56. if (callback == null)
  57. {
  58. Task.WaitAll(tasks.Keys.ToArray());
  59. foreach (var task in tasks.Keys)
  60. {
  61. var query = _queries.FirstOrDefault(x => Equals(x.Key, tasks[task]));
  62. query.Result = task.Result;
  63. }
  64. }
  65. else
  66. {
  67. Task.WhenAll(tasks.Keys.ToArray()).ContinueWith(t =>
  68. {
  69. foreach (var task in tasks.Keys)
  70. {
  71. var query = _queries.FirstOrDefault(x => Equals(x.Key, tasks[task]));
  72. query.Result = task.Result;
  73. }
  74. callback.Invoke(this);
  75. });
  76. }
  77. }
  78. public bool Contains(object key)
  79. {
  80. return _queries.Any(x => Equals(x.Key, key));
  81. }
  82. public CoreTable Get(object key)
  83. {
  84. var query = _queries.FirstOrDefault(x => Equals(x.Key, key));
  85. if (query == null)
  86. throw new Exception("Key does not exist");
  87. if (query.Result == null)
  88. throw new Exception("No result available");
  89. return query.Result;
  90. }
  91. public void Set(object key, CoreTable table)
  92. {
  93. var query = _queries.FirstOrDefault(x => Equals(x.Key, key));
  94. if (query == null)
  95. throw new Exception("Key does not exist");
  96. query.Result = table;
  97. }
  98. private class InternalQueryDef
  99. {
  100. public object Key { get; set; }
  101. public IQueryDef Query { get; set; }
  102. public CoreTable Result { get; set; }
  103. public Entity[] Updates { get; set; }
  104. }
  105. #region Simplified Generic Helpers
  106. public void Add<T>(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null)
  107. where T : Entity, IRemotable, IPersistent, new()
  108. {
  109. Add(new QueryDef<T>(filter, columns, sort), typeof(T));
  110. }
  111. public CoreTable Get<T>()
  112. {
  113. return Get(typeof(T));
  114. }
  115. public bool Contains<T>()
  116. {
  117. return Contains(typeof(T));
  118. }
  119. #endregion
  120. }
  121. }