Skip to content

Commit 3243b9b

Browse files
committed
introduce concept of DispatchStrategy
1 parent b2f27b4 commit 3243b9b

File tree

5 files changed

+287
-30
lines changed

5 files changed

+287
-30
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
229229
* @return the future of the value
230230
*/
231231
public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
232+
CompletableFuture<V> result = loadImpl(key, keyContext);
233+
options.getDispatchStrategy().loadCalled(this);
234+
return result;
235+
}
236+
237+
private CompletableFuture<V> loadImpl(@NonNull K key, @Nullable Object keyContext) {
232238
return helper.load(nonNull(key), keyContext);
233239
}
234240

@@ -275,8 +281,9 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
275281
if (i < keyContexts.size()) {
276282
keyContext = keyContexts.get(i);
277283
}
278-
collect.add(load(key, keyContext));
284+
collect.add(loadImpl(key, keyContext));
279285
}
286+
options.getDispatchStrategy().loadCalled(this);
280287
return CompletableFutureKit.allOf(collect);
281288
}
282289

@@ -302,8 +309,9 @@ public CompletableFuture<Map<K, V>> loadMany(Map<K, ?> keysAndContexts) {
302309
for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) {
303310
K key = entry.getKey();
304311
Object keyContext = entry.getValue();
305-
collect.put(key, load(key, keyContext));
312+
collect.put(key, loadImpl(key, keyContext));
306313
}
314+
options.getDispatchStrategy().loadCalled(this);
307315
return CompletableFutureKit.allOf(collect);
308316
}
309317

