forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheLock.cs
178 lines (160 loc) · 3.71 KB
/
CacheLock.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
using System;
using System.Collections;
using System.Runtime.Serialization;
using NHibernate.Cache.Access;
namespace NHibernate.Cache
{
/// <summary>
/// A soft lock which supports concurrent locking,
/// timestamped with the time it was released
/// </summary>
/// <remarks>
/// This class was named Lock in H2.1
/// </remarks>
[Serializable]
[DataContract]
public class CacheLock : ReadWriteCache.ILockable, ISoftLock, ReadWriteCache.IMinimalPutAwareLockable
{
private long unlockTimestamp = -1;
private int multiplicity = 1;
private bool concurrentLock = false;
private long timeout;
private int id;
private object version;
public CacheLock()
{
}
// Since 5.2
[Obsolete("Use object initializer instead.")]
public CacheLock(long timeout, int id, object version)
{
Timeout = timeout;
Id = id;
Version = version;
}
internal static CacheLock Create(long timeout, int id, object version)
{
return new CacheLock
{
Version = version,
Timeout = timeout,
Id = id
};
}
/// <summary>
/// Increment the lock, setting the
/// new lock timeout
/// </summary>
public CacheLock Lock(long timeout, int id)
{
WasLockedConcurrently = true;
Multiplicity++;
Timeout = timeout;
return this;
}
/// <summary>
/// Decrement the lock, setting the unlock
/// timestamp if now unlocked
/// </summary>
/// <param name="currentTimestamp"></param>
public void Unlock(long currentTimestamp)
{
if (--Multiplicity == 0)
{
UnlockTimestamp = currentTimestamp;
}
}
/// <summary>
/// Can the timestamped transaction re-cache this
/// locked item now?
/// </summary>
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator, bool minimalPut)
{
if (Timeout < txTimestamp)
{
return true;
}
if (Multiplicity > 0)
{
return false;
}
return Version == null
? !minimalPut && UnlockTimestamp < txTimestamp
: comparator.Compare(Version, newVersion) < (minimalPut ? 0 : 1);
//by requiring < 0, we rely on lock timeout in the case of an unsuccessful update!
}
/// <summary>
/// Can the timestamped transaction re-cache this
/// locked item now?
/// </summary>
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator)
{
return IsPuttable(txTimestamp, newVersion, comparator, true);
}
/// <summary>
/// Was this lock held concurrently by multiple
/// transactions?
/// </summary>
// 6.0 TODO convert to auto-property
[DataMember]
public bool WasLockedConcurrently
{
get => concurrentLock;
set => concurrentLock = value;
}
/// <summary>
/// Yes, this is a lock
/// </summary>
public bool IsLock => true;
/// <summary>
/// locks are not returned to the client!
/// </summary>
public bool IsGettable(long txTimestamp)
{
return false;
}
// 6.0 TODO convert to auto-property
[DataMember]
public int Id
{
get => id;
set => id = value;
}
// 6.0 TODO convert to auto-property
[DataMember]
public object Version
{
get => version;
set => version = value;
}
// 6.0 TODO convert to auto-property
[DataMember]
public long UnlockTimestamp
{
get => unlockTimestamp;
set => unlockTimestamp = value;
}
// 6.0 TODO convert to auto-property
[DataMember]
public int Multiplicity
{
get => multiplicity;
set => multiplicity = value;
}
// 6.0 TODO convert to auto-property
[DataMember]
public long Timeout
{
get => timeout;
set => timeout = value;
}
public override string ToString()
{
return "CacheLock{id=" + Id +
",version=" + Version +
",multiplicity=" + Multiplicity +
",unlockTimestamp=" + UnlockTimestamp +
"}";
}
}
}