@@ -298,8 +298,7 @@ public CompletableFuture<V> load(K key) {
298298 } else {
299299 stats .incrementBatchLoadCountBy (1 );
300300 // immediate execution of batch function
301- Object context = loaderOptions .getBatchContextProvider ().get ();
302- future = invokeLoaderImmediately (key , context );
301+ future = invokeLoaderImmediately (key );
303302 }
304303 if (cachingEnabled ) {
305304 futureCache .set (cacheKey , future );
@@ -331,25 +330,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
331330 }
332331 }
333332
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-
353333 /**
354334 * Dispatches the queued load requests to the batch execution function and returns a promise of the result.
355335 * <p>
@@ -420,7 +400,7 @@ private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<
420400 @ SuppressWarnings ("unchecked" )
421401 private CompletableFuture <List <V >> dispatchQueueBatch (List <K > keys , List <CompletableFuture <V >> queuedFutures ) {
422402 stats .incrementBatchLoadCountBy (keys .size ());
423- CompletionStage <List <V >> batchLoad = invokeBatchFunction (keys );
403+ CompletionStage <List <V >> batchLoad = invokeLoader (keys );
424404 return batchLoad
425405 .toCompletableFuture ()
426406 .thenApply (values -> {
@@ -463,31 +443,51 @@ private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Complet
463443 });
464444 }
465445
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 ) {
467467 CompletionStage <List <V >> batchLoad ;
468468 try {
469- Object context = loaderOptions .getBatchContextProvider ().get ();
469+ BatchLoaderEnvironment environment = loaderOptions .getBatchLoaderEnvironmentProvider ().get ();
470470 if (isMapLoader ()) {
471- batchLoad = invokeMapBatchLoader (keys , context );
471+ batchLoad = invokeMapBatchLoader (keys , environment );
472472 } else {
473- batchLoad = invokeListBatchLoader (keys , context );
473+ batchLoad = invokeListBatchLoader (keys , environment );
474474 }
475475 } catch (Exception e ) {
476476 batchLoad = CompletableFutureKit .failedFuture (e );
477477 }
478478 return batchLoad ;
479479 }
480480
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" );
483483 }
484484
485485 /*
486486 * Turns a map of results that MAY be smaller than the key list back into a list by mapping null
487487 * to missing elements.
488488 */
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" );
491491 return mapBatchLoad .thenApply (map -> {
492492 List <V > values = new ArrayList <>();
493493 for (K key : keys ) {
0 commit comments