aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skyframe/LocalRepositoryLookupValue.java
blob: c4c784a75c3b39b4dc4285c73c0dedba829ad93b (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
// Copyright 2016 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.lib.skyframe;

import com.google.common.collect.Interner;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.concurrent.BlazeInterners;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.RootedPath;
import com.google.devtools.build.skyframe.AbstractSkyKey;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.build.skyframe.SkyValue;

/**
 * A value that represents a local repository lookup result.
 *
 * <p>Local repository lookups will always produce a value. The {@code #getRepository} method
 * returns the name of the repository that the directory resides in.
 */
public abstract class LocalRepositoryLookupValue implements SkyValue {

  static Key key(RootedPath directory) {
    return Key.create(directory);
  }

  @AutoCodec.VisibleForSerialization
  @AutoCodec
  static class Key extends AbstractSkyKey<RootedPath> {
    private static final Interner<Key> interner = BlazeInterners.newWeakInterner();

    private Key(RootedPath arg) {
      super(arg);
    }

    @AutoCodec.VisibleForSerialization
    @AutoCodec.Instantiator
    static Key create(RootedPath arg) {
      return interner.intern(new Key(arg));
    }

    @Override
    public SkyFunctionName functionName() {
      return SkyFunctions.LOCAL_REPOSITORY_LOOKUP;
    }
  }

  private static final LocalRepositoryLookupValue MAIN_REPO_VALUE = new MainRepositoryLookupValue();
  private static final LocalRepositoryLookupValue NOT_FOUND_VALUE =
      new NotFoundLocalRepositoryLookupValue();

  public static LocalRepositoryLookupValue mainRepository() {
    return MAIN_REPO_VALUE;
  }

  public static LocalRepositoryLookupValue success(
      RepositoryName repositoryName, PathFragment path) {
    return new SuccessfulLocalRepositoryLookupValue(repositoryName, path);
  }

  public static LocalRepositoryLookupValue notFound() {
    return NOT_FOUND_VALUE;
  }

  /**
   * Returns {@code true} if the local repository lookup succeeded and the {@link #getRepository}
   * method will return a useful result.
   */
  public abstract boolean exists();

  /**
   * Returns the {@link RepositoryName} of the local repository contained in the directory which was
   * looked up, {@link RepositoryName#MAIN} if the directory is part of the main repository, or
   * throws a {@link IllegalStateException} if there was no repository found.
   */
  public abstract RepositoryName getRepository();

  /**
   * Returns the path to the local repository, or throws a {@link IllegalStateException} if there
   * was no repository found.
   */
  public abstract PathFragment getPath();

  /** Represents a successful lookup of the main repository. */
  public static final class MainRepositoryLookupValue extends LocalRepositoryLookupValue {

    // This should be a singleton value.
    private MainRepositoryLookupValue() {}

    @Override
    public boolean exists() {
      return true;
    }

    @Override
    public RepositoryName getRepository() {
      return RepositoryName.MAIN;
    }

    @Override
    public PathFragment getPath() {
      return PathFragment.EMPTY_FRAGMENT;
    }

    @Override
    public String toString() {
      return "MainRepositoryLookupValue";
    }

    @Override
    public boolean equals(Object obj) {
      // All MainRepositoryLookupValue instances are equivalent.
      return obj instanceof MainRepositoryLookupValue;
    }

    @Override
    public int hashCode() {
      return MainRepositoryLookupValue.class.getSimpleName().hashCode();
    }
  }

  /** Represents a successful lookup of a local repository. */
  public static final class SuccessfulLocalRepositoryLookupValue
      extends LocalRepositoryLookupValue {
    private final RepositoryName repositoryName;
    private final PathFragment path;

    public SuccessfulLocalRepositoryLookupValue(RepositoryName repositoryName, PathFragment path) {
      this.repositoryName = repositoryName;
      this.path = path;
    }

    @Override
    public boolean exists() {
      return true;
    }

    @Override
    public RepositoryName getRepository() {
      return repositoryName;
    }

    @Override
    public PathFragment getPath() {
      return path;
    }

    @Override
    public String toString() {
      return "SuccessfulLocalRepositoryLookupValue(" + repositoryName + ")";
    }

    @Override
    public boolean equals(Object obj) {
      if (!(obj instanceof SuccessfulLocalRepositoryLookupValue)) {
        return false;
      }
      SuccessfulLocalRepositoryLookupValue other = (SuccessfulLocalRepositoryLookupValue) obj;
      return repositoryName.equals(other.repositoryName);
    }

    @Override
    public int hashCode() {
      return repositoryName.hashCode();
    }
  }

  /** Represents the state where no repository was found, either local or the main repository. */
  public static final class NotFoundLocalRepositoryLookupValue extends LocalRepositoryLookupValue {

    // This should be a singleton value.
    private NotFoundLocalRepositoryLookupValue() {}

    @Override
    public boolean exists() {
      return false;
    }

    @Override
    public RepositoryName getRepository() {
      throw new IllegalStateException("Repository was not found");
    }

    @Override
    public PathFragment getPath() {
      throw new IllegalStateException("Repository was not found");
    }

    @Override
    public String toString() {
      return "NotFoundLocalRepositoryLookupValue";
    }

    @Override
    public boolean equals(Object obj) {
      // All NotFoundLocalRepositoryLookupValue instances are equivalent.
      return obj instanceof NotFoundLocalRepositoryLookupValue;
    }

    @Override
    public int hashCode() {
      return NotFoundLocalRepositoryLookupValue.class.getSimpleName().hashCode();
    }
  }
}