|
25 | 25 | import java.time.Clock;
|
26 | 26 | import java.time.Duration;
|
27 | 27 | import java.time.Instant;
|
28 |
| -import java.util.ArrayList; |
29 |
| -import java.util.Collections; |
30 |
| -import java.util.List; |
31 |
| -import java.util.Optional; |
| 28 | +import java.util.*; |
32 | 29 | import java.util.concurrent.CompletableFuture;
|
33 | 30 | import java.util.function.BiConsumer;
|
34 | 31 |
|
@@ -574,6 +571,35 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
|
574 | 571 | }
|
575 | 572 | }
|
576 | 573 |
|
| 574 | + /** |
| 575 | + * Requests to load the map of data provided by the specified keys asynchronously, and returns a composite future |
| 576 | + * of the resulting values. |
| 577 | + * <p> |
| 578 | + * If batching is enabled (the default), you'll have to call {@link DataLoader#dispatch()} at a later stage to |
| 579 | + * start batch execution. If you forget this call the future will never be completed (unless already completed, |
| 580 | + * and returned from cache). |
| 581 | + * <p> |
| 582 | + * The key context object may be useful in the batch loader interfaces such as {@link org.dataloader.BatchLoaderWithContext} or |
| 583 | + * {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data. |
| 584 | + * |
| 585 | + * @param keysAndContexts the map of keys to their respective contexts |
| 586 | + * |
| 587 | + * @return the composite future of the map of keys and values |
| 588 | + */ |
| 589 | + public CompletableFuture<Map<K, V>> loadMany(Map<K, ?> keysAndContexts) { |
| 590 | + nonNull(keysAndContexts); |
| 591 | + |
| 592 | + synchronized (this) { |
| 593 | + Map<K, CompletableFuture<V>> collect = new HashMap<>(keysAndContexts.size()); |
| 594 | + for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) { |
| 595 | + K key = entry.getKey(); |
| 596 | + Object keyContext = entry.getValue(); |
| 597 | + collect.put(key, load(key, keyContext)); |
| 598 | + } |
| 599 | + return CompletableFutureKit.allOf(collect); |
| 600 | + } |
| 601 | + } |
| 602 | + |
577 | 603 | /**
|
578 | 604 | * Dispatches the queued load requests to the batch execution function and returns a promise of the result.
|
579 | 605 | * <p>
|
|
0 commit comments