Skip to content

Commit ec0c5a0

Browse files
Implement breaking change minimizing suggestions
1 parent c355c34 commit ec0c5a0

File tree

7 files changed

+53
-32
lines changed

7 files changed

+53
-32
lines changed

src/NHibernate.Test/Async/QueryTest/MultiCriteriaFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
153153

154154
var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
155155
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
156-
// The first element is a timestamp, the second is aliases, then only we have the cached data.
157-
var cachedQuery = (IList)cachedListEntry[2] ?? throw new InvalidOperationException("Cached data is null");
156+
// The first element is a timestamp, then only we have the cached data.
157+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
158158

159159
var firstQueryResults = (IList)cachedQuery[0];
160160
firstQueryResults.Clear();

src/NHibernate.Test/Async/QueryTest/MultipleMixedQueriesFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
8383

8484
var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
8585
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
86-
// The first element is a timestamp, the second is aliases, then only we have the cached data.
87-
var cachedQuery = (IList) cachedListEntry[2] ?? throw new InvalidOperationException("Cached data is null");
86+
// The first element is a timestamp, then only we have the cached data.
87+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
8888

8989
var firstQueryResults = (IList)cachedQuery[0];
9090
firstQueryResults.Clear();

src/NHibernate.Test/Async/QueryTest/MultipleQueriesFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
8686

8787
var cacheHashtable = GetHashTableUsedAsQueryCache(Sfi);
8888
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
89-
// The first element is a timestamp, the second is aliases, then only we have the cached data.
90-
var cachedQuery = (IList) cachedListEntry[2] ?? throw new InvalidOperationException("Cached data is null");
89+
// The first element is a timestamp, then only we have the cached data.
90+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
9191

9292
var firstQueryResults = (IList)cachedQuery[0];
9393
firstQueryResults.Clear();

src/NHibernate.Test/QueryTest/MultiCriteriaFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public void CanGetMultiQueryFromSecondLevelCache()
141141

142142
var cacheHashtable = MultipleQueriesFixture.GetHashTableUsedAsQueryCache(Sfi);
143143
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
144-
// The first element is a timestamp, the second is aliases, then only we have the cached data.
145-
var cachedQuery = (IList)cachedListEntry[2] ?? throw new InvalidOperationException("Cached data is null");
144+
// The first element is a timestamp, then only we have the cached data.
145+
var cachedQuery = (IList)cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
146146

147147
var firstQueryResults = (IList)cachedQuery[0];
148148
firstQueryResults.Clear();

src/NHibernate.Test/QueryTest/MultipleMixedQueriesFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public void CanGetMultiQueryFromSecondLevelCache()
7171

7272
var cacheHashtable = MultipleQueriesFixture.GetHashTableUsedAsQueryCache(Sfi);
7373
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
74-
// The first element is a timestamp, the second is aliases, then only we have the cached data.
75-
var cachedQuery = (IList) cachedListEntry[2] ?? throw new InvalidOperationException("Cached data is null");
74+
// The first element is a timestamp, then only we have the cached data.
75+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
7676

7777
var firstQueryResults = (IList)cachedQuery[0];
7878
firstQueryResults.Clear();

src/NHibernate.Test/QueryTest/MultipleQueriesFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public void CanGetMultiQueryFromSecondLevelCache()
7474

7575
var cacheHashtable = GetHashTableUsedAsQueryCache(Sfi);
7676
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
77-
// The first element is a timestamp, the second is aliases, then only we have the cached data.
78-
var cachedQuery = (IList) cachedListEntry[2] ?? throw new InvalidOperationException("Cached data is null");
77+
// The first element is a timestamp, then only we have the cached data.
78+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
7979

8080
var firstQueryResults = (IList)cachedQuery[0];
8181
firstQueryResults.Clear();

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Linq;
54
using NHibernate.Cfg;
65
using NHibernate.Engine;
76
using NHibernate.Persister.Collection;
@@ -18,7 +17,7 @@ namespace NHibernate.Cache
1817
/// </summary>
1918
public partial class StandardQueryCache : IQueryCache, IBatchableQueryCache
2019
{
21-
private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof (StandardQueryCache));
20+
private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof(StandardQueryCache));
2221
private readonly string _regionName;
2322
private readonly UpdateTimestampsCache _updateTimestampsCache;
2423
private readonly CacheBase _cache;
@@ -133,7 +132,7 @@ public IList Get(
133132
return null;
134133
}
135134

136-
var timestamp = (long) cacheable[0];
135+
var timestamp = GetResultsMetadata(cacheable, out var aliases);
137136

