-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathTargetTree.java
254 lines (213 loc) · 7.45 KB
/
TargetTree.java
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
252
253
254
/*
* Copyright 2023 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.idea.blaze.common;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.common.graph.Traverser;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.nio.file.Path;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
/**
* Encapsulates a set of targets, represented as Labels.
*
* <p>This class uses a tree to store the set of targets so that finding all the child targets of a
* given directory is fast.
*/
public class TargetTree extends AbstractCollection<Label> {
public static final TargetTree EMPTY = new TargetTree(Node.EMPTY);
private static final Joiner PATH_JOINER = Joiner.on('/');
private final Node root;
private TargetTree(Node root) {
this.root = root;
}
// Required for the Builder class to be used in an autovalue builder class:
public static Builder builder() {
return Builder.root();
}
/** Returns the set of labels at the given path, excluding any labels in child packages. */
public Collection<Label> get(Path packagePath) {
return root.find(packagePath.iterator())
.map(LabelIterator::ofDirectTargets)
.map(ImmutableSet::copyOf)
.orElse(ImmutableSet.of());
}
@Override
public Iterator<Label> iterator() {
return LabelIterator.ofAllSubpackageTargets(root);
}
@Override
public int size() {
return root.size();
}
@Override
public boolean isEmpty() {
return root.isEmpty();
}
public TargetTree getSubpackages(Path pkg) {
return root.find(pkg.iterator())
.map(node -> Node.forPath(pkg, node))
.map(TargetTree::new)
.orElse(TargetTree.EMPTY);
}
private static class LabelIterator implements Iterator<Label> {
private final Iterator<Node> nodes;
private Node currentNode;
private Iterator<String> targetNames;
private Label next;
static LabelIterator ofAllSubpackageTargets(Node root) {
return new LabelIterator(
Traverser.forTree(Node::children).depthFirstPreOrder(root).iterator());
}
static LabelIterator ofDirectTargets(Node root) {
return new LabelIterator(Collections.singleton(root).iterator());
}
LabelIterator(Iterator<Node> nodes) {
this.nodes = nodes;
targetNames = Collections.emptyIterator();
moveToNext();
}
private void moveToNext() {
next = null;
while (!targetNames.hasNext()) {
if (!nodes.hasNext()) {
return;
}
currentNode = nodes.next();
targetNames = currentNode.targets().iterator();
}
next =
Label.fromWorkspacePackageAndName(
// TODO: b/334110669 - Consider multi workspace-builds.
Label.ROOT_WORKSPACE,
Path.of(PATH_JOINER.join(currentNode.path())),
targetNames.next());
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public Label next() {
Label current = next;
moveToNext();
return current;
}
}
@AutoValue
abstract static class Node {
static final String ROOT_NAME = "";
static final Node EMPTY =
new AutoValue_TargetTree_Node(ROOT_NAME, ImmutableSet.of(), ImmutableSet.of());
// We don't use the autovalue for this since it is initialized after instantiation, by the
// parent node. It's impossible to have the parent & child set in the constructor.
private Node parent;
abstract String name();
abstract ImmutableSet<String> targets();
abstract ImmutableSet<Node> children();
@Memoized
public ImmutableMap<String, Node> childMap() {
return Maps.uniqueIndex(children(), Node::name);
}
Iterator<String> path() {
if (parent == null) {
return Collections.emptyIterator();
}
return Iterators.concat(parent.path(), Collections.singleton(name()).iterator());
}
static Node create(String name, ImmutableSet<Node> children, ImmutableSet<String> content) {
return new AutoValue_TargetTree_Node(name, content, children);
}
/** Constructs a new node for the given path with an existing node as its only child. */
static Node forPath(Path path, Node child) {
// iterate backwards through the path elements to construct the new nodes bottom up, as
// required the the immutable data structure.
// The parent is filled in upon it's construction, see create(...) above.
for (int i = path.getNameCount() - 1; i >= 0; i--) {
// when i == 0, we're creating a root node
String name = i > 0 ? path.getName(i - 1).toString() : ROOT_NAME;
child = Node.create(name, ImmutableSet.of(child), ImmutableSet.of());
}
return child;
}
@Memoized
int size() {
return targets().size() + children().stream().mapToInt(Node::size).sum();
}
@Memoized
boolean isEmpty() {
return targets().isEmpty() && children().stream().allMatch(Node::isEmpty);
}
Optional<Node> find(Iterator<Path> path) {
if (!path.hasNext()) {
return Optional.of(this);
}
String childKey = path.next().toString();
Node child = childMap().get(childKey);
if (child == null) {
return Optional.empty();
}
return child.find(path);
}
}
/** Builder for {@link TargetTree}. */
public static class Builder {
private final String name;
private final ImmutableSet.Builder<String> content;
private final Map<String, Builder> children = Maps.newHashMap();
public static Builder root() {
return new Builder(Node.ROOT_NAME);
}
private Builder(String name) {
this.name = name;
content = ImmutableSet.builder();
}
public TargetTree build() {
return new TargetTree(buildNode());
}
Node buildNode() {
ImmutableSet.Builder<Node> builder = ImmutableSet.builder();
for (Builder childBuilder : children.values()) {
builder.add(childBuilder.buildNode());
}
Node parent = Node.create(name, builder.build(), content.build());
parent.children().forEach(child -> child.parent = parent);
return parent;
}
@CanIgnoreReturnValue
public Builder add(Label target) {
return add(target.getPackage().iterator(), target.getName().toString());
}
@CanIgnoreReturnValue
public Builder add(Iterator<Path> pkg, String targetName) {
if (!pkg.hasNext()) {
content.add(targetName);
return this;
}
children.computeIfAbsent(pkg.next().toString(), Builder::new).add(pkg, targetName);
return this;
}
}
}