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 primarily done by using the build number as the key (hence the key type is Integer
),
but this class also provides look up based on the build ID.
This class makes the following assumption about the on-disk layout of the data:
M>N <=> M.id > N.id
.
On certain platforms, there are symbolic links named after build numbers that link to the build ID. If these are available, they are used as a hint to speed up the lookup. Otherwise we rely on the assumption above and perform a binary search to locate the build. (notice that we'll have to do linear search if we don't have the consistent ordering assumption, which robs the whole point of doing lazy loading.)
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
and AbstractLazyLoadRunMap.Index.byId
.
Modifier and Type | Class and Description |
---|---|
static class |
AbstractLazyLoadRunMap.Direction |
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
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 abstract FilenameFilter |
createDirectoryFilter()
Lists the actual data directory
|
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 abstract 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()
If we have non-zero R in memory, we can return false right away.
|
Integer |
lastKey() |
protected R |
load(File dataDir,
jenkins.model.lazy.AbstractLazyLoadRunMap.Index editInPlace) |
protected R |
load(int n,
jenkins.model.lazy.AbstractLazyLoadRunMap.Index editInPlace)
Tries to load the record #N by using the shortcut.
|
protected R |
load(String id,
jenkins.model.lazy.AbstractLazyLoadRunMap.Index editInPlace) |
R |
newestBuild() |
R |
oldestBuild() |
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. |
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.
|
clear, clone, containsKey, containsValue, keySet, remove, size, toString, values
finalize, getClass, notify, notifyAll, wait, wait, wait
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, forEach, getOrDefault, merge, putIfAbsent, remove, remove, replace, replace, replaceAll, size
protected AbstractLazyLoadRunMap(File dir)
@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)
@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)
protected R load(int n, jenkins.model.lazy.AbstractLazyLoadRunMap.Index editInPlace)
protected R load(File dataDir, jenkins.model.lazy.AbstractLazyLoadRunMap.Index editInPlace)
editInPlace
- If non-null, update this data structure.
Otherwise do a copy-on-write of index
protected abstract int getNumberOf(R r)
Run.getNumber()
so that this class doesn't have to depend on it.protected abstract 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()
public boolean equals(Object o)
protected abstract FilenameFilter createDirectoryFilter()
Copyright © 2015. All rights reserved.