Skip to content

Commit

Permalink
Reduce memory usage
Browse files Browse the repository at this point in the history
Taking the full name of project from Jenkins leads to string duplication
problem as well as involves recursion. So, it's better to limit the
number of requests to Jenkins Core API as much as possible.
  • Loading branch information
Jimilian committed Dec 5, 2017
1 parent 7a5f54b commit 7d9ef5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
Expand Up @@ -341,8 +341,9 @@ public synchronized boolean isTriggered(@Nonnull GerritTriggeredEvent event, @No
if (pb == null) {
return false;
} else {
String fullName = project.getFullName();
for (Entry entry : pb.getEntries()) {
if (entry.isProject(project)) {
if (entry.isProject(fullName)) {
return true;
}
}
Expand All @@ -357,13 +358,14 @@ public synchronized boolean isTriggered(@Nonnull GerritTriggeredEvent event, @No
* @param project the project.
* @return true if so.
*/
public synchronized boolean isBuilding(GerritTriggeredEvent event, Job project) {
public synchronized boolean isBuilding(GerritTriggeredEvent event, @Nonnull Job project) {
MemoryImprint pb = memory.get(event);
if (pb == null) {
return false;
} else {
String fullName = project.getFullName();
for (Entry entry : pb.getEntries()) {
if (entry.isProject(project)) {
if (entry.isProject(fullName)) {
if (entry.getBuild() != null) {
return !entry.isBuildCompleted();
} else {
Expand Down Expand Up @@ -908,12 +910,14 @@ public Job getProject() {
@CheckForNull
@WithBridgeMethods(AbstractBuild.class)
public Run getBuild() {
Job p = getProject();
if (p != null && build != null) {
return p.getBuild(build);
} else {
return null;
if (build != null && project != null) {
Job p = getProject();
if (p != null) {
return p.getBuild(build);
}
}

return null;
}

/**
Expand Down Expand Up @@ -1057,15 +1061,15 @@ public String toString() {
* If the provided project is the same as this entry is referencing.
* It does so by checking the fullName for equality.
*
* @param other the other project to check
* @param otherName the other project name to check
* @return true if so.
* @see #getProject()
*/
public boolean isProject(Job other) {
if (this.project != null && other != null) {
return this.project.equals(other.getFullName());
public boolean isProject(String otherName) {
if (this.project != null && otherName != null) {
return this.project.equals(otherName);
} else {
return this.project == null && other == null;
return this.project == null && otherName == null;
}
}
}
Expand Down
Expand Up @@ -188,8 +188,9 @@ void addProject(Job project) {
* @param build the build.
*/
void setBuild(Run build) {
String fullName = build.getParent().getFullName();
for (TriggeredItemEntity entity : builds) {
if (entity.equals(build.getParent())) {
if (entity.equals(fullName)) {
entity.setBuild(build);
}
}
Expand Down
Expand Up @@ -206,8 +206,9 @@ public synchronized boolean hasOthers() {
* @return the other object if there is one, null if there is none.
*/
private synchronized TriggeredItemEntity findOtherBuild(Run build) {
String parentName = build.getParent().getFullName();
for (TriggeredItemEntity other : others) {
if (other.equals(build)) {
if (other.isSameBuild(build.getNumber(), parentName)) {
return other;
}
}
Expand All @@ -221,8 +222,9 @@ private synchronized TriggeredItemEntity findOtherBuild(Run build) {
* @return the other object, or null if none.
*/
private synchronized TriggeredItemEntity findOtherProject(Job project) {
String fullName = project.getFullName();
for (TriggeredItemEntity other : others) {
if (other.equals(project)) {
if (other.equals(fullName)) {
return other;
}
}
Expand Down
Expand Up @@ -201,25 +201,24 @@ public boolean equals(Object obj) {
}

/**
* If this object represents the same build as aBuild.
* @param aBuild the build.
* If this object represents the same build as specified by parameters.
* @param otherBuildNumber build number
* @param otherParentName project full name
* @return true if it is so.
*/
public boolean equals(Run aBuild) {
if (this.buildNumber != null) {
return projectId.equals(aBuild.getParent().getFullName())
&& this.buildNumber.equals(aBuild.getNumber());
}
return false;
public boolean isSameBuild(int otherBuildNumber, String otherParentName) {
return this.buildNumber != null
&& this.buildNumber == otherBuildNumber
&& projectId.equals(otherParentName);
}

/**
* If this object represents the same project as aProject.
* @param aProject the project to compare.
* @param aProjectFullname the project to compare.
* @return true if it is so.
*/
public boolean equals(Job aProject) {
return projectId.equals(aProject.getFullName());
public boolean equals(String aProjectFullname) {
return projectId.equals(aProjectFullname);
}

@Override
Expand Down

0 comments on commit 7d9ef5b

Please sign in to comment.