org.dataloader.BatchLoader Java Examples

The following examples show how to use org.dataloader.BatchLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataLoaderModule.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@Provides
DataLoaderRegistry dataLoaderRegistry(BookServiceGrpc.BookServiceFutureStub bookService) {

  // TODO: Use multibinder to modularize this, or automate this somehow
  BatchLoader<String, Book> bookBatchLoader =
      keys -> {
        ListenableFuture<List<Book>> listenableFuture =
            Futures.transform(
                bookService.listBooks(
                    ListBooksRequest.newBuilder()
                        .addAllIds(keys)
                        .setPageSize(keys.size())
                        .build()),
                resp -> resp.getBooksList(),
                MoreExecutors.directExecutor());
        return FutureConverter.toCompletableFuture(listenableFuture);
      };

  DataLoaderRegistry registry = new DataLoaderRegistry();
  registry.register("books", new DataLoader<>(bookBatchLoader));
  return registry;
}
 
Example #2
Source File: ReadmeExamples.java    From java-dataloader with Apache License 2.0 5 votes vote down vote up
private void tryBatcLoader() {
    DataLoader<String, User> dataLoader = DataLoader.newDataLoaderWithTry(new BatchLoader<String, Try<User>>() {
        @Override
        public CompletionStage<List<Try<User>>> load(List<String> keys) {
            return CompletableFuture.supplyAsync(() -> {
                List<Try<User>> users = new ArrayList<>();
                for (String key : keys) {
                    Try<User> userTry = loadUser(key);
                    users.add(userTry);
                }
                return users;
            });
        }
    });
}