Skip to content

Breaking change - adds a name to a DataLoader #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
321 changes: 13 additions & 308 deletions src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* <p>
* A call to the batch loader might result in individual exception failures for item with the returned list. if
* you want to capture these specific item failures then use {@link org.dataloader.Try} as a return value and
* create the data loader with {@link #newDataLoaderWithTry(BatchLoader)} form. The Try values will be interpreted
* create the data loader with {@link DataLoaderFactory#newDataLoaderWithTry(BatchLoader)} form. The Try values will be interpreted
* as either success values or cause the {@link #load(Object)} promise to complete exceptionally.
*
* @param <K> type parameter indicating the type of the data load keys
Expand All @@ -70,331 +70,29 @@
@NullMarked
public class DataLoader<K, V> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should DataLoader be final btw?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure


private final @Nullable String name;
private final DataLoaderHelper<K, V> helper;
private final StatisticsCollector stats;
private final CacheMap<Object, V> futureCache;
private final ValueCache<K, V> valueCache;
private final DataLoaderOptions options;
private final Object batchLoadFunction;

/**
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have been deprecated for FOREVER and since this is a breaking change chance - I am taking it

* Creates new DataLoader with the specified batch loader function and default options
* (batching, caching and unlimited batch size).
*
* @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
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction) {
return newDataLoader(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function with the provided options
*
* @param batchLoadFunction the batch load function to use
* @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
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* Creates new DataLoader with the specified batch loader function and default options
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
* <p>
* If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
* you can use this form to create the data loader.
* <p>
* Using Try objects allows you to capture a value returned or an exception that might
* have occurred trying to get a value. .
*
* @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
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction) {
return newDataLoaderWithTry(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function and with the provided options
* where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
*
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @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
*/
@Deprecated
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* Creates new DataLoader with the specified batch loader function and default options
* (batching, caching and unlimited batch size).
*
* @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
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction) {
return newDataLoader(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function with the provided options
*
* @param batchLoadFunction the batch load function to use
* @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
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* Creates new DataLoader with the specified batch loader function and default options
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
* <p>
* If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
* you can use this form to create the data loader.
* <p>
* Using Try objects allows you to capture a value returned or an exception that might
* have occurred trying to get a value. .
*
* @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
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
return newDataLoaderWithTry(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function and with the provided options
* where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
*
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @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
*/
@Deprecated
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* Creates new DataLoader with the specified batch loader function and default options
* (batching, caching and unlimited batch size).
*
* @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
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction) {
return newMappedDataLoader(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function with the provided options
*
* @param batchLoadFunction the batch load function to use
* @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
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* Creates new DataLoader with the specified batch loader function and default options
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
* <p>
* If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
* you can use this form to create the data loader.
* <p>
* Using Try objects allows you to capture a value returned or an exception that might
* have occurred trying to get a value. .
* <p>
*
* @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
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction) {
return newMappedDataLoaderWithTry(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function and with the provided options
* where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
*
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @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
*/
@Deprecated
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* Creates new DataLoader with the specified mapped batch loader function and default options
* (batching, caching and unlimited batch size).
*
* @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
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction) {
return newMappedDataLoader(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function with the provided options
*
* @param batchLoadFunction the batch load function to use
* @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
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* Creates new DataLoader with the specified batch loader function and default options
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
* <p>
* If it's important you to know the exact status of each item in a batch call and whether it threw exceptions then
* you can use this form to create the data loader.
* <p>
* Using Try objects allows you to capture a value returned or an exception that might
* have occurred trying to get a value. .
*
* @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
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
return newMappedDataLoaderWithTry(batchLoadFunction, null);
}

/**
* Creates new DataLoader with the specified batch loader function and with the provided options
* where the batch loader function returns a list of
* {@link org.dataloader.Try} objects.
*
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
* @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
*/
@Deprecated
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction, @Nullable DataLoaderOptions options) {
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
}

/**
* 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
public DataLoader(BatchLoader<K, V> batchLoadFunction) {
this((Object) batchLoadFunction, null);
}

/**
* Creates a new data loader with the provided batch load function and options.
*
* @param batchLoadFunction the batch load function to use
* @param options the batch load options
* @deprecated use {@link DataLoaderFactory} instead
*/
@Deprecated
public DataLoader(BatchLoader<K, V> batchLoadFunction, @Nullable DataLoaderOptions options) {
this((Object) batchLoadFunction, options);
}

@VisibleForTesting
DataLoader(Object batchLoadFunction, @Nullable DataLoaderOptions options) {
this(batchLoadFunction, options, Clock.systemUTC());
DataLoader(@Nullable String name, Object batchLoadFunction, @Nullable DataLoaderOptions options) {
this(name, batchLoadFunction, options, Clock.systemUTC());
}

@VisibleForTesting
DataLoader(Object batchLoadFunction, @Nullable DataLoaderOptions options, Clock clock) {
DataLoader(@Nullable String name, Object batchLoadFunction, @Nullable DataLoaderOptions options, Clock clock) {
DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions() : options;
this.futureCache = determineFutureCache(loaderOptions);
this.valueCache = determineValueCache(loaderOptions);
// order of keys matter in data loader
this.stats = nonNull(loaderOptions.getStatisticsCollector());
this.batchLoadFunction = nonNull(batchLoadFunction);
this.options = loaderOptions;
this.name = name;

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

/**
* @return the name of the DataLoader which can be null
*/
public @Nullable String getName() {
return name;
}

/**
* @return the options used to build this {@link DataLoader}
*/
Expand Down
Loading