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:
		@@ -15,11 +15,9 @@
 | 
			
		||||
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.Function;
 | 
			
		||||
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;
 | 
			
		||||
@@ -28,6 +26,7 @@ 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. */
 | 
			
		||||
@@ -165,26 +164,19 @@ public class Schema<T> {
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * 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(
 | 
			
		||||
            new Function<FieldDef<T, ?>, Values<T>>() {
 | 
			
		||||
              @Override
 | 
			
		||||
              public Values<T> apply(FieldDef<T, ?> f) {
 | 
			
		||||
  private Values<T> fieldValues(T obj, FieldDef<T, ?> f) {
 | 
			
		||||
    Object v;
 | 
			
		||||
    try {
 | 
			
		||||
      v = f.get(obj);
 | 
			
		||||
    } catch (OrmException e) {
 | 
			
		||||
                  logger.atSevere().withCause(e).log(
 | 
			
		||||
                      "error getting field %s of %s", f.getName(), obj);
 | 
			
		||||
      // 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) {
 | 
			
		||||
@@ -195,8 +187,27 @@ public class Schema<T> {
 | 
			
		||||
      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 (RuntimeException e) {
 | 
			
		||||
      if (e.getCause().getClass().equals(OrmException.class)) {
 | 
			
		||||
        return ImmutableList.of();
 | 
			
		||||
      }
 | 
			
		||||
      throw e;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user