|
| 1 | +package org.dataloader.orchestration; |
| 2 | + |
| 3 | +import org.dataloader.DataLoader; |
| 4 | +import org.dataloader.impl.Assertions; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +public class Orchestrator<K, V> { |
| 12 | + |
| 13 | + private final Tracker tracker; |
| 14 | + private final DataLoader<K, V> startingDL; |
| 15 | + private final List<Step<?, ?>> steps = new ArrayList<>(); |
| 16 | + |
| 17 | + /** |
| 18 | + * This will create a new {@link Orchestrator} that can allow multiple calls to multiple data-loader's |
| 19 | + * to be orchestrated so they all run optimally. |
| 20 | + * |
| 21 | + * @param dataLoader the data loader to start with |
| 22 | + * @param <K> the key type |
| 23 | + * @param <V> the value type |
| 24 | + * @return a new {@link Orchestrator} |
| 25 | + */ |
| 26 | + public static <K, V> Orchestrator<K, V> orchestrate(DataLoader<K, V> dataLoader) { |
| 27 | + return new Orchestrator<>(new Tracker(), dataLoader); |
| 28 | + } |
| 29 | + |
| 30 | + public Tracker getTracker() { |
| 31 | + return tracker; |
| 32 | + } |
| 33 | + |
| 34 | + private Orchestrator(Tracker tracker, DataLoader<K, V> dataLoader) { |
| 35 | + this.tracker = tracker; |
| 36 | + this.startingDL = dataLoader; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + public Step<K, V> load(K key) { |
| 41 | + return load(key, null); |
| 42 | + } |
| 43 | + |
| 44 | + public Step<K, V> load(K key, Object keyContext) { |
| 45 | + return Step.loadImpl(this, castAs(startingDL), key, keyContext); |
| 46 | + } |
| 47 | + |
| 48 | + static <T> T castAs(Object o) { |
| 49 | + //noinspection unchecked |
| 50 | + return (T) o; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + <KT, VT> void record(Step<KT, VT> step) { |
| 55 | + steps.add(step); |
| 56 | + tracker.incrementStepCount(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * This is the callback point saying to start the DataLoader loading process. |
| 61 | + * <p> |
| 62 | + * The type of object returned here depends on the value type of the last Step. We cant be truly generic |
| 63 | + * here and must be case. |
| 64 | + * |
| 65 | + * @param <VT> the value type |
| 66 | + * @return the final composed value |
| 67 | + */ |
| 68 | + <VT> CompletableFuture<VT> execute() { |
| 69 | + Assertions.assertState(!steps.isEmpty(), () -> "How can the steps to run be empty??"); |
| 70 | + int index = 0; |
| 71 | + Step<?, ?> firstStep = steps.get(index); |
| 72 | + |
| 73 | + CompletableFuture<Object> currentCF = castAs(firstStep.codeToRun().apply(null)); // first load uses variable capture |
| 74 | + whenComplete(index, firstStep, currentCF); |
| 75 | + |
| 76 | + for (index++; index < steps.size(); index++) { |
| 77 | + Step<?, ?> nextStep = steps.get(index); |
| 78 | + Function<Object, CompletableFuture<?>> codeToRun = castAs(nextStep.codeToRun()); |
| 79 | + CompletableFuture<Object> nextCF = currentCF.thenCompose(value -> castAs(codeToRun.apply(value))); |
| 80 | + currentCF = nextCF; |
| 81 | + |
| 82 | + // side effect when this step is complete |
| 83 | + whenComplete(index, nextStep, nextCF); |
| 84 | + } |
| 85 | + return castAs(currentCF); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + private void whenComplete(int index, Step<?, ?> step, CompletableFuture<Object> cf) { |
| 90 | + cf.whenComplete((v, throwable) -> { |
| 91 | + getTracker().loadCallComplete(step.dataLoader()); |
| 92 | + // replace with instrumentation code |
| 93 | + if (throwable != null) { |
| 94 | + // TODO - should we be cancelling future steps here - no need for dispatch tracking if they will never run |
| 95 | + System.out.println("A throwable has been thrown on step " + index + ": " + throwable.getMessage()); |
| 96 | + throwable.printStackTrace(System.out); |
| 97 | + } else { |
| 98 | + System.out.println("step " + index + " returned : " + v); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | +} |
0 commit comments