src/main/java/org/dataloader/DataLoaderOptions.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class DataLoaderOptions {
5555
private final ValueCacheOptions valueCacheOptions;
5656
private final BatchLoaderScheduler batchLoaderScheduler;
5757
private final DataLoaderInstrumentation instrumentation;
58+
private final DispatchStrategy dispatchStrategy;
5859

5960
/**
6061
* Creates a new data loader options with default settings.
@@ -72,6 +73,7 @@ public DataLoaderOptions() {
7273
valueCacheOptions = DEFAULT_VALUE_CACHE_OPTIONS;
7374
batchLoaderScheduler = null;
7475
instrumentation = DataLoaderInstrumentationHelper.NOOP_INSTRUMENTATION;
76+
dispatchStrategy = DispatchStrategy.NO_OP;
7577
}
7678

7779
private DataLoaderOptions(Builder builder) {
@@ -87,6 +89,7 @@ private DataLoaderOptions(Builder builder) {
8789
this.valueCacheOptions = builder.valueCacheOptions;
8890
this.batchLoaderScheduler = builder.batchLoaderScheduler;
8991
this.instrumentation = builder.instrumentation;
92+
this.dispatchStrategy = builder.dispatchStrategy;
9093
}
9194

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

127131
@Override
128132
public boolean equals(Object o) {
129-
if (o == null || getClass() != o.getClass()) return false;
133+
if (o == null || getClass() != o.getClass()) {
134+
return false;
135+
}
130136
DataLoaderOptions that = (DataLoaderOptions) o;
131137
return batchingEnabled == that.batchingEnabled
132-
&& cachingEnabled == that.cachingEnabled
133-
&& cachingExceptionsEnabled == that.cachingExceptionsEnabled
134-
&& maxBatchSize == that.maxBatchSize
135-
&& Objects.equals(cacheKeyFunction, that.cacheKeyFunction) &&
136-
Objects.equals(cacheMap, that.cacheMap) &&
137-
Objects.equals(valueCache, that.valueCache) &&
138-
Objects.equals(statisticsCollector, that.statisticsCollector) &&
139-
Objects.equals(environmentProvider, that.environmentProvider) &&
140-
Objects.equals(valueCacheOptions, that.valueCacheOptions) &&
141-
Objects.equals(batchLoaderScheduler, that.batchLoaderScheduler);
138+
&& cachingEnabled == that.cachingEnabled
139+
&& cachingExceptionsEnabled == that.cachingExceptionsEnabled
140+
&& maxBatchSize == that.maxBatchSize
141+
&& Objects.equals(cacheKeyFunction, that.cacheKeyFunction) &&
142+
Objects.equals(cacheMap, that.cacheMap) &&
143+
Objects.equals(valueCache, that.valueCache) &&
144+
Objects.equals(statisticsCollector, that.statisticsCollector) &&
145+
Objects.equals(environmentProvider, that.environmentProvider) &&
146+
Objects.equals(valueCacheOptions, that.valueCacheOptions) &&
147+
Objects.equals(batchLoaderScheduler, that.batchLoaderScheduler);
142148
}
143149

144150

@@ -254,7 +260,12 @@ public DataLoaderInstrumentation getInstrumentation() {
254260
return instrumentation;
255261
}
256262

263+
public DispatchStrategy getDispatchStrategy() {
264+
return dispatchStrategy;
265+
}
266+
257267
public static class Builder {
268+
private DispatchStrategy dispatchStrategy = DispatchStrategy.NO_OP;
258269
private boolean batchingEnabled;
259270
private boolean cachingEnabled;
260271
private boolean cachingExceptionsEnabled;
@@ -285,12 +296,14 @@ public Builder() {
285296
this.valueCacheOptions = other.valueCacheOptions;
286297
this.batchLoaderScheduler = other.batchLoaderScheduler;
287298
this.instrumentation = other.instrumentation;
299+
this.dispatchStrategy = other.dispatchStrategy;
288300
}
289301

290302
/**
291303
* Sets the option that determines whether batch loading is enabled.
292304
*
293305
* @param batchingEnabled {@code true} to enable batch loading, {@code false} otherwise
306+
*
294307
* @return this builder for fluent coding
295308
*/
296309
public Builder setBatchingEnabled(boolean batchingEnabled) {
@@ -302,6 +315,7 @@ public Builder setBatchingEnabled(boolean batchingEnabled) {
302315
* Sets the option that determines whether caching is enabled.
303316
*
304317
* @param cachingEnabled {@code true} to enable caching, {@code false} otherwise
318+
*
305319
* @return this builder for fluent coding
306320
*/
307321
public Builder setCachingEnabled(boolean cachingEnabled) {
@@ -313,6 +327,7 @@ public Builder setCachingEnabled(boolean cachingEnabled) {
313327
* Sets the option that determines whether exceptional values are cache enabled.
314328
*
315329
* @param cachingExceptionsEnabled {@code true} to enable caching exceptional values, {@code false} otherwise
330+
*
316331
* @return this builder for fluent coding
317332
*/
318333
public Builder setCachingExceptionsEnabled(boolean cachingExceptionsEnabled) {
@@ -324,6 +339,7 @@ public Builder setCachingExceptionsEnabled(boolean cachingExceptionsEnabled) {
324339
* Sets the function to use for creating the cache key, if caching is enabled.
325340
*
326341
* @param cacheKeyFunction the cache key function to use
342+
*
327343
* @return this builder for fluent coding
328344
*/
329345
public Builder setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
@@ -335,6 +351,7 @@ public Builder setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
335351
* Sets the cache map implementation to use for caching, if caching is enabled.
336352
*
337353
* @param cacheMap the cache map instance
354+
*
338355
* @return this builder for fluent coding
339356
*/
340357
public Builder setCacheMap(CacheMap<?, ?> cacheMap) {
@@ -346,6 +363,7 @@ public Builder setCacheMap(CacheMap<?, ?> cacheMap) {
346363
* Sets the value cache implementation to use for caching values, if caching is enabled.
347364
*
348365
* @param valueCache the value cache instance
366+
*
349367
* @return this builder for fluent coding
350368
*/
351369
public Builder setValueCache(ValueCache<?, ?> valueCache) {
@@ -358,6 +376,7 @@ public Builder setValueCache(ValueCache<?, ?> valueCache) {
358376
* before they are split into multiple class
359377
*
360378
* @param maxBatchSize the maximum batch size
379+
*
361380
* @return this builder for fluent coding
362381
*/
363382
public Builder setMaxBatchSize(int maxBatchSize) {
@@ -371,6 +390,7 @@ public Builder setMaxBatchSize(int maxBatchSize) {
371390
* a common value
372391
*
373392
* @param statisticsCollector the statistics collector to use
393+
*
374394
* @return this builder for fluent coding
375395
*/
376396
public Builder setStatisticsCollector(Supplier<StatisticsCollector> statisticsCollector) {
@@ -382,6 +402,7 @@ public Builder setStatisticsCollector(Supplier<StatisticsCollector> statisticsCo
382402
* Sets the batch loader environment provider that will be used to give context to batch load functions
383403
*
384404
* @param environmentProvider the batch loader context provider
405+
*
385406
* @return this builder for fluent coding
386407
*/
387408
public Builder setBatchLoaderContextProvider(BatchLoaderContextProvider environmentProvider) {
@@ -393,6 +414,7 @@ public Builder setBatchLoaderContextProvider(BatchLoaderContextProvider environm
393414
* Sets the {@link ValueCacheOptions} that control how the {@link ValueCache} will be used
394415
*
395416
* @param valueCacheOptions the value cache options
417+
*
396418
* @return this builder for fluent coding
397419
*/
398420
public Builder setValueCacheOptions(ValueCacheOptions valueCacheOptions) {
@@ -405,6 +427,7 @@ public Builder setValueCacheOptions(ValueCacheOptions valueCacheOptions) {
405427
* to some future time.
406428
*
407429
* @param batchLoaderScheduler the scheduler
430+
*
408431
* @return this builder for fluent coding
409432
*/
410433
public Builder setBatchLoaderScheduler(BatchLoaderScheduler batchLoaderScheduler) {
@@ -416,13 +439,19 @@ public Builder setBatchLoaderScheduler(BatchLoaderScheduler batchLoaderScheduler
416439
* Sets in a new {@link DataLoaderInstrumentation}
417440
*
418441
* @param instrumentation the new {@link DataLoaderInstrumentation}
442+
*
419443
* @return this builder for fluent coding
420444
*/
421445
public Builder setInstrumentation(DataLoaderInstrumentation instrumentation) {
422446
this.instrumentation = nonNull(instrumentation);
423447
return this;
424448
}
425449

450+
public Builder setDispatchStrategy(DispatchStrategy dispatchStrategy) {
451+
this.dispatchStrategy = dispatchStrategy;
452+
return this;
453+
}
454+
426455
public DataLoaderOptions build() {
427456
return new DataLoaderOptions(this);
428457
}

0 commit comments

Comments
 (0)