Class AbstractMapBackedStorageService

  • All Implemented Interfaces:
    net.shibboleth.utilities.java.support.component.Component, net.shibboleth.utilities.java.support.component.DestructableComponent, net.shibboleth.utilities.java.support.component.IdentifiableComponent, net.shibboleth.utilities.java.support.component.IdentifiedComponent, net.shibboleth.utilities.java.support.component.InitializableComponent, StorageCapabilities, StorageService

    public abstract class AbstractMapBackedStorageService
    extends AbstractStorageService
    Partial implementation of StorageService that stores data in-memory with no persistence using a simple map.

    Abstract methods supply the map of data to manipulate and the lock to use, which allows optimizations in cases where locking isn't required or data isn't shared.

    • Field Detail

      • log

        @Nonnull
        private final org.slf4j.Logger log
        Class logger.
    • Constructor Detail

      • AbstractMapBackedStorageService

        public AbstractMapBackedStorageService()
        Constructor.
    • Method Detail

      • create

        public boolean create​(@Nonnull @NotEmpty
                              String context,
                              @Nonnull @NotEmpty
                              String key,
                              @Nonnull @NotEmpty
                              String value,
                              @Nullable
                              Long expiration)
                       throws IOException
        Creates a new record in the store with an expiration.
        Parameters:
        context - a storage context label
        key - a key unique to context
        value - value to store
        expiration - expiration for record, or null
        Returns:
        true iff record was inserted, false iff a duplicate was found
        Throws:
        IOException - if fatal errors occur in the insertion process
      • read

        @Nullable
        public <T> StorageRecord<T> read​(@Nonnull @NotEmpty
                                         String context,
                                         @Nonnull @NotEmpty
                                         String key)
                                  throws IOException
        Returns an existing record from the store, if one exists.
        Type Parameters:
        T - type of record
        Parameters:
        context - a storage context label
        key - a key unique to context
        Returns:
        the record read back, if present, or null
        Throws:
        IOException - if errors occur in the read process
      • read

        @Nonnull
        public <T> net.shibboleth.utilities.java.support.collection.Pair<Long,​StorageRecord<T>> read​(@Nonnull @NotEmpty
                                                                                                           String context,
                                                                                                           @Nonnull @NotEmpty
                                                                                                           String key,
                                                                                                           long version)
                                                                                                    throws IOException
        Returns an existing record from the store, along with its version.

        The first member of the pair returned will contain the version of the record in the store, or will be null if no record exists. The second member will contain the record read back. If null, the record either didn't exist (if the first member was also null) or the record was the same version as that supplied by the caller.

        Type Parameters:
        T - type of record
        Parameters:
        context - a storage context label
        key - a key unique to context
        version - only return record if newer than supplied version
        Returns:
        a pair consisting of the version of the record read back, if any, and the record itself
        Throws:
        IOException - if errors occur in the read process
      • update

        public boolean update​(@Nonnull @NotEmpty
                              String context,
                              @Nonnull @NotEmpty
                              String key,
                              @Nonnull @NotEmpty
                              String value,
                              @Nullable
                              Long expiration)
                       throws IOException
        Updates an existing record in the store.
        Parameters:
        context - a storage context label
        key - a key unique to context
        value - updated value
        expiration - expiration for record, or null
        Returns:
        true if the update succeeded, false if the record does not exist
        Throws:
        IOException - if errors occur in the update process
      • updateWithVersion

        @Nullable
        public Long updateWithVersion​(long version,
                                      @Nonnull @NotEmpty
                                      String context,
                                      @Nonnull @NotEmpty
                                      String key,
                                      @Nonnull @NotEmpty
                                      String value,
                                      @Nullable
                                      Long expiration)
                               throws IOException,
                                      VersionMismatchException
        Updates an existing record in the store, if a version matches.
        Parameters:
        version - only update if the current version matches this value
        context - a storage context label
        key - a key unique to context
        value - updated value
        expiration - expiration for record, or null
        Returns:
        the version of the record after update, null if no record exists
        Throws:
        IOException - if errors occur in the update process
        VersionMismatchException - if the record has already been updated to a newer version
      • updateExpiration

        public boolean updateExpiration​(@Nonnull @NotEmpty
                                        String context,
                                        @Nonnull @NotEmpty
                                        String key,
                                        @Nullable
                                        Long expiration)
                                 throws IOException
        Updates expiration of an existing record in the store.
        Parameters:
        context - a storage context label
        key - a key unique to context
        expiration - expiration for record, or null
        Returns:
        true if the update succeeded, false if the record does not exist
        Throws:
        IOException - if errors occur in the update process
      • deleteWithVersion

        public boolean deleteWithVersion​(long version,
                                         String context,
                                         String key)
                                  throws IOException,
                                         VersionMismatchException
        Deletes an existing record from the store if it currently has a specified version.
        Parameters:
        version - record version to delete
        context - a storage context label
        key - a key unique to context
        Returns:
        true iff the record existed and was deleted
        Throws:
        IOException - if errors occur in the deletion process
        VersionMismatchException - if the record has already been updated to a newer version
      • delete

        public boolean delete​(@Nonnull @NotEmpty
                              String context,
                              @Nonnull @NotEmpty
                              String key)
                       throws IOException
        Deletes an existing record from the store.
        Parameters:
        context - a storage context label
        key - a key unique to context
        Returns:
        true iff the record existed and was deleted
        Throws:
        IOException - if errors occur in the deletion process
      • updateContextExpiration

        public void updateContextExpiration​(@Nonnull @NotEmpty
                                            String context,
                                            @Nullable
                                            Long expiration)
                                     throws IOException
        Updates the expiration time of all records in the context.
        Parameters:
        context - a storage context label
        expiration - a new expiration timestamp, or null
        Throws:
        IOException - if errors occur in the cleanup process
      • deleteContext

        public void deleteContext​(@Nonnull @NotEmpty
                                  String context)
                           throws IOException
        Forcibly removes all records in a given context along with any associated resources devoted to maintaining the context.
        Parameters:
        context - a storage context label
        Throws:
        IOException - if errors occur in the cleanup process
      • reap

        public void reap​(@Nonnull @NotEmpty
                         String context)
                  throws IOException
        Manually trigger a cleanup of expired records. The method MAY return without guaranteeing that cleanup has already occurred.
        Parameters:
        context - a storage context label
        Throws:
        IOException - if errors occur in the cleanup process
      • getLock

        @Nonnull
        protected abstract ReadWriteLock getLock()
        Get the shared lock to synchronize access.
        Returns:
        shared lock
      • getContextMap

        @Nonnull
        @NonnullElements
        @Live
        protected abstract Map<String,​Map<String,​MutableStorageRecord<?>>> getContextMap()
                                                                                              throws IOException
        Get the map of contexts to manipulate during operations.

        This method is guaranteed to be called under cover the lock returned by {getLock().

        Returns:
        map of contexts to manipulate
        Throws:
        IOException - to signal errors
      • setDirty

        protected void setDirty()
                         throws IOException
        A callback to indicate that data has been modified.

        This method is guaranteed to be called under cover the lock returned by {getLock().

        Throws:
        IOException - to signal an error
      • readImpl

        @Nonnull
        protected <T> net.shibboleth.utilities.java.support.collection.Pair<Long,​StorageRecord<T>> readImpl​(@Nonnull @NotEmpty
                                                                                                                  String context,
                                                                                                                  @Nonnull @NotEmpty
                                                                                                                  String key,
                                                                                                                  @Nullable
                                                                                                                  Long version)
                                                                                                           throws IOException
        Internal method to implement read functions.
        Type Parameters:
        T - type of object
        Parameters:
        context - a storage context label
        key - a key unique to context
        version - only return record if newer than optionally supplied version
        Returns:
        a pair consisting of the version of the record read back, if any, and the record itself
        Throws:
        IOException - if errors occur in the read process
      • updateImpl

        @Nullable
        protected Long updateImpl​(@Nullable
                                  Long version,
                                  @Nonnull @NotEmpty
                                  String context,
                                  @Nonnull @NotEmpty
                                  String key,
                                  @Nullable
                                  String value,
                                  @Nullable
                                  Long expiration)
                           throws IOException,
                                  VersionMismatchException
        Internal method to implement update functions.
        Parameters:
        version - only update if the current version matches this value
        context - a storage context label
        key - a key unique to context
        value - updated value
        expiration - expiration for record. or null
        Returns:
        the version of the record after update, null if no record exists
        Throws:
        IOException - if errors occur in the update process
        VersionMismatchException - if the record has already been updated to a newer version
      • deleteImpl

        protected boolean deleteImpl​(@Nullable @Positive
                                     Long version,
                                     @Nonnull @NotEmpty
                                     String context,
                                     @Nonnull @NotEmpty
                                     String key)
                              throws IOException,
                                     VersionMismatchException
        Internal method to implement delete functions.
        Parameters:
        version - only update if the current version matches this value
        context - a storage context label
        key - a key unique to context
        Returns:
        true iff the record existed and was deleted
        Throws:
        IOException - if errors occur in the update process
        VersionMismatchException - if the record has already been updated to a newer version
      • reapWithLock

        protected boolean reapWithLock​(@Nonnull @NonnullElements
                                       Map<String,​MutableStorageRecord<?>> dataMap,
                                       long expiration)
        Locates and removes expired records from the input map.

        This method MUST be called while holding a write lock, if locking is required.

        Parameters:
        dataMap - the map to reap
        expiration - time at which to consider records expired
        Returns:
        true iff anything was purged