138137
if (Log.IsDebugEnabled())
139138
Log.Debug("Checking query spaces for up-to-dateness [{0}]", StringHelper.CollectionToString(spaces));
@@ -148,7 +147,6 @@ public IList Get(
148147

149148
if (result != null && key.ResultTransformer?.AutoDiscoverTypes == true && result.Count > 0)
150149
{
151-
var aliases = (string[]) cacheable[1];
152150
key.ResultTransformer.SupplyAutoDiscoveredParameters(queryParameters.ResultTransformer, aliases);
153151
}
154152

@@ -174,7 +172,7 @@ public IList Get(QueryKey key, ICacheAssembler[] returnTypes, bool isNaturalKeyL
174172
return null;
175173
}
176174

177-
var timestamp = (long) cacheable[0];
175+
var timestamp = GetResultsMetadata(cacheable, out var _);
178176

179177
if (Log.IsDebugEnabled())
180178
Log.Debug("Checking query spaces for up-to-dateness [{0}]", StringHelper.CollectionToString(spaces));
@@ -256,10 +254,16 @@ public IList[] GetMany(
256254

257255
spacesToCheck.Add(querySpaces);
258256
checkedSpacesIndexes.Add(i);
259-
// The timestamp is the first element of the cache result.
260-
checkedSpacesTimestamp.Add((long) cacheable[0]);
257+
var timestamp = GetResultsMetadata(cacheable, out var aliases);
258+
checkedSpacesTimestamp.Add(timestamp);
261259
if (Log.IsDebugEnabled())
262260
Log.Debug("Checking query spaces for up-to-dateness [{0}]", StringHelper.CollectionToString(querySpaces));
261+
262+
var key = keys[i];
263+
if (key.ResultTransformer?.AutoDiscoverTypes == true && HasResults(cacheable))
264+
{
265+
key.ResultTransformer.SupplyAutoDiscoveredParameters(queryParameters[i].ResultTransformer, aliases);
266+
}
263267
}
264268

265269
var upToDates = spacesToCheck.Count > 0
@@ -298,12 +302,6 @@ public IList[] GetMany(
298302

299303
finalReturnTypes[i] = GetReturnTypes(key, returnTypes[i], cacheable);
300304
PerformBeforeAssemble(finalReturnTypes[i], session, cacheable);
301-
302-
if (key.ResultTransformer?.AutoDiscoverTypes == true && cacheable.Count > 2)
303-
{
304-
var aliases = (string[]) cacheable[1];
305-
key.ResultTransformer.SupplyAutoDiscoveredParameters(queryParams.ResultTransformer, aliases);
306-
}
307305
}
308306

309307
for (var i = 0; i < keys.Length; i++)
@@ -374,7 +372,12 @@ private static List<object> GetCacheableResult(
374372
IList result,
375373
long ts, string[] aliases)
376374
{
377-
var cacheable = new List<object>(result.Count + 2) { ts, aliases };
375+
var cacheable =
376+
new List<object>(result.Count + 1)
377+
{
378+
aliases == null ? ts : new object[] { ts, aliases }
379+
};
380+
378381
foreach (var row in result)
379382
{
380383
if (returnTypes.Length == 1)
@@ -383,19 +386,37 @@ private static List<object> GetCacheableResult(
383386
}
384387
else
385388
{
386-
cacheable.Add(TypeHelper.Disassemble((object[])row, returnTypes, null, session, null));
389+
cacheable.Add(TypeHelper.Disassemble((object[]) row, returnTypes, null, session, null));
387390
}
388391
}
389392

390393
return cacheable;
391394
}
392395

393-
private static IEnumerable<object> GetResultsEnumerable(IList results)
396+
private static long GetResultsMetadata(IList cacheable, out string[] aliases)
397+
{
398+
aliases = null;
399+
400+
var metadata = cacheable[0];
401+
var timestamp = metadata as long?;
402+
if (timestamp.HasValue)
403+
return timestamp.Value;
404+
405+
var metadataArray = (object[]) metadata;
406+
aliases = (string[]) metadataArray[1];
407+
return (long) metadataArray[0];
408+
}
409+
410+
private static bool HasResults(IList cacheable)
411+
// First element is the timestamp.
412+
=> cacheable.Count > 1;
413+
414+
private static IEnumerable<object> GetResultsEnumerable(IList cacheable)
394415
{
395-
// Skip first element, it is the timestamp, and the second, it is the aliases.
396-
for (var i = 2; i < results.Count; i++)
416+
// Skip first element, it is the timestamp.
417+
for (var i = 1; i < cacheable.Count; i++)
397418
{
398-
yield return results[i];
419+
yield return cacheable[i];
399420
}
400421
}
401422

@@ -404,7 +425,7 @@ private static ICacheAssembler[] GetReturnTypes(
404425
ICacheAssembler[] returnTypes,
405426
IList cacheable)
406427
{
407-
if (key.ResultTransformer?.AutoDiscoverTypes == true && cacheable.Count > 2)
428+
if (key.ResultTransformer?.AutoDiscoverTypes == true && HasResults(cacheable))
408429
{
409430
returnTypes = GuessTypes(cacheable);
410431
}

0 commit comments

Comments
 (0)