Skip to content

Commit

Permalink
Merge pull request #174 from graphql-java/transform-support
Browse files Browse the repository at this point in the history
Transform support for DataLoaders
  • Loading branch information
bbakerman authored Jan 20, 2025
2 parents 205aaa5 + 7426207 commit 2036771
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 90 deletions.
89 changes: 39 additions & 50 deletions src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

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

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

/**
* Creates new DataLoader with the specified batch loader function and default options
Expand All @@ -73,9 +80,7 @@ public class DataLoader<K, V> {
* @param batchLoadFunction the batch load function to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -90,9 +95,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -114,9 +117,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -133,9 +134,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
* @deprecated use {@link DataLoaderFactory} instead
*/
Expand All @@ -151,9 +150,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
* @param batchLoadFunction the batch load function to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -168,9 +165,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -192,9 +187,7 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -211,9 +204,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
* @deprecated use {@link DataLoaderFactory} instead
*/
Expand All @@ -229,9 +220,7 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
* @param batchLoadFunction the batch load function to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -246,9 +235,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -271,9 +258,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -290,9 +275,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
* @deprecated use {@link DataLoaderFactory} instead
*/
Expand All @@ -308,9 +291,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
* @param batchLoadFunction the batch load function to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -325,9 +306,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -349,9 +328,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -368,9 +345,7 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
* @param options the options to use
* @param <K> the key type
* @param <V> the value type
*
* @return a new DataLoader
*
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
* @deprecated use {@link DataLoaderFactory} instead
*/
Expand All @@ -383,7 +358,6 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
* Creates a new data loader with the provided batch load function, and default options.
*
* @param batchLoadFunction the batch load function to use
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -396,7 +370,6 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction) {
*
* @param batchLoadFunction the batch load function to use
* @param options the batch load options
*
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
Expand All @@ -416,6 +389,8 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
this.valueCache = determineValueCache(loaderOptions);
// order of keys matter in data loader
this.stats = nonNull(loaderOptions.getStatisticsCollector());
this.batchLoadFunction = nonNull(batchLoadFunction);
this.options = loaderOptions;

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

/**
* @return the options used to build this {@link DataLoader}
*/
public DataLoaderOptions getOptions() {
return options;
}

/**
* @return the batch load interface used to build this {@link DataLoader}
*/
public Object getBatchLoadFunction() {
return batchLoadFunction;
}

/**
* This allows you to change the current {@link DataLoader} and turn it into a new one
*
* @param builderConsumer the {@link DataLoaderFactory.Builder} consumer for changing the {@link DataLoader}
* @return a newly built {@link DataLoader} instance
*/
public DataLoader<K, V> transform(Consumer<DataLoaderFactory.Builder<K, V>> builderConsumer) {
DataLoaderFactory.Builder<K, V> builder = DataLoaderFactory.builder(this);
builderConsumer.accept(builder);
return builder.build();
}

/**
* This returns the last instant the data loader was dispatched. When the data loader is created this value is set to now.
*
Expand All @@ -457,7 +458,6 @@ public Duration getTimeSinceDispatch() {
* and returned from cache).
*
* @param key the key to load
*
* @return the future of the value
*/
public CompletableFuture<V> load(K key) {
Expand All @@ -475,7 +475,6 @@ public CompletableFuture<V> load(K key) {
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
*
* @param key the key to check
*
* @return an Optional to the future of the value
*/
public Optional<CompletableFuture<V>> getIfPresent(K key) {
Expand All @@ -494,7 +493,6 @@ public Optional<CompletableFuture<V>> getIfPresent(K key) {
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
*
* @param key the key to check
*
* @return an Optional to the future of the value
*/
public Optional<CompletableFuture<V>> getIfCompleted(K key) {
Expand All @@ -514,7 +512,6 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
*
* @param key the key to load
* @param keyContext a context object that is specific to this key
*
* @return the future of the value
*/
public CompletableFuture<V> load(K key, Object keyContext) {
Expand All @@ -530,7 +527,6 @@ public CompletableFuture<V> load(K key, Object keyContext) {
* and returned from cache).
*
* @param keys the list of keys to load
*
* @return the composite future of the list of values
*/
public CompletableFuture<List<V>> loadMany(List<K> keys) {
Expand All @@ -550,7 +546,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
*
* @param keys the list of keys to load
* @param keyContexts the list of key calling context objects
*
* @return the composite future of the list of values
*/
public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContexts) {
Expand Down Expand Up @@ -583,7 +578,6 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
* {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
*
* @param keysAndContexts the map of keys to their respective contexts
*
* @return the composite future of the map of keys and values
*/
public CompletableFuture<Map<K, V>> loadMany(Map<K, ?> keysAndContexts) {
Expand Down Expand Up @@ -656,7 +650,6 @@ public int dispatchDepth() {
* on the next load request.
*
* @param key the key to remove
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> clear(K key) {
Expand All @@ -670,7 +663,6 @@ public DataLoader<K, V> clear(K key) {
*
* @param key the key to remove
* @param handler a handler that will be called after the async remote clear completes
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> clear(K key, BiConsumer<Void, Throwable> handler) {
Expand All @@ -696,7 +688,6 @@ public DataLoader<K, V> clearAll() {
* Clears the entire cache map of the loader, and of the cached value store.
*
* @param handler a handler that will be called after the async remote clear all completes
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
Expand All @@ -714,7 +705,6 @@ public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
*
* @param key the key
* @param value the value
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> prime(K key, V value) {
Expand All @@ -726,7 +716,6 @@ public DataLoader<K, V> prime(K key, V value) {
*
* @param key the key
* @param error the exception to prime instead of a value
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> prime(K key, Exception error) {
Expand All @@ -740,7 +729,6 @@ public DataLoader<K, V> prime(K key, Exception error) {
*
* @param key the key
* @param value the value
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
Expand All @@ -760,7 +748,6 @@ public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
* If no cache key function is present in {@link DataLoaderOptions}, then the returned value equals the input key.
*
* @param key the input key
*
* @return the cache key after the input is transformed with the cache key function
*/
public Object getCacheKey(K key) {
Expand All @@ -779,6 +766,7 @@ public Statistics getStatistics() {

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

/**
* Gets the valueCache associated with this data loader passed in via {@link DataLoaderOptions#valueCache()}
*
* @return the valueCache of this data loader
*/
public ValueCache<K, V> getValueCache() {
Expand Down
Loading

0 comments on commit 2036771

Please sign in to comment.