Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
* @return the future of the value
*/
public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
CompletableFuture<V> result = loadImpl(key, keyContext);
options.getDispatchStrategy().loadCalled(this);
return result;
}
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this should be in DataLoaderHelper which is really the guts of the code


private CompletableFuture<V> loadImpl(@NonNull K key, @Nullable Object keyContext) {
return helper.load(nonNull(key), keyContext);
}

Expand Down Expand Up @@ -275,8 +281,9 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
if (i < keyContexts.size()) {
keyContext = keyContexts.get(i);
}
collect.add(load(key, keyContext));
collect.add(loadImpl(key, keyContext));
}
options.getDispatchStrategy().loadCalled(this);
return CompletableFutureKit.allOf(collect);
}

Expand All @@ -302,8 +309,9 @@ public CompletableFuture<Map<K, V>> loadMany(Map<K, ?> keysAndContexts) {
for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) {
K key = entry.getKey();
Object keyContext = entry.getValue();
collect.put(key, load(key, keyContext));
collect.put(key, loadImpl(key, keyContext));
}
options.getDispatchStrategy().loadCalled(this);
return CompletableFutureKit.allOf(collect);
}

Expand Down
51 changes: 40 additions & 11 deletions src/main/java/org/dataloader/DataLoaderOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class DataLoaderOptions {
private final ValueCacheOptions valueCacheOptions;
private final BatchLoaderScheduler batchLoaderScheduler;
private final DataLoaderInstrumentation instrumentation;
private final DispatchStrategy dispatchStrategy;

/**
* Creates a new data loader options with default settings.
Expand All @@ -72,6 +73,7 @@ public DataLoaderOptions() {
valueCacheOptions = DEFAULT_VALUE_CACHE_OPTIONS;
batchLoaderScheduler = null;
instrumentation = DataLoaderInstrumentationHelper.NOOP_INSTRUMENTATION;
dispatchStrategy = DispatchStrategy.NO_OP;
}

private DataLoaderOptions(Builder builder) {
Expand All @@ -87,6 +89,7 @@ private DataLoaderOptions(Builder builder) {
this.valueCacheOptions = builder.valueCacheOptions;
this.batchLoaderScheduler = builder.batchLoaderScheduler;
this.instrumentation = builder.instrumentation;
this.dispatchStrategy = builder.dispatchStrategy;
}

/**
Expand Down Expand Up @@ -116,6 +119,7 @@ public static DataLoaderOptions.Builder newOptions(DataLoaderOptions otherOption
* Will transform the current options in to a builder ands allow you to build a new set of options
*
* @param builderConsumer the consumer of a builder that has this objects starting values
*
* @return a new {@link DataLoaderOptions} object
*/
public DataLoaderOptions transform(Consumer<Builder> builderConsumer) {
Expand All @@ -126,19 +130,21 @@ public DataLoaderOptions transform(Consumer<Builder> builderConsumer) {

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
if (o == null || getClass() != o.getClass()) {
return false;
}
DataLoaderOptions that = (DataLoaderOptions) o;
return batchingEnabled == that.batchingEnabled
&& cachingEnabled == that.cachingEnabled
&& cachingExceptionsEnabled == that.cachingExceptionsEnabled
&& maxBatchSize == that.maxBatchSize
&& Objects.equals(cacheKeyFunction, that.cacheKeyFunction) &&
Objects.equals(cacheMap, that.cacheMap) &&
Objects.equals(valueCache, that.valueCache) &&
Objects.equals(statisticsCollector, that.statisticsCollector) &&
Objects.equals(environmentProvider, that.environmentProvider) &&
Objects.equals(valueCacheOptions, that.valueCacheOptions) &&
Objects.equals(batchLoaderScheduler, that.batchLoaderScheduler);
&& cachingEnabled == that.cachingEnabled
&& cachingExceptionsEnabled == that.cachingExceptionsEnabled
&& maxBatchSize == that.maxBatchSize
&& Objects.equals(cacheKeyFunction, that.cacheKeyFunction) &&
Objects.equals(cacheMap, that.cacheMap) &&
Objects.equals(valueCache, that.valueCache) &&
Objects.equals(statisticsCollector, that.statisticsCollector) &&
Objects.equals(environmentProvider, that.environmentProvider) &&
Objects.equals(valueCacheOptions, that.valueCacheOptions) &&
Objects.equals(batchLoaderScheduler, that.batchLoaderScheduler);
}


Expand Down Expand Up @@ -254,7 +260,12 @@ public DataLoaderInstrumentation getInstrumentation() {
return instrumentation;
}

public DispatchStrategy getDispatchStrategy() {
return dispatchStrategy;
}

public static class Builder {
private DispatchStrategy dispatchStrategy = DispatchStrategy.NO_OP;
private boolean batchingEnabled;
private boolean cachingEnabled;
private boolean cachingExceptionsEnabled;
Expand Down Expand Up @@ -285,12 +296,14 @@ public Builder() {
this.valueCacheOptions = other.valueCacheOptions;
this.batchLoaderScheduler = other.batchLoaderScheduler;
this.instrumentation = other.instrumentation;
this.dispatchStrategy = other.dispatchStrategy;
}

/**
* Sets the option that determines whether batch loading is enabled.
*
* @param batchingEnabled {@code true} to enable batch loading, {@code false} otherwise
*
* @return this builder for fluent coding
*/
public Builder setBatchingEnabled(boolean batchingEnabled) {
Expand All @@ -302,6 +315,7 @@ public Builder setBatchingEnabled(boolean batchingEnabled) {
* Sets the option that determines whether caching is enabled.
*
* @param cachingEnabled {@code true} to enable caching, {@code false} otherwise
*
* @return this builder for fluent coding
*/
public Builder setCachingEnabled(boolean cachingEnabled) {
Expand All @@ -313,6 +327,7 @@ public Builder setCachingEnabled(boolean cachingEnabled) {
* Sets the option that determines whether exceptional values are cache enabled.
*
* @param cachingExceptionsEnabled {@code true} to enable caching exceptional values, {@code false} otherwise
*
* @return this builder for fluent coding
*/
public Builder setCachingExceptionsEnabled(boolean cachingExceptionsEnabled) {
Expand All @@ -324,6 +339,7 @@ public Builder setCachingExceptionsEnabled(boolean cachingExceptionsEnabled) {
* Sets the function to use for creating the cache key, if caching is enabled.
*
* @param cacheKeyFunction the cache key function to use
*
* @return this builder for fluent coding
*/
public Builder setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
Expand All @@ -335,6 +351,7 @@ public Builder setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
* Sets the cache map implementation to use for caching, if caching is enabled.
*
* @param cacheMap the cache map instance
*
* @return this builder for fluent coding
*/
public Builder setCacheMap(CacheMap<?, ?> cacheMap) {
Expand All @@ -346,6 +363,7 @@ public Builder setCacheMap(CacheMap<?, ?> cacheMap) {
* Sets the value cache implementation to use for caching values, if caching is enabled.
*
* @param valueCache the value cache instance
*
* @return this builder for fluent coding
*/
public Builder setValueCache(ValueCache<?, ?> valueCache) {
Expand All @@ -358,6 +376,7 @@ public Builder setValueCache(ValueCache<?, ?> valueCache) {
* before they are split into multiple class
*
* @param maxBatchSize the maximum batch size
*
* @return this builder for fluent coding
*/
public Builder setMaxBatchSize(int maxBatchSize) {
Expand All @@ -371,6 +390,7 @@ public Builder setMaxBatchSize(int maxBatchSize) {
* a common value
*
* @param statisticsCollector the statistics collector to use
*
* @return this builder for fluent coding
*/
public Builder setStatisticsCollector(Supplier<StatisticsCollector> statisticsCollector) {
Expand All @@ -382,6 +402,7 @@ public Builder setStatisticsCollector(Supplier<StatisticsCollector> statisticsCo
* Sets the batch loader environment provider that will be used to give context to batch load functions
*
* @param environmentProvider the batch loader context provider
*
* @return this builder for fluent coding
*/
public Builder setBatchLoaderContextProvider(BatchLoaderContextProvider environmentProvider) {
Expand All @@ -393,6 +414,7 @@ public Builder setBatchLoaderContextProvider(BatchLoaderContextProvider environm
* Sets the {@link ValueCacheOptions} that control how the {@link ValueCache} will be used
*
* @param valueCacheOptions the value cache options
*
* @return this builder for fluent coding
*/
public Builder setValueCacheOptions(ValueCacheOptions valueCacheOptions) {
Expand All @@ -405,6 +427,7 @@ public Builder setValueCacheOptions(ValueCacheOptions valueCacheOptions) {
* to some future time.
*
* @param batchLoaderScheduler the scheduler
*
* @return this builder for fluent coding
*/
public Builder setBatchLoaderScheduler(BatchLoaderScheduler batchLoaderScheduler) {
Expand All @@ -416,13 +439,19 @@ public Builder setBatchLoaderScheduler(BatchLoaderScheduler batchLoaderScheduler
* Sets in a new {@link DataLoaderInstrumentation}
*
* @param instrumentation the new {@link DataLoaderInstrumentation}
*
* @return this builder for fluent coding
*/
public Builder setInstrumentation(DataLoaderInstrumentation instrumentation) {
this.instrumentation = nonNull(instrumentation);
return this;
}

public Builder setDispatchStrategy(DispatchStrategy dispatchStrategy) {
this.dispatchStrategy = dispatchStrategy;
return this;
}

public DataLoaderOptions build() {
return new DataLoaderOptions(this);
}
Expand Down
Loading
Loading