@@ -74,7 +74,7 @@ internal async Task<object[]> GetCollectionBatchAsync(ICollectionPersister colle
74
74
foreach ( KeyValuePair < CollectionEntry , IPersistentCollection > me in map )
75
75
{
76
76
cancellationToken . ThrowIfCancellationRequested ( ) ;
77
- if ( await ( ProcessKeyAsync ( me ) ) . ConfigureAwait ( false ) )
77
+ if ( ProcessKey ( me ) ?? await ( CheckCacheAndProcessResultAsync ( ) ) . ConfigureAwait ( false ) )
78
78
{
79
79
return keys ;
80
80
}
@@ -107,7 +107,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
107
107
{
108
108
for ( var j = 0 ; j < collectionKeys . Count ; j ++ )
109
109
{
110
- if ( await ( ProcessKeyAsync ( collectionKeys [ indexes [ j ] ] . Key ) ) . ConfigureAwait ( false ) )
110
+ if ( ProcessKey ( collectionKeys [ indexes [ j ] ] . Key ) == true )
111
111
{
112
112
return true ;
113
113
}
@@ -118,7 +118,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
118
118
var results = await ( AreCachedAsync ( collectionKeys , indexes , collectionPersister , batchableCache , checkCache , cancellationToken ) ) . ConfigureAwait ( false ) ;
119
119
for ( var j = 0 ; j < results . Length ; j ++ )
120
120
{
121
- if ( ! results [ j ] && await ( ProcessKeyAsync ( collectionKeys [ indexes [ j ] ] . Key , true ) ) . ConfigureAwait ( false ) )
121
+ if ( ! results [ j ] && ProcessKey ( collectionKeys [ indexes [ j ] ] . Key , true ) == true )
122
122
{
123
123
return true ;
124
124
}
@@ -132,91 +132,84 @@ async Task<bool> CheckCacheAndProcessResultAsync()
132
132
return false ;
133
133
}
134
134
135
- Task < bool > ProcessKeyAsync ( KeyValuePair < CollectionEntry , IPersistentCollection > me , bool ignoreCache = false )
135
+ bool ? ProcessKey ( KeyValuePair < CollectionEntry , IPersistentCollection > me , bool ignoreCache = false )
136
136
{
137
- try
137
+ var ce = me . Key ;
138
+ var collection = me . Value ;
139
+ if ( ce . LoadedKey == null )
138
140
{
139
- var ce = me . Key ;
140
- var collection = me . Value ;
141
- if ( ce . LoadedKey == null )
142
- {
143
- // the LoadedKey of the CollectionEntry might be null as it might have been reset to null
144
- // (see for example Collections.ProcessDereferencedCollection()
145
- // and CollectionEntry.AfterAction())
146
- // though we clear the queue on flush, it seems like a good idea to guard
147
- // against potentially null LoadedKey:s
148
- return Task . FromResult < bool > ( false ) ;
149
- }
141
+ // the LoadedKey of the CollectionEntry might be null as it might have been reset to null
142
+ // (see for example Collections.ProcessDereferencedCollection()
143
+ // and CollectionEntry.AfterAction())
144
+ // though we clear the queue on flush, it seems like a good idea to guard
145
+ // against potentially null LoadedKey:s
146
+ return false ;
147
+ }
150
148
151
- if ( collection . WasInitialized )
152
- {
153
- log . Warn ( "Encountered initialized collection in BatchFetchQueue, this should not happen." ) ;
154
- return Task . FromResult < bool > ( false ) ;
155
- }
149
+ if ( collection . WasInitialized )
150
+ {
151
+ log . Warn ( "Encountered initialized collection in BatchFetchQueue, this should not happen." ) ;
152
+ return false ;
153
+ }
156
154
157
- if ( checkForEnd && ( index == map . Count || index >= keyIndex . Value + batchSize ) )
155
+ if ( checkForEnd && ( index == map . Count || index >= keyIndex . Value + batchSize ) )
156
+ {
157
+ return true ;
158
+ }
159
+ if ( collectionPersister . KeyType . IsEqual ( key , ce . LoadedKey , collectionPersister . Factory ) )
160
+ {
161
+ if ( collectionEntries != null )
158
162
{
159
- return Task . FromResult < bool > ( true ) ;
163
+ collectionEntries [ 0 ] = ce ;
160
164
}
161
- if ( collectionPersister . KeyType . IsEqual ( key , ce . LoadedKey , collectionPersister . Factory ) )
165
+ keyIndex = index ;
166
+ }
167
+ else if ( ! checkCache || batchableCache == null )
168
+ {
169
+ if ( index < map . Count && ( ! keyIndex . HasValue || index < keyIndex . Value ) )
162
170
{
163
- if ( collectionEntries != null )
164
- {
165
- collectionEntries [ 0 ] = ce ;
166
- }
167
- keyIndex = index ;
171
+ collectionKeys . Add ( new KeyValuePair < KeyValuePair < CollectionEntry , IPersistentCollection > , int > ( me , index ) ) ;
172
+ return false ;
168
173
}
169
- else if ( ! checkCache || batchableCache == null )
170
- {
171
- if ( index < map . Count && ( ! keyIndex . HasValue || index < keyIndex . Value ) )
172
- {
173
- collectionKeys . Add ( new KeyValuePair < KeyValuePair < CollectionEntry , IPersistentCollection > , int > ( me , index ) ) ;
174
- return Task . FromResult < bool > ( false ) ;
175
- }
176
174
177
- // No need to check "!checkCache || !IsCached(ce.LoadedKey, collectionPersister)":
178
- // "batchableCache == null" already means there is no cache, so IsCached can only yield false.
179
- // (This method is now removed.)
180
- if ( collectionEntries != null )
181
- {
182
- collectionEntries [ i ] = ce ;
183
- }
184
- keys [ i ++ ] = ce . LoadedKey ;
185
- }
186
- else if ( ignoreCache )
175
+ // No need to check "!checkCache || !IsCached(ce.LoadedKey, collectionPersister)":
176
+ // "batchableCache == null" already means there is no cache, so IsCached can only yield false.
177
+ // (This method is now removed.)
178
+ if ( collectionEntries != null )
187
179
{
188
- if ( collectionEntries != null )
189
- {
190
- collectionEntries [ i ] = ce ;
191
- }
192
- keys [ i ++ ] = ce . LoadedKey ;
180
+ collectionEntries [ i ] = ce ;
193
181
}
194
- else
182
+ keys [ i ++ ] = ce . LoadedKey ;
183
+ }
184
+ else if ( ignoreCache )
185
+ {
186
+ if ( collectionEntries != null )
195
187
{
196
- collectionKeys . Add ( new KeyValuePair < KeyValuePair < CollectionEntry , IPersistentCollection > , int > ( me , index ) ) ;
197
- // Check the cache only when we have collected as many keys as are needed to fill the batch,
198
- // that are after the demanded key.
199
- if ( ! keyIndex . HasValue || index < keyIndex . Value + batchSize )
200
- {
201
- return Task . FromResult < bool > ( false ) ;
202
- }
203
- return CheckCacheAndProcessResultAsync ( ) ;
188
+ collectionEntries [ i ] = ce ;
204
189
}
205
- if ( i == batchSize )
190
+ keys [ i ++ ] = ce . LoadedKey ;
191
+ }
192
+ else
193
+ {
194
+ collectionKeys . Add ( new KeyValuePair < KeyValuePair < CollectionEntry , IPersistentCollection > , int > ( me , index ) ) ;
195
+ // Check the cache only when we have collected as many keys as are needed to fill the batch,
196
+ // that are after the demanded key.
197
+ if ( ! keyIndex . HasValue || index < keyIndex . Value + batchSize )
206
198
{
207
- i = 1 ; // End of array, start filling again from start
208
- if ( index == map . Count || keyIndex . HasValue )
209
- {
210
- checkForEnd = true ;
211
- return Task . FromResult < bool > ( index == map . Count || index >= keyIndex . Value + batchSize ) ;
212
- }
199
+ return false ;
213
200
}
214
- return Task . FromResult < bool > ( false ) ;
201
+ return null ;
215
202
}
216
- catch ( Exception ex )
203
+ if ( i == batchSize )
217
204
{
218
- return Task . FromException < bool > ( ex ) ;
205
+ i = 1 ; // End of array, start filling again from start
206
+ if ( index == map . Count || keyIndex . HasValue )
207
+ {
208
+ checkForEnd = true ;
209
+ return index == map . Count || index >= keyIndex . Value + batchSize ;
210
+ }
219
211
}
212
+ return false ;
220
213
}
221
214
}
222
215
@@ -273,7 +266,7 @@ internal async Task<object[]> GetEntityBatchAsync(IEntityPersister persister, ob
273
266
foreach ( var key in set )
274
267
{
275
268
cancellationToken . ThrowIfCancellationRequested ( ) ;
276
- if ( await ( ProcessKeyAsync ( key ) ) . ConfigureAwait ( false ) )
269
+ if ( ProcessKey ( key ) ?? await ( CheckCacheAndProcessResultAsync ( ) ) . ConfigureAwait ( false ) )
277
270
{
278
271
return ids ;
279
272
}
@@ -306,7 +299,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
306
299
{
307
300
for ( var j = 0 ; j < entityKeys . Count ; j ++ )
308
301
{
309
- if ( await ( ProcessKeyAsync ( entityKeys [ indexes [ j ] ] . Key ) ) . ConfigureAwait ( false ) )
302
+ if ( ProcessKey ( entityKeys [ indexes [ j ] ] . Key ) == true )
310
303
{
311
304
return true ;
312
305
}
@@ -317,7 +310,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
317
310
var results = await ( AreCachedAsync ( entityKeys , indexes , persister , batchableCache , checkCache , cancellationToken ) ) . ConfigureAwait ( false ) ;
318
311
for ( var j = 0 ; j < results . Length ; j ++ )
319
312
{
320
- if ( ! results [ j ] && await ( ProcessKeyAsync ( entityKeys [ indexes [ j ] ] . Key , true ) ) . ConfigureAwait ( false ) )
313
+ if ( ! results [ j ] && ProcessKey ( entityKeys [ indexes [ j ] ] . Key , true ) == true )
321
314
{
322
315
return true ;
323
316
}
@@ -331,62 +324,55 @@ async Task<bool> CheckCacheAndProcessResultAsync()
331
324
return false ;
332
325
}
333
326
334
- Task < bool > ProcessKeyAsync ( EntityKey key , bool ignoreCache = false )
327
+ bool ? ProcessKey ( EntityKey key , bool ignoreCache = false )
335
328
{
336
- try
329
+ //TODO: this needn't exclude subclasses...
330
+ if ( checkForEnd && ( index == set . Count || index >= idIndex . Value + batchSize ) )
337
331
{
338
- //TODO: this needn't exclude subclasses...
339
- if ( checkForEnd && ( index == set . Count || index >= idIndex . Value + batchSize ) )
340
- {
341
- return Task . FromResult < bool > ( true ) ;
342
- }
343
- if ( persister . IdentifierType . IsEqual ( id , key . Identifier ) )
344
- {
345
- idIndex = index ;
346
- }
347
- else if ( ! checkCache || batchableCache == null )
348
- {
349
- if ( index < set . Count && ( ! idIndex . HasValue || index < idIndex . Value ) )
350
- {
351
- entityKeys . Add ( new KeyValuePair < EntityKey , int > ( key , index ) ) ;
352
- return Task . FromResult < bool > ( false ) ;
353
- }
354
-
355
- // No need to check "!checkCache || !IsCached(key, persister)": "batchableCache == null"
356
- // already means there is no cache, so IsCached can only yield false. (This method is now
357
- // removed.)
358
- ids [ i ++ ] = key . Identifier ;
359
- }
360
- else if ( ignoreCache )
361
- {
362
- ids [ i ++ ] = key . Identifier ;
363
- }
364
- else
332
+ return true ;
333
+ }
334
+ if ( persister . IdentifierType . IsEqual ( id , key . Identifier ) )
335
+ {
336
+ idIndex = index ;
337
+ }
338
+ else if ( ! checkCache || batchableCache == null )
339
+ {
340
+ if ( index < set . Count && ( ! idIndex . HasValue || index < idIndex . Value ) )
365
341
{
366
342
entityKeys . Add ( new KeyValuePair < EntityKey , int > ( key , index ) ) ;
367
- // Check the cache only when we have collected as many keys as are needed to fill the batch,
368
- // that are after the demanded key.
369
- if ( ! idIndex . HasValue || index < idIndex . Value + batchSize )
370
- {
371
- return Task . FromResult < bool > ( false ) ;
372
- }
373
- return CheckCacheAndProcessResultAsync ( ) ;
343
+ return false ;
374
344
}
375
- if ( i == batchSize )
345
+
346
+ // No need to check "!checkCache || !IsCached(key, persister)": "batchableCache == null"
347
+ // already means there is no cache, so IsCached can only yield false. (This method is now
348
+ // removed.)
349
+ ids [ i ++ ] = key . Identifier ;
350
+ }
351
+ else if ( ignoreCache )
352
+ {
353
+ ids [ i ++ ] = key . Identifier ;
354
+ }
355
+ else
356
+ {
357
+ entityKeys . Add ( new KeyValuePair < EntityKey , int > ( key , index ) ) ;
358
+ // Check the cache only when we have collected as many keys as are needed to fill the batch,
359
+ // that are after the demanded key.
360
+ if ( ! idIndex . HasValue || index < idIndex . Value + batchSize )
376
361
{
377
- i = 1 ; // End of array, start filling again from start
378
- if ( index == set . Count || idIndex . HasValue )
379
- {
380
- checkForEnd = true ;
381
- return Task . FromResult < bool > ( index == set . Count || index >= idIndex . Value + batchSize ) ;
382
- }
362
+ return false ;
383
363
}
384
- return Task . FromResult < bool > ( false ) ;
364
+ return null ;
385
365
}
386
- catch ( Exception ex )
366
+ if ( i == batchSize )
387
367
{
388
- return Task . FromException < bool > ( ex ) ;
368
+ i = 1 ; // End of array, start filling again from start
369
+ if ( index == set . Count || idIndex . HasValue )
370
+ {
371
+ checkForEnd = true ;
372
+ return index == set . Count || index >= idIndex . Value + batchSize ;
373
+ }
389
374
}
375
+ return false ;
390
376
}
391
377
}
392
378
0 commit comments