|
|
@@ -1,4 +1,5 @@
|
|
|
using System;
|
|
|
+using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
|
@@ -20,12 +21,11 @@ namespace InABox.Core
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public Dictionary<object, CoreTable> Results()
|
|
|
+ public IEnumerable<KeyValuePair<object, CoreTable>> Results()
|
|
|
{
|
|
|
- Dictionary<object, CoreTable> result = new Dictionary<object, CoreTable>();
|
|
|
foreach (var query in _queries)
|
|
|
- result[query.Key] = query.Result;
|
|
|
- return result;
|
|
|
+ if(query.Result != null)
|
|
|
+ yield return new KeyValuePair<object, CoreTable>(query.Key, query.Result);
|
|
|
}
|
|
|
|
|
|
public void Clear()
|
|
|
@@ -40,50 +40,46 @@ namespace InABox.Core
|
|
|
|
|
|
if (_queries.Any(x => Equals(x.Key, key)))
|
|
|
throw new Exception("Key already exists");
|
|
|
- _queries.Add(new InternalQueryDef
|
|
|
- {
|
|
|
- Key = key,
|
|
|
- Query = query
|
|
|
- });
|
|
|
+ _queries.Add(new InternalQueryDef(key, query));
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void Query(Action<MultiQuery> callback = null)
|
|
|
+ public void Query(Action<MultiQuery>? callback = null)
|
|
|
{
|
|
|
- var tasks = new Dictionary<Task<CoreTable>, object>();
|
|
|
+ var tasks = new Dictionary<object, Task<CoreTable>>();
|
|
|
|
|
|
foreach (var query in _queries)
|
|
|
{
|
|
|
- var task = Task.Run(
|
|
|
- () =>
|
|
|
- {
|
|
|
- var client = ClientFactory.CreateClient(query.Query.Type);
|
|
|
- return client.Query(query.Query.Filter, query.Query.Columns, query.Query.SortOrder);
|
|
|
- }
|
|
|
- );
|
|
|
- tasks[task] = query.Key;
|
|
|
+ tasks[query.Key] = Task.Run(() =>
|
|
|
+ {
|
|
|
+ return Client.Create(query.Query.Type)
|
|
|
+ .Query(query.Query.Filter, query.Query.Columns, query.Query.SortOrder);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- if (callback == null)
|
|
|
+ if (callback is null)
|
|
|
{
|
|
|
- Task.WaitAll(tasks.Keys.ToArray());
|
|
|
+ Task.WaitAll(tasks.Values.ToArray());
|
|
|
|
|
|
- foreach (var task in tasks.Keys)
|
|
|
+ foreach(var query in _queries)
|
|
|
{
|
|
|
- var query = _queries.FirstOrDefault(x => Equals(x.Key, tasks[task]));
|
|
|
- query.Result = task.Result;
|
|
|
+ if(tasks.TryGetValue(query.Key, out var task))
|
|
|
+ {
|
|
|
+ query.Result = task.Result;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- Task.WhenAll(tasks.Keys.ToArray()).ContinueWith(t =>
|
|
|
+ Task.WhenAll(tasks.Values.ToArray()).ContinueWith(t =>
|
|
|
{
|
|
|
- foreach (var task in tasks.Keys)
|
|
|
+ foreach(var query in _queries)
|
|
|
{
|
|
|
- var query = _queries.FirstOrDefault(x => Equals(x.Key, tasks[task]));
|
|
|
- query.Result = task.Result;
|
|
|
+ if(tasks.TryGetValue(query.Key, out var task))
|
|
|
+ {
|
|
|
+ query.Result = task.Result;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
callback.Invoke(this);
|
|
|
});
|
|
|
}
|
|
|
@@ -106,9 +102,8 @@ namespace InABox.Core
|
|
|
|
|
|
public void Set(object key, CoreTable table)
|
|
|
{
|
|
|
- var query = _queries.FirstOrDefault(x => Equals(x.Key, key));
|
|
|
- if (query == null)
|
|
|
- throw new Exception("Key does not exist");
|
|
|
+ var query = _queries.FirstOrDefault(x => Equals(x.Key, key))
|
|
|
+ ?? throw new Exception("Key does not exist");
|
|
|
query.Result = table;
|
|
|
}
|
|
|
|
|
|
@@ -116,8 +111,13 @@ namespace InABox.Core
|
|
|
{
|
|
|
public object Key { get; set; }
|
|
|
public IQueryDef Query { get; set; }
|
|
|
- public CoreTable Result { get; set; }
|
|
|
- public Entity[] Updates { get; set; }
|
|
|
+ public CoreTable? Result { get; set; }
|
|
|
+
|
|
|
+ public InternalQueryDef(object key, IQueryDef query)
|
|
|
+ {
|
|
|
+ Key = key;
|
|
|
+ Query = query;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|