Class DelegatingDataLoader<K,V>
- java.lang.Object
-
- org.dataloader.DataLoader<K,V>
-
- org.dataloader.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
@PublicApi @NullMarked public class DelegatingDataLoader<K,V> extends DataLoader<K,V>
This delegatingDataLoader
makes it easier to create wrappers ofDataLoader
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 theload(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 Modifier and Type Field Description protected DataLoader<K,V>
delegate
-
Constructor Summary
Constructors Constructor Description DelegatingDataLoader(DataLoader<K,V> delegate)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DataLoader<K,V>
clear(K key)
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, java.util.function.BiConsumer<java.lang.Void,java.lang.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(java.util.function.BiConsumer<java.lang.Void,java.lang.Throwable> handler)
Clears the entire cache map of the loader, and of the cached value store.java.util.concurrent.CompletableFuture<java.util.List<V>>
dispatch()
Dispatches the queued load requests to the batch execution function and returns a promise of the result.java.util.List<V>
dispatchAndJoin()
NormallyDataLoader.dispatch()
is an asynchronous operation but this version will 'join' on the results if dispatch and wait for them to complete.int
dispatchDepth()
DispatchResult<V>
dispatchWithCounts()
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.java.lang.Object
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.CacheMap<java.lang.Object,V>
getCacheMap()
Gets the cacheMap associated with this data loader passed in viaDataLoaderOptions.cacheMap()
DataLoader<K,V>
getDelegate()
java.util.Optional<java.util.concurrent.CompletableFuture<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.java.util.Optional<java.util.concurrent.CompletableFuture<V>>
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.java.time.Instant
getLastDispatchTime()
This returns the last instant the data loader was dispatched.Statistics
getStatistics()
Gets the statistics associated with this data loader.java.time.Duration
getTimeSinceDispatch()
This returns theDuration
since the data loader was dispatched.ValueCache<K,V>
getValueCache()
Gets the valueCache associated with this data loader passed in viaDataLoaderOptions.valueCache()
java.util.concurrent.CompletableFuture<V>
load(@NonNull K key, @Nullable java.lang.Object keyContext)
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>
prime(K key, java.lang.Exception error)
Primes the cache with the given key and error.DataLoader<K,V>
prime(K key, java.util.concurrent.CompletableFuture<V> value)
Primes the cache with the given key and value.DataLoader<K,V>
prime(K key, V value)
Primes the cache with the given key and value.DataLoader<K,V>
transform(java.util.function.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 loader-
Methods inherited from class org.dataloader.DataLoader
getBatchLoadFunction, getOptions, load, loadMany, loadMany, loadMany, newDataLoader, newDataLoader, newDataLoader, newDataLoader, newDataLoaderWithTry, newDataLoaderWithTry, newDataLoaderWithTry, newDataLoaderWithTry, newMappedDataLoader, newMappedDataLoader, newMappedDataLoader, newMappedDataLoader, newMappedDataLoaderWithTry, newMappedDataLoaderWithTry, newMappedDataLoaderWithTry, newMappedDataLoaderWithTry
-
-
-
-
Field Detail
-
delegate
protected final DataLoader<K,V> delegate
-
-
Constructor Detail
-
DelegatingDataLoader
public DelegatingDataLoader(DataLoader<K,V> delegate)
-
-
Method Detail
-
unwrap
public static <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 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
public DataLoader<K,V> getDelegate()
-
load
public java.util.concurrent.CompletableFuture<V> load(@NonNull K key, @Nullable java.lang.Object keyContext)
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
public DataLoader<K,V> transform(java.util.function.Consumer<DataLoaderFactory.Builder<K,V>> builderConsumer)
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
public java.time.Instant 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
public java.time.Duration 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
public java.util.Optional<java.util.concurrent.CompletableFuture<V>> getIfPresent(K key)
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
public java.util.Optional<java.util.concurrent.CompletableFuture<V>> getIfCompleted(K key)
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
public java.util.concurrent.CompletableFuture<java.util.List<V>> 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
public DispatchResult<V> 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
public java.util.List<V> 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
public java.lang.Object getCacheKey(K key)
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
public Statistics 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
public CacheMap<java.lang.Object,V> 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
public ValueCache<K,V> 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
public DataLoader<K,V> clear(K key)
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
public DataLoader<K,V> clear(K key, java.util.function.BiConsumer<java.lang.Void,java.lang.Throwable> handler)
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
public DataLoader<K,V> 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
public DataLoader<K,V> clearAll(java.util.function.BiConsumer<java.lang.Void,java.lang.Throwable> handler)
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
public DataLoader<K,V> prime(K key, V value)
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
public DataLoader<K,V> prime(K key, java.lang.Exception error)
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
public DataLoader<K,V> prime(K key, java.util.concurrent.CompletableFuture<V> value)
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
-
-