Skip to content

Commit 2036771

Browse files
authored
Merge pull request #174 from graphql-java/transform-support
Transform support for DataLoaders
2 parents 205aaa5 + 7426207 commit 2036771

File tree

3 files changed

+160
-90
lines changed

3 files changed

+160
-90
lines changed

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

+39-50
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@
2525
import java.time.Clock;
2626
import java.time.Duration;
2727
import java.time.Instant;
28-
import java.util.*;
28+
import java.util.ArrayList;
29+
import java.util.Collections;
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.Optional;
2934
import java.util.concurrent.CompletableFuture;
3035
import java.util.function.BiConsumer;
36+
import java.util.function.Consumer;
3137

3238
import static org.dataloader.impl.Assertions.nonNull;
3339

@@ -54,7 +60,6 @@
5460
*
5561
* @param <K> type parameter indicating the type of the data load keys
5662
* @param <V> type parameter indicating the type of the data that is returned
57-
*
5863
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
5964
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
6065
*/
@@ -65,6 +70,8 @@ public class DataLoader<K, V> {
6570
private final StatisticsCollector stats;
6671
private final CacheMap<Object, V> futureCache;
6772
private final ValueCache<K, V> valueCache;
73+
private final DataLoaderOptions options;
74+
private final Object batchLoadFunction;
6875

6976
/**
7077
* Creates new DataLoader with the specified batch loader function and default options
@@ -73,9 +80,7 @@ public class DataLoader<K, V> {
7380
* @param batchLoadFunction the batch load function to use
7481
* @param <K> the key type
7582
* @param <V> the value type
76-
*
7783
* @return a new DataLoader
78-
*
7984
* @deprecated use {@link DataLoaderFactory} instead
8085
*/
8186
@Deprecated
@@ -90,9 +95,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
9095
* @param options the options to use
9196
* @param <K> the key type
9297
* @param <V> the value type
93-
*
9498
* @return a new DataLoader
95-
*
9699
* @deprecated use {@link DataLoaderFactory} instead
97100
*/
98101
@Deprecated
@@ -114,9 +117,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
114117
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
115118
* @param <K> the key type
116119
* @param <V> the value type
117-
*
118120
* @return a new DataLoader
119-
*
120121
* @deprecated use {@link DataLoaderFactory} instead
121122
*/
122123
@Deprecated
@@ -133,9 +134,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
133134
* @param options the options to use
134135
* @param <K> the key type
135136
* @param <V> the value type
136-
*
137137
* @return a new DataLoader
138-
*
139138
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
140139
* @deprecated use {@link DataLoaderFactory} instead
141140
*/
@@ -151,9 +150,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
151150
* @param batchLoadFunction the batch load function to use
152151
* @param <K> the key type
153152
* @param <V> the value type
154-
*
155153
* @return a new DataLoader
156-
*
157154
* @deprecated use {@link DataLoaderFactory} instead
158155
*/
159156
@Deprecated
@@ -168,9 +165,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
168165
* @param options the options to use
169166
* @param <K> the key type
170167
* @param <V> the value type
171-
*
172168
* @return a new DataLoader
173-
*
174169
* @deprecated use {@link DataLoaderFactory} instead
175170
*/
176171
@Deprecated
@@ -192,9 +187,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
192187
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
193188
* @param <K> the key type
194189
* @param <V> the value type
195-
*
196190
* @return a new DataLoader
197-
*
198191
* @deprecated use {@link DataLoaderFactory} instead
199192
*/
200193
@Deprecated
@@ -211,9 +204,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
211204
* @param options the options to use
212205
* @param <K> the key type
213206
* @param <V> the value type
214-
*
215207
* @return a new DataLoader
216-
*
217208
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
218209
* @deprecated use {@link DataLoaderFactory} instead
219210
*/
@@ -229,9 +220,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
229220
* @param batchLoadFunction the batch load function to use
230221
* @param <K> the key type
231222
* @param <V> the value type
232-
*
233223
* @return a new DataLoader
234-
*
235224
* @deprecated use {@link DataLoaderFactory} instead
236225
*/
237226
@Deprecated
@@ -246,9 +235,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
246235
* @param options the options to use
247236
* @param <K> the key type
248237
* @param <V> the value type
249-
*
250238
* @return a new DataLoader
251-
*
252239
* @deprecated use {@link DataLoaderFactory} instead
253240
*/
254241
@Deprecated
@@ -271,9 +258,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
271258
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
272259
* @param <K> the key type
273260
* @param <V> the value type
274-
*
275261
* @return a new DataLoader
276-
*
277262
* @deprecated use {@link DataLoaderFactory} instead
278263
*/
279264
@Deprecated
@@ -290,9 +275,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
290275
* @param options the options to use
291276
* @param <K> the key type
292277
* @param <V> the value type
293-
*
294278
* @return a new DataLoader
295-
*
296279
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
297280
* @deprecated use {@link DataLoaderFactory} instead
298281
*/
@@ -308,9 +291,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
308291
* @param batchLoadFunction the batch load function to use
309292
* @param <K> the key type
310293
* @param <V> the value type
311-
*
312294
* @return a new DataLoader
313-
*
314295
* @deprecated use {@link DataLoaderFactory} instead
315296
*/
316297
@Deprecated
@@ -325,9 +306,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
325306
* @param options the options to use
326307
* @param <K> the key type
327308
* @param <V> the value type
328-
*
329309
* @return a new DataLoader
330-
*
331310
* @deprecated use {@link DataLoaderFactory} instead
332311
*/
333312
@Deprecated
@@ -349,9 +328,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
349328
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
350329
* @param <K> the key type
351330
* @param <V> the value type
352-
*
353331
* @return a new DataLoader
354-
*
355332
* @deprecated use {@link DataLoaderFactory} instead
356333
*/
357334
@Deprecated
@@ -368,9 +345,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
368345
* @param options the options to use
369346
* @param <K> the key type
370347
* @param <V> the value type
371-
*
372348
* @return a new DataLoader
373-
*
374349
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
375350
* @deprecated use {@link DataLoaderFactory} instead
376351
*/
@@ -383,7 +358,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
383358
* Creates a new data loader with the provided batch load function, and default options.
384359
*
385360
* @param batchLoadFunction the batch load function to use
386-
*
387361
* @deprecated use {@link DataLoaderFactory} instead
388362
*/
389363
@Deprecated
@@ -396,7 +370,6 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction) {
396370
*
397371
* @param batchLoadFunction the batch load function to use
398372
* @param options the batch load options
399-
*
400373
* @deprecated use {@link DataLoaderFactory} instead
401374
*/
402375
@Deprecated
@@ -416,6 +389,8 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
416389
this.valueCache = determineValueCache(loaderOptions);
417390
// order of keys matter in data loader
418391
this.stats = nonNull(loaderOptions.getStatisticsCollector());
392+
this.batchLoadFunction = nonNull(batchLoadFunction);
393+
this.options = loaderOptions;
419394

420395
this.helper = new DataLoaderHelper<>(this, batchLoadFunction, loaderOptions, this.futureCache, this.valueCache, this.stats, clock);
421396
}
@@ -431,6 +406,32 @@ private ValueCache<K, V> determineValueCache(DataLoaderOptions loaderOptions) {
431406
return (ValueCache<K, V>) loaderOptions.valueCache().orElseGet(ValueCache::defaultValueCache);
432407
}
433408

