20
20
import org .dataloader .stats .Statistics ;
21
21
import org .dataloader .stats .StatisticsCollector ;
22
22
23
- import java .util .AbstractMap .SimpleImmutableEntry ;
24
23
import java .util .ArrayList ;
24
+ import java .util .Collections ;
25
25
import java .util .List ;
26
26
import java .util .concurrent .CompletableFuture ;
27
- import java .util .stream .Collectors ;
28
27
29
28
import static org .dataloader .impl .Assertions .nonNull ;
30
29
@@ -60,7 +59,6 @@ public class DataLoader<K, V> {
60
59
private final DataLoaderHelper <K , V > helper ;
61
60
private final DataLoaderOptions loaderOptions ;
62
61
private final CacheMap <Object , CompletableFuture <V >> futureCache ;
63
- private final List <SimpleImmutableEntry <K , CompletableFuture <V >>> loaderQueue ;
64
62
private final StatisticsCollector stats ;
65
63
66
64
/**
@@ -237,6 +235,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
237
235
* Using Try objects allows you to capture a value returned or an exception that might
238
236
* have occurred trying to get a value. .
239
237
* <p>
238
+ *
240
239
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
241
240
* @param <K> the key type
242
241
* @param <V> the value type
@@ -357,10 +356,9 @@ private DataLoader(Object batchLoadFunction, DataLoaderOptions options) {
357
356
this .loaderOptions = options == null ? new DataLoaderOptions () : options ;
358
357
this .futureCache = determineCacheMap (loaderOptions );
359
358
// order of keys matter in data loader
360
- this .loaderQueue = new ArrayList <>();
361
359
this .stats = nonNull (this .loaderOptions .getStatisticsCollector ());
362
360
363
- this .helper = new DataLoaderHelper <>(this , batchLoadFunction , this .loaderOptions , this .futureCache , this .loaderQueue , this . stats );
361
+ this .helper = new DataLoaderHelper <>(this , batchLoadFunction , this .loaderOptions , this .futureCache , this .stats );
364
362
}
365
363
366
364
@ SuppressWarnings ("unchecked" )
@@ -380,7 +378,26 @@ private CacheMap<Object, CompletableFuture<V>> determineCacheMap(DataLoaderOptio
380
378
* @return the future of the value
381
379
*/
382
380
public CompletableFuture <V > load (K key ) {
383
- return helper .load (key );
381
+ return load (key , null );
382
+ }
383
+
384
+ /**
385
+ * Requests to load the data with the specified key asynchronously, and returns a future of the resulting value.
386
+ * <p>
387
+ * If batching is enabled (the default), you'll have to call {@link DataLoader#dispatch()} at a later stage to
388
+ * start batch execution. If you forget this call the future will never be completed (unless already completed,
389
+ * and returned from cache).
390
+ * <p>
391
+ * The key context object may be useful in the batch loader interfaces such as {@link org.dataloader.BatchLoaderWithContext} or
392
+ * {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
393
+ *
394
+ * @param key the key to load
395
+ * @param keyContext a context object that is specific to this key
396
+ *
397
+ * @return the future of the value
398
+ */
399
+ public CompletableFuture <V > load (K key , Object keyContext ) {
400
+ return helper .load (key , keyContext );
384
401
}
385
402
386
403
/**
@@ -396,11 +413,39 @@ public CompletableFuture<V> load(K key) {
396
413
* @return the composite future of the list of values
397
414
*/
398
415
public CompletableFuture <List <V >> loadMany (List <K > keys ) {
399
- synchronized (this ) {
400
- List <CompletableFuture <V >> collect = keys .stream ()
401
- .map (this ::load )
402
- .collect (Collectors .toList ());
416
+ return loadMany (keys , Collections .emptyList ());
417
+ }
403
418
419
+ /**
420
+ * Requests to load the list of data provided by the specified keys asynchronously, and returns a composite future
421
+ * of the resulting values.
422
+ * <p>
423
+ * If batching is enabled (the default), you'll have to call {@link DataLoader#dispatch()} at a later stage to
424
+ * start batch execution. If you forget this call the future will never be completed (unless already completed,
425
+ * and returned from cache).
426
+ * <p>
427
+ * The key context object may be useful in the batch loader interfaces such as {@link org.dataloader.BatchLoaderWithContext} or
428
+ * {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
429
+ *
430
+ * @param keys the list of keys to load
431
+ * @param keyContexts the list of key calling context objects
432
+ *
433
+ * @return the composite future of the list of values
434
+ */
435
+ public CompletableFuture <List <V >> loadMany (List <K > keys , List <Object > keyContexts ) {
436
+ nonNull (keys );
437
+ nonNull (keyContexts );
438
+
439
+ synchronized (this ) {
440
+ List <CompletableFuture <V >> collect = new ArrayList <>();
441
+ for (int i = 0 ; i < keys .size (); i ++) {
442
+ K key = keys .get (i );
443
+ Object keyContext = null ;
444
+ if (i < keyContexts .size ()) {
445
+ keyContext = keyContexts .get (i );
446
+ }
447
+ collect .add (load (key , keyContext ));
448
+ }
404
449
return CompletableFutureKit .allOf (collect );
405
450
}
406
451
}
@@ -441,9 +486,7 @@ public List<V> dispatchAndJoin() {
441
486
* @return the depth of the batched key loads that need to be dispatched
442
487
*/
443
488
public int dispatchDepth () {
444
- synchronized (this ) {
445
- return loaderQueue .size ();
446
- }
489
+ return helper .dispatchDepth ();
447
490
}
448
491
449
492
0 commit comments