@@ -298,8 +298,7 @@ public CompletableFuture<V> load(K key) {
298
298
} else {
299
299
stats .incrementBatchLoadCountBy (1 );
300
300
// immediate execution of batch function
301
- Object context = loaderOptions .getBatchContextProvider ().get ();
302
- future = invokeLoaderImmediately (key , context );
301
+ future = invokeLoaderImmediately (key );
303
302
}
304
303
if (cachingEnabled ) {
305
304
futureCache .set (cacheKey , future );
@@ -331,25 +330,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
331
330
}
332
331
}
333
332
334
- private CompletableFuture <V > invokeLoaderImmediately (K key , Object context ) {
335
- List <K > keys = singletonList (key );
336
- CompletionStage <V > singleLoadCall ;
337
- if (isMapLoader ()) {
338
- singleLoadCall = mapBatchLoadFunction
339
- .load (keys , context )
340
- .thenApply (map -> map .get (key ));
341
- } else {
342
- singleLoadCall = batchLoadFunction
343
- .load (keys , context )
344
- .thenApply (list -> list .get (0 ));
345
- }
346
- return singleLoadCall .toCompletableFuture ();
347
- }
348
-
349
- private boolean isMapLoader () {
350
- return mapBatchLoadFunction != null ;
351
- }
352
-
353
333
/**
354
334
* Dispatches the queued load requests to the batch execution function and returns a promise of the result.
355
335
* <p>
@@ -420,7 +400,7 @@ private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<
420
400
@ SuppressWarnings ("unchecked" )
421
401
private CompletableFuture <List <V >> dispatchQueueBatch (List <K > keys , List <CompletableFuture <V >> queuedFutures ) {
422
402
stats .incrementBatchLoadCountBy (keys .size ());
423
- CompletionStage <List <V >> batchLoad = invokeBatchFunction (keys );
403
+ CompletionStage <List <V >> batchLoad = invokeLoader (keys );
424
404
return batchLoad
425
405
.toCompletableFuture ()
426
406
.thenApply (values -> {
@@ -463,31 +443,51 @@ private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Complet
463
443
});
464
444
}
465
445
466
- private CompletionStage <List <V >> invokeBatchFunction (List <K > keys ) {
446
+ private boolean isMapLoader () {
447
+ return mapBatchLoadFunction != null ;
448
+ }
449
+
450
+ private CompletableFuture <V > invokeLoaderImmediately (K key ) {
451
+ BatchLoaderEnvironment environment = loaderOptions .getBatchLoaderEnvironmentProvider ().get ();
452
+ List <K > keys = singletonList (key );
453
+ CompletionStage <V > singleLoadCall ;
454
+ if (isMapLoader ()) {
455
+ singleLoadCall = mapBatchLoadFunction
456
+ .load (keys , environment )
457
+ .thenApply (map -> map .get (key ));
458
+ } else {
459
+ singleLoadCall = batchLoadFunction
460
+ .load (keys , environment )
461
+ .thenApply (list -> list .get (0 ));
462
+ }
463
+ return singleLoadCall .toCompletableFuture ();
464
+ }
465
+
466
+ private CompletionStage <List <V >> invokeLoader (List <K > keys ) {
467
467
CompletionStage <List <V >> batchLoad ;
468
468
try {
469
- Object context = loaderOptions .getBatchContextProvider ().get ();
469
+ BatchLoaderEnvironment environment = loaderOptions .getBatchLoaderEnvironmentProvider ().get ();
470
470
if (isMapLoader ()) {
471
- batchLoad = invokeMapBatchLoader (keys , context );
471
+ batchLoad = invokeMapBatchLoader (keys , environment );
472
472
} else {
473
- batchLoad = invokeListBatchLoader (keys , context );
473
+ batchLoad = invokeListBatchLoader (keys , environment );
474
474
}
475
475
} catch (Exception e ) {
476
476
batchLoad = CompletableFutureKit .failedFuture (e );
477
477
}
478
478
return batchLoad ;
479
479
}
480
480
481
- private CompletionStage <List <V >> invokeListBatchLoader (List <K > keys , Object context ) {
482
- return nonNull (batchLoadFunction .load (keys , context ), "Your batch loader function MUST return a non null CompletionStage promise" );
481
+ private CompletionStage <List <V >> invokeListBatchLoader (List <K > keys , BatchLoaderEnvironment environment ) {
482
+ return nonNull (batchLoadFunction .load (keys , environment ), "Your batch loader function MUST return a non null CompletionStage promise" );
483
483
}
484
484
485
485
/*
486
486
* Turns a map of results that MAY be smaller than the key list back into a list by mapping null
487
487
* to missing elements.
488
488
*/
489
- private CompletionStage <List <V >> invokeMapBatchLoader (List <K > keys , Object context ) {
490
- CompletionStage <Map <K , V >> mapBatchLoad = nonNull (mapBatchLoadFunction .load (keys , context ), "Your batch loader function MUST return a non null CompletionStage promise" );
489
+ private CompletionStage <List <V >> invokeMapBatchLoader (List <K > keys , BatchLoaderEnvironment environment ) {
490
+ CompletionStage <Map <K , V >> mapBatchLoad = nonNull (mapBatchLoadFunction .load (keys , environment ), "Your batch loader function MUST return a non null CompletionStage promise" );
491
491
return mapBatchLoad .thenApply (map -> {
492
492
List <V > values = new ArrayList <>();
493
493
for (K key : keys ) {
0 commit comments