aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/skyframe/AbstractSkyFunctionEnvironment.java
blob: 7b8de1a47cbfaedec149a0b9ad9c1556b236573e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2015 Google Inc. 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.ImmutableMap;
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 <E extends Exception> ValueOrException<E> getValueOrException(SkyKey depKey,
      Class<E> exceptionClass) {
    return ValueOrExceptionUtils.downconvert(
        getValueOrException(depKey, exceptionClass, BottomException.class), exceptionClass);
  }

  private <E1 extends Exception, E2 extends Exception> ValueOrException2<E1, E2>
  getValueOrException(SkyKey depKey, Class<E1> exceptionClass1, Class<E2> exceptionClass2) {
    return ValueOrExceptionUtils.downconvert(getValueOrException(depKey, exceptionClass1,
        exceptionClass2, BottomException.class), exceptionClass1, exceptionClass2);
  }

  private <E1 extends Exception, E2 extends Exception, E3 extends Exception>
  ValueOrException3<E1, E2, E3> getValueOrException(SkyKey depKey, Class<E1> exceptionClass1,
      Class<E2> exceptionClass2, Class<E3> exceptionClass3) {
    return ValueOrExceptionUtils.downconvert(getValueOrException(depKey, exceptionClass1,
            exceptionClass2, exceptionClass3, BottomException.class), exceptionClass1,
        exceptionClass2, exceptionClass3);
  }

  private <E1 extends Exception, E2 extends Exception, E3 extends Exception,
      E4 extends Exception> ValueOrException4<E1, E2, E3, E4> getValueOrException(SkyKey depKey,
      Class<E1> exceptionClass1, Class<E2> exceptionClass2, Class<E3> exceptionClass3,
      Class<E4> exceptionClass4) {
    return getValueOrExceptions(ImmutableSet.of(depKey), exceptionClass1, exceptionClass2,
        exceptionClass3, exceptionClass4).get(depKey);
  }

  private <E1 extends Exception, E2 extends Exception, E3 extends Exception,
      E4 extends Exception> Map<SkyKey, ValueOrException4<E1, E2, E3, E4>> getValueOrExceptions(
      Set<SkyKey> depKeys, Class<E1> exceptionClass1, Class<E2> exceptionClass2,
      Class<E3> exceptionClass3, Class<E4> exceptionClass4) {
    SkyFunctionException.validateExceptionType(exceptionClass1);
    SkyFunctionException.validateExceptionType(exceptionClass2);
    SkyFunctionException.validateExceptionType(exceptionClass3);
    SkyFunctionException.validateExceptionType(exceptionClass4);
    Map<SkyKey, ValueOrUntypedException> valueOrExceptions =
        getValueOrUntypedExceptions(depKeys);
    ImmutableMap.Builder<SkyKey, ValueOrException4<E1, E2, E3, E4>> builder =
        ImmutableMap.builder();
    for (SkyKey depKey : depKeys) {
      ValueOrUntypedException voe = valueOrExceptions.get(depKey);
      SkyValue value = voe.getValue();
      if (value != null) {
        builder.put(depKey, ValueOrExceptionUtils.<E1, E2, E3, E4>ofValue(value));
        continue;
      }
      Exception e = voe.getException();
      if (e != null) {
        if (exceptionClass1.isInstance(e)) {
          builder.put(depKey, ValueOrExceptionUtils.<E1, E2, E3, E4>ofExn1(
              exceptionClass1.cast(e)));
          continue;
        }
        if (exceptionClass2.isInstance(e)) {
          builder.put(depKey, ValueOrExceptionUtils.<E1, E2, E3, E4>ofExn2(
              exceptionClass2.cast(e)));
          continue;
        }
        if (exceptionClass3.isInstance(e)) {
          builder.put(depKey, ValueOrExceptionUtils.<E1, E2, E3, E4>ofExn3(
              exceptionClass3.cast(e)));
          continue;
        }
        if (exceptionClass4.isInstance(e)) {
          builder.put(depKey, ValueOrExceptionUtils.<E1, E2, E3, E4>ofExn4(
              exceptionClass4.cast(e)));
          continue;
        }
      }
      valuesMissing = true;
      builder.put(depKey, ValueOrExceptionUtils.<E1, E2, E3, E4>ofNullValue());
    }
    return builder.build();
  }

  /** Implementations should set {@link #valuesMissing} as necessary. */
  protected abstract Map<SkyKey, ValueOrUntypedException> getValueOrUntypedExceptions(
      Set<SkyKey> 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 <E extends Exception> SkyValue getValueOrThrow(SkyKey depKey, Class<E> exceptionClass)
      throws E {
    return getValueOrException(depKey, exceptionClass).get();
  }

  @Override
  @Nullable
  public <E1 extends Exception, E2 extends Exception> SkyValue getValueOrThrow(SkyKey depKey,
      Class<E1> exceptionClass1, Class<E2> exceptionClass2) throws E1, E2 {
    return getValueOrException(depKey, exceptionClass1, exceptionClass2).get();
  }

  @Override
  @Nullable
  public <E1 extends Exception, E2 extends Exception,
      E3 extends Exception> SkyValue getValueOrThrow(SkyKey depKey, Class<E1> exceptionClass1,
      Class<E2> exceptionClass2, Class<E3> exceptionClass3) throws E1, E2, E3 {
    return getValueOrException(depKey, exceptionClass1, exceptionClass2, exceptionClass3).get();
  }

  @Override
  public <E1 extends Exception, E2 extends Exception, E3 extends Exception,
      E4 extends Exception> SkyValue getValueOrThrow(SkyKey depKey, Class<E1> exceptionClass1,
      Class<E2> exceptionClass2, Class<E3> exceptionClass3, Class<E4> exceptionClass4) throws E1,
      E2, E3, E4 {
    return getValueOrException(depKey, exceptionClass1, exceptionClass2, exceptionClass3,
        exceptionClass4).get();
  }

  @Override
  public Map<SkyKey, SkyValue> getValues(Iterable<SkyKey> depKeys) {
    return Maps.transformValues(getValuesOrThrow(depKeys, BottomException.class),
        GET_VALUE_FROM_VOE);
  }

  @Override
  public <E extends Exception> Map<SkyKey, ValueOrException<E>> getValuesOrThrow(
      Iterable<SkyKey> depKeys, Class<E> exceptionClass) {
    return Maps.transformValues(getValuesOrThrow(depKeys, exceptionClass, BottomException.class),
        makeSafeDowncastToVOEFunction(exceptionClass));
  }

  @Override
  public <E1 extends Exception,
      E2 extends Exception> Map<SkyKey, ValueOrException2<E1, E2>> getValuesOrThrow(
      Iterable<SkyKey> depKeys, Class<E1> exceptionClass1, Class<E2> exceptionClass2) {
    return Maps.transformValues(getValuesOrThrow(depKeys, exceptionClass1, exceptionClass2,
        BottomException.class), makeSafeDowncastToVOE2Function(exceptionClass1,
        exceptionClass2));
  }

  @Override
  public <E1 extends Exception, E2 extends Exception, E3 extends Exception> Map<SkyKey,
      ValueOrException3<E1, E2, E3>> getValuesOrThrow(Iterable<SkyKey> depKeys,
      Class<E1> exceptionClass1, Class<E2> exceptionClass2, Class<E3> exceptionClass3) {
    return Maps.transformValues(getValuesOrThrow(depKeys, exceptionClass1, exceptionClass2,
        exceptionClass3, BottomException.class), makeSafeDowncastToVOE3Function(exceptionClass1,
        exceptionClass2, exceptionClass3));
  }

  @Override
  public <E1 extends Exception, E2 extends Exception, E3 extends Exception,
      E4 extends Exception> Map<SkyKey, ValueOrException4<E1, E2, E3, E4>> getValuesOrThrow(
      Iterable<SkyKey> depKeys, Class<E1> exceptionClass1, Class<E2> exceptionClass2,
      Class<E3> exceptionClass3, Class<E4> exceptionClass4) {
    Set<SkyKey> keys = ImmutableSet.copyOf(depKeys);
    Map<SkyKey, ValueOrException4<E1, E2, E3, E4>> result = getValueOrExceptions(keys,
        exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4);
    return Collections.unmodifiableMap(result);
  }


  @Override
  public boolean valuesMissing() {
    return valuesMissing;
  }

  private static final Function<ValueOrException<BottomException>, SkyValue> GET_VALUE_FROM_VOE =
      new Function<ValueOrException<BottomException>, SkyValue>() {
        @Override
        public SkyValue apply(ValueOrException<BottomException> voe) {
          return ValueOrExceptionUtils.downconvert(voe);
        }
      };

  private static <E extends Exception>
  Function<ValueOrException2<E, BottomException>, ValueOrException<E>>
  makeSafeDowncastToVOEFunction(final Class<E> exceptionClass) {
    return new Function<ValueOrException2<E, BottomException>, ValueOrException<E>>() {
      @Override
      public ValueOrException<E> apply(ValueOrException2<E, BottomException> voe) {
        return ValueOrExceptionUtils.downconvert(voe, exceptionClass);
      }
    };
  }

  private static <E1 extends Exception, E2 extends Exception>
  Function<ValueOrException3<E1, E2, BottomException>, ValueOrException2<E1, E2>>
  makeSafeDowncastToVOE2Function(final Class<E1> exceptionClass1,
      final Class<E2> exceptionClass2) {
    return new Function<ValueOrException3<E1, E2, BottomException>,
        ValueOrException2<E1, E2>>() {
      @Override
      public ValueOrException2<E1, E2> apply(ValueOrException3<E1, E2, BottomException> voe) {
        return ValueOrExceptionUtils.downconvert(voe, exceptionClass1, exceptionClass2);
      }
    };
  }

  private static <E1 extends Exception, E2 extends Exception, E3 extends Exception>
  Function<ValueOrException4<E1, E2, E3, BottomException>, ValueOrException3<E1, E2, E3>>
  makeSafeDowncastToVOE3Function(final Class<E1> exceptionClass1,
      final Class<E2> exceptionClass2, final Class<E3> exceptionClass3) {
    return new Function<ValueOrException4<E1, E2, E3, BottomException>,
        ValueOrException3<E1, E2, E3>>() {
      @Override
      public ValueOrException3<E1, E2, E3> apply(ValueOrException4<E1, E2, E3,
          BottomException> voe) {
        return ValueOrExceptionUtils.downconvert(voe, exceptionClass1, exceptionClass2,
            exceptionClass3);
      }
    };
  }
}