Skip to content

Commit 22413be

Browse files
author
Harrison Cole
committed
DataLoader and CompletableFutureKit: add support for loadMany given a Map of keys and context objects.
1 parent 8df8b2f commit 22413be

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/main/java/org/dataloader/DataLoader.java

+30-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import java.time.Clock;
2626
import java.time.Duration;
2727
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.*;
3229
import java.util.concurrent.CompletableFuture;
3330
import java.util.function.BiConsumer;
3431

@@ -574,6 +571,35 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
574571
}
575572
}
576573

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+
577603
/**
578604
* Dispatches the queued load requests to the batch execution function and returns a promise of the result.
579605
* <p>

src/main/java/org/dataloader/impl/CompletableFutureKit.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.dataloader.annotations.Internal;
44

55
import java.util.List;
6+
import java.util.Map;
67
import java.util.concurrent.CompletableFuture;
78
import java.util.concurrent.ExecutionException;
9+
import java.util.stream.Collectors;
810

911
import static java.util.stream.Collectors.toList;
1012

@@ -48,10 +50,21 @@ public static <V> boolean failed(CompletableFuture<V> future) {
4850
}
4951

5052
public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> cfs) {
51-
return CompletableFuture.allOf(cfs.toArray(new CompletableFuture[0]))
53+
return CompletableFuture.allOf(cfs.toArray(CompletableFuture[]::new))
5254
.thenApply(v -> cfs.stream()
5355
.map(CompletableFuture::join)
5456
.collect(toList())
5557
);
5658
}
59+
60+
public static <K, V> CompletableFuture<Map<K, V>> allOf(Map<K, CompletableFuture<V>> cfs) {
61+
return CompletableFuture.allOf(cfs.values().toArray(CompletableFuture[]::new))
62+
.thenApply(v -> cfs.entrySet().stream()
63+
.collect(
64+
Collectors.toMap(
65+
Map.Entry::getKey,
66+
task -> task.getValue().join())
67+
)
68+
);
69+
}
5770
}

0 commit comments

Comments
 (0)