Skip to content

Add support for batch loading of a Map of key-value pairs. #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -574,6 +571,35 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
}
}

/**
* Requests to load the map of data provided by the specified keys asynchronously, and returns a composite future
* of the resulting values.
* <p>
* If batching is enabled (the default), you'll have to call {@link DataLoader#dispatch()} at a later stage to
* start batch execution. If you forget this call the future will never be completed (unless already completed,
* and returned from cache).
* <p>
* The key context object may be useful in the batch loader interfaces such as {@link org.dataloader.BatchLoaderWithContext} or
* {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
*
* @param keysAndContexts the map of keys to their respective contexts
*
* @return the composite future of the map of keys and values
*/
public CompletableFuture<Map<K, V>> loadMany(Map<K, ?> keysAndContexts) {
nonNull(keysAndContexts);

synchronized (this) {
Map<K, CompletableFuture<V>> collect = new HashMap<>(keysAndContexts.size());
for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) {
K key = entry.getKey();
Object keyContext = entry.getValue();
collect.put(key, load(key, keyContext));
}
return CompletableFutureKit.allOf(collect);
}
}

/**
* Dispatches the queued load requests to the batch execution function and returns a promise of the result.
* <p>
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/dataloader/impl/CompletableFutureKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.dataloader.annotations.Internal;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

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

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

public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> cfs) {
return CompletableFuture.allOf(cfs.toArray(new CompletableFuture[0]))
return CompletableFuture.allOf(cfs.toArray(CompletableFuture[]::new))
.thenApply(v -> cfs.stream()
.map(CompletableFuture::join)
.collect(toList())
);
}

public static <K, V> CompletableFuture<Map<K, V>> allOf(Map<K, CompletableFuture<V>> cfs) {
return CompletableFuture.allOf(cfs.values().toArray(CompletableFuture[]::new))
.thenApply(v -> cfs.entrySet().stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
task -> task.getValue().join())
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -50,10 +47,14 @@ public void context_is_passed_to_batch_loader_function() {
loader.load("A");
loader.load("B");
loader.loadMany(asList("C", "D"));
Map<String, ?> keysAndContexts = new LinkedHashMap<>();
keysAndContexts.put("E", null);
keysAndContexts.put("F", null);
loader.loadMany(keysAndContexts);

List<String> results = loader.dispatchAndJoin();

assertThat(results, equalTo(asList("A-ctx", "B-ctx", "C-ctx", "D-ctx")));
assertThat(results, equalTo(asList("A-ctx", "B-ctx", "C-ctx", "D-ctx", "E-ctx", "F-ctx")));
}

@Test
Expand All @@ -66,10 +67,14 @@ public void key_contexts_are_passed_to_batch_loader_function() {
loader.load("A", "aCtx");
loader.load("B", "bCtx");
loader.loadMany(asList("C", "D"), asList("cCtx", "dCtx"));
Map<String, String> keysAndContexts = new LinkedHashMap<>();
keysAndContexts.put("E", "eCtx");
keysAndContexts.put("F", "fCtx");
loader.loadMany(keysAndContexts);
Comment on lines +70 to +73
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used LinkedHashMap because it is null safe and retains ordering (so the test(s) should not be flaky).


List<String> results = loader.dispatchAndJoin();

assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx")));
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx", "E-ctx-m:eCtx-l:eCtx", "F-ctx-m:fCtx-l:fCtx")));
}

@Test
Expand All @@ -82,12 +87,17 @@ public void key_contexts_are_passed_to_batch_loader_function_when_batching_disab

CompletableFuture<String> aLoad = loader.load("A", "aCtx");
CompletableFuture<String> bLoad = loader.load("B", "bCtx");
CompletableFuture<List<String>> canDLoad = loader.loadMany(asList("C", "D"), asList("cCtx", "dCtx"));
CompletableFuture<List<String>> cAndDLoad = loader.loadMany(asList("C", "D"), asList("cCtx", "dCtx"));
Map<String, String> keysAndContexts = new LinkedHashMap<>();
keysAndContexts.put("E", "eCtx");
keysAndContexts.put("F", "fCtx");
CompletableFuture<Map<String, String>> eAndFLoad = loader.loadMany(keysAndContexts);

List<String> results = new ArrayList<>(asList(aLoad.join(), bLoad.join()));
results.addAll(canDLoad.join());
results.addAll(cAndDLoad.join());
results.addAll(eAndFLoad.join().values());

assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx")));
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx", "E-ctx-m:eCtx-l:eCtx", "F-ctx-m:fCtx-l:fCtx")));
}

@Test
Expand All @@ -101,9 +111,14 @@ public void missing_key_contexts_are_passed_to_batch_loader_function() {
loader.load("B");
loader.loadMany(asList("C", "D"), singletonList("cCtx"));

Map<String, String> keysAndContexts = new LinkedHashMap<>();
keysAndContexts.put("E", "eCtx");
keysAndContexts.put("F", null);
loader.loadMany(keysAndContexts);

List<String> results = loader.dispatchAndJoin();

assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:null-l:null", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:null-l:null")));
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:null-l:null", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:null-l:null", "E-ctx-m:eCtx-l:eCtx", "F-ctx-m:null-l:null")));
}

@Test
Expand All @@ -125,9 +140,14 @@ public void context_is_passed_to_map_batch_loader_function() {
loader.load("B");
loader.loadMany(asList("C", "D"), singletonList("cCtx"));

Map<String, String> keysAndContexts = new LinkedHashMap<>();
keysAndContexts.put("E", "eCtx");
keysAndContexts.put("F", null);
loader.loadMany(keysAndContexts);

List<String> results = loader.dispatchAndJoin();

assertThat(results, equalTo(asList("A-ctx-aCtx", "B-ctx-null", "C-ctx-cCtx", "D-ctx-null")));
assertThat(results, equalTo(asList("A-ctx-aCtx", "B-ctx-null", "C-ctx-cCtx", "D-ctx-null", "E-ctx-eCtx", "F-ctx-null")));
}

@Test
Expand All @@ -142,9 +162,14 @@ public void null_is_passed_as_context_if_you_do_nothing() {
loader.load("B");
loader.loadMany(asList("C", "D"));

Map<String, String> keysAndContexts = new LinkedHashMap<>();
keysAndContexts.put("E", null);
keysAndContexts.put("F", null);
loader.loadMany(keysAndContexts);

List<String> results = loader.dispatchAndJoin();

assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null")));
assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null", "E-null", "F-null")));
}

@Test
Expand All @@ -160,9 +185,14 @@ public void null_is_passed_as_context_to_map_loader_if_you_do_nothing() {
loader.load("B");
loader.loadMany(asList("C", "D"));

Map<String, String> keysAndContexts = new LinkedHashMap<>();
keysAndContexts.put("E", null);
keysAndContexts.put("F", null);
loader.loadMany(keysAndContexts);

List<String> results = loader.dispatchAndJoin();

assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null")));
assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null", "E-null", "F-null")));
}

@Test
Expand Down
12 changes: 7 additions & 5 deletions src/test/java/org/dataloader/DataLoaderStatsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -118,19 +119,20 @@ public void stats_are_collected_with_caching_disabled() {
loader.load("A");
loader.load("B");
loader.loadMany(asList("C", "D"));
loader.loadMany(Map.of("E", "E", "F", "F"));

Statistics stats = loader.getStatistics();
assertThat(stats.getLoadCount(), equalTo(4L));
assertThat(stats.getLoadCount(), equalTo(6L));
assertThat(stats.getBatchInvokeCount(), equalTo(0L));
assertThat(stats.getBatchLoadCount(), equalTo(0L));
assertThat(stats.getCacheHitCount(), equalTo(0L));

loader.dispatch();

stats = loader.getStatistics();
assertThat(stats.getLoadCount(), equalTo(4L));
assertThat(stats.getLoadCount(), equalTo(6L));
assertThat(stats.getBatchInvokeCount(), equalTo(1L));
assertThat(stats.getBatchLoadCount(), equalTo(4L));
assertThat(stats.getBatchLoadCount(), equalTo(6L));
assertThat(stats.getCacheHitCount(), equalTo(0L));

loader.load("A");
Expand All @@ -139,9 +141,9 @@ public void stats_are_collected_with_caching_disabled() {
loader.dispatch();

stats = loader.getStatistics();
assertThat(stats.getLoadCount(), equalTo(6L));
assertThat(stats.getLoadCount(), equalTo(8L));
assertThat(stats.getBatchInvokeCount(), equalTo(2L));
assertThat(stats.getBatchLoadCount(), equalTo(6L));
assertThat(stats.getBatchLoadCount(), equalTo(8L));
assertThat(stats.getCacheHitCount(), equalTo(0L));
}

Expand Down
Loading
Loading