public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> implements SortedMap<Integer,R>
SortedMap
that keeps build records by their build numbers, in the descending order
(newer ones first.)
The main thing about this class is that it encapsulates the lazy loading logic.
That is, while this class looks and feels like a normal SortedMap
from outside,
it actually doesn't have every item in the map instantiated yet. As items in the map get
requested, this class retrieves them
on demand, one by one.
The lookup is done by using the build number as the key (hence the key type is Integer
).
This class makes the following assumption about the on-disk layout of the data:
Some of the SortedMap
operations are weakly implemented. For example,
AbstractMap.size()
may be inaccurate because we only count the number of directories that look like
build records, without checking if they are loadable. But these weaknesses aren't distinguishable
from concurrent modifications, where another thread deletes a build while one thread iterates them.
Some of the SortedMap
operations are inefficiently implemented, by
loading all the build records eagerly. We hope to replace
these implementations by more efficient lazy-loading ones as we go.
Object lock of this
is used to make sure mutation occurs sequentially.
That is, ensure that only one thread is actually calling retrieve(File)
and
updating AbstractLazyLoadRunMap.Index.byNumber
.
Modifier and Type | Class and Description |
---|---|
static class |
AbstractLazyLoadRunMap.Direction |
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
protected File |
dir
Base directory for data.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractLazyLoadRunMap(File dir) |
Modifier and Type | Method and Description |
---|---|
protected R |
_put(R value) |
boolean |
baseDirInitialized() |
Comparator<? super Integer> |
comparator() |
protected BuildReference<R> |
createReference(R r)
Allow subtype to capture a reference.
|
Set<Map.Entry<Integer,R>> |
entrySet() |
boolean |
equals(Object o) |
Integer |
firstKey() |
R |
get(int n) |
R |
get(Object key) |
R |
getById(String id) |
R |
getByNumber(int n) |
protected String |
getIdOf(R r)
Subtype to provide
Run.getId() so that this class doesn't have to depend on it. |
SortedMap<Integer,R> |
getLoadedBuilds()
Returns a read-only view of records that has already been loaded.
|
protected abstract int |
getNumberOf(R r)
Subtype to provide
Run.getNumber() so that this class doesn't have to depend on it. |
int |
hashCode() |
SortedMap<Integer,R> |
headMap(Integer toKey) |
protected void |
initBaseDir(File dir) |
boolean |
isEmpty() |
Set<Integer> |
keySet() |
Integer |
lastKey() |
int |
maxNumberOnDisk() |
R |
newestBuild() |
R |
oldestBuild() |
protected void |
proposeNewNumber(int number) |
void |
purgeCache()
Let go of all the loaded references.
|
R |
put(Integer key,
R r) |
R |
put(R value) |
void |
putAll(Map<? extends Integer,? extends R> rhs) |
boolean |
removeValue(R run) |
void |
reset(TreeMap<Integer,R> builds)
Replaces all the current loaded Rs with the given ones.
|
protected abstract R |
retrieve(File dir)
Parses
R instance from data in the specified directory. |
boolean |
runExists(int number)
Checks if the specified build exists.
|
R |
search(int n,
AbstractLazyLoadRunMap.Direction d)
Finds the build #M where M is nearby the given 'n'.
|
SortedMap<Integer,R> |
subMap(Integer fromKey,
Integer toKey) |
SortedMap<Integer,R> |
tailMap(Integer fromKey) |
void |
updateBaseDir(File dir)
Updates base directory location after directory changes.
|
Collection<R> |
values() |
clear, clone, containsKey, containsValue, remove, size, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, forEach, getOrDefault, merge, putIfAbsent, remove, remove, replace, replace, replaceAll, size
protected File dir
@Restricted(value=org.kohsuke.accmod.restrictions.NoExternalUse.class) protected AbstractLazyLoadRunMap(File dir)
public Collection<R> values()
@Restricted(value=org.kohsuke.accmod.restrictions.NoExternalUse.class) protected void initBaseDir(File dir)
@Restricted(value=org.kohsuke.accmod.restrictions.NoExternalUse.class) public final boolean baseDirInitialized()
AbstractLazyLoadRunMap(java.io.File)
was called with a non-null param, or RunMap.load(Job, RunMap.Constructor)
was calledpublic final void updateBaseDir(File dir)
dir
- Directory locationpublic void purgeCache()
public Comparator<? super Integer> comparator()
comparator
in interface SortedMap<Integer,R>
public boolean isEmpty()
public SortedMap<Integer,R> getLoadedBuilds()
public R newestBuild()
public R oldestBuild()
public R get(int n)
public boolean runExists(int number)
number
- the build number to probe.true
if there is an run for the corresponding number, note that this does not mean that
the corresponding record will load.@CheckForNull public R search(int n, AbstractLazyLoadRunMap.Direction d)
n
- the index to start the search fromd
- defines what we mean by "nearby" above.
If EXACT, find #N or return null.
If ASC, finds the closest #M that satisfies M ≥ N.
If DESC, finds the closest #M that satisfies M ≤ N.public R getByNumber(int n)
@Restricted(value=org.kohsuke.accmod.restrictions.NoExternalUse.class) public int maxNumberOnDisk()
protected final void proposeNewNumber(int number) throws IllegalStateException
IllegalStateException
protected abstract int getNumberOf(R r)
Run.getNumber()
so that this class doesn't have to depend on it.protected String getIdOf(R r)
Run.getId()
so that this class doesn't have to depend on it.protected BuildReference<R> createReference(R r)
protected abstract R retrieve(File dir) throws IOException
R
instance from data in the specified directory.IOException
- if the parsing failed. This is just like returning null
except the caller will catch the exception and report it.public boolean removeValue(R run)
public void reset(TreeMap<Integer,R> builds)
public int hashCode()
Copyright © 2004–2022. All rights reserved.