Skip to content

Commit 1b3bec4

Browse files
committed
expand Strategy arguments, make sure strategy is called earlier
1 parent fa1e720 commit 1b3bec4

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,18 @@ CompletableFuture<V> load(K key, Object loadContext) {
188188
}
189189

190190
ctx.onDispatched();
191-
loaderOptions.getDispatchStrategy().loadCalled();
191+
loaderOptions.getDispatchStrategy().loadCalled(dataLoader, key, loadContext, loadCallFuture);
192+
loadCallFuture = loadCallFuture.whenComplete((result, error) -> loaderOptions.getDispatchStrategy().loadCompleted(dataLoader, result, error));
192193
loadCallFuture.whenComplete(ctx::onCompleted);
193-
loadCallFuture.whenComplete((result, error) -> loaderOptions.getDispatchStrategy().loadCompleted());
194194
return loadCallFuture;
195195
}
196196

197197
private CompletableFuture<V> incrementCacheHitAndReturnCF(DataLoaderInstrumentationContext<Object> ctx, K key, Object loadContext, CompletableFuture<V> cachedFuture) {
198198
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key, loadContext));
199199
ctx.onDispatched();
200-
loaderOptions.getDispatchStrategy().loadCalled();
200+
loaderOptions.getDispatchStrategy().loadCalled(dataLoader, key, loadContext, cachedFuture);
201+
cachedFuture = cachedFuture.whenComplete((result, error) -> loaderOptions.getDispatchStrategy().loadCompleted(dataLoader, result, error));
201202
cachedFuture.whenComplete(ctx::onCompleted);
202-
cachedFuture.whenComplete((result, error) -> loaderOptions.getDispatchStrategy().loadCompleted());
203203
return cachedFuture;
204204
}
205205

src/main/java/org/dataloader/DispatchStrategy.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.dataloader.annotations.PublicApi;
44
import org.jspecify.annotations.NullMarked;
5+
import org.jspecify.annotations.Nullable;
6+
7+
import java.util.concurrent.CompletableFuture;
58

69

710
/**
@@ -28,14 +31,14 @@ default void onRegistryCreation(DataLoaderRegistry registry) {
2831
/**
2932
* Called when a {@link DataLoader#load(Object)} is called on a dataloader
3033
*/
31-
default void loadCalled() {
34+
default void loadCalled(DataLoader<?, ?> dataLoader, Object key, @Nullable Object loadContext, CompletableFuture<?> result) {
3235

3336
}
3437

3538
/**
3639
* Called when a {@link DataLoader#load(Object)} is executed and completed on a dataloader
3740
*/
38-
default void loadCompleted() {
41+
default void loadCompleted(DataLoader<?, ?> dataLoader, @Nullable Object result, @Nullable Throwable error) {
3942

4043
}
4144
}

src/main/java/org/dataloader/strategy/GreedyLevelByLevelChainedDispatchStrategy.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.dataloader.strategy;
22

3+
import org.dataloader.DataLoader;
34
import org.dataloader.DataLoaderRegistry;
45
import org.dataloader.DispatchStrategy;
56
import org.dataloader.annotations.VisibleForTesting;
67
import org.dataloader.impl.Assertions;
8+
import org.jspecify.annotations.NullMarked;
79
import org.jspecify.annotations.Nullable;
810

911
import java.time.Duration;
12+
import java.util.concurrent.CompletableFuture;
1013
import java.util.concurrent.ScheduledExecutorService;
1114
import java.util.concurrent.ScheduledFuture;
1215
import java.util.concurrent.TimeUnit;
@@ -25,6 +28,7 @@
2528
* In practice this will greedily fill up DataLoader keys while walking the chain to provide a nice balance of
2629
* batching/dedupe/caching while not needing to worry about manually dispatching the tree.
2730
*/
31+
@NullMarked
2832
public class GreedyLevelByLevelChainedDispatchStrategy implements DispatchStrategy {
2933

3034
private static final Duration DEFAULT_FALLBACK_TIMEOUT = Duration.ofMillis(30);
@@ -35,7 +39,9 @@ public class GreedyLevelByLevelChainedDispatchStrategy implements DispatchStrate
3539
private final Object dispatchLock = new Object();
3640

3741
// only used for tests
38-
private Runnable onIteration;
42+
Runnable onIteration = () -> {
43+
44+
};
3945

4046
private final Duration fallbackTimeout;
4147
@Nullable private ScheduledFuture<?> fallbackDispatchFuture = null;
@@ -53,7 +59,7 @@ public void onRegistryCreation(DataLoaderRegistry registry) {
5359
}
5460

5561
@Override
56-
public void loadCalled() {
62+
public void loadCalled(DataLoader<?, ?> dataLoader, Object key, @Nullable Object loaderContext, CompletableFuture<?> result) {
5763
// initial load called
5864
pendingLoadCount.incrementAndGet();
5965
int previousTotal = totalWorkCount.getAndIncrement();
@@ -63,7 +69,7 @@ public void loadCalled() {
6369
}
6470

6571
@Override
66-
public void loadCompleted() {
72+
public void loadCompleted(DataLoader<?, ?> dataLoader, @Nullable Object result, @Nullable Throwable error) {
6773
pendingLoadCount.decrementAndGet();
6874
}
6975

0 commit comments

Comments
 (0)