Merge branch 'stable-2.16' into stable-3.0

* stable-2.16:
  Schema: Show only a single log for inexistent commits
  Schema: Refactor lambda in buildFields to a separate function
  ProjectJson: Use merge function for label value rendering

Change-Id: Ib33f890cfc254350f273b9192a755dd80caf0915
This commit is contained in:
Orgad Shaneh
2020-06-14 16:33:49 +03:00
committed by David Pursehouse
2 changed files with 47 additions and 23 deletions

View File

@@ -15,17 +15,18 @@
package com.google.gerrit.index;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.exceptions.StorageException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/** Specific version of a secondary index schema. */
@@ -163,6 +164,29 @@ public class Schema<T> {
return true;
}
private Values<T> fieldValues(T obj, FieldDef<T, ?> f) {
Object v;
try {
v = f.get(obj);
} catch (StorageException e) {
// StorageException is thrown when the object is not found. On this case,
// it is pointless to make further attempts for each field, so propagate
// the exception to return an empty list.
logger.atSevere().withCause(e).log("error getting field %s of %s", f.getName(), obj);
throw e;
} catch (RuntimeException e) {
logger.atSevere().withCause(e).log("error getting field %s of %s", f.getName(), obj);
return null;
}
if (v == null) {
return null;
} else if (f.isRepeatable()) {
return new Values<>(f, (Iterable<?>) v);
} else {
return new Values<>(f, Collections.singleton(v));
}
}
/**
* Build all fields in the schema from an input object.
*
@@ -172,26 +196,14 @@ public class Schema<T> {
* @return all non-null field values from the object.
*/
public final Iterable<Values<T>> buildFields(T obj) {
return FluentIterable.from(fields.values())
.transform(
f -> {
Object v;
try {
v = f.get(obj);
} catch (RuntimeException e) {
logger.atSevere().withCause(e).log(
"error getting field %s of %s", f.getName(), obj);
return null;
}
if (v == null) {
return null;
} else if (f.isRepeatable()) {
return new Values<>(f, (Iterable<?>) v);
} else {
return new Values<>(f, Collections.singleton(v));
}
})
.filter(Predicates.notNull());
try {
return fields.values().stream()
.map(f -> fieldValues(obj, f))
.filter(Objects::nonNull)
.collect(toImmutableList());
} catch (StorageException e) {
return ImmutableList.of();
}
}
@Override

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.project;
import static java.util.stream.Collectors.toMap;
import com.google.common.base.Strings;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.LabelValue;
import com.google.gerrit.extensions.common.LabelTypeInfo;
@@ -33,6 +34,7 @@ import java.util.List;
@Singleton
public class ProjectJson {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final AllProjectsName allProjects;
private final WebLinks webLinks;
@@ -49,7 +51,17 @@ public class ProjectJson {
for (LabelType t : projectState.getLabelTypes().getLabelTypes()) {
LabelTypeInfo labelInfo = new LabelTypeInfo();
labelInfo.values =
t.getValues().stream().collect(toMap(LabelValue::formatValue, LabelValue::getText));
t.getValues().stream()
.collect(
toMap(
LabelValue::formatValue,
LabelValue::getText,
(v1, v2) -> {
logger.atSevere().log(
"Duplicate values for project: %s, label: %s found: '%s':'%s'",
projectState.getName(), t.getName(), v1, v2);
return v1;
}));
labelInfo.defaultValue = t.getDefaultValue();
info.labels.put(t.getName(), labelInfo);
}