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:
@@ -15,17 +15,18 @@
|
|||||||
package com.google.gerrit.index;
|
package com.google.gerrit.index;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
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.MoreObjects;
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
import com.google.common.collect.FluentIterable;
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
|
import com.google.gerrit.exceptions.StorageException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/** Specific version of a secondary index schema. */
|
/** Specific version of a secondary index schema. */
|
||||||
@@ -163,24 +164,18 @@ public class Schema<T> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private Values<T> fieldValues(T obj, FieldDef<T, ?> f) {
|
||||||
* Build all fields in the schema from an input object.
|
|
||||||
*
|
|
||||||
* <p>Null values are omitted, as are fields which cause errors, which are logged.
|
|
||||||
*
|
|
||||||
* @param obj input object.
|
|
||||||
* @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;
|
Object v;
|
||||||
try {
|
try {
|
||||||
v = f.get(obj);
|
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) {
|
} catch (RuntimeException e) {
|
||||||
logger.atSevere().withCause(e).log(
|
logger.atSevere().withCause(e).log("error getting field %s of %s", f.getName(), obj);
|
||||||
"error getting field %s of %s", f.getName(), obj);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (v == null) {
|
if (v == null) {
|
||||||
@@ -190,8 +185,25 @@ public class Schema<T> {
|
|||||||
} else {
|
} else {
|
||||||
return new Values<>(f, Collections.singleton(v));
|
return new Values<>(f, Collections.singleton(v));
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.filter(Predicates.notNull());
|
|
||||||
|
/**
|
||||||
|
* Build all fields in the schema from an input object.
|
||||||
|
*
|
||||||
|
* <p>Null values are omitted, as are fields which cause errors, which are logged.
|
||||||
|
*
|
||||||
|
* @param obj input object.
|
||||||
|
* @return all non-null field values from the object.
|
||||||
|
*/
|
||||||
|
public final Iterable<Values<T>> buildFields(T obj) {
|
||||||
|
try {
|
||||||
|
return fields.values().stream()
|
||||||
|
.map(f -> fieldValues(obj, f))
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(toImmutableList());
|
||||||
|
} catch (StorageException e) {
|
||||||
|
return ImmutableList.of();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package com.google.gerrit.server.project;
|
|||||||
import static java.util.stream.Collectors.toMap;
|
import static java.util.stream.Collectors.toMap;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
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.LabelType;
|
||||||
import com.google.gerrit.common.data.LabelValue;
|
import com.google.gerrit.common.data.LabelValue;
|
||||||
import com.google.gerrit.extensions.common.LabelTypeInfo;
|
import com.google.gerrit.extensions.common.LabelTypeInfo;
|
||||||
@@ -33,6 +34,7 @@ import java.util.List;
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class ProjectJson {
|
public class ProjectJson {
|
||||||
|
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||||
|
|
||||||
private final AllProjectsName allProjects;
|
private final AllProjectsName allProjects;
|
||||||
private final WebLinks webLinks;
|
private final WebLinks webLinks;
|
||||||
@@ -49,7 +51,17 @@ public class ProjectJson {
|
|||||||
for (LabelType t : projectState.getLabelTypes().getLabelTypes()) {
|
for (LabelType t : projectState.getLabelTypes().getLabelTypes()) {
|
||||||
LabelTypeInfo labelInfo = new LabelTypeInfo();
|
LabelTypeInfo labelInfo = new LabelTypeInfo();
|
||||||
labelInfo.values =
|
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();
|
labelInfo.defaultValue = t.getDefaultValue();
|
||||||
info.labels.put(t.getName(), labelInfo);
|
info.labels.put(t.getName(), labelInfo);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user