Skip to content

Commit bf3edd2

Browse files
committed
Breaking change - adds a name to a DataLoader and deprecates a bunch of factory methods - added support in DLR for namings DLs
1 parent 02a485d commit bf3edd2

File tree

7 files changed

+133
-74
lines changed

7 files changed

+133
-74
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,11 @@ public ValueCache<K, V> getValueCache() {
492492
return valueCache;
493493
}
494494

495+
@Override
496+
public String toString() {
497+
return "DataLoader{" +
498+
"name='" + name + '\'' +
499+
", stats=" + stats +
500+
'}';
501+
}
495502
}

src/main/java/org/dataloader/DataLoaderFactory.java

+49-33
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.dataloader.annotations.PublicApi;
44
import org.jspecify.annotations.Nullable;
55

6+
import static org.dataloader.impl.Assertions.nonNull;
7+
68
/**
79
* A factory class to create {@link DataLoader}s
810
*/
@@ -23,6 +25,20 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
2325
return newDataLoader(batchLoadFunction, null);
2426
}
2527

28+
/**
29+
* Creates new DataLoader with the specified batch loader function and default options
30+
* (batching, caching and unlimited batch size).
31+
*
32+
* @param name the name to use
33+
* @param batchLoadFunction the batch load function to use
34+
* @param <K> the key type
35+
* @param <V> the value type
36+
* @return a new DataLoader
37+
*/
38+
public static <K, V> DataLoader<K, V> newDataLoader(String name, BatchLoader<K, V> batchLoadFunction) {
39+
return newDataLoader(name, batchLoadFunction, null);
40+
}
41+
2642
/**
2743
* Creates new DataLoader with the specified batch loader function with the provided options
2844
*
@@ -46,8 +62,8 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
4662
* @param <V> the value type
4763
* @return a new DataLoader
4864
*/
49-
public static <K, V> DataLoader<K, V> newDataLoader(@Nullable String name, BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
50-
return mkDataLoader(name, batchLoadFunction, options);
65+
public static <K, V> DataLoader<K, V> newDataLoader(String name, BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
66+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
5167
}
5268

5369
/**
@@ -99,8 +115,8 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
99115
* @return a new DataLoader
100116
* @see #newDataLoaderWithTry(BatchLoader)
101117
*/
102-
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(@Nullable String name, BatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
103-
return mkDataLoader(name, batchLoadFunction, options);
118+
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(String name, BatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
119+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
104120
}
105121

106122
/**
@@ -139,8 +155,8 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
139155
* @param <V> the value type
140156
* @return a new DataLoader
141157
*/
142-
public static <K, V> DataLoader<K, V> newDataLoader(@Nullable String name, BatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
143-
return mkDataLoader(name, batchLoadFunction, options);
158+
public static <K, V> DataLoader<K, V> newDataLoader(String name, BatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
159+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
144160
}
145161

146162
/**
@@ -192,8 +208,8 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
192208
* @return a new DataLoader
193209
* @see #newDataLoaderWithTry(BatchLoader)
194210
*/
195-
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(@Nullable String name, BatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
196-
return mkDataLoader(name, batchLoadFunction, options);
211+
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(String name, BatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
212+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
197213
}
198214

199215
/**
@@ -231,8 +247,8 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
231247
* @param <V> the value type
232248
* @return a new DataLoader
233249
*/
234-
public static <K, V> DataLoader<K, V> newMappedDataLoader(@Nullable String name, MappedBatchLoader<K, V> batchLoadFunction, @Nullable DataLoaderOptions options) {
235-
return mkDataLoader(name, batchLoadFunction, options);
250+
public static <K, V> DataLoader<K, V> newMappedDataLoader(String name, MappedBatchLoader<K, V> batchLoadFunction, @Nullable DataLoaderOptions options) {
251+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
236252
}
237253

238254
/**
@@ -285,8 +301,8 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
285301
* @return a new DataLoader
286302
* @see #newDataLoaderWithTry(BatchLoader)
287303
*/
288-
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(@Nullable String name, MappedBatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
289-
return mkDataLoader(name, batchLoadFunction, options);
304+
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(String name, MappedBatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
305+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
290306
}
291307

292308
/**
@@ -325,8 +341,8 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
325341
* @param <V> the value type
326342
* @return a new DataLoader
327343
*/
328-
public static <K, V> DataLoader<K, V> newMappedDataLoader(@Nullable String name, MappedBatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
329-
return mkDataLoader(name, batchLoadFunction, options);
344+
public static <K, V> DataLoader<K, V> newMappedDataLoader(String name, MappedBatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
345+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
330346
}
331347

332348
/**
@@ -378,8 +394,8 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
378394
* @return a new DataLoader
379395
* @see #newDataLoaderWithTry(BatchLoader)
380396
*/
381-
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(@Nullable String name, MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
382-
return mkDataLoader(name, batchLoadFunction, options);
397+
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(String name, MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
398+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
383399
}
384400

385401
/**
@@ -418,8 +434,8 @@ public static <K, V> DataLoader<K, V> newPublisherDataLoader(BatchPublisher<K, V
418434
* @param <V> the value type
419435
* @return a new DataLoader
420436
*/
421-
public static <K, V> DataLoader<K, V> newPublisherDataLoader(@Nullable String name, BatchPublisher<K, V> batchLoadFunction, DataLoaderOptions options) {
422-
return mkDataLoader(name, batchLoadFunction, options);
437+
public static <K, V> DataLoader<K, V> newPublisherDataLoader(String name, BatchPublisher<K, V> batchLoadFunction, DataLoaderOptions options) {
438+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
423439
}
424440

425441
/**
@@ -471,8 +487,8 @@ public static <K, V> DataLoader<K, V> newPublisherDataLoaderWithTry(BatchPublish
471487
* @return a new DataLoader
472488
* @see #newDataLoaderWithTry(BatchLoader)
473489
*/
474-
public static <K, V> DataLoader<K, V> newPublisherDataLoaderWithTry(@Nullable String name, BatchPublisher<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
475-
return mkDataLoader(name, batchLoadFunction, options);
490+
public static <K, V> DataLoader<K, V> newPublisherDataLoaderWithTry(String name, BatchPublisher<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
491+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
476492
}
477493

478494
/**
@@ -511,8 +527,8 @@ public static <K, V> DataLoader<K, V> newPublisherDataLoader(BatchPublisherWithC
511527
* @param <V> the value type
512528
* @return a new DataLoader
513529
*/
514-
public static <K, V> DataLoader<K, V> newPublisherDataLoader(@Nullable String name, BatchPublisherWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
515-
return mkDataLoader(name, batchLoadFunction, options);
530+
public static <K, V> DataLoader<K, V> newPublisherDataLoader(String name, BatchPublisherWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
531+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
516532
}
517533

518534
/**
@@ -564,8 +580,8 @@ public static <K, V> DataLoader<K, V> newPublisherDataLoaderWithTry(BatchPublish
564580
* @return a new DataLoader
565581
* @see #newPublisherDataLoaderWithTry(BatchPublisher)
566582
*/
567-
public static <K, V> DataLoader<K, V> newPublisherDataLoaderWithTry(@Nullable String name, BatchPublisherWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
568-
return mkDataLoader(name, batchLoadFunction, options);
583+
public static <K, V> DataLoader<K, V> newPublisherDataLoaderWithTry(String name, BatchPublisherWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
584+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
569585
}
570586

571587
/**
@@ -604,8 +620,8 @@ public static <K, V> DataLoader<K, V> newMappedPublisherDataLoader(MappedBatchPu
604620
* @param <V> the value type
605621
* @return a new DataLoader
606622
*/
607-
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoader(@Nullable String name, MappedBatchPublisher<K, V> batchLoadFunction, DataLoaderOptions options) {
608-
return mkDataLoader(name, batchLoadFunction, options);
623+
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoader(String name, MappedBatchPublisher<K, V> batchLoadFunction, DataLoaderOptions options) {
624+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
609625
}
610626

611627
/**
@@ -657,8 +673,8 @@ public static <K, V> DataLoader<K, V> newMappedPublisherDataLoaderWithTry(Mapped
657673
* @return a new DataLoader
658674
* @see #newDataLoaderWithTry(BatchLoader)
659675
*/
660-
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoaderWithTry(@Nullable String name, MappedBatchPublisher<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
661-
return mkDataLoader(name, batchLoadFunction, options);
676+
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoaderWithTry(String name, MappedBatchPublisher<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
677+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
662678
}
663679

664680
/**
@@ -697,8 +713,8 @@ public static <K, V> DataLoader<K, V> newMappedPublisherDataLoader(MappedBatchPu
697713
* @param <V> the value type
698714
* @return a new DataLoader
699715
*/
700-
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoader(@Nullable String name, MappedBatchPublisherWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
701-
return mkDataLoader(name, batchLoadFunction, options);
716+
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoader(String name, MappedBatchPublisherWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
717+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
702718
}
703719

704720
/**
@@ -750,11 +766,11 @@ public static <K, V> DataLoader<K, V> newMappedPublisherDataLoaderWithTry(Mapped
750766
* @return a new DataLoader
751767
* @see #newMappedPublisherDataLoaderWithTry(MappedBatchPublisher)
752768
*/
753-
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoaderWithTry(@Nullable String name, MappedBatchPublisherWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
754-
return mkDataLoader(name, batchLoadFunction, options);
769+
public static <K, V> DataLoader<K, V> newMappedPublisherDataLoaderWithTry(String name, MappedBatchPublisherWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
770+
return mkDataLoader(nonNull(name), batchLoadFunction, options);
755771
}
756772

757-
static <K, V> DataLoader<K, V> mkDataLoader(@Nullable String name, Object batchLoadFunction, DataLoaderOptions options) {
773+
static <K, V> DataLoader<K, V> mkDataLoader(@Nullable String name, Object batchLoadFunction, @Nullable DataLoaderOptions options) {
758774
return new DataLoader<>(name, batchLoadFunction, options);
759775
}
760776

src/main/java/org/dataloader/DataLoaderRegistry.java

+39-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.dataloader.instrumentation.DataLoaderInstrumentation;
66
import org.dataloader.instrumentation.DataLoaderInstrumentationHelper;
77
import org.dataloader.stats.Statistics;
8+
import org.jspecify.annotations.NullMarked;
9+
import org.jspecify.annotations.Nullable;
810

911
import java.util.ArrayList;
1012
import java.util.HashMap;
@@ -16,6 +18,8 @@
1618
import java.util.concurrent.ConcurrentHashMap;
1719
import java.util.function.Function;
1820

21+
import static org.dataloader.impl.Assertions.assertState;
22+
1923
/**
2024
* This allows data loaders to be registered together into a single place, so
2125
* they can be dispatched as one. It also allows you to retrieve data loaders by
@@ -35,9 +39,10 @@
3539
* are the same object, then nothing is changed, since the same instrumentation code is being run.
3640
*/
3741
@PublicApi
42+
@NullMarked
3843
public class DataLoaderRegistry {
3944
protected final Map<String, DataLoader<?, ?>> dataLoaders;
40-
protected final DataLoaderInstrumentation instrumentation;
45+
protected final @Nullable DataLoaderInstrumentation instrumentation;
4146

4247

4348
public DataLoaderRegistry() {
@@ -48,27 +53,30 @@ private DataLoaderRegistry(Builder builder) {
4853
this(builder.dataLoaders, builder.instrumentation);
4954
}
5055

51-
protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, DataLoaderInstrumentation instrumentation) {
56+
protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, @Nullable DataLoaderInstrumentation instrumentation) {
5257
this.dataLoaders = instrumentDLs(dataLoaders, instrumentation);
5358
this.instrumentation = instrumentation;
5459
}
5560

56-
private Map<String, DataLoader<?, ?>> instrumentDLs(Map<String, DataLoader<?, ?>> incomingDataLoaders, DataLoaderInstrumentation registryInstrumentation) {
61+
private Map<String, DataLoader<?, ?>> instrumentDLs(Map<String, DataLoader<?, ?>> incomingDataLoaders, @Nullable DataLoaderInstrumentation registryInstrumentation) {
5762
Map<String, DataLoader<?, ?>> dataLoaders = new ConcurrentHashMap<>(incomingDataLoaders);
5863
if (registryInstrumentation != null) {
59-
dataLoaders.replaceAll((k, existingDL) -> instrumentDL(registryInstrumentation, existingDL));
64+
dataLoaders.replaceAll((k, existingDL) -> nameAndInstrumentDL(k, registryInstrumentation, existingDL));
6065
}
6166
return dataLoaders;
6267
}
6368

6469
/**
6570
* Can be called to tweak a {@link DataLoader} so that it has the registry {@link DataLoaderInstrumentation} added as the first one.
6671
*
72+
* @param key the key used to register the data loader
6773
* @param registryInstrumentation the common registry {@link DataLoaderInstrumentation}
6874
* @param existingDL the existing data loader
6975
* @return a new {@link DataLoader} or the same one if there is nothing to change
7076
*/
71-
private static DataLoader<?, ?> instrumentDL(DataLoaderInstrumentation registryInstrumentation, DataLoader<?, ?> existingDL) {
77+
private static DataLoader<?, ?> nameAndInstrumentDL(String key, @Nullable DataLoaderInstrumentation registryInstrumentation, DataLoader<?, ?> existingDL) {
78+
existingDL = checkAndSetName(key, existingDL);
79+
7280
if (registryInstrumentation == null) {
7381
return existingDL;
7482
}
@@ -97,6 +105,15 @@ protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, DataLoad
97105
}
98106
}
99107

108+
private static DataLoader<?, ?> checkAndSetName(String key, DataLoader<?, ?> dataLoader) {
109+
if (dataLoader.getName() == null) {
110+
return dataLoader.transform(b -> b.name(key));
111+
}
112+
assertState(key.equals(dataLoader.getName()),
113+
() -> String.format("Data loader name '%s' is not the same as registered key '%s'", dataLoader.getName(), key));
114+
return dataLoader;
115+
}
116+
100117
private static DataLoader<?, ?> mkInstrumentedDataLoader(DataLoader<?, ?> existingDL, DataLoaderOptions options, DataLoaderInstrumentation newInstrumentation) {
101118
return existingDL.transform(builder -> builder.options(setInInstrumentation(options, newInstrumentation)));
102119
}
@@ -108,7 +125,7 @@ private static DataLoaderOptions setInInstrumentation(DataLoaderOptions options,
108125
/**
109126
* @return the {@link DataLoaderInstrumentation} associated with this registry which can be null
110127
*/
111-
public DataLoaderInstrumentation getInstrumentation() {
128+
public @Nullable DataLoaderInstrumentation getInstrumentation() {
112129
return instrumentation;
113130
}
114131

@@ -120,10 +137,23 @@ public DataLoaderInstrumentation getInstrumentation() {
120137
* @return this registry
121138
*/
122139
public DataLoaderRegistry register(String key, DataLoader<?, ?> dataLoader) {
123-
dataLoaders.put(key, instrumentDL(instrumentation, dataLoader));
140+
dataLoaders.put(key, nameAndInstrumentDL(key, instrumentation, dataLoader));
124141
return this;
125142
}
126143

144+
/**
145+
* This will register a new dataloader and then return it. It might have been wrapped into a new instance
146+
* because of the registry instrumentation for example.
147+
*
148+
* @param key the key to put the data loader under
149+
* @param dataLoader the data loader to register
150+
* @return the data loader instance that was registered
151+
*/
152+
public <K, V> DataLoader<K, V> registerAndGet(String key, DataLoader<?, ?> dataLoader) {
153+
dataLoaders.put(key, nameAndInstrumentDL(key, instrumentation, dataLoader));
154+
return getDataLoader(key);
155+
}
156+
127157
/**
128158
* Computes a data loader if absent or return it if it was
129159
* already registered at that key.
@@ -142,7 +172,7 @@ public <K, V> DataLoader<K, V> computeIfAbsent(final String key,
142172
final Function<String, DataLoader<?, ?>> mappingFunction) {
143173
return (DataLoader<K, V>) dataLoaders.computeIfAbsent(key, (k) -> {
144174
DataLoader<?, ?> dl = mappingFunction.apply(k);
145-
return instrumentDL(instrumentation, dl);
175+
return nameAndInstrumentDL(key, instrumentation, dl);
146176
});
147177
}
148178

@@ -262,7 +292,7 @@ public static Builder newRegistry() {
262292
public static class Builder {
263293

264294
private final Map<String, DataLoader<?, ?>> dataLoaders = new HashMap<>();
265-
private DataLoaderInstrumentation instrumentation;
295+
private @Nullable DataLoaderInstrumentation instrumentation;
266296

267297
/**
268298
* This will register a new dataloader

0 commit comments

Comments
 (0)