Make ProjectCache#get return an Optional<ProjectState>
This refactoring forces callers to think about how to handle projects that do not exist explicitly. As a general guidance, API handlers will want to throw a BadRequestException and components in the inner works of Gerrit where content should have been pre-validated should throw an IllegalStateException. But overall, it's a case-by-case decision. In a follow-up commit, we will remove #checkedGet and move callers to get instead. Change-Id: I95bbc22ad5f3279e6f40f1d05c8d7235d601e32d
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.server.query.change;
|
||||
|
||||
import static com.google.gerrit.server.project.ProjectCache.illegalState;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
@@ -915,7 +916,9 @@ public class ChangeData {
|
||||
return false;
|
||||
}
|
||||
String mergeStrategy =
|
||||
mergeUtilFactory.create(projectCache.get(project())).mergeStrategyName();
|
||||
mergeUtilFactory
|
||||
.create(projectCache.get(project()).orElseThrow(illegalState(project())))
|
||||
.mergeStrategyName();
|
||||
mergeable =
|
||||
mergeabilityCache.get(ps.commitId(), ref, str.type, mergeStrategy, c.getDest(), repo);
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -16,6 +16,7 @@ package com.google.gerrit.server.query.change;
|
||||
|
||||
import static com.google.common.base.MoreObjects.firstNonNull;
|
||||
import static com.google.common.flogger.LazyArgs.lazy;
|
||||
import static com.google.gerrit.server.project.ProjectCache.noSuchProject;
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
@@ -218,10 +219,7 @@ public class ConflictsPredicate {
|
||||
|
||||
ProjectState getProjectState() throws NoSuchProjectException {
|
||||
if (projectState == null) {
|
||||
projectState = projectCache.get(cd.project());
|
||||
if (projectState == null) {
|
||||
throw new NoSuchProjectException(cd.project());
|
||||
}
|
||||
projectState = projectCache.get(cd.project()).orElseThrow(noSuchProject(cd.project()));
|
||||
}
|
||||
return projectState;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.google.gerrit.server.permissions.PermissionBackendException;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
import com.google.gerrit.server.project.ProjectState;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class EqualsLabelPredicate extends ChangeIndexPredicate {
|
||||
protected final ProjectCache projectCache;
|
||||
@@ -60,14 +61,14 @@ public class EqualsLabelPredicate extends ChangeIndexPredicate {
|
||||
return false;
|
||||
}
|
||||
|
||||
ProjectState project = projectCache.get(c.getDest().project());
|
||||
if (project == null) {
|
||||
Optional<ProjectState> project = projectCache.get(c.getDest().project());
|
||||
if (!project.isPresent()) {
|
||||
// The project has disappeared.
|
||||
//
|
||||
return false;
|
||||
}
|
||||
|
||||
LabelType labelType = type(project.getLabelTypes(), label);
|
||||
LabelType labelType = type(project.get().getLabelTypes(), label);
|
||||
if (labelType == null) {
|
||||
return false; // Label is not defined by this project.
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.google.gerrit.server.project.ProjectState;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ParentProjectPredicate extends OrPredicate<ChangeData> {
|
||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||
@@ -40,15 +41,15 @@ public class ParentProjectPredicate extends OrPredicate<ChangeData> {
|
||||
|
||||
protected static List<Predicate<ChangeData>> predicates(
|
||||
ProjectCache projectCache, ChildProjects childProjects, String value) {
|
||||
ProjectState projectState = projectCache.get(Project.nameKey(value));
|
||||
if (projectState == null) {
|
||||
Optional<ProjectState> projectState = projectCache.get(Project.nameKey(value));
|
||||
if (!projectState.isPresent()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Predicate<ChangeData>> r = new ArrayList<>();
|
||||
r.add(new ProjectPredicate(projectState.getName()));
|
||||
r.add(new ProjectPredicate(projectState.get().getName()));
|
||||
try {
|
||||
for (ProjectInfo p : childProjects.list(projectState.getNameKey())) {
|
||||
for (ProjectInfo p : childProjects.list(projectState.get().getNameKey())) {
|
||||
r.add(new ProjectPredicate(p.name));
|
||||
}
|
||||
} catch (PermissionBackendException e) {
|
||||
|
||||
Reference in New Issue
Block a user