Skip to content

Commit d5fb5e8

Browse files
committed
Initialize collections on demand
1 parent 15a4372 commit d5fb5e8

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/NHibernate/Action/BulkOperationCleanupAction.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public partial class BulkOperationCleanupAction :
2020
ICacheableExecutable
2121
{
2222
private readonly ISessionFactoryImplementor _factory;
23-
private readonly HashSet<string> _affectedEntityNames = new HashSet<string>();
24-
private readonly HashSet<string> _affectedCollectionRoles = new HashSet<string>();
23+
private readonly HashSet<string> _affectedEntityNames;
24+
private readonly HashSet<string> _affectedCollectionRoles;
2525
private readonly string[] _spaces;
2626
private readonly bool _hasCache;
2727

@@ -34,12 +34,22 @@ public BulkOperationCleanupAction(ISessionImplementor session, IQueryable[] affe
3434
if (queryables.HasCache)
3535
{
3636
_hasCache = true;
37+
if (_affectedEntityNames == null)
38+
{
39+
_affectedEntityNames = new HashSet<string>();
40+
}
41+
3742
_affectedEntityNames.Add(queryables.EntityName);
3843
}
3944

4045
var roles = _factory.GetCollectionRolesByEntityParticipant(queryables.EntityName);
4146
if (roles != null)
4247
{
48+
if (_affectedCollectionRoles == null)
49+
{
50+
_affectedCollectionRoles = new HashSet<string>();
51+
}
52+
4353
_affectedCollectionRoles.UnionWith(roles);
4454
}
4555

@@ -70,12 +80,22 @@ public BulkOperationCleanupAction(ISessionImplementor session, ISet<string> quer
7080
if (persister.HasCache)
7181
{
7282
_hasCache = true;
83+
if (_affectedEntityNames == null)
84+
{
85+
_affectedEntityNames = new HashSet<string>();
86+
}
87+
7388
_affectedEntityNames.Add(persister.EntityName);
7489
}
7590

7691
var roles = session.Factory.GetCollectionRolesByEntityParticipant(persister.EntityName);
7792
if (roles != null)
7893
{
94+
if (_affectedCollectionRoles == null)
95+
{
96+
_affectedCollectionRoles = new HashSet<string>();
97+
}
98+
7999
_affectedCollectionRoles.UnionWith(roles);
80100
}
81101

@@ -135,15 +155,15 @@ public void ExecuteAfterTransactionCompletion(bool success)
135155

136156
private void EvictCollectionRegions()
137157
{
138-
if (_affectedCollectionRoles != null && _affectedCollectionRoles.Any())
158+
if (_affectedCollectionRoles != null)
139159
{
140160
_factory.EvictCollection(_affectedCollectionRoles);
141161
}
142162
}
143163

144164
private void EvictEntityRegions()
145165
{
146-
if (_affectedEntityNames != null && _affectedEntityNames.Any())
166+
if (_affectedEntityNames != null)
147167
{
148168
_factory.EvictEntity(_affectedEntityNames);
149169
}

src/NHibernate/Async/Action/BulkOperationCleanupAction.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,11 @@ private Task EvictCollectionRegionsAsync(CancellationToken cancellationToken)
7575
{
7676
return Task.FromCanceled<object>(cancellationToken);
7777
}
78-
try
79-
{
80-
if (_affectedCollectionRoles != null && _affectedCollectionRoles.Any())
81-
{
82-
return _factory.EvictCollectionAsync(_affectedCollectionRoles, cancellationToken);
83-
}
84-
return Task.CompletedTask;
85-
}
86-
catch (Exception ex)
78+
if (_affectedCollectionRoles != null)
8779
{
88-
return Task.FromException<object>(ex);
80+
return _factory.EvictCollectionAsync(_affectedCollectionRoles, cancellationToken);
8981
}
82+
return Task.CompletedTask;
9083
}
9184

9285
private Task EvictEntityRegionsAsync(CancellationToken cancellationToken)
@@ -95,18 +88,11 @@ private Task EvictEntityRegionsAsync(CancellationToken cancellationToken)
9588
{
9689
return Task.FromCanceled<object>(cancellationToken);
9790
}
98-
try
99-
{
100-
if (_affectedEntityNames != null && _affectedEntityNames.Any())
101-
{
102-
return _factory.EvictEntityAsync(_affectedEntityNames, cancellationToken);
103-
}
104-
return Task.CompletedTask;
105-
}
106-
catch (Exception ex)
91+
if (_affectedEntityNames != null)
10792
{
108-
return Task.FromException<object>(ex);
93+
return _factory.EvictEntityAsync(_affectedEntityNames, cancellationToken);
10994
}
95+
return Task.CompletedTask;
11096
}
11197

11298
#endregion

0 commit comments

Comments
 (0)