@@ -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
170170Often there is a need to call the batch loader function with some sort of call context environment, such as the calling users security
171171credentials 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
201193The 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.
297289In 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
303295Calling 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
397389You 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