58
58
* <p>
59
59
* A call to the batch loader might result in individual exception failures for item with the returned list. if
60
60
* you want to capture these specific item failures then use {@link org.dataloader.Try} as a return value and
61
- * create the data loader with {@link #newDataLoaderWithTry(BatchLoader)} form. The Try values will be interpreted
61
+ * create the data loader with {@link DataLoaderFactory #newDataLoaderWithTry(BatchLoader)} form. The Try values will be interpreted
62
62
* as either success values or cause the {@link #load(Object)} promise to complete exceptionally.
63
63
*
64
64
* @param <K> type parameter indicating the type of the data load keys
70
70
@ NullMarked
71
71
public class DataLoader <K , V > {
72
72
73
+ private final @ Nullable String name ;
73
74
private final DataLoaderHelper <K , V > helper ;
74
75
private final StatisticsCollector stats ;
75
76
private final CacheMap <Object , V > futureCache ;
76
77
private final ValueCache <K , V > valueCache ;
77
78
private final DataLoaderOptions options ;
78
79
private final Object batchLoadFunction ;
79
80
80
- /**
81
- * Creates new DataLoader with the specified batch loader function and default options
82
- * (batching, caching and unlimited batch size).
83
- *
84
- * @param batchLoadFunction the batch load function to use
85
- * @param <K> the key type
86
- * @param <V> the value type
87
- * @return a new DataLoader
88
- * @deprecated use {@link DataLoaderFactory} instead
89
- */
90
- @ Deprecated
91
- public static <K , V > DataLoader <K , V > newDataLoader (BatchLoader <K , V > batchLoadFunction ) {
92
- return newDataLoader (batchLoadFunction , null );
93
- }
94
-
95
- /**
96
- * Creates new DataLoader with the specified batch loader function with the provided options
97
- *
98
- * @param batchLoadFunction the batch load function to use
99
- * @param options the options to use
100
- * @param <K> the key type
101
- * @param <V> the value type
102
- * @return a new DataLoader
103
- * @deprecated use {@link DataLoaderFactory} instead
104
- */
105
- @ Deprecated
106
- public static <K , V > DataLoader <K , V > newDataLoader (BatchLoader <K , V > batchLoadFunction , @ Nullable DataLoaderOptions options ) {
107
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
108
- }
109
-
110
- /**
111
- * Creates new DataLoader with the specified batch loader function and default options
112
- * (batching, caching and unlimited batch size) where the batch loader function returns a list of
113
- * {@link org.dataloader.Try} objects.
114
- * <p>
115
- * If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
116
- * you can use this form to create the data loader.
117
- * <p>
118
- * Using Try objects allows you to capture a value returned or an exception that might
119
- * have occurred trying to get a value. .
120
- *
121
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
122
- * @param <K> the key type
123
- * @param <V> the value type
124
- * @return a new DataLoader
125
- * @deprecated use {@link DataLoaderFactory} instead
126
- */
127
- @ Deprecated
128
- public static <K , V > DataLoader <K , V > newDataLoaderWithTry (BatchLoader <K , Try <V >> batchLoadFunction ) {
129
- return newDataLoaderWithTry (batchLoadFunction , null );
130
- }
131
-
132
- /**
133
- * Creates new DataLoader with the specified batch loader function and with the provided options
134
- * where the batch loader function returns a list of
135
- * {@link org.dataloader.Try} objects.
136
- *
137
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
138
- * @param options the options to use
139
- * @param <K> the key type
140
- * @param <V> the value type
141
- * @return a new DataLoader
142
- * @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
143
- * @deprecated use {@link DataLoaderFactory} instead
144
- */
145
- @ Deprecated
146
- public static <K , V > DataLoader <K , V > newDataLoaderWithTry (BatchLoader <K , Try <V >> batchLoadFunction , @ Nullable DataLoaderOptions options ) {
147
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
148
- }
149
-
150
- /**
151
- * Creates new DataLoader with the specified batch loader function and default options
152
- * (batching, caching and unlimited batch size).
153
- *
154
- * @param batchLoadFunction the batch load function to use
155
- * @param <K> the key type
156
- * @param <V> the value type
157
- * @return a new DataLoader
158
- * @deprecated use {@link DataLoaderFactory} instead
159
- */
160
- @ Deprecated
161
- public static <K , V > DataLoader <K , V > newDataLoader (BatchLoaderWithContext <K , V > batchLoadFunction ) {
162
- return newDataLoader (batchLoadFunction , null );
163
- }
164
-
165
- /**
166
- * Creates new DataLoader with the specified batch loader function with the provided options
167
- *
168
- * @param batchLoadFunction the batch load function to use
169
- * @param options the options to use
170
- * @param <K> the key type
171
- * @param <V> the value type
172
- * @return a new DataLoader
173
- * @deprecated use {@link DataLoaderFactory} instead
174
- */
175
- @ Deprecated
176
- public static <K , V > DataLoader <K , V > newDataLoader (BatchLoaderWithContext <K , V > batchLoadFunction , @ Nullable DataLoaderOptions options ) {
177
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
178
- }
179
-
180
- /**
181
- * Creates new DataLoader with the specified batch loader function and default options
182
- * (batching, caching and unlimited batch size) where the batch loader function returns a list of
183
- * {@link org.dataloader.Try} objects.
184
- * <p>
185
- * If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
186
- * you can use this form to create the data loader.
187
- * <p>
188
- * Using Try objects allows you to capture a value returned or an exception that might
189
- * have occurred trying to get a value. .
190
- *
191
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
192
- * @param <K> the key type
193
- * @param <V> the value type
194
- * @return a new DataLoader
195
- * @deprecated use {@link DataLoaderFactory} instead
196
- */
197
- @ Deprecated
198
- public static <K , V > DataLoader <K , V > newDataLoaderWithTry (BatchLoaderWithContext <K , Try <V >> batchLoadFunction ) {
199
- return newDataLoaderWithTry (batchLoadFunction , null );
200
- }
201
-
202
- /**
203
- * Creates new DataLoader with the specified batch loader function and with the provided options
204
- * where the batch loader function returns a list of
205
- * {@link org.dataloader.Try} objects.
206
- *
207
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
208
- * @param options the options to use
209
- * @param <K> the key type
210
- * @param <V> the value type
211
- * @return a new DataLoader
212
- * @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
213
- * @deprecated use {@link DataLoaderFactory} instead
214
- */
215
- @ Deprecated
216
- public static <K , V > DataLoader <K , V > newDataLoaderWithTry (BatchLoaderWithContext <K , Try <V >> batchLoadFunction , @ Nullable DataLoaderOptions options ) {
217
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
218
- }
219
-
220
- /**
221
- * Creates new DataLoader with the specified batch loader function and default options
222
- * (batching, caching and unlimited batch size).
223
- *
224
- * @param batchLoadFunction the batch load function to use
225
- * @param <K> the key type
226
- * @param <V> the value type
227
- * @return a new DataLoader
228
- * @deprecated use {@link DataLoaderFactory} instead
229
- */
230
- @ Deprecated
231
- public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoader <K , V > batchLoadFunction ) {
232
- return newMappedDataLoader (batchLoadFunction , null );
233
- }
234
-
235
- /**
236
- * Creates new DataLoader with the specified batch loader function with the provided options
237
- *
238
- * @param batchLoadFunction the batch load function to use
239
- * @param options the options to use
240
- * @param <K> the key type
241
- * @param <V> the value type
242
- * @return a new DataLoader
243
- * @deprecated use {@link DataLoaderFactory} instead
244
- */
245
- @ Deprecated
246
- public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoader <K , V > batchLoadFunction , @ Nullable DataLoaderOptions options ) {
247
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
248
- }
249
-
250
- /**
251
- * Creates new DataLoader with the specified batch loader function and default options
252
- * (batching, caching and unlimited batch size) where the batch loader function returns a list of
253
- * {@link org.dataloader.Try} objects.
254
- * <p>
255
- * If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
256
- * you can use this form to create the data loader.
257
- * <p>
258
- * Using Try objects allows you to capture a value returned or an exception that might
259
- * have occurred trying to get a value. .
260
- * <p>
261
- *
262
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
263
- * @param <K> the key type
264
- * @param <V> the value type
265
- * @return a new DataLoader
266
- * @deprecated use {@link DataLoaderFactory} instead
267
- */
268
- @ Deprecated
269
- public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoader <K , Try <V >> batchLoadFunction ) {
270
- return newMappedDataLoaderWithTry (batchLoadFunction , null );
271
- }
272
-
273
- /**
274
- * Creates new DataLoader with the specified batch loader function and with the provided options
275
- * where the batch loader function returns a list of
276
- * {@link org.dataloader.Try} objects.
277
- *
278
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
279
- * @param options the options to use
280
- * @param <K> the key type
281
- * @param <V> the value type
282
- * @return a new DataLoader
283
- * @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
284
- * @deprecated use {@link DataLoaderFactory} instead
285
- */
286
- @ Deprecated
287
- public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoader <K , Try <V >> batchLoadFunction , @ Nullable DataLoaderOptions options ) {
288
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
289
- }
290
-
291
- /**
292
- * Creates new DataLoader with the specified mapped batch loader function and default options
293
- * (batching, caching and unlimited batch size).
294
- *
295
- * @param batchLoadFunction the batch load function to use
296
- * @param <K> the key type
297
- * @param <V> the value type
298
- * @return a new DataLoader
299
- * @deprecated use {@link DataLoaderFactory} instead
300
- */
301
- @ Deprecated
302
- public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoaderWithContext <K , V > batchLoadFunction ) {
303
- return newMappedDataLoader (batchLoadFunction , null );
304
- }
305
-
306
- /**
307
- * Creates new DataLoader with the specified batch loader function with the provided options
308
- *
309
- * @param batchLoadFunction the batch load function to use
310
- * @param options the options to use
311
- * @param <K> the key type
312
- * @param <V> the value type
313
- * @return a new DataLoader
314
- * @deprecated use {@link DataLoaderFactory} instead
315
- */
316
- @ Deprecated
317
- public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoaderWithContext <K , V > batchLoadFunction , @ Nullable DataLoaderOptions options ) {
318
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
319
- }
320
-
321
- /**
322
- * Creates new DataLoader with the specified batch loader function and default options
323
- * (batching, caching and unlimited batch size) where the batch loader function returns a list of
324
- * {@link org.dataloader.Try} objects.
325
- * <p>
326
- * If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
327
- * you can use this form to create the data loader.
328
- * <p>
329
- * Using Try objects allows you to capture a value returned or an exception that might
330
- * have occurred trying to get a value. .
331
- *
332
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
333
- * @param <K> the key type
334
- * @param <V> the value type
335
- * @return a new DataLoader
336
- * @deprecated use {@link DataLoaderFactory} instead
337
- */
338
- @ Deprecated
339
- public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoaderWithContext <K , Try <V >> batchLoadFunction ) {
340
- return newMappedDataLoaderWithTry (batchLoadFunction , null );
341
- }
342
-
343
- /**
344
- * Creates new DataLoader with the specified batch loader function and with the provided options
345
- * where the batch loader function returns a list of
346
- * {@link org.dataloader.Try} objects.
347
- *
348
- * @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
349
- * @param options the options to use
350
- * @param <K> the key type
351
- * @param <V> the value type
352
- * @return a new DataLoader
353
- * @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
354
- * @deprecated use {@link DataLoaderFactory} instead
355
- */
356
- @ Deprecated
357
- public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoaderWithContext <K , Try <V >> batchLoadFunction , @ Nullable DataLoaderOptions options ) {
358
- return DataLoaderFactory .mkDataLoader (batchLoadFunction , options );
359
- }
360
-
361
- /**
362
- * Creates a new data loader with the provided batch load function, and default options.
363
- *
364
- * @param batchLoadFunction the batch load function to use
365
- * @deprecated use {@link DataLoaderFactory} instead
366
- */
367
- @ Deprecated
368
- public DataLoader (BatchLoader <K , V > batchLoadFunction ) {
369
- this ((Object ) batchLoadFunction , null );
370
- }
371
-
372
- /**
373
- * Creates a new data loader with the provided batch load function and options.
374
- *
375
- * @param batchLoadFunction the batch load function to use
376
- * @param options the batch load options
377
- * @deprecated use {@link DataLoaderFactory} instead
378
- */
379
- @ Deprecated
380
- public DataLoader (BatchLoader <K , V > batchLoadFunction , @ Nullable DataLoaderOptions options ) {
381
- this ((Object ) batchLoadFunction , options );
382
- }
383
-
384
81
@ VisibleForTesting
385
- DataLoader (Object batchLoadFunction , @ Nullable DataLoaderOptions options ) {
386
- this (batchLoadFunction , options , Clock .systemUTC ());
82
+ DataLoader (@ Nullable String name , Object batchLoadFunction , @ Nullable DataLoaderOptions options ) {
83
+ this (name , batchLoadFunction , options , Clock .systemUTC ());
387
84
}
388
85
389
86
@ VisibleForTesting
390
- DataLoader (Object batchLoadFunction , @ Nullable DataLoaderOptions options , Clock clock ) {
87
+ DataLoader (@ Nullable String name , Object batchLoadFunction , @ Nullable DataLoaderOptions options , Clock clock ) {
391
88
DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions () : options ;
392
89
this .futureCache = determineFutureCache (loaderOptions );
393
90
this .valueCache = determineValueCache (loaderOptions );
394
91
// order of keys matter in data loader
395
92
this .stats = nonNull (loaderOptions .getStatisticsCollector ());
396
93
this .batchLoadFunction = nonNull (batchLoadFunction );
397
94
this .options = loaderOptions ;
95
+ this .name = name ;
398
96
399
97
this .helper = new DataLoaderHelper <>(this , batchLoadFunction , loaderOptions , this .futureCache , this .valueCache , this .stats , clock );
400
98
}
@@ -410,6 +108,13 @@ private ValueCache<K, V> determineValueCache(DataLoaderOptions loaderOptions) {
410
108
return (ValueCache <K , V >) loaderOptions .valueCache ().orElseGet (ValueCache ::defaultValueCache );
411
109
}
412
110
111
+ /**
112
+ * @return the name of the DataLoader which can be null
113
+ */
114
+ public @ Nullable String getName () {
115
+ return name ;
116
+ }
117
+
413
118
/**
414
119
* @return the options used to build this {@link DataLoader}
415
120
*/
0 commit comments