提交 0500ec9c 编写于 作者: K Kohsuke Kawaguchi

overridden a number of Job methods in AbstractProject to avoid the slow path...

overridden a number of Job methods in AbstractProject to avoid the slow path (of loading everything)
上级 b35678d5
......@@ -79,9 +79,11 @@ import hudson.util.AlternativeUiTextProvider.Message;
import hudson.util.DescribableList;
import hudson.util.EditDistance;
import hudson.util.FormValidation;
import hudson.util.RunList;
import hudson.widgets.BuildHistoryWidget;
import hudson.widgets.HistoryWidget;
import jenkins.model.Jenkins;
import jenkins.model.lazy.AbstractLazyLoadRunMap.Direction;
import jenkins.scm.DefaultSCMCheckoutStrategyImpl;
import jenkins.scm.SCMCheckoutStrategy;
import jenkins.scm.SCMCheckoutStrategyDescriptor;
......@@ -961,6 +963,51 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
this.builds.remove(run);
}
/**
* {@inheritDoc}
*
* More efficient implementation.
*/
@Override
public R getBuild(String id) {
return builds.getById(id);
}
/**
* {@inheritDoc}
*
* More efficient implementation.
*/
@Override
public R getBuildByNumber(int n) {
return builds.getByNumber(n);
}
/**
* {@inheritDoc}
*
* More efficient implementation.
*/
@Override
public R getFirstBuild() {
return builds.oldestBuild();
}
@Override
public R getLastBuild() {
return builds.newestBuild();
}
@Override
public R getNearestBuild(int n) {
return builds.search(n, Direction.ASC);
}
@Override
public R getNearestOldBuild(int n) {
return builds.search(n, Direction.DESC);
}
/**
* Determines Class&lt;R>.
*/
......@@ -1632,7 +1679,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
@Override
protected HistoryWidget createHistoryWidget() {
return new BuildHistoryWidget<R>(this,getBuilds(),HISTORY_ADAPTER);
return new BuildHistoryWidget<R>(this,builds,HISTORY_ADAPTER);
}
public boolean isParameterized() {
......
......@@ -518,12 +518,21 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
*
* @return never null. The first entry is the latest build.
*/
@Exported
@Exported(name="allBuilds",visibility=-2)
@WithBridgeMethods(List.class)
public RunList<RunT> getBuilds() {
return RunList.fromRuns(_getRuns().values());
}
/**
* Gets the read-only view of the recent builds.
*
*/
@Exported(name="builds")
public RunList<RunT> getNewBuilds() {
return getBuilds().newBuilds();
}
/**
* Obtains all the {@link Run}s whose build numbers matches the given {@link RangeSet}.
*/
......@@ -600,7 +609,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
* This is useful when you'd like to fetch a build but the exact build might
* be already gone (deleted, rotated, etc.)
*/
public final RunT getNearestBuild(int n) {
public RunT getNearestBuild(int n) {
SortedMap<Integer, ? extends RunT> m = _getRuns().headMap(n - 1); // the map should
// include n, so n-1
if (m.isEmpty())
......@@ -614,7 +623,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
* This is useful when you'd like to fetch a build but the exact build might
* be already gone (deleted, rotated, etc.)
*/
public final RunT getNearestOldBuild(int n) {
public RunT getNearestOldBuild(int n) {
SortedMap<Integer, ? extends RunT> m = _getRuns().tailMap(n);
if (m.isEmpty())
return null;
......@@ -626,7 +635,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
StaplerResponse rsp) {
try {
// try to interpret the token as build number
return _getRuns().get(Integer.valueOf(token));
return getBuildByNumber(Integer.valueOf(token));
} catch (NumberFormatException e) {
// try to map that to widgets
for (Widget w : getWidgets()) {
......
......@@ -33,6 +33,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.logging.Level;
......@@ -50,7 +51,7 @@ import static jenkins.model.lazy.AbstractLazyLoadRunMap.Direction.*;
*
* @author Kohsuke Kawaguchi
*/
public final class RunMap<R extends Run<?,R>> extends AbstractLazyLoadRunMap<R> {
public final class RunMap<R extends Run<?,R>> extends AbstractLazyLoadRunMap<R> implements Iterable<R> {
/**
* Read-only view of this map.
*/
......@@ -83,6 +84,33 @@ public final class RunMap<R extends Run<?,R>> extends AbstractLazyLoadRunMap<R>
return removeValue(run);
}
/**
* Walks through builds, newer ones first.
*/
public Iterator<R> iterator() {
return new Iterator<R>() {
R last = null;
R next = newestBuild();
public boolean hasNext() {
return next!=null;
}
public R next() {
R last = next;
if (last!=null)
next = last.getPreviousBuild();
return last;
}
public void remove() {
if (last==null)
throw new UnsupportedOperationException();
removeValue(last);
}
};
}
@Override
public boolean removeValue(R run) {
if(run.nextBuild!=null)
......
......@@ -189,17 +189,25 @@ public abstract class AbstractLazyLoadRunMap<R> extends AbstractMap<Integer,R> i
}
public Integer firstKey() {
R r = search(Integer.MAX_VALUE, DESC);
R r = newestBuild();
if (r==null) throw new NoSuchElementException();
return getNumberOf(r);
}
public Integer lastKey() {
R r = search(Integer.MIN_VALUE, ASC);
R r = oldestBuild();
if (r==null) throw new NoSuchElementException();
return getNumberOf(r);
}
public R newestBuild() {
return search(Integer.MAX_VALUE, DESC);
}
public R oldestBuild() {
return search(Integer.MIN_VALUE, ASC);
}
@Override
public R get(Object key) {
if (key instanceof Integer) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册