1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
- using System . Linq ;
5
4
using NHibernate . Cfg ;
6
5
using NHibernate . Engine ;
7
6
using NHibernate . Persister . Collection ;
@@ -18,7 +17,7 @@ namespace NHibernate.Cache
18
17
/// </summary>
19
18
public partial class StandardQueryCache : IQueryCache , IBatchableQueryCache
20
19
{
21
- private static readonly INHibernateLogger Log = NHibernateLogger . For ( typeof ( StandardQueryCache ) ) ;
20
+ private static readonly INHibernateLogger Log = NHibernateLogger . For ( typeof ( StandardQueryCache ) ) ;
22
21
private readonly string _regionName ;
23
22
private readonly UpdateTimestampsCache _updateTimestampsCache ;
24
23
private readonly CacheBase _cache ;
@@ -133,7 +132,7 @@ public IList Get(
133
132
return null ;
134
133
}
135
134
136
- var timestamp = ( long ) cacheable [ 0 ] ;
135
+ var timestamp = GetResultsMetadata ( cacheable , out var aliases ) ;
137
136
138
137
if ( Log . IsDebugEnabled ( ) )
139
138
Log . Debug ( "Checking query spaces for up-to-dateness [{0}]" , StringHelper . CollectionToString ( spaces ) ) ;
@@ -148,7 +147,6 @@ public IList Get(
148
147
149
148
if ( result != null && key . ResultTransformer ? . AutoDiscoverTypes == true && result . Count > 0 )
150
149
{
151
- var aliases = ( string [ ] ) cacheable [ 1 ] ;
152
150
key . ResultTransformer . SupplyAutoDiscoveredParameters ( queryParameters . ResultTransformer , aliases ) ;
153
151
}
154
152
@@ -174,7 +172,7 @@ public IList Get(QueryKey key, ICacheAssembler[] returnTypes, bool isNaturalKeyL
174
172
return null ;
175
173
}
176
174
177
- var timestamp = ( long ) cacheable [ 0 ] ;
175
+ var timestamp = GetResultsMetadata ( cacheable , out var _ ) ;
178
176
179
177
if ( Log . IsDebugEnabled ( ) )
180
178
Log . Debug ( "Checking query spaces for up-to-dateness [{0}]" , StringHelper . CollectionToString ( spaces ) ) ;
@@ -256,10 +254,16 @@ public IList[] GetMany(
256
254
257
255
spacesToCheck . Add ( querySpaces ) ;
258
256
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 ) ;
261
259
if ( Log . IsDebugEnabled ( ) )
262
260
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
+ }
263
267
}
264
268
265
269
var upToDates = spacesToCheck . Count > 0
@@ -298,12 +302,6 @@ public IList[] GetMany(
298
302
299
303
finalReturnTypes [ i ] = GetReturnTypes ( key , returnTypes [ i ] , cacheable ) ;
300
304
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
- }
307
305
}
308
306
309
307
for ( var i = 0 ; i < keys . Length ; i ++ )
@@ -374,7 +372,12 @@ private static List<object> GetCacheableResult(
374
372
IList result ,
375
373
long ts , string [ ] aliases )
376
374
{
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
+
378
381
foreach ( var row in result )
379
382
{
380
383
if ( returnTypes . Length == 1 )
@@ -383,19 +386,37 @@ private static List<object> GetCacheableResult(
383
386
}
384
387
else
385
388
{
386
- cacheable . Add ( TypeHelper . Disassemble ( ( object [ ] ) row , returnTypes , null , session , null ) ) ;
389
+ cacheable . Add ( TypeHelper . Disassemble ( ( object [ ] ) row , returnTypes , null , session , null ) ) ;
387
390
}
388
391
}
389
392
390
393
return cacheable ;
391
394
}
392
395
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 )
394
415
{
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 ++ )
397
418
{
398
- yield return results [ i ] ;
419
+ yield return cacheable [ i ] ;
399
420
}
400
421
}
401
422
@@ -404,7 +425,7 @@ private static ICacheAssembler[] GetReturnTypes(
404
425
ICacheAssembler [ ] returnTypes ,
405
426
IList cacheable )
406
427
{
407
- if ( key . ResultTransformer ? . AutoDiscoverTypes == true && cacheable . Count > 2 )
428
+ if ( key . ResultTransformer ? . AutoDiscoverTypes == true && HasResults ( cacheable ) )
408
429
{
409
430
returnTypes = GuessTypes ( cacheable ) ;
410
431
}
0 commit comments