Skip to content

Commit ea8a429

Browse files
committed
Now with repeated factory methods of the new different loader interfaces
1 parent 8d3636e commit ea8a429

13 files changed

+424
-234
lines changed

README.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ a list of keys
7070
}
7171
};
7272

73-
DataLoader<Long, User> userLoader = new DataLoader<>(userBatchLoader);
73+
DataLoader<Long, User> userLoader = DataLoader.newDataLoader(userBatchLoader);
7474

7575
```
7676

@@ -169,33 +169,25 @@ That said, with key caching turn on (the default), it will still be more efficie
169169

170170
Often there is a need to call the batch loader function with some sort of call context environment, such as the calling users security
171171
credentials or the database connection parameters. You can do this by implementing a
172-
`org.dataloader.BatchLoaderEnvironmentProvider`.
172+
`org.dataloader.BatchLoaderEnvironmentProvider` and using one of the `xxxWithContext` batch loading interfaces
173+
such as `org.dataloader.BatchLoaderWithContext`.
173174

174175
```java
175-
BatchLoader<String, String> batchLoader = new BatchLoader<String, String>() {
176-
//
177-
// the reason this method exists is for backwards compatibility. There are a large
178-
// number of existing dataloader clients out there that used this method before the call context was invented
179-
// and hence to preserve compatibility we have this unfortunate method declaration.
180-
//
181-
@Override
182-
public CompletionStage<List<String>> load(List<String> keys) {
183-
throw new UnsupportedOperationException();
184-
}
176+
BatchLoaderEnvironment batchLoaderEnvironment = BatchLoaderEnvironment.newBatchLoaderEnvironment()
177+
.context(SecurityCtx.getCallingUserCtx()).build();
178+
179+
DataLoaderOptions options = DataLoaderOptions.newOptions()
180+
.setBatchLoaderEnvironmentProvider(() -> batchLoaderEnvironment);
185181

182+
BatchLoaderWithContext<String, String> batchLoader = new BatchLoaderWithContext<String, String>() {
186183
@Override
187184
public CompletionStage<List<String>> load(List<String> keys, BatchLoaderEnvironment environment) {
188185
SecurityCtx callCtx = environment.getContext();
189186
return callDatabaseForResults(callCtx, keys);
190187
}
191188
};
192189

193-
BatchLoaderEnvironment batchLoaderEnvironment = BatchLoaderEnvironment.newBatchLoaderEnvironment()
194-
.context(SecurityCtx.getCallingUserCtx()).build();
195-
196-
DataLoaderOptions options = DataLoaderOptions.newOptions()
197-
.setBatchLoaderEnvironmentProvider(() -> batchLoaderEnvironment);
198-
DataLoader<String, String> loader = new DataLoader<>(batchLoader, options);
190+
DataLoader<String, String> loader = DataLoader.newDataLoader(batchLoader, options);
199191
```
200192

201193
The batch loading code will now receive this environment object and it can be used to get context perhaps allowing it
@@ -214,7 +206,7 @@ For example, let's assume you want to load users from a database, you could prob
214206
Given say 10 user id keys you might only get 7 results back. This can be more naturally represented in a map
215207
than in an order list of values when returning values from the batch loader function.
216208

217-
You can use `org.dataloader.MapBatchLoader` for this purpose.
209+
You can use `org.dataloader.MappedBatchLoader` for this purpose.
218210

219211
When the map is processed by the `DataLoader` code, any keys that are missing in the map
220212
will be replaced with null values. The semantics that the number of `DataLoader.load` requests
@@ -297,7 +289,7 @@ react to that, in a type safe manner.
297289
In certain uncommon cases, a DataLoader which does not cache may be desirable.
298290

299291
```java
300-
new DataLoader<String, User>(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false));
292+
DataLoader.newDataLoader(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false));
301293
```
302294

303295
Calling the above will ensure that every call to `.load()` will produce a new promise, and requested keys will not be saved in memory.
@@ -391,7 +383,7 @@ However you can create your own custom cache and supply it to the data loader on
391383
```java
392384
MyCustomCache customCache = new MyCustomCache();
393385
DataLoaderOptions options = DataLoaderOptions.newOptions().setCacheMap(customCache);
394-
new DataLoader<String, User>(userBatchLoader, options);
386+
DataLoader.newDataLoader(userBatchLoader, options);
395387
```
396388

397389
You could choose to use one of the fancy cache implementations from Guava or Kaffeine and wrap it in a `CacheMap` wrapper ready

src/main/java/org/dataloader/BatchLoader.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,12 @@ public interface BatchLoader<K, V> {
7878
/**
7979
* Called to batch load the provided keys and return a promise to a list of values.
8080
*
81-
* If you need calling context then implement the {@link #load(java.util.List, BatchLoaderEnvironment)} method
82-
* instead which is the preferred method.
81+
* If you need calling context then implement {@link org.dataloader.BatchLoaderWithContext}
8382
*
8483
* @param keys the collection of keys to load
8584
*
8685
* @return a promise of the values for those keys
87-
*
8886
*/
8987
CompletionStage<List<V>> load(List<K> keys);
90-
91-
/**
92-
* Called to batch load the provided keys and return a promise to a list of values. This default
93-
* version can be given an environment object to that maybe be useful during the call. A typical use case
94-
* is passing in security credentials or database connection details say.
95-
*
96-
* This method is implemented as a default method in order to preserve the API for previous
97-
* callers. It is always called first by the {@link org.dataloader.DataLoader} code and simply
98-
* delegates to the {@link #load(java.util.List)} method.
99-
*
100-
* @param keys the collection of keys to load
101-
* @param environment an environment object that can help with the call
102-
*
103-
* @return a promise of the values for those keys
104-
*/
105-
@SuppressWarnings({"unused", "deprecation"})
106-
default CompletionStage<List<V>> load(List<K> keys, BatchLoaderEnvironment environment) {
107-
return load(keys);
108-
}
10988
}
89+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.dataloader;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CompletionStage;
5+
6+
/**
7+
* This form of {@link org.dataloader.BatchLoader} is given a {@link org.dataloader.BatchLoaderEnvironment} object
8+
* that encapsulates the calling context. A typical use case is passing in security credentials or database connection details
9+
* say.
10+
*
11+
* See {@link org.dataloader.BatchLoader} for more details on the design invariants that you must implement in order to
12+
* use this interface.
13+
*/
14+
public interface BatchLoaderWithContext<K, V> {
15+
/**
16+
* Called to batch load the provided keys and return a promise to a list of values. This default
17+
* version can be given an environment object to that maybe be useful during the call. A typical use case
18+
* is passing in security credentials or database connection details say.
19+
*
20+
* @param keys the collection of keys to load
21+
* @param environment an environment object that can help with the call
22+
*
23+
* @return a promise of the values for those keys
24+
*/
25+
CompletionStage<List<V>> load(List<K> keys, BatchLoaderEnvironment environment);
26+
}

0 commit comments

Comments
 (0)