// Copyright 2015 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.skyframe; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.google.devtools.build.skyframe.ValueOrExceptionUtils.BottomException; import java.util.Collections; import java.util.Map; import java.util.Set; import javax.annotation.Nullable; /** * Basic implementation of {@link SkyFunction.Environment} in which all convenience methods delegate * to a single abstract method. */ @VisibleForTesting public abstract class AbstractSkyFunctionEnvironment implements SkyFunction.Environment { protected boolean valuesMissing = false; private ValueOrException getValueOrException(SkyKey depKey, Class exceptionClass) { return ValueOrExceptionUtils.downconvert( getValueOrException(depKey, exceptionClass, BottomException.class), exceptionClass); } private ValueOrException2 getValueOrException(SkyKey depKey, Class exceptionClass1, Class exceptionClass2) { return ValueOrExceptionUtils.downconvert(getValueOrException(depKey, exceptionClass1, exceptionClass2, BottomException.class), exceptionClass1, exceptionClass2); } private ValueOrException3 getValueOrException( SkyKey depKey, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3) { return ValueOrExceptionUtils.downconvert( getValueOrException(depKey, exceptionClass1, exceptionClass2, exceptionClass3, BottomException.class), exceptionClass1, exceptionClass2, exceptionClass3); } private ValueOrException4 getValueOrException( SkyKey depKey, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3, Class exceptionClass4) { return ValueOrExceptionUtils.downconvert( getValueOrException(depKey, exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4, BottomException.class), exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4); } private ValueOrException5 getValueOrException( SkyKey depKey, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3, Class exceptionClass4, Class exceptionClass5) { return getValueOrExceptions( ImmutableSet.of(depKey), exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4, exceptionClass5).get(depKey); } private Map> getValueOrExceptions( Set depKeys, final Class exceptionClass1, final Class exceptionClass2, final Class exceptionClass3, final Class exceptionClass4, final Class exceptionClass5) { SkyFunctionException.validateExceptionType(exceptionClass1); SkyFunctionException.validateExceptionType(exceptionClass2); SkyFunctionException.validateExceptionType(exceptionClass3); SkyFunctionException.validateExceptionType(exceptionClass4); SkyFunctionException.validateExceptionType(exceptionClass5); Map valueOrExceptions = getValueOrUntypedExceptions(depKeys); for (ValueOrUntypedException voe : valueOrExceptions.values()) { SkyValue value = voe.getValue(); if (value == null) { Exception e = voe.getException(); if (e == null || (!exceptionClass1.isInstance(e) && !exceptionClass2.isInstance(e) && !exceptionClass3.isInstance(e) && !exceptionClass4.isInstance(e) && !exceptionClass5.isInstance(e))) { valuesMissing = true; break; } } } // We transform the values directly to avoid the transient memory cost of creating a new map. return Maps.transformValues( valueOrExceptions, new Function>() { @Override public ValueOrException5 apply(ValueOrUntypedException voe) { SkyValue value = voe.getValue(); if (value != null) { return ValueOrExceptionUtils.ofValue(value); } Exception e = voe.getException(); if (e != null) { if (exceptionClass1.isInstance(e)) { return ValueOrExceptionUtils.ofExn1(exceptionClass1.cast(e)); } if (exceptionClass2.isInstance(e)) { return ValueOrExceptionUtils.ofExn2(exceptionClass2.cast(e)); } if (exceptionClass3.isInstance(e)) { return ValueOrExceptionUtils.ofExn3(exceptionClass3.cast(e)); } if (exceptionClass4.isInstance(e)) { return ValueOrExceptionUtils.ofExn4(exceptionClass4.cast(e)); } if (exceptionClass5.isInstance(e)) { return ValueOrExceptionUtils.ofExn5(exceptionClass5.cast(e)); } } return ValueOrExceptionUtils.ofNullValue(); } }); } /** Implementations should set {@link #valuesMissing} as necessary. */ protected abstract Map getValueOrUntypedExceptions( Set depKeys); @Override @Nullable public SkyValue getValue(SkyKey depKey) { try { return getValueOrThrow(depKey, BottomException.class); } catch (BottomException e) { throw new IllegalStateException("shouldn't reach here"); } } @Override @Nullable public SkyValue getValueOrThrow(SkyKey depKey, Class exceptionClass) throws E { return getValueOrException(depKey, exceptionClass).get(); } @Override @Nullable public SkyValue getValueOrThrow( SkyKey depKey, Class exceptionClass1, Class exceptionClass2) throws E1, E2 { return getValueOrException(depKey, exceptionClass1, exceptionClass2).get(); } @Override @Nullable public SkyValue getValueOrThrow( SkyKey depKey, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3) throws E1, E2, E3 { return getValueOrException(depKey, exceptionClass1, exceptionClass2, exceptionClass3).get(); } @Override public SkyValue getValueOrThrow( SkyKey depKey, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3, Class exceptionClass4) throws E1, E2, E3, E4 { return getValueOrException( depKey, exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4).get(); } @Override public SkyValue getValueOrThrow( SkyKey depKey, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3, Class exceptionClass4, Class exceptionClass5 ) throws E1, E2, E3, E4, E5 { return getValueOrException( depKey, exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4, exceptionClass5).get(); } @Override public Map getValues(Iterable depKeys) { return Maps.transformValues(getValuesOrThrow(depKeys, BottomException.class), GET_VALUE_FROM_VOE); } @Override public Map> getValuesOrThrow( Iterable depKeys, Class exceptionClass) { return Maps.transformValues( getValuesOrThrow(depKeys, exceptionClass, BottomException.class), makeSafeDowncastToVOEFunction(exceptionClass)); } @Override public Map> getValuesOrThrow( Iterable depKeys, Class exceptionClass1, Class exceptionClass2) { return Maps.transformValues( getValuesOrThrow(depKeys, exceptionClass1, exceptionClass2, BottomException.class), makeSafeDowncastToVOE2Function(exceptionClass1, exceptionClass2)); } @Override public Map> getValuesOrThrow( Iterable depKeys, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3) { return Maps.transformValues( getValuesOrThrow(depKeys, exceptionClass1, exceptionClass2, exceptionClass3, BottomException.class), makeSafeDowncastToVOE3Function(exceptionClass1, exceptionClass2, exceptionClass3)); } @Override public Map> getValuesOrThrow( Iterable depKeys, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3, Class exceptionClass4) { return Maps.transformValues( getValuesOrThrow(depKeys, exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4, BottomException.class), makeSafeDowncastToVOE4Function(exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4)); } @Override public Map> getValuesOrThrow( Iterable depKeys, Class exceptionClass1, Class exceptionClass2, Class exceptionClass3, Class exceptionClass4, Class exceptionClass5) { Set keys = ImmutableSet.copyOf(depKeys); Map> result = getValueOrExceptions(keys, exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4, exceptionClass5); return Collections.unmodifiableMap(result); } @Override public boolean valuesMissing() { return valuesMissing; } private static final Function, SkyValue> GET_VALUE_FROM_VOE = new Function, SkyValue>() { @Override public SkyValue apply(ValueOrException voe) { return ValueOrExceptionUtils.downconvert(voe); } }; private static Function, ValueOrException> makeSafeDowncastToVOEFunction(final Class exceptionClass) { return new Function, ValueOrException>() { @Override public ValueOrException apply(ValueOrException2 voe) { return ValueOrExceptionUtils.downconvert(voe, exceptionClass); } }; } private static Function, ValueOrException2> makeSafeDowncastToVOE2Function( final Class exceptionClass1, final Class exceptionClass2) { return new Function, ValueOrException2>() { @Override public ValueOrException2 apply(ValueOrException3 voe) { return ValueOrExceptionUtils.downconvert(voe, exceptionClass1, exceptionClass2); } }; } private static Function, ValueOrException3> makeSafeDowncastToVOE3Function( final Class exceptionClass1, final Class exceptionClass2, final Class exceptionClass3) { return new Function, ValueOrException3>() { @Override public ValueOrException3 apply( ValueOrException4 voe) { return ValueOrExceptionUtils.downconvert( voe, exceptionClass1, exceptionClass2, exceptionClass3); } }; } private static Function, ValueOrException4> makeSafeDowncastToVOE4Function( final Class exceptionClass1, final Class exceptionClass2, final Class exceptionClass3, final Class exceptionClass4) { return new Function, ValueOrException4>() { @Override public ValueOrException4 apply( ValueOrException5 voe) { return ValueOrExceptionUtils.downconvert( voe, exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4); } }; } }