17
17
package org .dataloader ;
18
18
19
19
import org .dataloader .annotations .PublicApi ;
20
+ import org .dataloader .annotations .VisibleForTesting ;
20
21
import org .dataloader .impl .CompletableFutureKit ;
21
22
import org .dataloader .stats .Statistics ;
22
23
import org .dataloader .stats .StatisticsCollector ;
23
24
25
+ import java .time .Clock ;
26
+ import java .time .Instant ;
24
27
import java .util .ArrayList ;
25
28
import java .util .Collections ;
26
29
import java .util .List ;
52
55
*
53
56
* @param <K> type parameter indicating the type of the data load keys
54
57
* @param <V> type parameter indicating the type of the data that is returned
58
+ *
55
59
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
56
60
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
57
61
*/
@@ -69,6 +73,7 @@ public class DataLoader<K, V> {
69
73
* @param batchLoadFunction the batch load function to use
70
74
* @param <K> the key type
71
75
* @param <V> the value type
76
+ *
72
77
* @return a new DataLoader
73
78
*/
74
79
public static <K , V > DataLoader <K , V > newDataLoader (BatchLoader <K , V > batchLoadFunction ) {
@@ -82,6 +87,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
82
87
* @param options the options to use
83
88
* @param <K> the key type
84
89
* @param <V> the value type
90
+ *
85
91
* @return a new DataLoader
86
92
*/
87
93
public static <K , V > DataLoader <K , V > newDataLoader (BatchLoader <K , V > batchLoadFunction , DataLoaderOptions options ) {
@@ -102,6 +108,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
102
108
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
103
109
* @param <K> the key type
104
110
* @param <V> the value type
111
+ *
105
112
* @return a new DataLoader
106
113
*/
107
114
public static <K , V > DataLoader <K , V > newDataLoaderWithTry (BatchLoader <K , Try <V >> batchLoadFunction ) {
@@ -117,7 +124,9 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
117
124
* @param options the options to use
118
125
* @param <K> the key type
119
126
* @param <V> the value type
127
+ *
120
128
* @return a new DataLoader
129
+ *
121
130
* @see #newDataLoaderWithTry(BatchLoader)
122
131
*/
123
132
@ SuppressWarnings ("unchecked" )
@@ -132,6 +141,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
132
141
* @param batchLoadFunction the batch load function to use
133
142
* @param <K> the key type
134
143
* @param <V> the value type
144
+ *
135
145
* @return a new DataLoader
136
146
*/
137
147
public static <K , V > DataLoader <K , V > newDataLoader (BatchLoaderWithContext <K , V > batchLoadFunction ) {
@@ -145,6 +155,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
145
155
* @param options the options to use
146
156
* @param <K> the key type
147
157
* @param <V> the value type
158
+ *
148
159
* @return a new DataLoader
149
160
*/
150
161
public static <K , V > DataLoader <K , V > newDataLoader (BatchLoaderWithContext <K , V > batchLoadFunction , DataLoaderOptions options ) {
@@ -165,6 +176,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
165
176
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
166
177
* @param <K> the key type
167
178
* @param <V> the value type
179
+ *
168
180
* @return a new DataLoader
169
181
*/
170
182
public static <K , V > DataLoader <K , V > newDataLoaderWithTry (BatchLoaderWithContext <K , Try <V >> batchLoadFunction ) {
@@ -180,7 +192,9 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
180
192
* @param options the options to use
181
193
* @param <K> the key type
182
194
* @param <V> the value type
195
+ *
183
196
* @return a new DataLoader
197
+ *
184
198
* @see #newDataLoaderWithTry(BatchLoader)
185
199
*/
186
200
public static <K , V > DataLoader <K , V > newDataLoaderWithTry (BatchLoaderWithContext <K , Try <V >> batchLoadFunction , DataLoaderOptions options ) {
@@ -194,6 +208,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
194
208
* @param batchLoadFunction the batch load function to use
195
209
* @param <K> the key type
196
210
* @param <V> the value type
211
+ *
197
212
* @return a new DataLoader
198
213
*/
199
214
public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoader <K , V > batchLoadFunction ) {
@@ -207,6 +222,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
207
222
* @param options the options to use
208
223
* @param <K> the key type
209
224
* @param <V> the value type
225
+ *
210
226
* @return a new DataLoader
211
227
*/
212
228
public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoader <K , V > batchLoadFunction , DataLoaderOptions options ) {
@@ -228,6 +244,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
228
244
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
229
245
* @param <K> the key type
230
246
* @param <V> the value type
247
+ *
231
248
* @return a new DataLoader
232
249
*/
233
250
public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoader <K , Try <V >> batchLoadFunction ) {
@@ -243,7 +260,9 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
243
260
* @param options the options to use
244
261
* @param <K> the key type
245
262
* @param <V> the value type
263
+ *
246
264
* @return a new DataLoader
265
+ *
247
266
* @see #newDataLoaderWithTry(BatchLoader)
248
267
*/
249
268
public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoader <K , Try <V >> batchLoadFunction , DataLoaderOptions options ) {
@@ -257,6 +276,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
257
276
* @param batchLoadFunction the batch load function to use
258
277
* @param <K> the key type
259
278
* @param <V> the value type
279
+ *
260
280
* @return a new DataLoader
261
281
*/
262
282
public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoaderWithContext <K , V > batchLoadFunction ) {
@@ -270,6 +290,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
270
290
* @param options the options to use
271
291
* @param <K> the key type
272
292
* @param <V> the value type
293
+ *
273
294
* @return a new DataLoader
274
295
*/
275
296
public static <K , V > DataLoader <K , V > newMappedDataLoader (MappedBatchLoaderWithContext <K , V > batchLoadFunction , DataLoaderOptions options ) {
@@ -290,6 +311,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
290
311
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
291
312
* @param <K> the key type
292
313
* @param <V> the value type
314
+ *
293
315
* @return a new DataLoader
294
316
*/
295
317
public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoaderWithContext <K , Try <V >> batchLoadFunction ) {
@@ -305,7 +327,9 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
305
327
* @param options the options to use
306
328
* @param <K> the key type
307
329
* @param <V> the value type
330
+ *
308
331
* @return a new DataLoader
332
+ *
309
333
* @see #newDataLoaderWithTry(BatchLoader)
310
334
*/
311
335
public static <K , V > DataLoader <K , V > newMappedDataLoaderWithTry (MappedBatchLoaderWithContext <K , Try <V >> batchLoadFunction , DataLoaderOptions options ) {
@@ -337,14 +361,30 @@ private DataLoader(Object batchLoadFunction, DataLoaderOptions options) {
337
361
// order of keys matter in data loader
338
362
this .stats = nonNull (loaderOptions .getStatisticsCollector ());
339
363
340
- this .helper = new DataLoaderHelper <>(this , batchLoadFunction , loaderOptions , this .futureCache , this .stats );
364
+ this .helper = new DataLoaderHelper <>(this , batchLoadFunction , loaderOptions , this .futureCache , this .stats , clock ());
365
+ }
366
+
367
+ @ VisibleForTesting
368
+ Clock clock () {
369
+ return Clock .systemUTC ();
341
370
}
342
371
343
372
@ SuppressWarnings ("unchecked" )
344
373
private CacheMap <Object , CompletableFuture <V >> determineCacheMap (DataLoaderOptions loaderOptions ) {
345
374
return loaderOptions .cacheMap ().isPresent () ? (CacheMap <Object , CompletableFuture <V >>) loaderOptions .cacheMap ().get () : CacheMap .simpleMap ();
346
375
}
347
376
377
+
378
+ /**
379
+ * This returns the last instant the data loader was dispatched. When the data loader is created this value is set to now.
380
+ *
381
+ * @return the instant since the last dispatch
382
+ */
383
+ public Instant getLastDispatchTime () {
384
+ return helper .getLastDispatchTime ();
385
+ }
386
+
387
+
348
388
/**
349
389
* Requests to load the data with the specified key asynchronously, and returns a future of the resulting value.
350
390
* <p>
@@ -353,6 +393,7 @@ private CacheMap<Object, CompletableFuture<V>> determineCacheMap(DataLoaderOptio
353
393
* and returned from cache).
354
394
*
355
395
* @param key the key to load
396
+ *
356
397
* @return the future of the value
357
398
*/
358
399
public CompletableFuture <V > load (K key ) {
@@ -370,6 +411,7 @@ public CompletableFuture<V> load(K key) {
370
411
* NOTE : This will NOT cause a data load to happen. You must called {@link #load(Object)} for that to happen.
371
412
*
372
413
* @param key the key to check
414
+ *
373
415
* @return an Optional to the future of the value
374
416
*/
375
417
public Optional <CompletableFuture <V >> getIfPresent (K key ) {
@@ -388,6 +430,7 @@ public Optional<CompletableFuture<V>> getIfPresent(K key) {
388
430
* NOTE : This will NOT cause a data load to happen. You must called {@link #load(Object)} for that to happen.
389
431
*
390
432
* @param key the key to check
433
+ *
391
434
* @return an Optional to the future of the value
392
435
*/
393
436
public Optional <CompletableFuture <V >> getIfCompleted (K key ) {
@@ -407,6 +450,7 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
407
450
*
408
451
* @param key the key to load
409
452
* @param keyContext a context object that is specific to this key
453
+ *
410
454
* @return the future of the value
411
455
*/
412
456
public CompletableFuture <V > load (K key , Object keyContext ) {
@@ -422,6 +466,7 @@ public CompletableFuture<V> load(K key, Object keyContext) {
422
466
* and returned from cache).
423
467
*
424
468
* @param keys the list of keys to load
469
+ *
425
470
* @return the composite future of the list of values
426
471
*/
427
472
public CompletableFuture <List <V >> loadMany (List <K > keys ) {
@@ -441,6 +486,7 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
441
486
*
442
487
* @param keys the list of keys to load
443
488
* @param keyContexts the list of key calling context objects
489
+ *
444
490
* @return the composite future of the list of values
445
491
*/
446
492
public CompletableFuture <List <V >> loadMany (List <K > keys , List <Object > keyContexts ) {
@@ -517,6 +563,7 @@ public int dispatchDepth() {
517
563
* on the next load request.
518
564
*
519
565
* @param key the key to remove
566
+ *
520
567
* @return the data loader for fluent coding
521
568
*/
522
569
public DataLoader <K , V > clear (K key ) {
@@ -544,6 +591,7 @@ public DataLoader<K, V> clearAll() {
544
591
*
545
592
* @param key the key
546
593
* @param value the value
594
+ *
547
595
* @return the data loader for fluent coding
548
596
*/
549
597
public DataLoader <K , V > prime (K key , V value ) {
@@ -561,6 +609,7 @@ public DataLoader<K, V> prime(K key, V value) {
561
609
*
562
610
* @param key the key
563
611
* @param error the exception to prime instead of a value
612
+ *
564
613
* @return the data loader for fluent coding
565
614
*/
566
615
public DataLoader <K , V > prime (K key , Exception error ) {
@@ -578,6 +627,7 @@ public DataLoader<K, V> prime(K key, Exception error) {
578
627
* If no cache key function is present in {@link DataLoaderOptions}, then the returned value equals the input key.
579
628
*
580
629
* @param key the input key
630
+ *
581
631
* @return the cache key after the input is transformed with the cache key function
582
632
*/
583
633
public Object getCacheKey (K key ) {
0 commit comments