Class 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 + "|");
        }
    };
    
    • 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 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
      • 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 class DataLoader<K,​V>
        Returns:
        the instant since the last dispatch
      • getTimeSinceDispatch

        public java.time.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 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 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 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 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 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 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 java.util.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 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 class DataLoader<K,​V>
        Parameters:
        key - the input key
        Returns:
        the cache key after the input is transformed with the cache key function
      • 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,
                                           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 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​(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 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,
                                           java.lang.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,
                                           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. 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