-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathCollectionAction.cs
200 lines (171 loc) · 5.9 KB
/
CollectionAction.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Runtime.Serialization;
using NHibernate.Cache;
using NHibernate.Cache.Access;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.Persister;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.Util;
namespace NHibernate.Action
{
/// <summary>
/// Any action relating to insert/update/delete of a collection
/// </summary>
[Serializable]
public abstract partial class CollectionAction :
IAsyncExecutable,
IComparable<CollectionAction>,
IDeserializationCallback,
IAfterTransactionCompletionProcess,
ICacheableExecutable
{
private readonly object key;
[NonSerialized] private ICollectionPersister persister;
private readonly ISessionImplementor session;
private readonly string collectionRole;
private readonly IPersistentCollection collection;
private ISoftLock softLock;
/// <summary>
/// Initializes a new instance of <see cref="CollectionAction"/>.
/// </summary>
/// <param name="persister">The <see cref="ICollectionPersister"/> that is responsible for the persisting the Collection.</param>
/// <param name="collection">The Persistent collection.</param>
/// <param name="key">The identifier of the Collection.</param>
/// <param name="session">The <see cref="ISessionImplementor"/> that the Action is occurring in.</param>
protected CollectionAction(ICollectionPersister persister, IPersistentCollection collection, object key,
ISessionImplementor session)
{
this.persister = persister;
this.session = session;
this.key = key;
collectionRole = persister.Role;
this.collection = collection;
}
protected internal IPersistentCollection Collection
{
get { return collection; }
}
protected internal ICollectionPersister Persister
{
get { return persister; }
}
//Since v5.3
[Obsolete("Please use GetKey() instead.")]
protected internal object Key => GetKey();
protected object GetKey()
{
if (key is DelayedPostInsertIdentifier)
{
// need to look it up
var finalKey = persister.CollectionType.GetKeyOfOwner(collection.Owner, session);
if (finalKey == key)
{
// we may be screwed here since the collection action is about to execute
// and we do not know the final owner key value
}
return finalKey;
}
return key;
}
protected internal ISessionImplementor Session
{
get { return session; }
}
#region IExecutable Members
public string[] QueryCacheSpaces
{
get
{
// 6.0 TODO: Use IPersister.SupportsQueryCache property once IPersister's todo is done.
return persister.SupportsQueryCache() ? persister.CollectionSpaces : null;
}
}
/// <summary>
/// What spaces (tables) are affected by this action?
/// </summary>
public string[] PropertySpaces
{
get { return Persister.CollectionSpaces; }
}
/// <summary> Called before executing any actions</summary>
public virtual void BeforeExecutions()
{
// we need to obtain the lock before any actions are
// executed, since this may be an inverse="true"
// bidirectional association and it is one of the
// earlier entity actions which actually updates
// the database (this action is responsible for
// second-level cache invalidation only)
if (persister.HasCache)
{
CacheKey ck = session.GenerateCacheKey(key, persister.KeyType, persister.Role);
softLock = persister.Cache.Lock(ck, null);
}
}
/// <summary>Execute this action</summary>
public abstract void Execute();
IBeforeTransactionCompletionProcess IAsyncExecutable.BeforeTransactionCompletionProcess =>
null;
IAfterTransactionCompletionProcess IAsyncExecutable.AfterTransactionCompletionProcess =>
persister.HasCache ? this : null;
//Since v5.2
[Obsolete("This property is not used and will be removed in a future version.")]
public virtual BeforeTransactionCompletionProcessDelegate BeforeTransactionCompletionProcess =>
null;
//Since v5.2
[Obsolete("This property is not used and will be removed in a future version.")]
public virtual AfterTransactionCompletionProcessDelegate AfterTransactionCompletionProcess =>
persister.HasCache ? ExecuteAfterTransactionCompletion : default(AfterTransactionCompletionProcessDelegate);
public virtual void ExecuteAfterTransactionCompletion(bool success)
{
var ck = session.GenerateCacheKey(key, persister.KeyType, persister.Role);
persister.Cache.Release(ck, softLock);
}
#endregion
public ISoftLock Lock
{
get { return softLock; }
}
protected internal void Evict()
{
if (persister.HasCache)
{
CacheKey ck = session.GenerateCacheKey(key, persister.KeyType, persister.Role);
persister.Cache.Evict(ck);
}
}
#region IComparable<CollectionAction> Members
///<summary>
///Compares the current object with another object of the same type.
///</summary>
///<returns>
///A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the other parameter.Zero This object is equal to other. Greater than zero This object is greater than other.
///</returns>
///<param name="other">An object to compare with this object.</param>
public virtual int CompareTo(CollectionAction other)
{
//sort first by role name
int roleComparison = string.Compare(collectionRole, other.collectionRole);
if (roleComparison != 0)
{
return roleComparison;
}
//then by fk
return persister.KeyType.Compare(key, other.key);
}
#endregion
public override string ToString()
{
return StringHelper.Unqualify(GetType().FullName) + MessageHelper.InfoString(collectionRole, key);
}
#region IDeserializationCallback Members
void IDeserializationCallback.OnDeserialization(object sender)
{
persister = session.Factory.GetCollectionPersister(collectionRole);
}
#endregion
}
}