Skip to content

Commit 6cbb55a

Browse files
committed
Don't ignore minimalPut parameter in ReadWriteCache
1 parent 9984b54 commit 6cbb55a

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

src/NHibernate/Async/Cache/ReadWriteCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public async Task<bool[]> PutManyAsync(
156156
var version = versions[i];
157157
var lockable = (ILockable) lockables[i];
158158
bool puttable = lockable == null ||
159-
lockable.IsPuttable(timestamp, version, versionComparers[i]);
159+
lockable.IsPuttable(timestamp, version, versionComparers[i], minimalPuts[i]);
160160
if (puttable)
161161
{
162162
putBatch.Add(key, CachedItem.Create(values[i], Cache.NextTimestamp(), version));
@@ -222,7 +222,7 @@ public async Task<bool> PutAsync(CacheKey key, object value, long txTimestamp, o
222222
ILockable lockable = (ILockable) await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
223223

224224
bool puttable = lockable == null ||
225-
lockable.IsPuttable(txTimestamp, version, versionComparator);
225+
lockable.IsPuttable(txTimestamp, version, versionComparator, minimalPut);
226226

227227
if (puttable)
228228
{

src/NHibernate/Cache/CacheLock.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace NHibernate.Cache
1414
/// </remarks>
1515
[Serializable]
1616
[DataContract]
17-
public class CacheLock : ReadWriteCache.ILockable, ISoftLock
17+
public class CacheLock : ReadWriteCache.ILockable, ISoftLock, ReadWriteCache.ILockableNextVer
1818
{
1919
private long unlockTimestamp = -1;
2020
private int multiplicity = 1;
@@ -75,7 +75,7 @@ public void Unlock(long currentTimestamp)
7575
/// Can the timestamped transaction re-cache this
7676
/// locked item now?
7777
/// </summary>
78-
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator)
78+
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator, bool minimalPut)
7979
{
8080
if (Timeout < txTimestamp)
8181
{
@@ -85,10 +85,20 @@ public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator
8585
{
8686
return false;
8787
}
88-
return Version == null ?
89-
UnlockTimestamp < txTimestamp :
90-
comparator.Compare(Version, newVersion) < 0;
91-
//by requiring <, we rely on lock timeout in the case of an unsuccessful update!
88+
89+
return Version == null
90+
? UnlockTimestamp < txTimestamp
91+
: comparator.Compare(Version, newVersion) < (minimalPut ? 0 : 1);
92+
//by requiring <, we rely on lock timeout in the case of an unsuccessful update!
93+
}
94+
95+
/// <summary>
96+
/// Can the timestamped transaction re-cache this
97+
/// locked item now?
98+
/// </summary>
99+
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator)
100+
{
101+
return IsPuttable(txTimestamp, newVersion, comparator, true);
92102
}
93103

94104
/// <summary>

src/NHibernate/Cache/CachedItem.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace NHibernate.Cache
1010
/// </summary>
1111
[Serializable]
1212
[DataContract]
13-
public class CachedItem : ReadWriteCache.ILockable
13+
public class CachedItem : ReadWriteCache.ILockable, ReadWriteCache.ILockableNextVer
1414
{
1515
private long freshTimestamp;
1616
private object value;
@@ -104,15 +104,21 @@ public bool IsGettable(long txTimestamp)
104104
/// <param name="txTimestamp"></param>
105105
/// <param name="newVersion"></param>
106106
/// <param name="comparator"></param>
107+
/// <param name="minimalPut"></param>
107108
/// <returns></returns>
109+
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator, bool minimalPut)
110+
{
111+
if (Version == null)
112+
return !minimalPut;
113+
114+
return comparator.Compare(Version, newVersion) < (minimalPut ? 0 : 1);
115+
}
116+
108117
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator)
109118
{
110-
// we really could refresh the item if it
111-
// is not a lock, but it might be slower
112-
//return freshTimestamp < txTimestamp
113-
return Version != null && comparator.Compare(Version, newVersion) < 0;
119+
return IsPuttable(txTimestamp, newVersion, comparator, true);
114120
}
115-
121+
116122
public override string ToString()
117123
{
118124
return "Item{version=" + Version +

src/NHibernate/Cache/ReadWriteCache.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
@@ -6,6 +7,22 @@
67

78
namespace NHibernate.Cache
89
{
10+
public static class LocableExtension
11+
{
12+
//TODO 6.0: Remove after ILockableNextVer merge
13+
internal static bool IsPuttable(this ReadWriteCache.ILockable lockable, long txTimestamp, object newVersion, IComparer comparator, bool minimalPut)
14+
{
15+
if (lockable is ReadWriteCache.ILockableNextVer l)
16+
{
17+
return l.IsPuttable(txTimestamp, newVersion, comparator, minimalPut);
18+
}
19+
20+
#pragma warning disable CS0618
21+
return lockable.IsPuttable(txTimestamp, newVersion, comparator);
22+
#pragma warning restore CS0618
23+
}
24+
}
25+
926
/// <summary>
1027
/// Caches data that is sometimes updated while maintaining the semantics of
1128
/// "read committed" isolation level. If the database is set to "repeatable
@@ -24,11 +41,20 @@ namespace NHibernate.Cache
2441
/// </remarks>
2542
public partial class ReadWriteCache : IBatchableCacheConcurrencyStrategy
2643
{
44+
//TODO 6.0: Merge with ILockable
45+
internal interface ILockableNextVer
46+
{
47+
bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator, bool minimalPut);
48+
}
49+
2750
public interface ILockable
2851
{
2952
CacheLock Lock(long timeout, int id);
3053
bool IsLock { get; }
3154
bool IsGettable(long txTimestamp);
55+
56+
// Since 5.4
57+
[Obsolete("Use overload with minimalPuts parameter")]
3258
bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator);
3359
}
3460

@@ -237,7 +263,7 @@ public bool[] PutMany(
237263
var version = versions[i];
238264
var lockable = (ILockable) lockables[i];
239265
bool puttable = lockable == null ||
240-
lockable.IsPuttable(timestamp, version, versionComparers[i]);
266+
lockable.IsPuttable(timestamp, version, versionComparers[i], minimalPuts[i]);
241267
if (puttable)
242268
{
243269
putBatch.Add(key, CachedItem.Create(values[i], Cache.NextTimestamp(), version));
@@ -301,7 +327,7 @@ public bool Put(CacheKey key, object value, long txTimestamp, object version, IC
301327
ILockable lockable = (ILockable) Cache.Get(key);
302328

303329
bool puttable = lockable == null ||
304-
lockable.IsPuttable(txTimestamp, version, versionComparator);
330+
lockable.IsPuttable(txTimestamp, version, versionComparator, minimalPut);
305331

306332
if (puttable)
307333
{

0 commit comments

Comments
 (0)