-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathCollectionRemoveAction.cs
125 lines (111 loc) · 3.95 KB
/
CollectionRemoveAction.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
using System;
using System.Diagnostics;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Event;
using NHibernate.Persister.Collection;
namespace NHibernate.Action
{
[Serializable]
public sealed partial class CollectionRemoveAction : CollectionAction
{
private readonly bool emptySnapshot;
private readonly object affectedOwner;
/// <summary>
/// Removes a persistent collection from its loaded owner.
/// </summary>
/// <param name="collection">The collection to to remove; must be non-null </param>
/// <param name="persister"> The collection's persister </param>
/// <param name="id">The collection key </param>
/// <param name="emptySnapshot">Indicates if the snapshot is empty </param>
/// <param name="session">The session </param>
/// <remarks>Use this constructor when the collection is non-null.</remarks>
public CollectionRemoveAction(IPersistentCollection collection, ICollectionPersister persister, object id,
bool emptySnapshot, ISessionImplementor session)
: base(persister, collection, id, session)
{
if (collection == null)
{
throw new AssertionFailure("collection == null");
}
this.emptySnapshot = emptySnapshot;
affectedOwner = session.PersistenceContext.GetLoadedCollectionOwnerOrNull(collection);
}
/// <summary>
/// Removes a persistent collection from a specified owner.
/// </summary>
/// <param name="affectedOwner">The collection's owner; must be non-null </param>
/// <param name="persister"> The collection's persister </param>
/// <param name="id">The collection key </param>
/// <param name="emptySnapshot">Indicates if the snapshot is empty </param>
/// <param name="session">The session </param>
/// <remarks> Use this constructor when the collection to be removed has not been loaded. </remarks>
public CollectionRemoveAction(object affectedOwner, ICollectionPersister persister, object id, bool emptySnapshot,
ISessionImplementor session) : base(persister, null, id, session)
{
if (affectedOwner == null)
{
throw new AssertionFailure("affectedOwner == null");
}
this.emptySnapshot = emptySnapshot;
this.affectedOwner = affectedOwner;
}
public override void Execute()
{
bool statsEnabled = Session.Factory.Statistics.IsStatisticsEnabled;
Stopwatch stopwatch = null;
if (statsEnabled)
{
stopwatch = Stopwatch.StartNew();
}
PreRemove();
if (!emptySnapshot)
{
Persister.Remove(GetKey(), Session);
}
IPersistentCollection collection = Collection;
if (collection != null)
{
var entry = Session.PersistenceContext.GetCollectionEntry(collection);
entry.AfterAction(collection);
}
Evict();
PostRemove();
if (statsEnabled)
{
stopwatch.Stop();
Session.Factory.StatisticsImplementor.RemoveCollection(Persister.Role, stopwatch.Elapsed);
}
}
private void PreRemove()
{
IPreCollectionRemoveEventListener[] preListeners = Session.Listeners.PreCollectionRemoveEventListeners;
if (preListeners.Length > 0)
{
PreCollectionRemoveEvent preEvent = new PreCollectionRemoveEvent(Persister, Collection, (IEventSource) Session,
affectedOwner);
for (int i = 0; i < preListeners.Length; i++)
{
preListeners[i].OnPreRemoveCollection(preEvent);
}
}
}
private void PostRemove()
{
IPostCollectionRemoveEventListener[] postListeners = Session.Listeners.PostCollectionRemoveEventListeners;
if (postListeners.Length > 0)
{
PostCollectionRemoveEvent postEvent = new PostCollectionRemoveEvent(Persister, Collection, (IEventSource) Session,
affectedOwner);
for (int i = 0; i < postListeners.Length; i++)
{
postListeners[i].OnPostRemoveCollection(postEvent);
}
}
}
public override int CompareTo(CollectionAction other)
{
return 0;
}
}
}