Package org.opensaml.storage
Class AbstractMapBackedStorageService
- All Implemented Interfaces:
Component
,DestructableComponent
,IdentifiableComponent
,IdentifiedComponent
,InitializableComponent
,StorageCapabilities
,StorageService
- Direct Known Subclasses:
ClientStorageService
,MemoryStorageService
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Creates a new record in the store with an expiration.boolean
Deletes an existing record from the store.void
deleteContext
(String context) Forcibly removes all records in a given context along with any associated resources devoted to maintaining the context.protected boolean
deleteImpl
(Long version, String context, String key) Internal method to implement delete functions.boolean
deleteWithVersion
(long version, String context, String key) Deletes an existing record from the store if it currently has a specified version.protected abstract Map<String,
Map<String, MutableStorageRecord<?>>> Get the map of contexts to manipulate during operations.protected abstract ReadWriteLock
getLock()
Get the shared lock to synchronize access.<T> StorageRecord<T>
Returns an existing record from the store, if one exists.<T> Pair<Long,
StorageRecord<T>> Returns an existing record from the store, along with its version.protected <T> Pair<Long,
StorageRecord<T>> Internal method to implement read functions.void
Manually trigger a cleanup of expired records.protected boolean
reapWithLock
(Map<String, MutableStorageRecord<?>> dataMap, long expiration) Locates and removes expired records from the input map.protected void
setDirty()
A callback to indicate that data has been modified.boolean
Updates an existing record in the store.void
updateContextExpiration
(String context, Long expiration) Updates the expiration time of all records in the context.boolean
updateExpiration
(String context, String key, Long expiration) Updates expiration of an existing record in the store.protected Long
Internal method to implement update functions.updateWithVersion
(long version, String context, String key, String value, Long expiration) Updates an existing record in the store, if a version matches.Methods inherited from class org.opensaml.storage.AbstractStorageService
create, create, delete, deleteWithVersion, doDestroy, doInitialize, getCapabilities, getCleanupInterval, getCleanupTask, getCleanupTaskTimer, getContextSize, getKeySize, getValueSize, read, setCleanupInterval, setCleanupTaskTimer, setContextSize, setKeySize, setValueSize, update, update, updateExpiration, updateWithVersion, updateWithVersion
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent
setId
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractIdentifiedInitializableComponent
getId
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractInitializableComponent
destroy, initialize, isDestroyed, isInitialized
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.shibboleth.utilities.java.support.component.IdentifiedComponent
getId
-
Field Details
-
log
@Nonnull private final org.slf4j.Logger logClass logger.
-
-
Constructor Details
-
AbstractMapBackedStorageService
public AbstractMapBackedStorageService()Constructor.
-
-
Method Details
-
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 labelkey
- a key unique to contextvalue
- value to storeexpiration
- 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 labelkey
- 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> 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 labelkey
- a key unique to contextversion
- 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 labelkey
- a key unique to contextvalue
- updated valueexpiration
- 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 valuecontext
- a storage context labelkey
- a key unique to contextvalue
- updated valueexpiration
- 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 processVersionMismatchException
- 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 labelkey
- a key unique to contextexpiration
- 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 deletecontext
- a storage context labelkey
- a key unique to context- Returns:
- true iff the record existed and was deleted
- Throws:
IOException
- if errors occur in the deletion processVersionMismatchException
- 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 labelkey
- 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 labelexpiration
- a new expiration timestamp, or null- Throws:
IOException
- if errors occur in the cleanup process
-
deleteContext
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
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
Get the shared lock to synchronize access.- Returns:
- shared lock
-
getContextMap
@Nonnull @NonnullElements @Live protected abstract Map<String,Map<String, getContextMap() throws IOExceptionMutableStorageRecord<?>>> 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
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> 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 labelkey
- a key unique to contextversion
- 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 valuecontext
- a storage context labelkey
- a key unique to contextvalue
- updated valueexpiration
- 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 processVersionMismatchException
- 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 valuecontext
- a storage context labelkey
- a key unique to context- Returns:
- true iff the record existed and was deleted
- Throws:
IOException
- if errors occur in the update processVersionMismatchException
- 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 reapexpiration
- time at which to consider records expired- Returns:
- true iff anything was purged
-