K
- type parameter indicating the type of keys to use for data load requests.V
- type parameter indicating the type of values returned@FunctionalInterface @PublicSpi public interface BatchLoader<K,V>
There are a few constraints that must be upheld:
For example, if your batch function was provided the list of keys:
[ 2, 9, 6, 1 ]and loading from a back-end service returned this list of values:
[ { id: 9, name: 'Chicago' }, { id: 1, name: 'New York' }, { id: 2, name: 'San Francisco' }, ]then the batch loader function contract has been broken.
The back-end service returned results in a different order than we requested, likely because it was more efficient for it to do so. Also, it omitted a result for key 6, which we may interpret as no value existing for that key.
To uphold the constraints of the batch function, it must return a List of values the same length as the List of keys, and re-order them to ensure each index aligns with the original keys [ 2, 9, 6, 1 ]:
[ { id: 2, name: 'San Francisco' }, { id: 9, name: 'Chicago' }, null, { id: 1, name: 'New York' } ]
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.CompletionStage<java.util.List<V>> |
load(java.util.List<K> keys)
Called to batch load the provided keys and return a promise to a list of values.
|
java.util.concurrent.CompletionStage<java.util.List<V>> load(java.util.List<K> keys)
If you need calling context then implement BatchLoaderWithContext
keys
- the collection of keys to load