();
+ String rootURL = jenkins.getRootUrl();
+ if (rootURL==null) return null;
- props.put("url", rootURL);
- try {
- props.put("version",String.valueOf(Jenkins.getVersion()));
- } catch (IllegalArgumentException e) {
- // failed to parse the version number
+ props.put("url", rootURL);
+ try {
+ props.put("version",String.valueOf(Jenkins.getVersion()));
+ } catch (IllegalArgumentException e) {
+ // failed to parse the version number
+ }
+
+ TcpSlaveAgentListener tal = jenkins.getTcpSlaveAgentListener();
+ if (tal!=null)
+ props.put("slave-port",String.valueOf(tal.getPort()));
+
+ props.put("server-id", Util.getDigestOf(jenkins.getSecretKey()));
+
+ URL jenkins_url = new URL(rootURL);
+ int jenkins_port = jenkins_url.getPort();
+ if (jenkins_port == -1) {
+ jenkins_port = 80;
+ }
+ if (jenkins_url.getPath().length() > 0) {
+ props.put("path", jenkins_url.getPath());
+ }
+
+ jmdns.registerService(ServiceInfo.create("_hudson._tcp.local.","jenkins",
+ jenkins_port,0,0,props)); // for backward compatibility
+ jmdns.registerService(ServiceInfo.create("_jenkins._tcp.local.","jenkins",
+ jenkins_port,0,0,props));
+
+ // Make Jenkins appear in Safari's Bonjour bookmarks
+ jmdns.registerService(ServiceInfo.create("_http._tcp.local.","Jenkins",
+ jenkins_port,0,0,props));
+ } catch (IOException e) {
+ LOGGER.log(Level.WARNING,"Failed to advertise the service to DNS multi-cast",e);
+ }
+ return null;
}
-
- TcpSlaveAgentListener tal = hudson.getTcpSlaveAgentListener();
- if (tal!=null)
- props.put("slave-port",String.valueOf(tal.getPort()));
-
- props.put("server-id", Util.getDigestOf(hudson.getSecretKey()));
-
- URL jenkins_url = new URL(rootURL);
- int jenkins_port = jenkins_url.getPort();
- if (jenkins_port == -1) {
- jenkins_port = 80;
- }
- if (jenkins_url.getPath().length() > 0) {
- props.put("path", jenkins_url.getPath());
- }
-
- jmdns.registerService(ServiceInfo.create("_hudson._tcp.local.","hudson",
- jenkins_port,0,0,props)); // for backward compatibility
- jmdns.registerService(ServiceInfo.create("_jenkins._tcp.local.","jenkins",
- jenkins_port,0,0,props));
-
- // Make Jenkins appear in Safari's Bonjour bookmarks
- jmdns.registerService(ServiceInfo.create("_http._tcp.local.","Jenkins",
- jenkins_port,0,0,props));
- } catch (IOException e) {
- LOGGER.log(Level.WARNING,"Failed to advertise the service to DNS multi-cast",e);
- }
+ });
}
public void close() {
diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java
index 2d1bc35d4e..6b7d7e2e1e 100644
--- a/core/src/main/java/hudson/model/AbstractBuild.java
+++ b/core/src/main/java/hudson/model/AbstractBuild.java
@@ -62,6 +62,8 @@ import hudson.util.Iterators;
import hudson.util.LogTaskListener;
import hudson.util.VariableResolver;
import jenkins.model.Jenkins;
+import jenkins.model.lazy.AbstractLazyLoadRunMap.Direction;
+import jenkins.model.lazy.BuildReference;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
@@ -154,6 +156,20 @@ public abstract class AbstractBuild,R extends Abs
*/
protected transient List buildEnvironments;
+ /**
+ * Pointers to form bi-directional link between adjacent {@link AbstractBuild}s.
+ *
+ *
+ * Unlike {@link Run}, {@link AbstractBuild}s do lazy-loading, so we don't use
+ * {@link Run#previousBuild} and {@link Run#nextBuild}, and instead use these
+ * fields and point to {@link #selfReference} of adjacent builds.
+ */
+ private volatile transient BuildReference previousBuild, nextBuild;
+
+ /*package*/ final transient BuildReference selfReference = new BuildReference(getId(),_this());
+
+
+
protected AbstractBuild(P job) throws IOException {
super(job);
}
@@ -170,6 +186,80 @@ public abstract class AbstractBuild,R extends Abs
return getParent();
}
+ @Override
+ void dropLinks() {
+ super.dropLinks();
+
+ if(nextBuild!=null) {
+ AbstractBuild nb = nextBuild.get();
+ if (nb!=null) nb.previousBuild = previousBuild;
+ }
+ if(previousBuild!=null) {
+ AbstractBuild pb = previousBuild.get();
+ if (pb!=null) pb.nextBuild = nextBuild;
+ }
+ }
+
+ @Override
+ public R getPreviousBuild() {
+ while (true) {
+ BuildReference r = previousBuild; // capture the value once
+
+ if (r==null) {
+ // having two neighbors pointing to each other is important to make RunMap.removeValue work
+ R pb = getParent().builds.search(number-1, Direction.DESC);
+ if (pb!=null) {
+ ((AbstractBuild)pb).nextBuild = selfReference; // establish bi-di link
+ this.previousBuild = pb.selfReference;
+ return pb;
+ } else {
+ // this indicates that we know there's no previous build
+ // (as opposed to we don't know if/what our previous build is.
+ this.previousBuild = selfReference;
+ return null;
+ }
+ }
+ if (r==selfReference)
+ return null;
+
+ R referent = r.get();
+ if (referent!=null) return referent;
+
+ // the reference points to a GC-ed object, drop the reference and do it again
+ this.previousBuild = null;
+ }
+ }
+
+ @Override
+ public R getNextBuild() {
+ while (true) {
+ BuildReference r = nextBuild; // capture the value once
+
+ if (r==null) {
+ // having two neighbors pointing to each other is important to make RunMap.removeValue work
+ R nb = getParent().builds.search(number+1, Direction.ASC);
+ if (nb!=null) {
+ ((AbstractBuild)nb).previousBuild = selfReference; // establish bi-di link
+ this.nextBuild = nb.selfReference;
+ return nb;
+ } else {
+ // this indicates that we know there's no next build
+ // (as opposed to we don't know if/what our next build is.
+ this.nextBuild = selfReference;
+ return null;
+ }
+ }
+ if (r==selfReference)
+ return null;
+
+ R referent = r.get();
+ if (referent!=null) return referent;
+
+ // the reference points to a GC-ed object, drop the reference and do it again
+ this.nextBuild = null;
+ }
+ }
+
/**
* Returns a {@link Slave} on which this build was done.
*
diff --git a/core/src/main/java/hudson/model/AbstractProject.java b/core/src/main/java/hudson/model/AbstractProject.java
index 046e3fc7bf..07139d444d 100644
--- a/core/src/main/java/hudson/model/AbstractProject.java
+++ b/core/src/main/java/hudson/model/AbstractProject.java
@@ -46,7 +46,6 @@ import hudson.model.Descriptor.FormException;
import hudson.model.Fingerprint.RangeSet;
import hudson.model.Queue.Executable;
import hudson.model.Queue.Task;
-import hudson.model.listeners.SCMListener;
import hudson.model.queue.QueueTaskFuture;
import hudson.model.queue.SubTask;
import hudson.model.Queue.WaitingItem;
@@ -80,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;
@@ -160,7 +161,7 @@ public abstract class AbstractProject,R extends A
* {@link Run#getPreviousBuild()}
*/
@Restricted(NoExternalUse.class)
- protected transient /*almost final*/ RunMap builds = new RunMap();
+ protected transient RunMap builds = new RunMap();
/**
* The quiet period. Null to delegate to the system default.
@@ -269,13 +270,19 @@ public abstract class AbstractProject,R extends A
public void onLoad(ItemGroup extends Item> parent, String name) throws IOException {
super.onLoad(parent, name);
- if (this.builds==null)
- this.builds = new RunMap();
- this.builds.load(this,new Constructor() {
+ RunMap builds = new RunMap(getBuildDir(), new Constructor() {
public R create(File dir) throws IOException {
return loadBuild(dir);
}
});
+ if (this.builds!=null) {
+ // if we are reloading, keep all those that are still building intact
+ for (R r : this.builds.getLoadedBuilds().values()) {
+ if (r.isBuilding())
+ builds.put(r);
+ }
+ }
+ this.builds = builds;
if(triggers==null) {
// it didn't exist in < 1.28
@@ -947,8 +954,8 @@ public abstract class AbstractProject,R extends A
}
@Override
- public SortedMap _getRuns() {
- return builds.getView();
+ public RunMap _getRuns() {
+ return builds;
}
@Override
@@ -956,6 +963,51 @@ public abstract class AbstractProject,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<R>.
*/
@@ -1627,7 +1679,7 @@ public abstract class AbstractProject
,R extends A
@Override
protected HistoryWidget createHistoryWidget() {
- return new BuildHistoryWidget(this,getBuilds(),HISTORY_ADAPTER);
+ return new BuildHistoryWidget(this,builds,HISTORY_ADAPTER);
}
public boolean isParameterized() {
diff --git a/core/src/main/java/hudson/model/Job.java b/core/src/main/java/hudson/model/Job.java
index 998139c30c..182cb319c9 100644
--- a/core/src/main/java/hudson/model/Job.java
+++ b/core/src/main/java/hudson/model/Job.java
@@ -518,12 +518,22 @@ public abstract class Job, RunT extends Run getBuilds() {
return RunList.fromRuns(_getRuns().values());
}
+ /**
+ * Gets the read-only view of the recent builds.
+ *
+ * @since 1.LAZYLOAD
+ */
+ @Exported(name="builds")
+ public RunList getNewBuilds() {
+ return getBuilds().newBuilds();
+ }
+
/**
* Obtains all the {@link Run}s whose build numbers matches the given {@link RangeSet}.
*/
@@ -600,7 +610,7 @@ public abstract class Job, RunT extends Run m = _getRuns().headMap(n - 1); // the map should
// include n, so n-1
if (m.isEmpty())
@@ -614,7 +624,7 @@ public abstract class Job, RunT extends Run m = _getRuns().tailMap(n);
if (m.isEmpty())
return null;
@@ -626,7 +636,7 @@ public abstract class Job, RunT extends Run, RunT extends Run _getRuns();
diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java
index eb44e7c2f4..e5043378ae 100644
--- a/core/src/main/java/hudson/model/Run.java
+++ b/core/src/main/java/hudson/model/Run.java
@@ -1,9 +1,9 @@
/*
* The MIT License
*
- * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
+ * Copyright (c) 2004-2012, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Daniel Dyer, Red Hat, Inc., Tom Huybrechts, Romain Seguy, Yahoo! Inc.,
- * Darek Ostolski
+ * Darek Ostolski, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -44,7 +44,6 @@ import hudson.model.Descriptor.FormException;
import hudson.model.listeners.RunListener;
import hudson.model.listeners.SaveableListener;
import hudson.security.PermissionScope;
-import jenkins.model.Jenkins.MasterComputer;
import hudson.search.SearchIndexBuilder;
import hudson.security.ACL;
import hudson.security.AccessControlled;
@@ -97,6 +96,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
+import jenkins.model.lazy.BuildReference;
import jenkins.util.io.OnMaster;
import net.sf.json.JSONObject;
import org.apache.commons.io.input.NullInputStream;
@@ -325,7 +325,9 @@ public abstract class Run ,RunT extends Run,RunT extends Run,RunT extends Run,RunT extends Run,RunT extends Run,RunT extends Run,RunT extends Run,RunT extends Run> extends AbstractMap implements SortedMap {
- // copy-on-write map
- private transient volatile SortedMap builds =
- new TreeMap(COMPARATOR);
-
+// in practice R is always bound by AbstractBuild, but making that change causes all kinds of
+// signature breakage.
+public final class RunMap> extends AbstractLazyLoadRunMap implements Iterable {
/**
* Read-only view of this map.
*/
private final SortedMap view = Collections.unmodifiableSortedMap(this);
- public Set> entrySet() {
- // since the map is copy-on-write, make sure no one modifies it
- return Collections.unmodifiableSet(builds.entrySet());
+ private Constructor cons;
+
+ // TODO: before first complete build
+ // patch up next/previous build link
+
+
+ /**
+ * @deprecated as of 1.LAZYLOAD
+ * Use {@link #RunMap(File, Constructor)}.
+ */
+ public RunMap() {
+ super(null); // will be set later
}
- public synchronized R put(R value) {
- return put(value.getNumber(),value);
+ /**
+ * @param cons
+ * Used to create new instance of {@link Run}.
+ */
+ public RunMap(File baseDir, Constructor cons) {
+ super(baseDir);
+ this.cons = cons;
+ }
+
+ public boolean remove(R run) {
+ return removeValue(run);
+ }
+
+ /**
+ * Walks through builds, newer ones first.
+ */
+ public Iterator iterator() {
+ return new Iterator() {
+ 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 synchronized R put(Integer key, R value) {
- // copy-on-write update
- TreeMap m = new TreeMap(builds);
-
- R r = update(m, key, value);
-
- this.builds = m;
- return r;
- }
-
- @Override
- public synchronized void putAll(Map extends Integer,? extends R> rhs) {
- // copy-on-write update
- TreeMap m = new TreeMap(builds);
-
- for (Map.Entry extends Integer,? extends R> e : rhs.entrySet())
- update(m, e.getKey(), e.getValue());
-
- this.builds = m;
- }
-
- private R update(TreeMap m, Integer key, R value) {
- // things are bit tricky because this map is order so that the newest one comes first,
- // yet 'nextBuild' refers to the newer build.
- R first = m.isEmpty() ? null : m.get(m.firstKey());
- R r = m.put(key, value);
- SortedMap head = m.headMap(key);
- if(!head.isEmpty()) {
- R prev = m.get(head.lastKey());
- value.previousBuild = prev.previousBuild;
- value.nextBuild = prev;
- if(value.previousBuild!=null)
- value.previousBuild.nextBuild = value;
- prev.previousBuild=value;
- } else {
- value.previousBuild = first;
- value.nextBuild = null;
- if(first!=null)
- first.nextBuild = value;
- }
- return r;
- }
-
- public synchronized boolean remove(R run) {
- if(run.nextBuild!=null)
- run.nextBuild.previousBuild = run.previousBuild;
- if(run.previousBuild!=null)
- run.previousBuild.nextBuild = run.nextBuild;
-
- // copy-on-write update
- TreeMap m = new TreeMap(builds);
- R r = m.remove(run.getNumber());
- this.builds = m;
-
- return r!=null;
- }
-
- public synchronized void reset(TreeMap builds) {
- this.builds = new TreeMap(COMPARATOR);
- putAll(builds);
+ public boolean removeValue(R run) {
+ run.dropLinks();
+ return super.removeValue(run);
}
/**
@@ -138,51 +128,24 @@ public final class RunMap> extends AbstractMap imp
return view;
}
-//
-// SortedMap delegation
-//
- public Comparator super Integer> comparator() {
- return builds.comparator();
- }
-
- public SortedMap subMap(Integer fromKey, Integer toKey) {
- return builds.subMap(fromKey, toKey);
- }
-
- public SortedMap headMap(Integer toKey) {
- return builds.headMap(toKey);
- }
-
- public SortedMap tailMap(Integer fromKey) {
- return builds.tailMap(fromKey);
- }
-
- public Integer firstKey() {
- return builds.firstKey();
- }
-
- public Integer lastKey() {
- return builds.lastKey();
- }
-
/**
* This is the newest build (with the biggest build number)
*/
public R newestValue() {
- SortedMap _builds = builds;
- if (_builds.isEmpty()) return null;
- return _builds.get(firstKey());
+ return search(Integer.MAX_VALUE, DESC);
}
/**
* This is the oldest build (with the smallest build number)
*/
public R oldestValue() {
- SortedMap _builds = builds;
- if (_builds.isEmpty()) return null;
- return _builds.get(lastKey());
+ return search(Integer.MIN_VALUE, ASC);
}
+ /**
+ * @deprecated as of 1.LAZYLOAD
+ * Use {@link ReverseComparator}
+ */
public static final Comparator COMPARATOR = new Comparator() {
public int compare(Comparable o1, Comparable o2) {
return -o1.compareTo(o2);
@@ -196,21 +159,33 @@ public final class RunMap> extends AbstractMap imp
R create(File dir) throws IOException;
}
+ @Override
+ protected final int getNumberOf(R r) {
+ return r.getNumber();
+ }
+
+ @Override
+ protected final String getIdOf(R r) {
+ return r.getId();
+ }
+
/**
- * Fills in {@link RunMap} by loading build records from the file system.
- *
- * @param job
- * Job that owns this map.
- * @param cons
- * Used to create new instance of {@link Run}.
+ * Reuses the same reference as much as we can.
+ *
+ * If concurrency ends up creating a few extra, that's OK, because
+ * we are really just trying to reduce the # of references we create.
*/
- public synchronized void load(Job job, Constructor cons) {
+ @Override
+ protected BuildReference createReference(R r) {
+ if (r instanceof AbstractBuild) return ((AbstractBuild)r).selfReference;
+ else return super.createReference(r);
+ }
+
+ @Override
+ protected FilenameFilter createDirectoryFilter() {
final SimpleDateFormat formatter = Run.ID_FORMATTER.get();
- TreeMap builds = new TreeMap(RunMap.COMPARATOR);
- File buildDir = job.getBuildDir();
- buildDir.mkdirs();
- String[] buildDirs = buildDir.list(new FilenameFilter() {
+ return new FilenameFilter() {
public boolean accept(File dir, String name) {
// JENKINS-1461 sometimes create bogus data directories with impossible dates, such as year 0, April 31st,
// or August 0th. Date object doesn't roundtrip those, so we eventually fail to load this data.
@@ -231,53 +206,49 @@ public final class RunMap> extends AbstractMap imp
}
return false;
}
- });
+ };
+ }
- // keep all those that are building intact.
- Map building = Maps.newHashMap();
- for (R b=newestValue(); b!=null && b.isBuilding(); b=b.getPreviousBuild()) {
- building.put(b.getNumber(), b);
- }
-
- for( String build : buildDirs ) {
- File d = new File(buildDir,build);
- if(new File(d,"build.xml").exists()) {
- // if the build result file isn't in the directory, ignore it.
- try {
- R b = cons.create(d);
- R existing = builds.put(b.getNumber(), b);
- if (existing != null) {
- LOGGER.log(Level.WARNING, "multiple runs claiming to be #{0}; using run from {1}", new Object[] {b.getNumber(), d});
- }
- } catch (IOException e) {
- LOGGER.log(Level.WARNING, "could not load " + d, e);
- } catch (InstantiationError e) {
- LOGGER.log(Level.WARNING, "could not load " + d, e);
- }
+ @Override
+ protected R retrieve(File d) throws IOException {
+ if(new File(d,"build.xml").exists()) {
+ // if the build result file isn't in the directory, ignore it.
+ try {
+ R b = cons.create(d);
+ b.onLoad();
+ if (LOGGER.isLoggable(FINE) || LOG_RETRIEVAL)
+ LOGGER.log(LOG_RETRIEVAL?INFO:FINE,"Loaded " + b.getFullDisplayName(),new ThisIsHowItsLoaded());
+ return b;
+ } catch (IOException e) {
+ LOGGER.log(Level.WARNING, "could not load " + d, e);
+ } catch (InstantiationError e) {
+ LOGGER.log(Level.WARNING, "could not load " + d, e);
}
}
+ return null;
+ }
- // overlay what's currently building on top of what's loaded
- builds.putAll(building);
- /*
- // we probably aren't saving every little changes during the build to disk,
- // so it's risky to reload these from disk.
- for (R b : building.values()) {
- try {
- b.reload();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InstantiationError e) {
- e.printStackTrace();
- }
- }
- */
-
- reset(builds);
-
- for (R r : builds.values())
- r.onLoad();
+ /**
+ * Backward compatibility method that notifies {@link RunMap} of who the owner is.
+ *
+ * Traditionally, this method blocked and loaded all the build records on the disk,
+ * but now all the actual loading happens lazily.
+ *
+ * @param job
+ * Job that owns this map.
+ * @param cons
+ * Used to create new instance of {@link Run}.
+ * @deprecated as of 1.LAZYLOAD
+ * Use {@link #RunMap(File, Constructor)}
+ */
+ public void load(Job job, Constructor cons) {
+ this.cons = cons;
+ initBaseDir(job.getBuildDir());
}
private static final Logger LOGGER = Logger.getLogger(RunMap.class.getName());
+
+ public static boolean LOG_RETRIEVAL = false;
+
+ private static class ThisIsHowItsLoaded extends Exception {}
}
diff --git a/core/src/main/java/hudson/tasks/Fingerprinter.java b/core/src/main/java/hudson/tasks/Fingerprinter.java
index d94bc0f0cc..6600bd4aa0 100644
--- a/core/src/main/java/hudson/tasks/Fingerprinter.java
+++ b/core/src/main/java/hudson/tasks/Fingerprinter.java
@@ -70,6 +70,7 @@ import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
@@ -295,6 +296,8 @@ public class Fingerprinter extends Recorder implements Serializable, DependecyDe
private final AbstractBuild build;
+ private static final Random rand = new Random();
+
/**
* From file name to the digest.
*/
@@ -340,11 +343,15 @@ public class Fingerprinter extends Recorder implements Serializable, DependecyDe
}
public void onLoad() {
- Run pb = build.getPreviousBuild();
- if (pb!=null) {
- FingerprintAction a = pb.getAction(FingerprintAction.class);
- if (a!=null)
- compact(a);
+ // share data structure with nearby builds, but to keep lazy loading efficient,
+ // don't go back the history forever.
+ if (rand.nextInt(2)!=0) {
+ Run pb = build.getPreviousBuild();
+ if (pb!=null) {
+ FingerprintAction a = pb.getAction(FingerprintAction.class);
+ if (a!=null)
+ compact(a);
+ }
}
}
diff --git a/core/src/main/java/hudson/tasks/LogRotator.java b/core/src/main/java/hudson/tasks/LogRotator.java
index 8e1a154403..389f0d4565 100644
--- a/core/src/main/java/hudson/tasks/LogRotator.java
+++ b/core/src/main/java/hudson/tasks/LogRotator.java
@@ -23,22 +23,22 @@
*/
package hudson.tasks;
+import com.google.common.collect.Lists;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Run;
-
import org.kohsuke.stapler.DataBoundConstructor;
import java.io.IOException;
import java.util.Calendar;
+import java.util.Collection;
import java.util.GregorianCalendar;
-import static java.util.logging.Level.FINE;
-import static java.util.logging.Level.FINER;
-
import java.util.List;
import java.util.logging.Logger;
+import static java.util.logging.Level.*;
+
/**
* Deletes old builds.
*
@@ -115,7 +115,7 @@ public class LogRotator implements Describable {
if(numToKeep!=-1) {
List extends Run,?>> builds = job.getBuilds();
- for (Run r : builds.subList(Math.min(builds.size(),numToKeep),builds.size())) {
+ for (Run r : copy(builds.subList(Math.min(builds.size(), numToKeep), builds.size()))) {
if (r.isKeepLog()) {
LOGGER.log(FINER,r.getFullDisplayName()+" is not GC-ed because it's marked as a keeper");
continue;
@@ -136,7 +136,7 @@ public class LogRotator implements Describable {
if(daysToKeep!=-1) {
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR,-daysToKeep);
- for( Run r : job.getBuilds() ) {
+ for( Run r : copy(job.getBuilds()) ) {
if (r.isKeepLog()) {
LOGGER.log(FINER,r.getFullDisplayName()+" is not GC-ed because it's marked as a keeper");
continue;
@@ -160,7 +160,7 @@ public class LogRotator implements Describable {
if(artifactNumToKeep!=null && artifactNumToKeep!=-1) {
List extends Run,?>> builds = job.getBuilds();
- for (Run r : builds.subList(Math.min(builds.size(),artifactNumToKeep),builds.size())) {
+ for (Run r : copy(builds.subList(Math.min(builds.size(), artifactNumToKeep), builds.size()))) {
if (r.isKeepLog()) {
LOGGER.log(FINER,r.getFullDisplayName()+" is not purged of artifacts because it's marked as a keeper");
continue;
@@ -180,7 +180,7 @@ public class LogRotator implements Describable {
if(artifactDaysToKeep!=null && artifactDaysToKeep!=-1) {
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR,-artifactDaysToKeep);
- for( Run r : job.getBuilds() ) {
+ for( Run r : copy(job.getBuilds())) {
if (r.isKeepLog()) {
LOGGER.log(FINER,r.getFullDisplayName()+" is not purged of artifacts because it's marked as a keeper");
continue;
@@ -200,7 +200,13 @@ public class LogRotator implements Describable {
r.deleteArtifacts();
}
}
+ }
+ /**
+ * Creates a copy since we'll be deleting some entries from them.
+ */
+ private Collection copy(Iterable src) {
+ return Lists.newArrayList(src);
}
public int getDaysToKeep() {
diff --git a/core/src/main/java/hudson/util/Iterators.java b/core/src/main/java/hudson/util/Iterators.java
index f01b2635ca..b00540e9c5 100644
--- a/core/src/main/java/hudson/util/Iterators.java
+++ b/core/src/main/java/hudson/util/Iterators.java
@@ -23,8 +23,11 @@
*/
package hudson.util;
+import com.google.common.base.Objects;
+import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
+import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -326,4 +329,51 @@ public class Iterators {
public static Iterator sequence(Iterator extends T>... iterators) {
return com.google.common.collect.Iterators.concat(iterators);
}
+
+ /**
+ * Returns the elements in the base iterator until it hits any element that doesn't satisfy the filter.
+ * Then the rest of the elements in the base iterator gets ignored.
+ *
+ * @since 1.LAZYLOAD
+ */
+ public static Iterator limit(final Iterator extends T> base, final CountingPredicate super T> filter) {
+ return new Iterator() {
+ private T next;
+ private boolean end;
+ private int index=0;
+ public boolean hasNext() {
+ fetch();
+ return next!=null;
+ }
+
+ public T next() {
+ fetch();
+ T r = next;
+ next = null;
+ return r;
+ }
+
+ private void fetch() {
+ if (next==null && !end) {
+ if (base.hasNext()) {
+ next = base.next();
+ if (!filter.apply(index++,next)) {
+ next = null;
+ end = true;
+ }
+ } else {
+ end = true;
+ }
+ }
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ public interface CountingPredicate {
+ boolean apply(int index, T input);
+ }
}
diff --git a/core/src/main/java/hudson/util/RunList.java b/core/src/main/java/hudson/util/RunList.java
index 67e9d50d89..60166d9e71 100644
--- a/core/src/main/java/hudson/util/RunList.java
+++ b/core/src/main/java/hudson/util/RunList.java
@@ -23,6 +23,9 @@
*/
package hudson.util;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Iterators;
import hudson.model.AbstractBuild;
import hudson.model.Item;
import hudson.model.Job;
@@ -30,6 +33,7 @@ import hudson.model.Node;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.View;
+import hudson.util.Iterators.CountingPredicate;
import java.util.AbstractList;
import java.util.ArrayList;
@@ -44,83 +48,193 @@ import java.util.List;
/**
* {@link List} of {@link Run}s, sorted in the descending date order.
*
- * TODO: this should be immutable
- *
* @author Kohsuke Kawaguchi
*/
-public class RunList extends ArrayList {
+public class RunList extends AbstractList {
+
+ private Iterable base;
+
+ private R first;
+ private Integer size;
+
public RunList() {
+ base = Collections.emptyList();
}
public RunList(Job j) {
- addAll(j.getBuilds());
- }
-
- public R getFirstBuild() {
- return isEmpty() ? null : get(size()-1);
- }
-
- public R getLastBuild() {
- return isEmpty() ? null : get(0);
+ base = j.getBuilds();
}
public RunList(View view) {// this is a type unsafe operation
+ List> jobs = new ArrayList>();
for (Item item : view.getItems())
for (Job,?> j : item.getAllJobs())
- addAll((Collection)j.getBuilds());
- Collections.sort(this,Run.ORDER_BY_DATE);
+ jobs.add(((Job)j).getBuilds());
+
+ this.base = combine(jobs);
}
public RunList(Collection extends Job> jobs) {
+ List> src = new ArrayList>();
for (Job j : jobs)
- addAll(j.getBuilds());
- Collections.sort(this,Run.ORDER_BY_DATE);
+ src.add(j.getBuilds());
+ this.base = combine(src);
}
- private RunList(Collection extends R> c, boolean hack) {
- super(c);
+ private Iterable combine(Iterable> jobs) {
+ return Iterables.mergeSorted(jobs, new Comparator() {
+ public int compare(R o1, R o2) {
+ long lhs = o1.getTimeInMillis();
+ long rhs = o2.getTimeInMillis();
+ if (lhs > rhs) return -1;
+ if (lhs < rhs) return 1;
+ return 0;
+ }
+ });
+ }
+
+ private RunList(Iterable c) {
+ base = c;
+ }
+
+ @Override
+ public Iterator iterator() {
+ return base.iterator();
+ }
+
+ /**
+ * @deprecated as of 1.LAZYLOAD
+ * {@link RunList}, despite its name, should be really used as {@link Iterable}, not as {@link List}.
+ */
+ @Override
+ public int size() {
+ if (size==null) {
+ int sz=0;
+ for (R r : this) {
+ first = r;
+ sz++;
+ }
+ size = sz;
+ }
+ return size;
+ }
+
+ /**
+ * @deprecated as of 1.LAZYLOAD
+ * {@link RunList}, despite its name, should be really used as {@link Iterable}, not as {@link List}.
+ */
+ @Override
+ public R get(int index) {
+ return Iterators.get(iterator(),index);
+ }
+
+ @Override
+ public int indexOf(Object o) {
+ int index=0;
+ for (R r : this) {
+ if (r.equals(o))
+ return index;
+ index++;
+ }
+ return -1;
+ }
+
+ @Override
+ public int lastIndexOf(Object o) {
+ int a = -1;
+ int index=0;
+ for (R r : this) {
+ if (r.equals(o))
+ a = index;
+ index++;
+ }
+ return a;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return !iterator().hasNext();
+ }
+
+ public R getFirstBuild() {
+ size();
+ return first;
+ }
+
+ public R getLastBuild() {
+ Iterator itr = iterator();
+ return itr.hasNext() ? itr.next() : null;
}
public static
RunList fromRuns(Collection extends R> runs) {
- return new RunList(runs,false);
+ return new RunList((Iterable)runs);
+ }
+
+ /**
+ * Returns elements that satisfy the given predicate.
+ */
+ // for compatibility reasons, this method doesn't create a new list but updates the current one
+ private RunList filter(Predicate predicate) {
+ size = null;
+ first = null;
+ base = Iterables.filter(base,predicate);
+ return this;
+ }
+
+ /**
+ * Returns the first streak of the elements that satisfy the given predicate.
+ *
+ * For example, {@code filter([1,2,3,4],odd)==[1,3]} but {@code limit([1,2,3,4],odd)==[1]}.
+ */
+ private RunList limit(final CountingPredicate predicate) {
+ size = null;
+ first = null;
+ final Iterable nested = base;
+ base = new Iterable() {
+ public Iterator iterator() {
+ return hudson.util.Iterators.limit(nested.iterator(),predicate);
+ }
+
+ @Override
+ public String toString() {
+ return Iterables.toString(this);
+ }
+ };
+ return this;
}
/**
* Filter the list to non-successful builds only.
*/
public RunList failureOnly() {
- for (Iterator itr = iterator(); itr.hasNext();) {
- Run r = itr.next();
- if(r.getResult()==Result.SUCCESS)
- itr.remove();
- }
- return this;
+ return filter(new Predicate() {
+ public boolean apply(R r) {
+ return r.getResult()!=Result.SUCCESS;
+ }
+ });
}
/**
* Filter the list to builds on a single node only
*/
- public RunList node(Node node) {
- for (Iterator itr = iterator(); itr.hasNext();) {
- Run r = itr.next();
- if (!(r instanceof AbstractBuild) || ((AbstractBuild)r).getBuiltOn()!=node) {
- itr.remove();
+ public RunList node(final Node node) {
+ return filter(new Predicate() {
+ public boolean apply(R r) {
+ return (r instanceof AbstractBuild) && ((AbstractBuild)r).getBuiltOn()==node;
}
- }
- return this;
+ });
}
/**
* Filter the list to regression builds only.
*/
public RunList regressionOnly() {
- for (Iterator itr = iterator(); itr.hasNext();) {
- Run r = itr.next();
- if(!r.getBuildStatusSummary().isWorse)
- itr.remove();
- }
- return this;
+ return filter(new Predicate() {
+ public boolean apply(R r) {
+ return r.getBuildStatusSummary().isWorse;
+ }
+ });
}
/**
@@ -128,30 +242,17 @@ public class RunList extends ArrayList {
*
* {@code s<=;e}.
*/
- public RunList byTimestamp(long start, long end) {
- AbstractList TIMESTAMP_ADAPTER = new AbstractList() {
- public Long get(int index) {
- return RunList.this.get(index).getTimeInMillis();
+ public RunList byTimestamp(final long start, final long end) {
+ return
+ limit(new CountingPredicate() {
+ public boolean apply(int index,R r) {
+ return r.getTimeInMillis()() {
+ public boolean apply(R r) {
+ return start<=r.getTimeInMillis();
}
- };
- Comparator DESCENDING_ORDER = new Comparator() {
- public int compare(Long o1, Long o2) {
- if (o1 > o2) return -1;
- if (o1 < o2) return +1;
- return 0;
- }
- };
-
- int s = Collections.binarySearch(TIMESTAMP_ADAPTER, start, DESCENDING_ORDER);
- if (s<0) s=-(s+1); // min is inclusive
- int e = Collections.binarySearch(TIMESTAMP_ADAPTER, end, DESCENDING_ORDER);
- if (e<0) e=-(e+1); else e++; // max is exclusive, so the exact match should be excluded
-
- return fromRuns(subList(e,s));
+ });
}
/**
@@ -160,27 +261,21 @@ public class RunList extends ArrayList {
* if it changes.
*/
public RunList newBuilds() {
- GregorianCalendar threshold = new GregorianCalendar();
- threshold.add(Calendar.DAY_OF_YEAR,-7);
+ GregorianCalendar cal = new GregorianCalendar();
+ cal.add(Calendar.DAY_OF_YEAR, -7);
+ final long t = cal.getTimeInMillis();
- int count=0;
-
- for (Iterator itr = iterator(); itr.hasNext();) {
- R r = itr.next();
- if(r.isBuilding()) {
- // can't publish on-going builds
- itr.remove();
- continue;
+ // can't publish on-going builds
+ return filter(new Predicate() {
+ public boolean apply(R r) {
+ return !r.isBuilding();
}
- // at least put 10 items
- if(count<10) {
- count++;
- continue;
+ })
+ // put at least 10 builds, but otherwise ignore old builds
+ .limit(new CountingPredicate() {
+ public boolean apply(int index, R r) {
+ return index < 10 || r.getTimeInMillis() >= t;
}
- // anything older than 7 days will be ignored
- if(r.getTimestamp().before(threshold))
- itr.remove();
- }
- return this;
+ });
}
}
diff --git a/core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java b/core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java
new file mode 100644
index 0000000000..9507d62d79
--- /dev/null
+++ b/core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java
@@ -0,0 +1,668 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) 2012, CloudBees, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package jenkins.model.lazy;
+
+import hudson.model.Run;
+import org.kohsuke.accmod.Restricted;
+import org.kohsuke.accmod.restrictions.NoExternalUse;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.lang.ref.Reference;
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import static jenkins.model.lazy.AbstractLazyLoadRunMap.Direction.*;
+import static jenkins.model.lazy.Boundary.*;
+
+/**
+ * {@link 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 {@link SortedMap} from outside,
+ * it actually doesn't have every item in the map instantiated yet. As items in the map get
+ * requested, this class {@link #retrieve(File) 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 {@link Integer}),
+ * but this class also provides look up based on {@linkplain #getIdOf(Object) the build ID}.
+ *
+ *
+ * This class makes the following assumption about the on-disk layout of the data:
+ *
+ *
+ * - Every build is stored in a directory, named after its ID.
+ *
- ID and build number are in the consistent order. That is,
+ * if there are two builds #M and #N, {@code 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 {@link SortedMap} operations are weakly implemented. For example,
+ * {@link #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 {@link SortedMap} operations are inefficiently implemented, by
+ * {@linkplain #all() loading all the build records eagerly}. We hope to replace
+ * these implementations by more efficient lazy-loading ones as we go.
+ *
+ *
+ * Object lock of {@code this} is used to make sure mutation occurs sequentially.
+ * That is, ensure that only one thread is actually calling {@link #retrieve(File)} and
+ * updating {@link Index#byNumber} and {@link Index#byId}.
+ *
+ * @author Kohsuke Kawaguchi
+ * @since 1.LAZYLOAD
+ */
+public abstract class AbstractLazyLoadRunMap extends AbstractMap implements SortedMap {
+ /**
+ * Used in {@link #all()} to quickly determine if we've already loaded everything.
+ */
+ private boolean fullyLoaded;
+
+ /**
+ * Currently visible index.
+ * Updated atomically. Once set to this field, the index object may not be modified.
+ */
+ private volatile Index index = new Index();
+
+ /**
+ * Pair of two maps into a single class, so that the changes can be made visible atomically,
+ * and updates can happen concurrently to read.
+ *
+ * The idiom is that you put yourself in a synchronized block, {@linkplain #copy() make a copy of this},
+ * update the copy, then set it to {@link #index}.
+ */
+ private class Index {
+ /**
+ * Stores the mapping from build number to build, for builds that are already loaded.
+ */
+ private final TreeMap> byNumber;
+
+ /**
+ * Stores the build ID to build number for builds that we already know
+ */
+ private final TreeMap> byId;
+
+ private Index() {
+ byId = new TreeMap>();
+ byNumber = new TreeMap>(COMPARATOR);
+ }
+
+ private Index(Index rhs) {
+ byId = new TreeMap>(rhs.byId);
+ byNumber = new TreeMap>(rhs.byNumber);
+ }
+ }
+
+ /**
+ * Build IDs found as directories, in the ascending order.
+ */
+ // copy on write
+ private SortedList idOnDisk = new SortedList(Collections.emptyList());
+
+ /**
+ * Build bumber shortcuts found on disk, in the ascending order.
+ */
+ // copy on write
+ private SortedIntList numberOnDisk = new SortedIntList(0);
+
+ /**
+ * Base directory for data.
+ * In effect this is treated as a final field, but can't mark it final
+ * because the compatibility requires that we make it settable
+ * in the first call after the constructor.
+ */
+ private File dir;
+
+ protected AbstractLazyLoadRunMap(File dir) {
+ initBaseDir(dir);
+ }
+
+ @Restricted(NoExternalUse.class)
+ protected void initBaseDir(File dir) {
+ assert this.dir==null;
+ this.dir = dir;
+ if (dir!=null)
+ loadIdOnDisk();
+ }
+
+ private void loadIdOnDisk() {
+ String[] buildDirs = dir.list(createDirectoryFilter());
+ if (buildDirs==null) buildDirs=EMPTY_STRING_ARRAY;
+ // wrap into ArrayList to enable mutation
+ Arrays.sort(buildDirs);
+ idOnDisk = new SortedList(new ArrayList(Arrays.asList(buildDirs)));
+
+ // TODO: should we check that shortcuts is a symlink?
+ String[] shortcuts = dir.list();
+ if (shortcuts==null) shortcuts=EMPTY_STRING_ARRAY;
+ SortedIntList list = new SortedIntList(shortcuts.length/2);
+ for (String s : shortcuts) {
+ try {
+ list.add(Integer.parseInt(s));
+ } catch (NumberFormatException e) {
+ // this isn't a shortcut
+ }
+ }
+ list.sort();
+ numberOnDisk = list;
+ }
+
+ public Comparator super Integer> comparator() {
+ return COMPARATOR;
+ }
+
+ /**
+ * If we have non-zero R in memory, we can return false right away.
+ * If we have zero R in memory, try loading one and see if we can find something.
+ */
+ @Override
+ public boolean isEmpty() {
+ return index.byId.isEmpty() && search(Integer.MAX_VALUE, DESC)==null;
+ }
+
+ @Override
+ public Set> entrySet() {
+ return Collections.unmodifiableSet(new BuildReferenceMapAdapter(this,all()).entrySet());
+ }
+
+ /**
+ * Returns a read-only view of records that has already been loaded.
+ */
+ public SortedMap getLoadedBuilds() {
+ return Collections.unmodifiableSortedMap(new BuildReferenceMapAdapter(this, index.byNumber));
+ }
+
+ /**
+ * @param fromKey
+ * Biggest build number to be in the returned set.
+ * @param toKey
+ * Smallest build number-1 to be in the returned set (-1 because this is exclusive)
+ */
+ public SortedMap subMap(Integer fromKey, Integer toKey) {
+ // TODO: if this method can produce a lazy map, that'd be wonderful
+ // because due to the lack of floor/ceil/higher/lower kind of methods
+ // to look up keys in SortedMap, various places of Jenkins rely on
+ // subMap+firstKey/lastKey combo.
+
+ R start = search(fromKey, DESC);
+ if (start==null) return EMPTY_SORTED_MAP;
+
+ R end = search(toKey, ASC);
+ if (end==null) return EMPTY_SORTED_MAP;
+
+ for (R i=start; i!=end; ) {
+ i = search(getNumberOf(i)-1,DESC);
+ assert i!=null;
+ }
+
+ return Collections.unmodifiableSortedMap(new BuildReferenceMapAdapter(this, index.byNumber.subMap(fromKey, toKey)));
+ }
+
+ public SortedMap headMap(Integer toKey) {
+ return subMap(Integer.MAX_VALUE, toKey);
+ }
+
+ public SortedMap tailMap(Integer fromKey) {
+ return subMap(fromKey, Integer.MIN_VALUE);
+ }
+
+ public Integer firstKey() {
+ R r = newestBuild();
+ if (r==null) throw new NoSuchElementException();
+ return getNumberOf(r);
+ }
+
+ public Integer lastKey() {
+ 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) {
+ int n = (Integer) key;
+ return get(n);
+ }
+ return super.get(key);
+ }
+
+ public R get(int n) {
+ return search(n,Direction.EXACT);
+ }
+
+ /**
+ * Finds the build #M where M is nearby the given 'n'.
+ *
+ *
+ *
+ *
+ * @param n
+ * the index to start the search from
+ * @param d
+ * 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 search(final int n, final Direction d) {
+ Entry> c = index.byNumber.ceilingEntry(n);
+ if (c!=null && c.getKey()== n) {
+ R r = c.getValue().get();
+ if (r!=null)
+ return r; // found the exact #n
+ }
+
+ // at this point we know that we don't have #n loaded yet
+
+ {// check numberOnDisk as a cache to see if we can find it there
+ int npos = numberOnDisk.find(n);
+ if (npos>=0) {// found exact match
+ R r = load(numberOnDisk.get(npos), null);
+ if (r!=null)
+ return r;
+ }
+
+ switch (d) {
+ case ASC:
+ case DESC:
+ // didn't find the exact match, but what's the nearest ascending value in the cache?
+ int neighbor = (d==ASC?HIGHER:LOWER).apply(npos);
+ if (numberOnDisk.isInRange(neighbor)) {
+ R r = getByNumber(numberOnDisk.get(neighbor));
+ if (r!=null) {
+ // make sure that the cache is accurate by looking at the previous ID
+ // and it actually satisfies the constraint
+ int prev = (d==ASC?LOWER:HIGHER).apply(idOnDisk.find(getIdOf(r)));
+ if (idOnDisk.isInRange(prev)) {
+ R pr = getById(idOnDisk.get(prev));
+ // sign*sign is making sure that #pr and #r sandwiches #n.
+ if (pr!=null && signOfCompare(getNumberOf(pr),n)*signOfCompare(n,getNumberOf(r))>0)
+ return r;
+ else {
+ // cache is lying. there's something fishy.
+ // ignore the cache and do the slow search
+ }
+ } else {
+ // r is the build with youngest ID
+ return r;
+ }
+ } else {
+ // cache says we should have a build but we didn't.
+ // ignore the cache and do the slow search
+ }
+ }
+ break;
+ case EXACT:
+ // fall through
+ }
+
+ // didn't find it in the cache, but don't give up yet
+ // maybe the cache just doesn't exist.
+ // so fall back to the slow search
+ }
+
+ // capture the snapshot and work off with it since it can be overwritten by other threads
+ SortedList idOnDisk = this.idOnDisk;
+ boolean clonedIdOnDisk = false;
+
+ // slow path: we have to find the build from idOnDisk.
+ // first, narrow down the candidate IDs to try by using two known number-to-ID mapping
+ if (idOnDisk.isEmpty()) return null;
+
+ Entry> f = index.byNumber.floorEntry(n);
+
+ // if bound is null, use a sentinel value
+ String cid = c==null ? "\u0000" : c.getValue().id;
+ String fid = f==null ? "\uFFFF" : f.getValue().id;
+
+ // We know that the build we are looking for exists in this range
+ // we will narrow this down via binary search
+ int lo = idOnDisk.higher(cid);
+ int hi = idOnDisk.lower(fid)+1;
+
+ // invariant: 0<=lo<=pivot<=hi<=idOnDisk.size()
+
+ while (lo(idOnDisk);
+ clonedIdOnDisk = true;
+ }
+ idOnDisk.remove(pivot);
+ continue;
+ }
+
+ int found = getNumberOf(r);
+ if (found==n)
+ return r; // exact match
+
+ if (foundb) return 1;
+ if (a ref = createReference(r);
+ BuildReference old = copy.byId.put(id,ref);
+ copy.byNumber.put(n,ref);
+ index = copy;
+
+ /*
+ search relies on the fact that every object added via
+ put() method be available in the xyzOnDisk index, so I'm adding them here
+ however, this is awfully inefficient. I wonder if there's any better way to do this?
+ */
+ if (!idOnDisk.contains(id)) {
+ ArrayList a = new ArrayList(idOnDisk);
+ a.add(id);
+ Collections.sort(a);
+ idOnDisk = new SortedList(a);
+ }
+
+ if (!numberOnDisk.contains(n)) {
+ SortedIntList a = new SortedIntList(numberOnDisk);
+ a.add(n);
+ a.sort();
+ numberOnDisk = a;
+ }
+
+ return unwrap(old);
+ }
+
+ private R unwrap(Reference ref) {
+ return ref!=null ? ref.get() : null;
+ }
+
+ @Override
+ public synchronized void putAll(Map extends Integer,? extends R> rhs) {
+ Index copy = copy();
+ for (R r : rhs.values()) {
+ String id = getIdOf(r);
+ BuildReference ref = createReference(r);
+ copy.byId.put(id,ref);
+ copy.byNumber.put(getNumberOf(r),ref);
+ }
+ index = copy;
+ }
+
+ /**
+ * Loads all the build records to fully populate the map.
+ * Calling this method results in eager loading everything,
+ * so the whole point of this class is to avoid this call as much as possible
+ * for typical code path.
+ *
+ * @return
+ * fully populated map.
+ */
+ private TreeMap> all() {
+ if (!fullyLoaded) {
+ synchronized (this) {
+ if (!fullyLoaded) {
+ Index copy = copy();
+ for (String id : idOnDisk) {
+ if (!copy.byId.containsKey(id))
+ load(id,copy);
+ }
+ index = copy;
+ fullyLoaded = true;
+ }
+ }
+ }
+ return index.byNumber;
+ }
+
+ /**
+ * Creates a duplicate for the COW data structure in preparation for mutation.
+ */
+ private Index copy() {
+ return new Index(index);
+ }
+
+ /**
+ * Tries to load the record #N by using the shortcut.
+ *
+ * @return null if the data failed to load.
+ */
+ protected R load(int n, Index editInPlace) {
+ R r = null;
+ File shortcut = new File(dir,String.valueOf(n));
+ if (shortcut.isDirectory()) {
+ synchronized (this) {
+ r = load(shortcut,editInPlace);
+
+ // make sure what we actually loaded is #n,
+ // because the shortcuts can lie.
+ if (r!=null && getNumberOf(r)!=n)
+ r = null;
+
+ if (r==null) {
+ // if failed to locate, record that fact
+ SortedIntList update = new SortedIntList(numberOnDisk);
+ update.removeValue(n);
+ numberOnDisk = update;
+ }
+ }
+ }
+ return r;
+ }
+
+
+ protected R load(String id, Index editInPlace) {
+ return load(new File(dir,id),editInPlace);
+ }
+
+ /**
+ * @param editInPlace
+ * If non-null, update this data structure.
+ * Otherwise do a copy-on-write of {@link #index}
+ */
+ protected synchronized R load(File dataDir, Index editInPlace) {
+ try {
+ R r = retrieve(dataDir);
+ if (r==null) return null;
+
+ Index copy = editInPlace!=null ? editInPlace : new Index(index);
+
+ String id = getIdOf(r);
+ BuildReference ref = createReference(r);
+ copy.byId.put(id,ref);
+ copy.byNumber.put(getNumberOf(r),ref);
+
+ if (editInPlace==null) index = copy;
+
+ return r;
+ } catch (IOException e) {
+ LOGGER.log(Level.WARNING, "Failed to load "+dataDir,e);
+ }
+ return null;
+ }
+
+ /**
+ * Subtype to provide {@link Run#getNumber()} so that this class doesn't have to depend on it.
+ */
+ protected abstract int getNumberOf(R r);
+ /**
+ * Subtype to provide {@link Run#getId()} so that this class doesn't have to depend on it.
+ */
+ protected abstract String getIdOf(R r);
+
+ /**
+ * Allow subtype to capture a reference.
+ */
+ protected BuildReference createReference(R r) {
+ return new BuildReference(getIdOf(r),r);
+ }
+
+
+ /**
+ * Parses {@code R} instance from data in the specified directory.
+ *
+ * @return
+ * null if the parsing failed.
+ * @throws IOException
+ * if the parsing failed. This is just like returning null
+ * except the caller will catch the exception and report it.
+ */
+ protected abstract R retrieve(File dir) throws IOException;
+
+ public synchronized boolean removeValue(R run) {
+ Index copy = copy();
+ copy.byNumber.remove(getNumberOf(run));
+ BuildReference old = copy.byId.remove(getIdOf(run));
+ this.index = copy;
+
+ return unwrap(old)!=null;
+ }
+
+ /**
+ * Replaces all the current loaded Rs with the given ones.
+ */
+ public synchronized void reset(TreeMap builds) {
+ Index index = new Index();
+ for (R r : builds.values()) {
+ String id = getIdOf(r);
+ BuildReference ref = createReference(r);
+ index.byId.put(id,ref);
+ index.byNumber.put(getNumberOf(r),ref);
+ }
+
+ this.index = index;
+ }
+
+ @Override
+ public int hashCode() {
+ return System.identityHashCode(this);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return o==this;
+ }
+
+ /**
+ * Lists the actual data directory
+ */
+ protected abstract FilenameFilter createDirectoryFilter();
+
+ private static final Comparator COMPARATOR = new Comparator() {
+ public int compare(Comparable o1, Comparable o2) {
+ return -o1.compareTo(o2);
+ }
+ };
+
+ public enum Direction {
+ ASC, DESC, EXACT
+ }
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ private static final SortedMap EMPTY_SORTED_MAP = Collections.unmodifiableSortedMap(new TreeMap());
+
+ private static final Logger LOGGER = Logger.getLogger(AbstractLazyLoadRunMap.class.getName());
+}
diff --git a/core/src/main/java/jenkins/model/lazy/Boundary.java b/core/src/main/java/jenkins/model/lazy/Boundary.java
new file mode 100644
index 0000000000..00459523d2
--- /dev/null
+++ b/core/src/main/java/jenkins/model/lazy/Boundary.java
@@ -0,0 +1,63 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) 2012, CloudBees, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package jenkins.model.lazy;
+
+/**
+ * ceil/floor/lower/higher implementations
+ * that takes the return value of a binary search as an input.
+ *
+ *
+ * Consider a sorted array of int X={xi} and a binary search of p on it.
+ * this class provides likes of {@code ceil(X,p)} which is the smallest xi
+ * that still satisfies xi >= p.
+ *
+ * Simiarly, {@link #HIGHER} is the smallest xi
+ * that still satisfies xi > p.
+ *
+ * @author Kohsuke Kawaguchi
+ */
+enum Boundary {
+ LOWER(-1,-1),
+ HIGHER(1,0),
+ FLOOR(0,-1),
+ CEIL(0,0);
+
+ private final int offsetOfExactMatch, offsetOfInsertionPoint;
+
+ private Boundary(int offsetOfExactMatch, int offsetOfInsertionPoint) {
+ this.offsetOfExactMatch = offsetOfExactMatch;
+ this.offsetOfInsertionPoint = offsetOfInsertionPoint;
+ }
+
+ /**
+ * Computes the boundary value.
+ */
+ public int apply(int binarySearchOutput) {
+ int r = binarySearchOutput;
+ if (r>=0) return r+offsetOfExactMatch; // if we had some x_i==p
+
+ int ip = -(r+1);
+ return ip+offsetOfInsertionPoint;
+ }
+}
diff --git a/core/src/main/java/jenkins/model/lazy/BuildReference.java b/core/src/main/java/jenkins/model/lazy/BuildReference.java
new file mode 100644
index 0000000000..328e4c665d
--- /dev/null
+++ b/core/src/main/java/jenkins/model/lazy/BuildReference.java
@@ -0,0 +1,42 @@
+package jenkins.model.lazy;
+
+import java.lang.ref.SoftReference;
+
+/**
+ * {@link SoftReference} to a build object.
+ *
+ *
+ * To be able to re-retrieve the referent in case it is lost, this class
+ * remembers its ID (the job name is provided by the context because a {@link BuildReference}
+ * belongs to one and only {@link AbstractLazyLoadRunMap}.)
+ *
+ *
+ * We use this ID for equality/hashCode so that we can have a collection of {@link BuildReference}
+ * and find things in it.
+ *
+ * @author Kohsuke Kawaguchi
+ * @since 1.LAZYLOAD
+ */
+public final class BuildReference extends SoftReference {
+ final String id;
+
+ public BuildReference(String id, R referent) {
+ super(referent);
+ this.id = id;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ BuildReference that = (BuildReference) o;
+ return id.equals(that.id);
+
+ }
+
+ @Override
+ public int hashCode() {
+ return id.hashCode();
+ }
+}
diff --git a/core/src/main/java/jenkins/model/lazy/BuildReferenceMapAdapter.java b/core/src/main/java/jenkins/model/lazy/BuildReferenceMapAdapter.java
new file mode 100644
index 0000000000..50ddc5df4b
--- /dev/null
+++ b/core/src/main/java/jenkins/model/lazy/BuildReferenceMapAdapter.java
@@ -0,0 +1,355 @@
+package jenkins.model.lazy;
+
+import groovy.util.MapEntry;
+import hudson.util.AdaptedIterator;
+
+import javax.annotation.Nullable;
+import java.lang.reflect.Array;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+
+/**
+ * Take {@code SortedMap>} and make it look like {@code SortedMap}.
+ *
+ * When {@link BuildReference} lost the build object, we'll use {@link AbstractLazyLoadRunMap#getById(String)}
+ * to obtain one.
+ *
+ * @author Kohsuke Kawaguchi
+ */
+class BuildReferenceMapAdapter implements SortedMap {
+ private final AbstractLazyLoadRunMap loader;
+
+ private final SortedMap> core;
+
+ BuildReferenceMapAdapter(AbstractLazyLoadRunMap loader, SortedMap> core) {
+ this.loader = loader;
+ this.core = core;
+ }
+
+ private R unwrap(@Nullable BuildReference ref) {
+ if (ref==null) return null;
+
+ R v = ref.get();
+ if (v==null)
+ v = loader.getById(ref.id);
+ return v;
+ }
+
+ private BuildReference wrap(@Nullable R value) {
+ if (value==null) return null;
+ return loader.createReference(value);
+ }
+
+
+
+
+ public Comparator super Integer> comparator() {
+ return core.comparator();
+ }
+
+ public SortedMap subMap(Integer fromKey, Integer toKey) {
+ return new BuildReferenceMapAdapter(loader,core.subMap(fromKey, toKey));
+ }
+
+ public SortedMap headMap(Integer toKey) {
+ return new BuildReferenceMapAdapter(loader,core.headMap(toKey));
+ }
+
+ public SortedMap tailMap(Integer fromKey) {
+ return new BuildReferenceMapAdapter(loader,core.tailMap(fromKey));
+ }
+
+ public Integer firstKey() {
+ return core.firstKey();
+ }
+
+ public Integer lastKey() {
+ return core.lastKey();
+ }
+
+ public Set keySet() {
+ return core.keySet();
+ }
+
+ public Collection values() {
+ return new CollectionAdapter(core.values());
+ }
+
+ public Set> entrySet() {
+ return new SetAdapter(core.entrySet());
+ }
+
+ public int size() {
+ return core.size();
+ }
+
+ public boolean isEmpty() {
+ return core.isEmpty();
+ }
+
+ public boolean containsKey(Object key) {
+ return core.containsKey(key);
+ }
+
+ public boolean containsValue(Object value) {
+ return core.containsValue(value);
+ }
+
+ public R get(Object key) {
+ return unwrap(core.get(key));
+ }
+
+ public R put(Integer key, R value) {
+ return unwrap(core.put(key, wrap(value)));
+ }
+
+ public R remove(Object key) {
+ return unwrap(core.remove(key));
+ }
+
+ public void putAll(Map extends Integer, ? extends R> m) {
+ for (Entry extends Integer, ? extends R> e : m.entrySet())
+ put(e.getKey(), e.getValue());
+ }
+
+ public void clear() {
+ core.clear();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return core.equals(o);
+ }
+
+ @Override
+ public int hashCode() {
+ return core.hashCode();
+ }
+
+ private class CollectionAdapter implements Collection {
+ private final Collection> core;
+
+ private CollectionAdapter(Collection> core) {
+ this.core = core;
+ }
+
+ public int size() {
+ return core.size();
+ }
+
+ public boolean isEmpty() {
+ return core.isEmpty();
+ }
+
+ public boolean contains(Object o) {
+ // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
+ throw new UnsupportedOperationException();
+ }
+
+ public Iterator iterator() {
+ return new AdaptedIterator,R>(core.iterator()) {
+ protected R adapt(BuildReference ref) {
+ return unwrap(ref);
+ }
+ };
+ }
+
+ public Object[] toArray() {
+ return _unwrap(core.toArray());
+ }
+
+ public T[] toArray(T[] a) {
+ int size = size();
+ T[] r = a;
+ if (r.length>size)
+ r = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
+
+ Iterator itr = iterator();
+ int i=0;
+
+ while (itr.hasNext()) {
+ r[i++] = (T)itr.next();
+ }
+
+ return r;
+ }
+
+ public boolean add(R value) {
+ return core.add(wrap(value));
+ }
+
+ public boolean remove(Object o) {
+// return core.remove(o);
+ // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean containsAll(Collection> c) {
+ for (Object o : c) {
+ if (!contains(o))
+ return false;
+ }
+ return true;
+ }
+
+ public boolean addAll(Collection extends R> c) {
+ boolean b=false;
+ for (R r : c) {
+ b |= add(r);
+ }
+ return b;
+ }
+
+ public boolean removeAll(Collection> c) {
+ boolean b=false;
+ for (Object o : c) {
+ b|=remove(o);
+ }
+ return b;
+ }
+
+ public boolean retainAll(Collection> c) {
+ // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
+ throw new UnsupportedOperationException();
+ }
+
+ public void clear() {
+ core.clear();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return core.equals(o);
+ }
+
+ @Override
+ public int hashCode() {
+ return core.hashCode();
+ }
+
+ private Object[] _unwrap(Object[] r) {
+ for (int i=0; i) r[i]);
+ return r;
+ }
+ }
+
+ private class SetAdapter implements Set> {
+ private final Set>> core;
+
+ private SetAdapter(Set>> core) {
+ this.core = core;
+ }
+
+ public int size() {
+ return core.size();
+ }
+
+ public boolean isEmpty() {
+ return core.isEmpty();
+ }
+
+ public boolean contains(Object o) {
+ // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
+ throw new UnsupportedOperationException();
+ }
+
+ public Iterator> iterator() {
+ return new AdaptedIterator>,Entry>(core.iterator()) {
+ protected Entry adapt(Entry> e) {
+ return _unwrap(e);
+ }
+ };
+ }
+
+ public Object[] toArray() {
+ return _unwrap(core.toArray());
+ }
+
+ public T[] toArray(T[] a) {
+ int size = size();
+ T[] r = a;
+ if (r.length>size)
+ r = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
+
+ Iterator> itr = iterator();
+ int i=0;
+
+ while (itr.hasNext()) {
+ r[i++] = (T)itr.next();
+ }
+
+ return r;
+ }
+
+ public boolean add(Entry value) {
+ return core.add(_wrap(value));
+ }
+
+ public boolean remove(Object o) {
+// return core.remove(o);
+ // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean containsAll(Collection> c) {
+ for (Object o : c) {
+ if (!contains(o))
+ return false;
+ }
+ return true;
+ }
+
+ public boolean addAll(Collection extends Entry> c) {
+ boolean b=false;
+ for (Entry r : c) {
+ b |= add(r);
+ }
+ return b;
+ }
+
+ public boolean removeAll(Collection> c) {
+ boolean b=false;
+ for (Object o : c) {
+ b|=remove(o);
+ }
+ return b;
+ }
+
+ public boolean retainAll(Collection> c) {
+ // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
+ throw new UnsupportedOperationException();
+ }
+
+ public void clear() {
+ core.clear();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return core.equals(o);
+ }
+
+ @Override
+ public int hashCode() {
+ return core.hashCode();
+ }
+
+ private Entry> _wrap(Entry e) {
+ return new MapEntry(e.getKey(),wrap(e.getValue()));
+ }
+ private Entry _unwrap(Entry> e) {
+ return new MapEntry(e.getKey(),unwrap(e.getValue()));
+ }
+
+ private Object[] _unwrap(Object[] r) {
+ for (int i=0; i} that uses {@code int} for storage.
+ *
+ * Plus a number of binary-search related methods that assume the array is sorted in the ascending order.
+ *
+ * @author Kohsuke Kawaguchi
+ */
+class SortedIntList extends AbstractList {
+ private int[] data;
+ private int size;
+
+ public SortedIntList(int capacity) {
+ this.data = new int[capacity];
+ this.size = 0;
+ }
+
+ /**
+ * Internal copy constructor.
+ */
+ public SortedIntList(SortedIntList rhs) {
+ this.data = Arrays.copyOf(rhs.data,rhs.data.length+8);
+ this.size = rhs.size;
+ }
+
+ /**
+ * Binary search to find the position of the given string.
+ *
+ * @return
+ * -(insertionPoint+1) if the exact string isn't found.
+ * That is, -1 means the probe would be inserted at the very beginning.
+ */
+ public int find(int probe) {
+ return Arrays.binarySearch(data, 0, size, probe);
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return o instanceof Integer && contains(((Integer)o).intValue());
+ }
+
+ public boolean contains(int i) {
+ return find(i)>=0;
+ }
+
+ @Override
+ public Integer get(int index) {
+ if (size<=index) throw new IndexOutOfBoundsException();
+ return data[index];
+ }
+
+ @Override
+ public int size() {
+ return size;
+ }
+
+ @Override
+ public boolean add(Integer i) {
+ return add(i.intValue());
+ }
+
+ public boolean add(int i) {
+ ensureCapacity(size+1);
+ data[size++] = i;
+ return true;
+ }
+
+ private void ensureCapacity(int i) {
+ if (data.length