Skip to content

Commit 3904878

Browse files
committed
Introduces a Factory for creating DataLoaders
1 parent b570b56 commit 3904878

14 files changed

+554
-120
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ a list of keys
7070
}
7171
};
7272

73-
DataLoader<Long, User> userLoader = DataLoader.newDataLoader(userBatchLoader);
73+
DataLoader<Long, User> userLoader = DataLoaderFactory.newDataLoader(userBatchLoader);
7474

7575
```
7676

@@ -188,7 +188,7 @@ for the context object.
188188
}
189189
};
190190

191-
DataLoader<String, String> loader = DataLoader.newDataLoader(batchLoader, options);
191+
DataLoader<String, String> loader = DataLoaderFactory.newDataLoader(batchLoader, options);
192192
```
193193

194194
The batch loading code will now receive this environment object and it can be used to get context perhaps allowing it
@@ -219,7 +219,7 @@ You can gain access to them as a map by key or as the original list of context o
219219
}
220220
};
221221

222-
DataLoader<String, String> loader = DataLoader.newDataLoader(batchLoader, options);
222+
DataLoader<String, String> loader = DataLoaderFactory.newDataLoader(batchLoader, options);
223223
loader.load("keyA", "contextForA");
224224
loader.load("keyB", "contextForB");
225225
```
@@ -255,7 +255,7 @@ For example, let's assume you want to load users from a database, you could prob
255255
}
256256
};
257257

258-
DataLoader<Long, User> userLoader = DataLoader.newMappedDataLoader(mapBatchLoader);
258+
DataLoader<Long, User> userLoader = DataLoaderFactory.newMappedDataLoader(mapBatchLoader);
259259

260260
// ...
261261
```
@@ -295,7 +295,7 @@ DataLoader supports this type and you can use this form to create a batch loader
295295
and some of which may have failed. From that data loader can infer the right behavior in terms of the `load(x)` promise.
296296

297297
```java
298-
DataLoader<String, User> dataLoader = DataLoader.newDataLoaderWithTry(new BatchLoader<String, Try<User>>() {
298+
DataLoader<String, User> dataLoader = DataLoaderFactory.newDataLoaderWithTry(new BatchLoader<String, Try<User>>() {
299299
@Override
300300
public CompletionStage<List<Try<User>>> load(List<String> keys) {
301301
return CompletableFuture.supplyAsync(() -> {
@@ -320,7 +320,7 @@ react to that, in a type safe manner.
320320
In certain uncommon cases, a DataLoader which does not cache may be desirable.
321321

322322
```java
323-
DataLoader.newDataLoader(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false));
323+
DataLoaderFactory.newDataLoader(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false));
324324
```
325325

326326
Calling the above will ensure that every call to `.load()` will produce a new promise, and requested keys will not be saved in memory.
@@ -387,7 +387,7 @@ You can configure the statistics collector used when you build the data loader
387387

388388
```java
389389
DataLoaderOptions options = DataLoaderOptions.newOptions().setStatisticsCollector(() -> new ThreadLocalStatisticsCollector());
390-
DataLoader<String,User> userDataLoader = DataLoader.newDataLoader(userBatchLoader,options);
390+
DataLoader<String,User> userDataLoader = DataLoaderFactory.newDataLoader(userBatchLoader,options);
391391

