forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulkOperationCleanupAction.cs
173 lines (149 loc) · 4.99 KB
/
BulkOperationCleanupAction.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NHibernate.Engine;
using NHibernate.Persister;
using NHibernate.Persister.Entity;
using IQueryable = NHibernate.Persister.Entity.IQueryable;
namespace NHibernate.Action
{
/// <summary>
/// Implementation of BulkOperationCleanupAction.
/// </summary>
[Serializable]
public partial class BulkOperationCleanupAction : IAsyncExecutable, IAfterTransactionCompletionProcess, ICacheableExecutable
{
private readonly ISessionImplementor session;
private readonly HashSet<string> affectedEntityNames = new HashSet<string>();
private readonly HashSet<string> affectedCollectionRoles = new HashSet<string>();
private readonly string[] spaces;
private readonly string[] queryCacheSpaces;
public BulkOperationCleanupAction(ISessionImplementor session, IQueryable[] affectedQueryables)
{
this.session = session;
var affectedSpaces = new HashSet<string>();
var affectedQueryCacheSpaces = new HashSet<string>();
foreach (var affectedQueryable in affectedQueryables)
{
if (affectedQueryable.HasCache)
{
affectedEntityNames.Add(affectedQueryable.EntityName);
}
ISet<string> roles = session.Factory.GetCollectionRolesByEntityParticipant(affectedQueryable.EntityName);
if (roles != null)
{
affectedCollectionRoles.UnionWith(roles);
}
// 6.0 TODO: Use IPersister.SupportsQueryCache property once IPersister's todo is done.
var supportsQuerySpace = affectedQueryable.SupportsQueryCache();
foreach (var querySpace in affectedQueryable.QuerySpaces)
{
affectedSpaces.Add(querySpace);
if (supportsQuerySpace)
{
affectedQueryCacheSpaces.Add(querySpace);
}
}
}
spaces = affectedSpaces.ToArray();
queryCacheSpaces = affectedQueryCacheSpaces.ToArray();
}
/// <summary>
/// Create an action that will evict collection and entity regions based on queryspaces (table names).
/// </summary>
public BulkOperationCleanupAction(ISessionImplementor session, ISet<string> querySpaces)
{
//from H3.2 TODO: cache the autodetected information and pass it in instead.
this.session = session;
var affectedSpaces = new HashSet<string>(querySpaces);
var affectedQueryCacheSpaces = new HashSet<string>();
foreach (var persister in session.Factory.GetEntityPersisters(querySpaces))
{
if (persister.HasCache)
{
affectedEntityNames.Add(persister.EntityName);
}
ISet<string> roles = session.Factory.GetCollectionRolesByEntityParticipant(persister.EntityName);
if (roles != null)
{
affectedCollectionRoles.UnionWith(roles);
}
// 6.0 TODO: Use IPersister.SupportsQueryCache property once IPersister's todo is done.
var supportsQuerySpace = persister.SupportsQueryCache();
foreach (var querySpace in persister.QuerySpaces)
{
affectedSpaces.Add(querySpace);
if (supportsQuerySpace)
{
affectedQueryCacheSpaces.Add(querySpace);
}
}
}
spaces = affectedSpaces.ToArray();
queryCacheSpaces = affectedQueryCacheSpaces.ToArray();
}
#region IExecutable Members
/// <inheritdoc />
public string[] QueryCacheSpaces => queryCacheSpaces;
public string[] PropertySpaces
{
get { return spaces; }
}
public void BeforeExecutions()
{
// nothing to do
}
public void Execute()
{
// nothing to do
}
//Since v5.2
[Obsolete("This property is not used and will be removed in a future version.")]
public BeforeTransactionCompletionProcessDelegate BeforeTransactionCompletionProcess =>
null;
//Since v5.2
[Obsolete("This property is not used and will be removed in a future version.")]
public AfterTransactionCompletionProcessDelegate AfterTransactionCompletionProcess =>
ExecuteAfterTransactionCompletion;
IBeforeTransactionCompletionProcess IAsyncExecutable.BeforeTransactionCompletionProcess =>
null;
IAfterTransactionCompletionProcess IAsyncExecutable.AfterTransactionCompletionProcess =>
this;
public void ExecuteAfterTransactionCompletion(bool success)
{
EvictEntityRegions();
EvictCollectionRegions();
}
private void EvictCollectionRegions()
{
if (affectedCollectionRoles != null && affectedCollectionRoles.Any())
{
session.Factory.EvictCollection(affectedCollectionRoles);
}
}
private void EvictEntityRegions()
{
if (affectedEntityNames != null && affectedEntityNames.Any())
{
session.Factory.EvictEntity(affectedEntityNames);
}
}
#endregion
// Since v5.2
[Obsolete("This method has no more usage in NHibernate and will be removed in a future version.")]
public virtual void Init()
{
EvictEntityRegions();
EvictCollectionRegions();
}
// Since v5.2
[Obsolete("This method has no more usage in NHibernate and will be removed in a future version.")]
public virtual async Task InitAsync(CancellationToken cancellationToken)
{
await EvictEntityRegionsAsync(cancellationToken);
await EvictCollectionRegionsAsync(cancellationToken);
}
}
}