public abstract class KeyedDataStorage<T,P> extends Object
One typical pattern of data storage in Hudson is the one that Fingerprint
uses, where each data is keyed by an unique key (MD5 sum), and that key is used
to determine the file system location of the data.
On memory, each data is represented by one object (Fingerprint
), and
write access to the same data is coordinated by using synchronization.
With such storage, care has to be taken to ensure that there's only one data object in memory for any given key. That means load and create operation needs to be synchronized. This class implements this logic in a fairly efficient way, and thus intends to help plugins that want to use such data storage.
FingerprintMap
Modifier and Type | Field and Description |
---|---|
AtomicInteger |
cacheHit
Number of cache hits (of all the total queries.)
|
AtomicInteger |
loadFailure
Number of failures in loading data.
|
AtomicInteger |
totalQuery
Total number of queries into this storage.
|
AtomicInteger |
weakRefLost
Among cache misses, number of times when we had
SoftReference
but lost its value due to GC. |
Constructor and Description |
---|
KeyedDataStorage() |
Modifier and Type | Method and Description |
---|---|
protected abstract T |
create(String key,
P createParams)
Creates a new data object.
|
T |
get(String key)
Finds the data object that matches the given key if available, or null
if not found.
|
protected T |
get(String key,
boolean createIfNotExist,
P createParams)
Implementation of get/getOrCreate.
|
T |
getOrCreate(String key,
P createParams)
Atomically gets the existing data object if any, or if it doesn't exist
create it and return it. |
String |
getPerformanceStats()
Gets the short summary of performance statistics.
|
protected abstract T |
load(String key)
Attempts to load an existing data object from disk.
|
void |
resetPerformanceStats() |
public final AtomicInteger totalQuery
public final AtomicInteger cacheHit
public final AtomicInteger weakRefLost
SoftReference
but lost its value due to GC.
totalQuery-cacheHit-weakRefLost
means cache miss.public final AtomicInteger loadFailure
@NonNull public T getOrCreate(String key, P createParams) throws IOException
create
it and return it.createParams
- Additional parameters needed to create a new data object. Can be null.key
.IOException
- Loading error@CheckForNull public T get(String key) throws IOException
key
IOException
- Loading error@CheckForNull protected T get(@NonNull String key, boolean createIfNotExist, P createParams) throws IOException
key
IOException
- Loading error@CheckForNull protected abstract T load(String key) throws IOException
KeyedDataStorage
class serializes the requests so that
no two threads call the load(String)
method with the
same parameter concurrently. This ensures that there's only
up to one data object for any key.
IOException
- if load operation fails. This exception will be
propagated to the caller.@NonNull protected abstract T create(@NonNull String key, @NonNull P createParams) throws IOException
This method is called by getOrCreate(String,Object)
if the data that matches the specified key does not exist.
Because of concurrency, another thread might call get(String)
as soon as a new data object is created, so it's important that
this method returns a properly initialized "valid" object.
IOException
- if the method fails to create a new data object, it can throw
IOException
(or any other exception) and that will be
propagated to the caller.public void resetPerformanceStats()
public String getPerformanceStats()
Copyright © 2004–2021. All rights reserved.