392392
```
393393

@@ -414,7 +414,7 @@ However you can create your own custom cache and supply it to the data loader on
414414
```java
415415
MyCustomCache customCache = new MyCustomCache();
416416
DataLoaderOptions options = DataLoaderOptions.newOptions().setCacheMap(customCache);
417-
DataLoader.newDataLoader(userBatchLoader, options);
417+
DataLoaderFactory.newDataLoader(userBatchLoader, options);
418418
```
419419

420420
You could choose to use one of the fancy cache implementations from Guava or Kaffeine and wrap it in a `CacheMap` wrapper ready

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

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
6060
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
6161
*/
62+
@SuppressWarnings("UnusedReturnValue")
6263
@PublicApi
6364
public class DataLoader<K, V> {
6465

@@ -75,7 +76,10 @@ public class DataLoader<K, V> {
7576
* @param <V> the value type
7677
*
7778
* @return a new DataLoader
79+
*
80+
* @deprecated use {@link DataLoaderFactory} instead
7881
*/
82+
@Deprecated
7983
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction) {
8084
return newDataLoader(batchLoadFunction, null);
8185
}
@@ -89,9 +93,12 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
8993
* @param <V> the value type
9094
*
9195
* @return a new DataLoader
96+
*
97+
* @deprecated use {@link DataLoaderFactory} instead
9298
*/
99+
@Deprecated
93100
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
94-
return new DataLoader<>(batchLoadFunction, options);
101+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
95102
}
96103

97104
/**
@@ -110,7 +117,10 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
110117
* @param <V> the value type
111118
*
112119
* @return a new DataLoader
120+
*
121+
* @deprecated use {@link DataLoaderFactory} instead
113122
*/
123+
@Deprecated
114124
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction) {
115125
return newDataLoaderWithTry(batchLoadFunction, null);
116126
}
@@ -127,11 +137,12 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
127137
*
128138
* @return a new DataLoader
129139
*
130-
* @see #newDataLoaderWithTry(BatchLoader)
140+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
141+
* @deprecated use {@link DataLoaderFactory} instead
131142
*/
132-
@SuppressWarnings("unchecked")
143+
@Deprecated
133144
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
134-
return new DataLoader<>((BatchLoader<K, V>) batchLoadFunction, options);
145+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
135146
}
136147

137148
/**
@@ -143,7 +154,10 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
143154
* @param <V> the value type
144155
*
145156
* @return a new DataLoader
157+
*
158+
* @deprecated use {@link DataLoaderFactory} instead
146159
*/
160+
@Deprecated
147161
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction) {
148162
return newDataLoader(batchLoadFunction, null);
149163
}
@@ -157,9 +171,12 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
157171
* @param <V> the value type
158172
*
159173
* @return a new DataLoader
174+
*
175+
* @deprecated use {@link DataLoaderFactory} instead
160176
*/
177+
@Deprecated
161178
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
162-
return new DataLoader<>(batchLoadFunction, options);
179+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
163180
}
164181

165182
/**
@@ -178,7 +195,10 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
178195
* @param <V> the value type
179196
*
180197
* @return a new DataLoader
198+
*
199+
* @deprecated use {@link DataLoaderFactory} instead
181200
*/
201+
@Deprecated
182202
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
183203
return newDataLoaderWithTry(batchLoadFunction, null);
184204
}
@@ -195,10 +215,12 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
195215
*
196216
* @return a new DataLoader
197217
*
198-
* @see #newDataLoaderWithTry(BatchLoader)
218+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
219+
* @deprecated use {@link DataLoaderFactory} instead
199220
*/
221+
@Deprecated
200222
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
201-
return new DataLoader<>(batchLoadFunction, options);
223+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
202224
}
203225

204226
/**
@@ -210,7 +232,10 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
210232
* @param <V> the value type
211233
*
212234
* @return a new DataLoader
235+
*
236+
* @deprecated use {@link DataLoaderFactory} instead
213237
*/
238+
@Deprecated
214239
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction) {
215240
return newMappedDataLoader(batchLoadFunction, null);
216241
}
@@ -224,9 +249,12 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
224249
* @param <V> the value type
225250
*
226251
* @return a new DataLoader
252+
*
253+
* @deprecated use {@link DataLoaderFactory} instead
227254
*/
255+
@Deprecated
228256
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
229-
return new DataLoader<>(batchLoadFunction, options);
257+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
230258
}
231259

232260
/**
@@ -246,7 +274,10 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
246274
* @param <V> the value type
247275
*
248276
* @return a new DataLoader
277+
*
278+
* @deprecated use {@link DataLoaderFactory} instead
249279
*/
280+
@Deprecated
250281
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction) {
251282
return newMappedDataLoaderWithTry(batchLoadFunction, null);
252283
}
@@ -263,10 +294,12 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
263294
*
264295
* @return a new DataLoader
265296
*
266-
* @see #newDataLoaderWithTry(BatchLoader)
297+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
298+
* @deprecated use {@link DataLoaderFactory} instead
267299
*/
300+
@Deprecated
268301
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
269-
return new DataLoader<>(batchLoadFunction, options);
302+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
270303
}
271304

272305
/**
@@ -278,7 +311,10 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
278311
* @param <V> the value type
279312
*
280313
* @return a new DataLoader
314+
*
315+
* @deprecated use {@link DataLoaderFactory} instead
281316
*/
317+
@Deprecated
282318
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction) {
283319
return newMappedDataLoader(batchLoadFunction, null);
284320
}
@@ -292,9 +328,12 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
292328
* @param <V> the value type
293329
*
294330
* @return a new DataLoader
331+
*
332+
* @deprecated use {@link DataLoaderFactory} instead
295333
*/
334+
@Deprecated
296335
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
297-
return new DataLoader<>(batchLoadFunction, options);
336+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
298337
}
299338

300339
/**
@@ -313,7 +352,10 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
313352
* @param <V> the value type
314353
*
315354
* @return a new DataLoader
355+
*
356+
* @deprecated use {@link DataLoaderFactory} instead
316357
*/
358+
@Deprecated
317359
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
318360
return newMappedDataLoaderWithTry(batchLoadFunction, null);
319361
}
@@ -330,32 +372,40 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
330372
*
331373
* @return a new DataLoader
332374
*
333-
* @see #newDataLoaderWithTry(BatchLoader)
375+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
376+
* @deprecated use {@link DataLoaderFactory} instead
334377
*/
378+
@Deprecated
335379
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
336-
return new DataLoader<>(batchLoadFunction, options);
380+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
337381
}
338382

339383
/**
340384
* Creates a new data loader with the provided batch load function, and default options.
341385
*
342386
* @param batchLoadFunction the batch load function to use
387+
*
388+
* @deprecated use {@link DataLoaderFactory} instead
343389
*/
390+
@Deprecated
344391
public DataLoader(BatchLoader<K, V> batchLoadFunction) {
345-
this(batchLoadFunction, null);
392+
this((Object) batchLoadFunction, null);
346393
}
347394

348395
/**
349396
* Creates a new data loader with the provided batch load function and options.
350397
*
351398
* @param batchLoadFunction the batch load function to use
352399
* @param options the batch load options
400+
*
401+
* @deprecated use {@link DataLoaderFactory} instead
353402
*/
403+
@Deprecated
354404
public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
355405
this((Object) batchLoadFunction, options);
356406
}
357407

358-
private DataLoader(Object batchLoadFunction, DataLoaderOptions options) {
408+
DataLoader(Object batchLoadFunction, DataLoaderOptions options) {
359409
DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions() : options;
360410
this.futureCache = determineCacheMap(loaderOptions);
361411
// order of keys matter in data loader

0 commit comments

Comments
 (0)