-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathFutureBatch.cs
89 lines (77 loc) · 2.31 KB
/
FutureBatch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace NHibernate.Impl
{
public abstract class FutureBatch<TQueryApproach, TMultiApproach>
{
private readonly List<TQueryApproach> queries = new List<TQueryApproach>();
private readonly IList<System.Type> resultTypes = new List<System.Type>();
private int index;
private IList results;
private bool isCacheable = true;
private string cacheRegion;
protected readonly SessionImpl session;
protected FutureBatch(SessionImpl session)
{
this.session = session;
}
public IList Results
{
get
{
if (results == null)
{
GetResults();
}
return results;
}
}
public void Add<TResult>(TQueryApproach query)
{
if (queries.Count == 0)
{
cacheRegion = CacheRegion(query);
}
queries.Add(query);
resultTypes.Add(typeof(TResult));
index = queries.Count - 1;
isCacheable = isCacheable && IsQueryCacheable(query);
isCacheable = isCacheable && (cacheRegion == CacheRegion(query));
}
public void Add(TQueryApproach query)
{
Add<object>(query);
}
public IFutureValue<TResult> GetFutureValue<TResult>()
{
int currentIndex = index;
return new FutureValue<TResult>(() => GetCurrentResult<TResult>(currentIndex));
}
public IEnumerable<TResult> GetEnumerator<TResult>()
{
int currentIndex = index;
return new DelayedEnumerator<TResult>(() => GetCurrentResult<TResult>(currentIndex));
}
private void GetResults()
{
var multiApproach = CreateMultiApproach(isCacheable, cacheRegion);
for (int i = 0; i < queries.Count; i++)
{
AddTo(multiApproach, queries[i], resultTypes[i]);
}
results = GetResultsFrom(multiApproach);
ClearCurrentFutureBatch();
}
private IEnumerable<TResult> GetCurrentResult<TResult>(int currentIndex)
{
return ((IList)Results[currentIndex]).Cast<TResult>();
}
protected abstract TMultiApproach CreateMultiApproach(bool isCacheable, string cacheRegion);
protected abstract void AddTo(TMultiApproach multiApproach, TQueryApproach query, System.Type resultType);
protected abstract IList GetResultsFrom(TMultiApproach multiApproach);
protected abstract void ClearCurrentFutureBatch();
protected abstract bool IsQueryCacheable(TQueryApproach query);
protected abstract string CacheRegion(TQueryApproach query);
}
}