Skip to content

Commit 2eb399b

Browse files
andimarekfxbonnet
authored andcommitted
Fix value cache batch loader environment
1 parent e04c76d commit 2eb399b

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/main/java/org/dataloader/DataLoaderHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ CompletableFuture<List<V>> invokeLoader(BatchLoaderEnvironment environment, List
480480
// we missed some keys from cache, so send them to the batch loader
481481
// and then fill in their values
482482
//
483-
CompletableFuture<List<V>> batchLoad = invokeLoader(environment, missedKeys, missedKeyContexts, missedQueuedFutures);
483+
BatchLoaderEnvironment missedEnvironment = mkBatchLoaderEnv(missedKeys, missedKeyContexts);
484+
CompletableFuture<List<V>> batchLoad = invokeLoader(missedEnvironment, missedKeys, missedKeyContexts, missedQueuedFutures);
484485
return batchLoad.thenCompose(missedValues -> {
485486
assertResultSize(missedKeys, missedValues);
486487

src/test/java/org/dataloader/DataLoaderBatchLoaderEnvironmentTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.dataloader;
22

3+
import org.dataloader.fixtures.CustomValueCache;
34
import org.junit.jupiter.api.Test;
45

56
import java.util.*;
67
import java.util.concurrent.CompletableFuture;
78
import java.util.concurrent.atomic.AtomicInteger;
9+
import java.util.concurrent.atomic.AtomicReference;
810
import java.util.stream.Collectors;
911

1012
import static java.util.Arrays.asList;
@@ -216,4 +218,35 @@ public void mmap_semantics_apply_to_batch_loader_context() {
216218
assertThat(results, equalTo(asList("A-ctx-m:overridesCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "A-ctx-m:overridesCtx-l:overridesCtx")));
217219
}
218220

221+
@Test
222+
public void value_cache_misses_are_passed_matching_key_contexts_to_batch_loader_function() {
223+
CustomValueCache valueCache = new CustomValueCache();
224+
valueCache.asMap().put("A", "cachedA");
225+
226+
AtomicReference<List<String>> batchKeysRef = new AtomicReference<>();
227+
BatchLoaderWithContext<String, String> batchLoader = (keys, environment) -> {
228+
batchKeysRef.set(new ArrayList<>(keys));
229+
AtomicInteger index = new AtomicInteger(0);
230+
List<String> list = keys.stream().map(k -> {
231+
int i = index.getAndIncrement();
232+
Object keyContextM = environment.getKeyContexts().get(k);
233+
Object keyContextL = environment.getKeyContextsList().get(i);
234+
return k + "-m:" + keyContextM + "-l:" + keyContextL;
235+
}).collect(Collectors.toList());
236+
return CompletableFuture.completedFuture(list);
237+
};
238+
DataLoaderOptions options = DataLoaderOptions.newOptions()
239+
.setValueCache(valueCache)
240+
.build();
241+
DataLoader<String, String> loader = newDataLoader(batchLoader, options);
242+
243+
loader.load("A", "aCtx");
244+
loader.load("B", "bCtx");
245+
246+
List<String> results = loader.dispatchAndJoin();
247+
248+
assertThat(batchKeysRef.get(), equalTo(singletonList("B")));
249+
assertThat(results, equalTo(asList("cachedA", "B-m:bCtx-l:bCtx")));
250+
}
251+
219252
}

0 commit comments

Comments
 (0)