@@ -70,7 +70,7 @@ a list of keys
70
70
}
71
71
};
72
72
73
- DataLoader<Long , User > userLoader = new DataLoader<> (userBatchLoader);
73
+ DataLoader<Long , User > userLoader = DataLoader . newDataLoader (userBatchLoader);
74
74
75
75
```
76
76
@@ -169,33 +169,25 @@ That said, with key caching turn on (the default), it will still be more efficie
169
169
170
170
Often there is a need to call the batch loader function with some sort of call context environment, such as the calling users security
171
171
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 ` .
173
174
174
175
``` 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);
185
181
182
+ BatchLoaderWithContext<String , String > batchLoader = new BatchLoaderWithContext<String , String > () {
186
183
@Override
187
184
public CompletionStage<List<String > > load (List<String > keys , BatchLoaderEnvironment environment ) {
188
185
SecurityCtx callCtx = environment. getContext();
189
186
return callDatabaseForResults(callCtx, keys);
190
187
}
191
188
};
192
189
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);
199
191
```
200
192
201
193
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
214
206
Given say 10 user id keys you might only get 7 results back. This can be more naturally represented in a map
215
207
than in an order list of values when returning values from the batch loader function.
216
208
217
- You can use ` org.dataloader.MapBatchLoader ` for this purpose.
209
+ You can use ` org.dataloader.MappedBatchLoader ` for this purpose.
218
210
219
211
When the map is processed by the ` DataLoader ` code, any keys that are missing in the map
220
212
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.
297
289
In certain uncommon cases, a DataLoader which does not cache may be desirable.
298
290
299
291
``` java
300
- new DataLoader< String , User > (userBatchLoader, DataLoaderOptions . newOptions(). setCachingEnabled(false ));
292
+ DataLoader . newDataLoader (userBatchLoader, DataLoaderOptions . newOptions(). setCachingEnabled(false ));
301
293
```
302
294
303
295
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
391
383
``` java
392
384
MyCustomCache customCache = new MyCustomCache ();
393
385
DataLoaderOptions options = DataLoaderOptions . newOptions(). setCacheMap(customCache);
394
- new DataLoader< String , User > (userBatchLoader, options);
386
+ DataLoader . newDataLoader (userBatchLoader, options);
395
387
```
396
388
397
389
You could choose to use one of the fancy cache implementations from Guava or Kaffeine and wrap it in a ` CacheMap ` wrapper ready
0 commit comments