409+
/**
410+
* @return the options used to build this {@link DataLoader}
411+
*/
412+
public DataLoaderOptions getOptions() {
413+
return options;
414+
}
415+
416+
/**
417+
* @return the batch load interface used to build this {@link DataLoader}
418+
*/
419+
public Object getBatchLoadFunction() {
420+
return batchLoadFunction;
421+
}
422+
423+
/**
424+
* This allows you to change the current {@link DataLoader} and turn it into a new one
425+
*
426+
* @param builderConsumer the {@link DataLoaderFactory.Builder} consumer for changing the {@link DataLoader}
427+
* @return a newly built {@link DataLoader} instance
428+
*/
429+
public DataLoader<K, V> transform(Consumer<DataLoaderFactory.Builder<K, V>> builderConsumer) {
430+
DataLoaderFactory.Builder<K, V> builder = DataLoaderFactory.builder(this);
431+
builderConsumer.accept(builder);
432+
return builder.build();
433+
}
434+
434435
/**
435436
* This returns the last instant the data loader was dispatched. When the data loader is created this value is set to now.
436437
*
@@ -457,7 +458,6 @@ public Duration getTimeSinceDispatch() {
457458
* and returned from cache).
458459
*
459460
* @param key the key to load
460-
*
461461
* @return the future of the value
462462
*/
463463
public CompletableFuture<V> load(K key) {
@@ -475,7 +475,6 @@ public CompletableFuture<V> load(K key) {
475475
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
476476
*
477477
* @param key the key to check
478-
*
479478
* @return an Optional to the future of the value
480479
*/
481480
public Optional<CompletableFuture<V>> getIfPresent(K key) {
@@ -494,7 +493,6 @@ public Optional<CompletableFuture<V>> getIfPresent(K key) {
494493
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
495494
*
496495
* @param key the key to check
497-
*
498496
* @return an Optional to the future of the value
499497
*/
500498
public Optional<CompletableFuture<V>> getIfCompleted(K key) {
@@ -514,7 +512,6 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
514512
*
515513
* @param key the key to load
516514
* @param keyContext a context object that is specific to this key
517-
*
518515
* @return the future of the value
519516
*/
520517
public CompletableFuture<V> load(K key, Object keyContext) {
@@ -530,7 +527,6 @@ public CompletableFuture<V> load(K key, Object keyContext) {
530527
* and returned from cache).
531528
*
532529
* @param keys the list of keys to load
533-
*
534530
* @return the composite future of the list of values
535531
*/
536532
public CompletableFuture<List<V>> loadMany(List<K> keys) {
@@ -550,7 +546,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
550546
*
551547
* @param keys the list of keys to load
552548
* @param keyContexts the list of key calling context objects
553-
*
554549
* @return the composite future of the list of values
555550
*/
556551
public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContexts) {
@@ -583,7 +578,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
583578
* {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
584579
*
585580
* @param keysAndContexts the map of keys to their respective contexts
586-
*
587581
* @return the composite future of the map of keys and values
588582
*/
589583
public CompletableFuture<Map<K, V>> loadMany(Map<K, ?> keysAndContexts) {
@@ -656,7 +650,6 @@ public int dispatchDepth() {
656650
* on the next load request.
657651
*
658652
* @param key the key to remove
659-
*
660653
* @return the data loader for fluent coding
661654
*/
662655
public DataLoader<K, V> clear(K key) {
@@ -670,7 +663,6 @@ public DataLoader<K, V> clear(K key) {
670663
*
671664
* @param key the key to remove
672665
* @param handler a handler that will be called after the async remote clear completes
673-
*
674666
* @return the data loader for fluent coding
675667
*/
676668
public DataLoader<K, V> clear(K key, BiConsumer<Void, Throwable> handler) {
@@ -696,7 +688,6 @@ public DataLoader<K, V> clearAll() {
696688
* Clears the entire cache map of the loader, and of the cached value store.
697689
*
698690
* @param handler a handler that will be called after the async remote clear all completes
699-
*
700691
* @return the data loader for fluent coding
701692
*/
702693
public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
@@ -714,7 +705,6 @@ public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
714705
*
715706
* @param key the key
716707
* @param value the value
717-
*
718708
* @return the data loader for fluent coding
719709
*/
720710
public DataLoader<K, V> prime(K key, V value) {
@@ -726,7 +716,6 @@ public DataLoader<K, V> prime(K key, V value) {
726716
*
727717
* @param key the key
728718
* @param error the exception to prime instead of a value
729-
*
730719
* @return the data loader for fluent coding
731720
*/
732721
public DataLoader<K, V> prime(K key, Exception error) {
@@ -740,7 +729,6 @@ public DataLoader<K, V> prime(K key, Exception error) {
740729
*
741730
* @param key the key
742731
* @param value the value
743-
*
744732
* @return the data loader for fluent coding
745733
*/
746734
public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
@@ -760,7 +748,6 @@ public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
760748
* If no cache key function is present in {@link DataLoaderOptions}, then the returned value equals the input key.
761749
*
762750
* @param key the input key
763-
*
764751
* @return the cache key after the input is transformed with the cache key function
765752
*/
766753
public Object getCacheKey(K key) {
@@ -779,6 +766,7 @@ public Statistics getStatistics() {
779766

780767
/**
781768
* Gets the cacheMap associated with this data loader passed in via {@link DataLoaderOptions#cacheMap()}
769+
*
782770
* @return the cacheMap of this data loader
783771
*/
784772
public CacheMap<Object, V> getCacheMap() {
@@ -788,6 +776,7 @@ public CacheMap<Object, V> getCacheMap() {
788776

789777
/**
790778
* Gets the valueCache associated with this data loader passed in via {@link DataLoaderOptions#valueCache()}
779+
*
791780
* @return the valueCache of this data loader
792781
*/
793782
public ValueCache<K, V> getValueCache() {

0 commit comments

Comments
 (0)