Class DelegatingDataLoader<K,V>
- Type Parameters:
K
- type parameter indicating the type of the data load keysV
- type parameter indicating the type of the data that is returned
DataLoader
makes it easier to create wrappers of DataLoader
s in case you want to change how
values are returned for example.
The most common way would be to make a new DelegatingDataLoader
subclass that overloads the load(Object, Object)
method.
For example the following allows you to change the returned value in some way :
DataLoader<String, String> rawLoader = createDataLoader();
DelegatingDataLoader<String, String> delegatingDataLoader = new DelegatingDataLoader<>(rawLoader) {
public CompletableFuture<String> load(@NonNull String key, @Nullable Object keyContext) {
CompletableFuture<String> cf = super.load(key, keyContext);
return cf.thenApply(v -> "|" + v + "|");
}
};
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDataLoader<K,
V> Clears the future with the specified key from the cache, if caching is enabled, so it will be re-fetched on the next load request.DataLoader<K,
V> clear
(K key, BiConsumer<Void, Throwable> handler) Clears the future with the specified key from the cache remote value store, if caching is enabled and a remote store is set, so it will be re-fetched and stored on the next load request.DataLoader<K,
V> clearAll()
Clears the entire cache map of the loader.DataLoader<K,
V> clearAll
(BiConsumer<Void, Throwable> handler) Clears the entire cache map of the loader, and of the cached value store.dispatch()
Dispatches the queued load requests to the batch execution function and returns a promise of the result.NormallyDataLoader.dispatch()
is an asynchronous operation but this version will 'join' on the results if dispatch and wait for them to complete.int
Dispatches the queued load requests to the batch execution function and returns both the promise of the result and the number of entries that were dispatched.getCacheKey
(K key) Gets the object that is used in the internal cache map as key, by applying the cache key function to the provided key.Gets the cacheMap associated with this data loader passed in viaDataLoaderOptions.cacheMap()
DataLoader<K,
V> getIfCompleted
(K key) This will return an optional promise to a value previously loaded via aDataLoader.load(Object)
call that has in fact been completed or empty if no call has been made for that key or the promise has not completed yet.getIfPresent
(K key) This will return an optional promise to a value previously loaded via aDataLoader.load(Object)
call or empty if not call has been made for that key.This returns the last instant the data loader was dispatched.Gets the statistics associated with this data loader.This returns theDuration
since the data loader was dispatched.ValueCache<K,
V> Gets the valueCache associated with this data loader passed in viaDataLoaderOptions.valueCache()
TheDataLoader.load(Object)
andDataLoader.loadMany(List)
type methods all call back to theDataLoader.load(Object, Object)
and hence we don't override them.DataLoader<K,
V> Primes the cache with the given key and error.DataLoader<K,
V> prime
(K key, CompletableFuture<V> value) Primes the cache with the given key and value.DataLoader<K,
V> Primes the cache with the given key and value.DataLoader<K,
V> transform
(Consumer<DataLoaderFactory.Builder<K, V>> builderConsumer) This allows you to change the currentDataLoader
and turn it into a new onestatic <K,
V> DataLoader<K, V> unwrap
(DataLoader<K, V> dataLoader) This can be called to unwrap a givenDataLoader
such that if it's aDelegatingDataLoader
the underlyingDataLoader
is returned otherwise it's just passed in data loaderMethods inherited from class org.dataloader.DataLoader
getBatchLoadFunction, getName, getOptions, load, loadMany, loadMany, loadMany, toString
-
Field Details
-
delegate
-
-
Constructor Details
-
DelegatingDataLoader
-
-
Method Details
-
unwrap
This can be called to unwrap a givenDataLoader
such that if it's aDelegatingDataLoader
the underlyingDataLoader
is returned otherwise it's just passed in data loader- Type Parameters:
K
- type parameter indicating the type of the data load keysV
- type parameter indicating the type of the data that is returned- Parameters:
dataLoader
- the dataLoader to unwrap- Returns:
- the delegate dataLoader OR just this current one if it's not wrapped
-
getDelegate
-
load
TheDataLoader.load(Object)
andDataLoader.loadMany(List)
type methods all call back to theDataLoader.load(Object, Object)
and hence we don't override them.- Overrides:
load
in classDataLoader<K,
V> - Parameters:
key
- the key to loadkeyContext
- a context object that is specific to this key- Returns:
- the future of the value
-
transform
Description copied from class:DataLoader
This allows you to change the currentDataLoader
and turn it into a new one- Overrides:
transform
in classDataLoader<K,
V> - Parameters:
builderConsumer
- theDataLoaderFactory.Builder
consumer for changing theDataLoader
- Returns:
- a newly built
DataLoader
instance
-
getLastDispatchTime
Description copied from class:DataLoader
This returns the last instant the data loader was dispatched. When the data loader is created this value is set to now.- Overrides:
getLastDispatchTime
in classDataLoader<K,
V> - Returns:
- the instant since the last dispatch
-
getTimeSinceDispatch
Description copied from class:DataLoader
This returns theDuration
since the data loader was dispatched. When the data loader is created this is zero.- Overrides:
getTimeSinceDispatch
in classDataLoader<K,
V> - Returns:
- the time duration since the last dispatch
-
getIfPresent
Description copied from class:DataLoader
This will return an optional promise to a value previously loaded via aDataLoader.load(Object)
call or empty if not call has been made for that key.If you do get a present CompletableFuture it does not mean it has been dispatched and completed yet. It just means it's at least pending and in cache.
If caching is disabled there will never be a present Optional returned.
NOTE : This will NOT cause a data load to happen. You must call
DataLoader.load(Object)
for that to happen.- Overrides:
getIfPresent
in classDataLoader<K,
V> - Parameters:
key
- the key to check- Returns:
- an Optional to the future of the value
-
getIfCompleted
Description copied from class:DataLoader
This will return an optional promise to a value previously loaded via aDataLoader.load(Object)
call that has in fact been completed or empty if no call has been made for that key or the promise has not completed yet.If you do get a present CompletableFuture it means it has been dispatched and completed. Completed is defined as
CompletableFuture.isDone()
returning true.If caching is disabled there will never be a present Optional returned.
NOTE : This will NOT cause a data load to happen. You must call
DataLoader.load(Object)
for that to happen.- Overrides:
getIfCompleted
in classDataLoader<K,
V> - Parameters:
key
- the key to check- Returns:
- an Optional to the future of the value
-
dispatch
Description copied from class:DataLoader
Dispatches the queued load requests to the batch execution function and returns a promise of the result.If batching is disabled, or there are no queued requests, then a succeeded promise is returned.
- Overrides:
dispatch
in classDataLoader<K,
V> - Returns:
- the promise of the queued load requests
-
dispatchWithCounts
Description copied from class:DataLoader
Dispatches the queued load requests to the batch execution function and returns both the promise of the result and the number of entries that were dispatched.If batching is disabled, or there are no queued requests, then a succeeded promise with no entries dispatched is returned.
- Overrides:
dispatchWithCounts
in classDataLoader<K,
V> - Returns:
- the promise of the queued load requests and the number of keys dispatched.
-
dispatchAndJoin
Description copied from class:DataLoader
NormallyDataLoader.dispatch()
is an asynchronous operation but this version will 'join' on the results if dispatch and wait for them to complete. If theCompletableFuture
callbacks make more calls to this data loader then theDataLoader.dispatchDepth()
will be > 0 and this method will loop around and wait for any other extra batch loads to occur.- Overrides:
dispatchAndJoin
in classDataLoader<K,
V> - Returns:
- the list of all results when the
DataLoader.dispatchDepth()
reached 0
-
dispatchDepth
public int dispatchDepth()- Overrides:
dispatchDepth
in classDataLoader<K,
V> - Returns:
- the depth of the batched key loads that need to be dispatched
-
getCacheKey
Description copied from class:DataLoader
Gets the object that is used in the internal cache map as key, by applying the cache key function to the provided key.If no cache key function is present in
DataLoaderOptions
, then the returned value equals the input key.- Overrides:
getCacheKey
in classDataLoader<K,
V> - Parameters:
key
- the input key- Returns:
- the cache key after the input is transformed with the cache key function
-
getStatistics
Description copied from class:DataLoader
Gets the statistics associated with this data loader. These will have been gather via theStatisticsCollector
passed in viaDataLoaderOptions.getStatisticsCollector()
- Overrides:
getStatistics
in classDataLoader<K,
V> - Returns:
- statistics for this data loader
-
getCacheMap
Description copied from class:DataLoader
Gets the cacheMap associated with this data loader passed in viaDataLoaderOptions.cacheMap()
- Overrides:
getCacheMap
in classDataLoader<K,
V> - Returns:
- the cacheMap of this data loader
-
getValueCache
Description copied from class:DataLoader
Gets the valueCache associated with this data loader passed in viaDataLoaderOptions.valueCache()
- Overrides:
getValueCache
in classDataLoader<K,
V> - Returns:
- the valueCache of this data loader
-
clear
Description copied from class:DataLoader
Clears the future with the specified key from the cache, if caching is enabled, so it will be re-fetched on the next load request.- Overrides:
clear
in classDataLoader<K,
V> - Parameters:
key
- the key to remove- Returns:
- the data loader for fluent coding
-
clear
Description copied from class:DataLoader
Clears the future with the specified key from the cache remote value store, if caching is enabled and a remote store is set, so it will be re-fetched and stored on the next load request.- Overrides:
clear
in classDataLoader<K,
V> - Parameters:
key
- the key to removehandler
- a handler that will be called after the async remote clear completes- Returns:
- the data loader for fluent coding
-
clearAll
Description copied from class:DataLoader
Clears the entire cache map of the loader.- Overrides:
clearAll
in classDataLoader<K,
V> - Returns:
- the data loader for fluent coding
-
clearAll
Description copied from class:DataLoader
Clears the entire cache map of the loader, and of the cached value store.- Overrides:
clearAll
in classDataLoader<K,
V> - Parameters:
handler
- a handler that will be called after the async remote clear all completes- Returns:
- the data loader for fluent coding
-
prime
Description copied from class:DataLoader
Primes the cache with the given key and value. Note this will only prime the future cache and not the value store. UseValueCache.set(Object, Object)
if you want to prime it with values before use- Overrides:
prime
in classDataLoader<K,
V> - Parameters:
key
- the keyvalue
- the value- Returns:
- the data loader for fluent coding
-
prime
Description copied from class:DataLoader
Primes the cache with the given key and error.- Overrides:
prime
in classDataLoader<K,
V> - Parameters:
key
- the keyerror
- the exception to prime instead of a value- Returns:
- the data loader for fluent coding
-
prime
Description copied from class:DataLoader
Primes the cache with the given key and value. Note this will only prime the future cache and not the value store. UseValueCache.set(Object, Object)
if you want to prime it with values before use- Overrides:
prime
in classDataLoader<K,
V> - Parameters:
key
- the keyvalue
- the value- Returns:
- the data loader for fluent coding
-