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 DataLoaders 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.intDispatches 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 theDurationsince 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 currentDataLoaderand turn it into a new onestatic <K,V> DataLoader<K, V> unwrap(DataLoader<K, V> dataLoader) This can be called to unwrap a givenDataLoadersuch that if it's aDelegatingDataLoaderthe underlyingDataLoaderis 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 givenDataLoadersuch that if it's aDelegatingDataLoaderthe underlyingDataLoaderis 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:
loadin 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:DataLoaderThis allows you to change the currentDataLoaderand turn it into a new one- Overrides:
transformin classDataLoader<K,V> - Parameters:
builderConsumer- theDataLoaderFactory.Builderconsumer for changing theDataLoader- Returns:
- a newly built
DataLoaderinstance
-
getLastDispatchTime
Description copied from class:DataLoaderThis returns the last instant the data loader was dispatched. When the data loader is created this value is set to now.- Overrides:
getLastDispatchTimein classDataLoader<K,V> - Returns:
- the instant since the last dispatch
-
getTimeSinceDispatch
Description copied from class:DataLoaderThis returns theDurationsince the data loader was dispatched. When the data loader is created this is zero.- Overrides:
getTimeSinceDispatchin classDataLoader<K,V> - Returns:
- the time duration since the last dispatch
-
getIfPresent
Description copied from class:DataLoaderThis 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:
getIfPresentin classDataLoader<K,V> - Parameters:
key- the key to check- Returns:
- an Optional to the future of the value
-
getIfCompleted
Description copied from class:DataLoaderThis 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:
getIfCompletedin classDataLoader<K,V> - Parameters:
key- the key to check- Returns:
- an Optional to the future of the value
-
dispatch
Description copied from class:DataLoaderDispatches 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:
dispatchin classDataLoader<K,V> - Returns:
- the promise of the queued load requests
-
dispatchWithCounts
Description copied from class:DataLoaderDispatches 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:
dispatchWithCountsin classDataLoader<K,V> - Returns:
- the promise of the queued load requests and the number of keys dispatched.
-
dispatchAndJoin
Description copied from class:DataLoaderNormallyDataLoader.dispatch()is an asynchronous operation but this version will 'join' on the results if dispatch and wait for them to complete. If theCompletableFuturecallbacks 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:
dispatchAndJoinin classDataLoader<K,V> - Returns:
- the list of all results when the
DataLoader.dispatchDepth()reached 0
-
dispatchDepth
public int dispatchDepth()- Overrides:
dispatchDepthin classDataLoader<K,V> - Returns:
- the depth of the batched key loads that need to be dispatched
-
getCacheKey
Description copied from class:DataLoaderGets 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:
getCacheKeyin 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:DataLoaderGets the statistics associated with this data loader. These will have been gather via theStatisticsCollectorpassed in viaDataLoaderOptions.getStatisticsCollector()- Overrides:
getStatisticsin classDataLoader<K,V> - Returns:
- statistics for this data loader
-
getCacheMap
Description copied from class:DataLoaderGets the cacheMap associated with this data loader passed in viaDataLoaderOptions.cacheMap()- Overrides:
getCacheMapin classDataLoader<K,V> - Returns:
- the cacheMap of this data loader
-
getValueCache
Description copied from class:DataLoaderGets the valueCache associated with this data loader passed in viaDataLoaderOptions.valueCache()- Overrides:
getValueCachein classDataLoader<K,V> - Returns:
- the valueCache of this data loader
-
clear
Description copied from class:DataLoaderClears 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:
clearin classDataLoader<K,V> - Parameters:
key- the key to remove- Returns:
- the data loader for fluent coding
-
clear
Description copied from class:DataLoaderClears 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:
clearin 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:DataLoaderClears the entire cache map of the loader.- Overrides:
clearAllin classDataLoader<K,V> - Returns:
- the data loader for fluent coding
-
clearAll
Description copied from class:DataLoaderClears the entire cache map of the loader, and of the cached value store.- Overrides:
clearAllin 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:DataLoaderPrimes 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:
primein classDataLoader<K,V> - Parameters:
key- the keyvalue- the value- Returns:
- the data loader for fluent coding
-
prime
Description copied from class:DataLoaderPrimes the cache with the given key and error.- Overrides:
primein 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:DataLoaderPrimes 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:
primein classDataLoader<K,V> - Parameters:
key- the keyvalue- the value- Returns:
- the data loader for fluent coding
-