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 keys
V - type parameter indicating the type of the data that is returned

@PublicApi @NullMarked public class DelegatingDataLoader<K,V> extends DataLoader<K,V>
This delegating 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 Details

  • Constructor Details

    • DelegatingDataLoader

      public DelegatingDataLoader(DataLoader<K,V> delegate)
  • Method Details

    • unwrap

      public static <K, V> DataLoader<K,V> unwrap(DataLoader<K,V> dataLoader)
      This can be called to unwrap a given DataLoader such that if it's a DelegatingDataLoader the underlying DataLoader is returned otherwise it's just passed in data loader
      Type Parameters:
      K - type parameter indicating the type of the data load keys
      V - 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 CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext)
      The DataLoader.load(Object) and DataLoader.loadMany(List) type methods all call back to the DataLoader.load(Object, Object) and hence we don't override them.
      Overrides:
      load in class DataLoader<K,V>
      Parameters:
      key - the key to load
      keyContext - a context object that is specific to this key
      Returns:
      the future of the value
    • transform

      public DataLoader<K,V> transform(Consumer<DataLoaderFactory.Builder<K,V>> builderConsumer)
      Description copied from class: DataLoader
      This allows you to change the current DataLoader and turn it into a new one
      Overrides:
      transform in class DataLoader<K,V>
      Parameters:
      builderConsumer - the DataLoaderFactory.Builder consumer for changing the DataLoader
      Returns:
      a newly built DataLoader instance
    • getLastDispatchTime

      public 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 class DataLoader<K,V>
      Returns:
      the instant since the last dispatch
    • getTimeSinceDispatch

      public Duration getTimeSinceDispatch()
      Description copied from class: DataLoader
      This returns the Duration since the data loader was dispatched. When the data loader is created this is zero.
      Overrides:
      getTimeSinceDispatch in class DataLoader<K,V>
      Returns:
      the time duration since the last dispatch
    • getIfPresent

      public Optional<CompletableFuture<V>> getIfPresent(K key)
      Description copied from class: DataLoader
      This will return an optional promise to a value previously loaded via a DataLoader.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 class DataLoader<K,V>
      Parameters:
      key - the key to check
      Returns:
      an Optional to the future of the value
    • getIfCompleted

      public Optional<CompletableFuture<V>> getIfCompleted(K key)
      Description copied from class: DataLoader
      This will return an optional promise to a value previously loaded via a DataLoader.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 class DataLoader<K,V>
      Parameters:
      key - the key to check
      Returns:
      an Optional to the future of the value
    • dispatch

      public CompletableFuture<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 class DataLoader<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 class DataLoader<K,V>
      Returns:
      the promise of the queued load requests and the number of keys dispatched.
    • dispatchAndJoin

      public List<V> dispatchAndJoin()
      Description copied from class: DataLoader
      Normally DataLoader.dispatch() is an asynchronous operation but this version will 'join' on the results if dispatch and wait for them to complete. If the CompletableFuture callbacks make more calls to this data loader then the DataLoader.dispatchDepth() will be > 0 and this method will loop around and wait for any other extra batch loads to occur.
      Overrides:
      dispatchAndJoin in class DataLoader<K,V>
      Returns:
      the list of all results when the DataLoader.dispatchDepth() reached 0
    • dispatchDepth

      public int dispatchDepth()
      Overrides:
      dispatchDepth in class DataLoader<K,V>
      Returns:
      the depth of the batched key loads that need to be dispatched
    • getCacheKey

      public 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 class DataLoader<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 the StatisticsCollector passed in via DataLoaderOptions.getStatisticsCollector()
      Overrides:
      getStatistics in class DataLoader<K,V>
      Returns:
      statistics for this data loader
    • getCacheMap

      public CacheMap<Object,V> getCacheMap()
      Description copied from class: DataLoader
      Gets the cacheMap associated with this data loader passed in via DataLoaderOptions.cacheMap()
      Overrides:
      getCacheMap in class DataLoader<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 via DataLoaderOptions.valueCache()
      Overrides:
      getValueCache in class DataLoader<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 class DataLoader<K,V>
      Parameters:
      key - the key to remove
      Returns:
      the data loader for fluent coding
    • clear

      public DataLoader<K,V> clear(K key, BiConsumer<Void,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 class DataLoader<K,V>
      Parameters:
      key - the key to remove
      handler - 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 class DataLoader<K,V>
      Returns:
      the data loader for fluent coding
    • clearAll

      public DataLoader<K,V> clearAll(BiConsumer<Void,Throwable> handler)
      Description copied from class: DataLoader
      Clears the entire cache map of the loader, and of the cached value store.
      Overrides:
      clearAll in class DataLoader<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. Use ValueCache.set(Object, Object) if you want to prime it with values before use
      Overrides:
      prime in class DataLoader<K,V>
      Parameters:
      key - the key
      value - the value
      Returns:
      the data loader for fluent coding
    • prime

      public DataLoader<K,V> prime(K key, Exception error)
      Description copied from class: DataLoader
      Primes the cache with the given key and error.
      Overrides:
      prime in class DataLoader<K,V>
      Parameters:
      key - the key
      error - the exception to prime instead of a value
      Returns:
      the data loader for fluent coding
    • prime

      public DataLoader<K,V> prime(K key, 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. Use ValueCache.set(Object, Object) if you want to prime it with values before use
      Overrides:
      prime in class DataLoader<K,V>
      Parameters:
      key - the key
      value - the value
      Returns:
      the data loader for fluent coding