Class AbstractMetadataCache<IdentifierType,​MetadataType>

    • Constructor Detail

      • AbstractMetadataCache

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

        AbstractMetadataCache​(@Nullable
                              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 Detail

      • 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
      • 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

        @Nonnull
        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.
      • setIdentifierExtractionStrategy

        public void setIdentifierExtractionStrategy​(@Nonnull
                                                    Function<MetadataType,​IdentifierType> strategy)
        Set the MetadataType to IdentifierType extraction time strategy.
        Parameters:
        strategy - the 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.
      • lookupIndexedIdentifier

        @Nonnull
        @NonnullElements
        protected List<MetadataType> lookupIndexedIdentifier​(@Nonnull
                                                             IdentifierType identifier)
        Lookup the specified entityID 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
      • 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 managment 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.

        TODO check lock.
      • 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.