Merge changes If23916d2,I70110e99 into stable-2.16

* changes:
  Schema: Show only a single log for inexistent commits
  Schema: Refactor lambda in buildFields to a separate function
This commit is contained in:
David Pursehouse
2020-06-13 02:34:29 +00:00
committed by Gerrit Code Review

View File

@@ -15,11 +15,9 @@
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.Function;
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;
@@ -28,6 +26,7 @@ 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. */
@@ -165,6 +164,30 @@ public class Schema<T> {
return true; return true;
} }
private Values<T> fieldValues(T obj, FieldDef<T, ?> f) {
Object v;
try {
v = f.get(obj);
} catch (OrmException e) {
// OrmException 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 new RuntimeException(
e); // work around throwing checked exceptions from methods used in Streams
} 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. * Build all fields in the schema from an input object.
* *
@@ -174,29 +197,17 @@ public class Schema<T> {
* @return all non-null field values from the object. * @return all non-null field values from the object.
*/ */
public final Iterable<Values<T>> buildFields(T obj) { public final Iterable<Values<T>> buildFields(T obj) {
return FluentIterable.from(fields.values()) try {
.transform( return fields.values().stream()
new Function<FieldDef<T, ?>, Values<T>>() { .map(f -> fieldValues(obj, f))
@Override .filter(Objects::nonNull)
public Values<T> apply(FieldDef<T, ?> f) { .collect(toImmutableList());
Object v; } catch (RuntimeException e) {
try { if (e.getCause().getClass().equals(OrmException.class)) {
v = f.get(obj); return ImmutableList.of();
} catch (OrmException e) { }
logger.atSevere().withCause(e).log( throw e;
"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());
} }
@Override @Override