Skip to content

Commit c08dd81

Browse files
committed
Initial cut of the DataLoader as an interface
1 parent 96adddc commit c08dd81

14 files changed

+716
-624
lines changed

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

Lines changed: 20 additions & 514 deletions
Large diffs are not rendered by default.
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
package org.dataloader;
2+
3+
4+
import org.dataloader.impl.DataLoaderImpl;
5+
6+
public class DataLoaderFactory {
7+
8+
/**
9+
* Creates new DataLoader with the specified batch loader function and default options
10+
* (batching, caching and unlimited batch size).
11+
*
12+
* @param batchLoadFunction the batch load function to use
13+
* @param <K> the key type
14+
* @param <V> the value type
15+
* @return a new DataLoader
16+
*/
17+
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction) {
18+
return newDataLoader(batchLoadFunction, null);
19+
}
20+
21+
/**
22+
* Creates new DataLoader with the specified batch loader function with the provided options
23+
*
24+
* @param batchLoadFunction the batch load function to use
25+
* @param options the options to use
26+
* @param <K> the key type
27+
* @param <V> the value type
28+
* @return a new DataLoader
29+
*/
30+
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
31+
return new DataLoaderImpl<>(batchLoadFunction, options);
32+
}
33+
34+
/**
35+
* Creates new DataLoader with the specified batch loader function and default options
36+
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
37+
* {@link org.dataloader.Try} objects.
38+
* <p>
39+
* If its important you to know the exact status of each item in a batch call and whether it threw exceptions then
40+
* you can use this form to create the data loader.
41+
* <p>
42+
* Using Try objects allows you to capture a value returned or an exception that might
43+
* have occurred trying to get a value. .
44+
*
45+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
46+
* @param <K> the key type
47+
* @param <V> the value type
48+
* @return a new DataLoader
49+
*/
50+
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction) {
51+
return newDataLoaderWithTry(batchLoadFunction, null);
52+
}
53+
54+
/**
55+
* Creates new DataLoader with the specified batch loader function and with the provided options
56+
* where the batch loader function returns a list of
57+
* {@link org.dataloader.Try} objects.
58+
*
59+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
60+
* @param options the options to use
61+
* @param <K> the key type
62+
* @param <V> the value type
63+
* @return a new DataLoader
64+
* @see #newDataLoaderWithTry(BatchLoader)
65+
*/
66+
@SuppressWarnings("unchecked")
67+
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
68+
return new DataLoaderImpl<>(batchLoadFunction, options);
69+
}
70+
71+
/**
72+
* Creates new DataLoader with the specified batch loader function and default options
73+
* (batching, caching and unlimited batch size).
74+
*
75+
* @param batchLoadFunction the batch load function to use
76+
* @param <K> the key type
77+
* @param <V> the value type
78+
* @return a new DataLoader
79+
*/
80+
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction) {
81+
return newDataLoader(batchLoadFunction, null);
82+
}
83+
84+
/**
85+
* Creates new DataLoader with the specified batch loader function with the provided options
86+
*
87+
* @param batchLoadFunction the batch load function to use
88+
* @param options the options to use
89+
* @param <K> the key type
90+
* @param <V> the value type
91+
* @return a new DataLoader
92+
*/
93+
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
94+
return new DataLoaderImpl<>(batchLoadFunction, options);
95+
}
96+
97+
/**
98+
* Creates new DataLoader with the specified batch loader function and default options
99+
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
100+
* {@link org.dataloader.Try} objects.
101+
* <p>
102+
* If its important you to know the exact status of each item in a batch call and whether it threw exceptions then
103+
* you can use this form to create the data loader.
104+
* <p>
105+
* Using Try objects allows you to capture a value returned or an exception that might
106+
* have occurred trying to get a value. .
107+
*
108+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
109+
* @param <K> the key type
110+
* @param <V> the value type
111+
* @return a new DataLoader
112+
*/
113+
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
114+
return newDataLoaderWithTry(batchLoadFunction, null);
115+
}
116+
117+
/**
118+
* Creates new DataLoader with the specified batch loader function and with the provided options
119+
* where the batch loader function returns a list of
120+
* {@link org.dataloader.Try} objects.
121+
*
122+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
123+
* @param options the options to use
124+
* @param <K> the key type
125+
* @param <V> the value type
126+
* @return a new DataLoader
127+
* @see #newDataLoaderWithTry(BatchLoader)
128+
*/
129+
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
130+
return new DataLoaderImpl<>(batchLoadFunction, options);
131+
}
132+
133+
/**
134+
* Creates new DataLoader with the specified batch loader function and default options
135+
* (batching, caching and unlimited batch size).
136+
*
137+
* @param batchLoadFunction the batch load function to use
138+
* @param <K> the key type
139+
* @param <V> the value type
140+
* @return a new DataLoader
141+
*/
142+
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction) {
143+
return newMappedDataLoader(batchLoadFunction, null);
144+
}
145+
146+
/**
147+
* Creates new DataLoader with the specified batch loader function with the provided options
148+
*
149+
* @param batchLoadFunction the batch load function to use
150+
* @param options the options to use
151+
* @param <K> the key type
152+
* @param <V> the value type
153+
* @return a new DataLoader
154+
*/
155+
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
156+
return new DataLoaderImpl<>(batchLoadFunction, options);
157+
}
158+
159+
/**
160+
* Creates new DataLoader with the specified batch loader function and default options
161+
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
162+
* {@link org.dataloader.Try} objects.
163+
* <p>
164+
* If its important you to know the exact status of each item in a batch call and whether it threw exceptions then
165+
* you can use this form to create the data loader.
166+
* <p>
167+
* Using Try objects allows you to capture a value returned or an exception that might
168+
* have occurred trying to get a value. .
169+
* <p>
170+
*
171+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
172+
* @param <K> the key type
173+
* @param <V> the value type
174+
* @return a new DataLoader
175+
*/
176+
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction) {
177+
return newMappedDataLoaderWithTry(batchLoadFunction, null);
178+
}
179+
180+
/**
181+
* Creates new DataLoader with the specified batch loader function and with the provided options
182+
* where the batch loader function returns a list of
183+
* {@link org.dataloader.Try} objects.
184+
*
185+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
186+
* @param options the options to use
187+
* @param <K> the key type
188+
* @param <V> the value type
189+
* @return a new DataLoader
190+
* @see #newDataLoaderWithTry(BatchLoader)
191+
*/
192+
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
193+
return new DataLoaderImpl<>(batchLoadFunction, options);
194+
}
195+
196+
/**
197+
* Creates new DataLoader with the specified mapped batch loader function and default options
198+
* (batching, caching and unlimited batch size).
199+
*
200+
* @param batchLoadFunction the batch load function to use
201+
* @param <K> the key type
202+
* @param <V> the value type
203+
* @return a new DataLoader
204+
*/
205+
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction) {
206+
return newMappedDataLoader(batchLoadFunction, null);
207+
}
208+
209+
/**
210+
* Creates new DataLoader with the specified batch loader function with the provided options
211+
*
212+
* @param batchLoadFunction the batch load function to use
213+
* @param options the options to use
214+
* @param <K> the key type
215+
* @param <V> the value type
216+
* @return a new DataLoader
217+
*/
218+
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
219+
return new DataLoaderImpl<>(batchLoadFunction, options);
220+
}
221+
222+
/**
223+
* Creates new DataLoader with the specified batch loader function and default options
224+
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
225+
* {@link org.dataloader.Try} objects.
226+
* <p>
227+
* If its important you to know the exact status of each item in a batch call and whether it threw exceptions then
228+
* you can use this form to create the data loader.
229+
* <p>
230+
* Using Try objects allows you to capture a value returned or an exception that might
231+
* have occurred trying to get a value. .
232+
*
233+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
234+
* @param <K> the key type
235+
* @param <V> the value type
236+
* @return a new DataLoader
237+
*/
238+
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
239+
return newMappedDataLoaderWithTry(batchLoadFunction, null);
240+
}
241+
242+
/**
243+
* Creates new DataLoader with the specified batch loader function and with the provided options
244+
* where the batch loader function returns a list of
245+
* {@link org.dataloader.Try} objects.
246+
*
247+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
248+
* @param options the options to use
249+
* @param <K> the key type
250+
* @param <V> the value type
251+
* @return a new DataLoader
252+
* @see #newDataLoaderWithTry(BatchLoader)
253+
*/
254+
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
255+
return new DataLoaderImpl<>(batchLoadFunction, options);
256+
}
257+
258+
}

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public class DataLoaderOptions {
3737
private boolean batchingEnabled;
3838
private boolean cachingEnabled;
3939
private boolean cachingExceptionsEnabled;
40-
private CacheKey cacheKeyFunction;
41-
private CacheMap cacheMap;
40+
private CacheKey<?> cacheKeyFunction;
41+
private CacheMap<?, ?> cacheMap;
4242
private int maxBatchSize;
4343
private Supplier<StatisticsCollector> statisticsCollector;
4444
private BatchLoaderContextProvider environmentProvider;
@@ -92,7 +92,6 @@ public boolean batchingEnabled() {
9292
* Sets the option that determines whether batch loading is enabled.
9393
*
9494
* @param batchingEnabled {@code true} to enable batch loading, {@code false} otherwise
95-
*
9695
* @return the data loader options for fluent coding
9796
*/
9897
public DataLoaderOptions setBatchingEnabled(boolean batchingEnabled) {
@@ -113,7 +112,6 @@ public boolean cachingEnabled() {
113112
* Sets the option that determines whether caching is enabled.
114113
*
115114
* @param cachingEnabled {@code true} to enable caching, {@code false} otherwise
116-
*
117115
* @return the data loader options for fluent coding
118116
*/
119117
public DataLoaderOptions setCachingEnabled(boolean cachingEnabled) {
@@ -123,7 +121,7 @@ public DataLoaderOptions setCachingEnabled(boolean cachingEnabled) {
123121

124122
/**
125123
* Option that determines whether to cache exceptional values (the default), or not.
126-
*
124+
* <p>
127125
* For short lived caches (that is request caches) it makes sense to cache exceptions since
128126
* its likely the key is still poisoned. However if you have long lived caches, then it may make
129127
* sense to set this to false since the downstream system may have recovered from its failure
@@ -136,10 +134,9 @@ public boolean cachingExceptionsEnabled() {
136134
}
137135

138136
/**
139-
* Sets the option that determines whether exceptional values are cachedis enabled.
137+
* Sets the option that determines whether exceptional values are cached.
140138
*
141139
* @param cachingExceptionsEnabled {@code true} to enable caching exceptional values, {@code false} otherwise
142-
*
143140
* @return the data loader options for fluent coding
144141
*/
145142
public DataLoaderOptions setCachingExceptionsEnabled(boolean cachingExceptionsEnabled) {
@@ -162,10 +159,9 @@ public Optional<CacheKey> cacheKeyFunction() {
162159
* Sets the function to use for creating the cache key, if caching is enabled.
163160
*
164161
* @param cacheKeyFunction the cache key function to use
165-
*
166162
* @return the data loader options for fluent coding
167163
*/
168-
public DataLoaderOptions setCacheKeyFunction(CacheKey cacheKeyFunction) {
164+
public DataLoaderOptions setCacheKeyFunction(CacheKey<?> cacheKeyFunction) {
169165
this.cacheKeyFunction = cacheKeyFunction;
170166
return this;
171167
}
@@ -177,18 +173,17 @@ public DataLoaderOptions setCacheKeyFunction(CacheKey cacheKeyFunction) {
177173
*
178174
* @return an optional with the cache map instance, or empty
179175
*/
180-
public Optional<CacheMap> cacheMap() {
176+
public Optional<CacheMap<?, ?>> cacheMap() {
181177
return Optional.ofNullable(cacheMap);
182178
}
183179

184180
/**
185181
* Sets the cache map implementation to use for caching, if caching is enabled.
186182
*
187183
* @param cacheMap the cache map instance
188-
*
189184
* @return the data loader options for fluent coding
190185
*/
191-
public DataLoaderOptions setCacheMap(CacheMap cacheMap) {
186+
public DataLoaderOptions setCacheMap(CacheMap<?,?> cacheMap) {
192187
this.cacheMap = cacheMap;
193188
return this;
194189
}
@@ -208,7 +203,6 @@ public int maxBatchSize() {
208203
* before they are split into multiple class
209204
*
210205
* @param maxBatchSize the maximum batch size
211-
*
212206
* @return the data loader options for fluent coding
213207
*/
214208
public DataLoaderOptions setMaxBatchSize(int maxBatchSize) {
@@ -229,7 +223,6 @@ public StatisticsCollector getStatisticsCollector() {
229223
* a common value
230224
*
231225
* @param statisticsCollector the statistics collector to use
232-
*
233226
* @return the data loader options for fluent coding
234227
*/
235228
public DataLoaderOptions setStatisticsCollector(Supplier<StatisticsCollector> statisticsCollector) {
@@ -248,7 +241,6 @@ public BatchLoaderContextProvider getBatchLoaderContextProvider() {
248241
* Sets the batch loader environment provider that will be used to give context to batch load functions
249242
*
250243
* @param contextProvider the batch loader context provider
251-
*
252244
* @return the data loader options for fluent coding
253245
*/
254246
public DataLoaderOptions setBatchLoaderContextProvider(BatchLoaderContextProvider contextProvider) {

src/main/java/org/dataloader/impl/CompletableFutureKit.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static <V> CompletableFuture<V> failedFuture(Exception e) {
2020
return future;
2121
}
2222

23-
public static Throwable cause(CompletableFuture completableFuture) {
23+
public static Throwable cause(CompletableFuture<?> completableFuture) {
2424
if (!completableFuture.isCompletedExceptionally()) {
2525
return null;
2626
}
@@ -38,11 +38,11 @@ public static Throwable cause(CompletableFuture completableFuture) {
3838
}
3939
}
4040

41-
public static boolean succeeded(CompletableFuture future) {
41+
public static boolean succeeded(CompletableFuture<?> future) {
4242
return future.isDone() && !future.isCompletedExceptionally();
4343
}
4444

45-
public static boolean failed(CompletableFuture future) {
45+
public static boolean failed(CompletableFuture<?> future) {
4646
return future.isDone() && future.isCompletedExceptionally();
4747
}
4848

0 commit comments

Comments
 (0)