Interface MappedBatchLoader<K,V extends @Nullable Object>

Type Parameters:
K - type parameter indicating the type of keys to use for data load requests.
V - type parameter indicating the type of values returned

@PublicSpi @NullMarked public interface MappedBatchLoader<K,V extends @Nullable Object>
A function that is invoked for batch loading a map of data values indicated by the provided set of keys. The function returns a promise of a map of results of individual load requests.

There are a few constraints that must be upheld:

  • The keys MUST be able to be first class keys in a Java map. Get your equals() and hashCode() methods in order
  • The caller of the DataLoader that uses this batch loader function MUST be able to cope with null values coming back as results

This form is useful when you don't have a 1:1 mapping of keys to values or when null is an acceptable value for a missing value.

For example, let's assume you want to load users from a database, you could probably use a query that looks like this:

    SELECT * FROM User WHERE id IN (keys)
 

Given say 10 user id keys you might only get 7 results back. This can be more naturally represented in a map than in an ordered list of values from the batch loader function.

When the map is processed by the DataLoader code, any keys that are missing in the map will be replaced with null values. The semantic that the number of DataLoader.load(Object) requests are matched with and equal number of values is kept.

This means that if 10 keys are asked for then DataLoader.dispatch() will return a promise of 10 value results and each of the DataLoader.load(Object) will complete with a value, null or an exception.

  • Method Summary

    Modifier and Type
    Method
    Description
    load(Set<K> keys)
    Called to batch load the provided keys and return a promise to a map of values.
  • Method Details

    • load

      CompletionStage<Map<K,V>> load(Set<K> keys)
      Called to batch load the provided keys and return a promise to a map of values.
      Parameters:
      keys - the set of keys to load
      Returns:
      a promise to a map of values for those keys