Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions src/main/java/org/dataloader/DataLoaderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,18 @@ CompletableFuture<V> load(K key, Object loadContext) {
}

ctx.onDispatched();
loaderOptions.getDispatchStrategy().loadCalled();
loadCallFuture.whenComplete(ctx::onCompleted);
loadCallFuture.whenComplete((result, error) -> loaderOptions.getDispatchStrategy().loadCompleted());
return loadCallFuture;
}

private CompletableFuture<V> incrementCacheHitAndReturnCF(DataLoaderInstrumentationContext<Object> ctx, K key, Object loadContext, CompletableFuture<V> cachedFuture) {
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key, loadContext));
ctx.onDispatched();
loaderOptions.getDispatchStrategy().loadCalled();
cachedFuture.whenComplete(ctx::onCompleted);
cachedFuture.whenComplete((result, error) -> loaderOptions.getDispatchStrategy().loadCompleted());
return cachedFuture;
}

Expand Down
62 changes: 51 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,22 @@ 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) &&
Objects.equals(dispatchStrategy, that.dispatchStrategy);
}


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

/**
* @return the {@link DispatchStrategy} to use for dispatching batch loads
*/
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 +300,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 +319,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 +331,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 +343,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 +355,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 +367,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 +380,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 +394,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 +406,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 +418,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 +431,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 +443,26 @@ 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;
}

/**
* Sets in a new {@link DispatchStrategy}
*
* @param dispatchStrategy the new {@link DispatchStrategy}
*
* @return the builder for fluent coding
*/
public Builder setDispatchStrategy(DispatchStrategy dispatchStrategy) {
this.dispatchStrategy = dispatchStrategy;
return this;
}

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