Class AbstractMetadataCache<IdentifierType,MetadataType>

Type Parameters:
IdentifierType - the metadata identifier type
MetadataType - the metadata type
All Implemented Interfaces:
MetadataCache<MetadataType>, Component, DestructableComponent, IdentifiableComponent, IdentifiedComponent, InitializableComponent
Direct Known Subclasses:
BatchMetadataCache, DynamicMetadataCache, FetchThroughMetadataCache

public abstract class AbstractMetadataCache<IdentifierType,MetadataType> extends AbstractIdentifiableInitializableComponent implements MetadataCache<MetadataType>
A base MetadataCache implementation. Supports the following:
  • A configurable backing store to store metadata.
  • Individual entity metadata filtering during cache save.
  • Configuration of type specific functions via strategies.
  • Field Details

  • Constructor Details

    • AbstractMetadataCache

      AbstractMetadataCache(@Nonnull BackingStore<IdentifierType,MetadataType> store)
      Package private constructor. For safe-construction through the factory only.
      Parameters:
      store - the metadata backing store.
    • AbstractMetadataCache

      AbstractMetadataCache(@Nonnull BackingStore<IdentifierType,MetadataType> store, @Nullable ScheduledExecutorService executor)
      Package private constructor. For safe-construction through the factory only.

      Accepts an executor. Mostly used for testing.

      Parameters:
      store - the backing store.
      executor - the scheduled executor
  • Method Details

    • getLogPrefix

      @Nonnull @NotEmpty protected String getLogPrefix()
      Return a prefix for logging messages for this component.
      Returns:
      a string for insertion at the beginning of any log messages
    • doInitialize

      protected void doInitialize() throws ComponentInitializationException
      Overrides:
      doInitialize in class AbstractIdentifiedInitializableComponent
      Throws:
      ComponentInitializationException
    • doDestroy

      protected void doDestroy()
      Overrides:
      doDestroy in class AbstractInitializableComponent
    • getExecutorService

      @NonnullAfterInit protected ScheduledExecutorService getExecutorService()
      Get the background executor service.
      Returns:
      the executor service.
    • setMetadataValidPredicate

      public void setMetadataValidPredicate(@Nonnull Predicate<MetadataType> predicate)
      Set the predicate which determines if a piece of metadata is valid or not.
      Parameters:
      predicate - the predicate.
    • getMetadataValidPredicate

      @NonnullAfterInit protected Predicate<MetadataType> getMetadataValidPredicate()
      Get the predicate which determines if a piece of metadata is valid or not.
      Returns:
      the predicate.
    • setCriteriaToIdentifierStrategy

      public void setCriteriaToIdentifierStrategy(@Nonnull Function<CriteriaSet,IdentifierType> strategy)
      Set the CriteriaSet to IdentifierType strategy.
      Parameters:
      strategy - the strategy.
    • getCriteriaToIdentifierStrategy

      @NonnullAfterInit protected Function<CriteriaSet,IdentifierType> getCriteriaToIdentifierStrategy()
      Get the identifier from criteria set extraction strategy.
      Returns:
      the identifier extraction strategy.
    • setIdentifierExtractionStrategy

      public void setIdentifierExtractionStrategy(@Nonnull Function<MetadataType,IdentifierType> strategy)
      Set the MetadataType to IdentifierType extraction time strategy.
      Parameters:
      strategy - the strategy.
    • getIdentifierExtractionStrategy

      @NonnullAfterInit protected Function<MetadataType,IdentifierType> getIdentifierExtractionStrategy()
      Get the identifier extraction strategy.
      Returns:
      the identifier extraction strategy.
    • setMetadataFilterStrategy

      public void setMetadataFilterStrategy(@Nonnull BiFunction<MetadataType,MetadataFilterContext,MetadataType> strategy)
      Set the metadata filtering strategy.
      Parameters:
      strategy - the metadata filtering strategy.
    • getMetadataFilterStrategy

      Get the metadata filtering strategy.
      Returns:
      the filtering strategy.
    • setMetadataBeforeRemovalHook

      public void setMetadataBeforeRemovalHook(@Nullable BiConsumer<List<MetadataType>,IdentifierType> hook)
      Set a hook to run before a metadata cache entry is removed from the cache.

      The hook is required to gracefully handle null metadata lists.

      Parameters:
      hook - the hook to run.
    • setRefreshDelayFactor

      public void setRefreshDelayFactor(@Nonnull Float factor)
      Sets the delay factor used to compute the next refresh time. The delay must be between 0.0 and 1.0, exclusive.

      Defaults to: 0.75.

      Parameters:
      factor - delay factor used to compute the next refresh time
    • getRefreshDelayFactor

      @NonnullAfterInit protected Float getRefreshDelayFactor()
      Get the refresh delay factor.
      Returns:
      the refresh delay factor.
    • lookupIdentifier

      @Nonnull @NonnullElements protected List<MetadataType> lookupIdentifier(@Nonnull @NotEmpty IdentifierType identifier) throws MetadataCacheException
      Get list of descriptors matching the identifier.
      Parameters:
      identifier - the identifier to lookup
      Returns:
      a list of metadata
      Throws:
      MetadataCacheException - if an error occurs.
    • lookupIndexedIdentifier

      @Nonnull @NonnullElements protected List<MetadataType> lookupIndexedIdentifier(@Nonnull IdentifierType identifier)
      Lookup the specified identifier from the index. The returned list will be a copy of what is stored in the backing index and is safe to be manipulated by callers.
      Parameters:
      identifier - the identifier to lookup
      Returns:
      list copy of indexed metadata, may be empty, will never be null
    • getBackingStore

      @Nonnull protected BackingStore<IdentifierType,MetadataType> getBackingStore()
      Get the backing store.
      Returns:
      the backing store. Can be null.
    • freshLoad

      protected void freshLoad(@Nonnull List<MetadataType> metadataToStore)
      Clear the backing store and load each metadata entry one at a time.
      Parameters:
      metadataToStore - the metadata to load into the cache.
    • writeToBackingStore

      protected void writeToBackingStore(@Nonnull MetadataType metadata)
      Write the given metadata to the backing store index.

      Should happen within an appropriate lock to ensure thread-safety.

      Parameters:
      metadata - the metadata to add to the backing store.
    • newFilterContext

      @Nonnull protected MetadataFilterContext newFilterContext()
      Get a new instance of MetadataFilterContext to be used when filtering metadata.

      This default implementation just returns an empty context with as metadatasource for passing to the filter strategy. The strategy can then add to the context as appropriate.

      Returns:
      the new filter context instance
    • shouldAttemptRefresh

      protected boolean shouldAttemptRefresh(@Nonnull MetadataManagementData<IdentifierType> mgmtData)
      Determine if the metadata should be refreshed based on stored refresh trigger time.
      Parameters:
      mgmtData - the entity'd management data
      Returns:
      true if should attempt refresh, false otherwise
    • hasExpired

      protected boolean hasExpired(@Nonnull MetadataManagementData<IdentifierType> mgmtData)
      Determine if the metadata has expired based on the expiration time set in the management metadata.
      Parameters:
      mgmtData - management data
      Returns:
      true iff the metadata has expired
    • invalidate

      protected void invalidate(@Nonnull IdentifierType identifier)
      Remove/discard from the backing store all metadata for the entity with the given identifier.
      Parameters:
      identifier - the ID of the metadata to remove
    • invalidateAll

      protected void invalidateAll()
      Remove/discard all metadata for the backing store.

      Ensure thread-safety is observed in the calling method.

    • errorHandlingWrapper

      @Nonnull protected Runnable errorHandlingWrapper(@Nonnull Runnable action)
      Create a wrapper for runnables that catches any throwable and logs it. Useful to prevent thread death from an uncaught exception.
      Parameters:
      action - the runnable to wrap
      Returns:
      the wrapped runnable.