-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
820 additions
and
15 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
daemon/src/main/java/org/mvndaemon/mvnd/builder/DagWidth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* 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 org.mvndaemon.mvnd.builder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class DagWidth<K> { | ||
|
||
private final DependencyGraph<K> graph; | ||
private final Map<K, Set<K>> allUpstreams = new HashMap<>(); | ||
|
||
public DagWidth(DependencyGraph<K> graph) { | ||
this.graph = graph; | ||
graph.getProjects().forEach(this::allUpstreams); | ||
} | ||
|
||
public int getMaxWidth() { | ||
return getMaxWidth(Integer.MAX_VALUE); | ||
} | ||
|
||
public int getMaxWidth(int maxmax) { | ||
return getMaxWidth(maxmax, Long.MAX_VALUE); | ||
} | ||
|
||
public int getMaxWidth(int maxmax, long maxTimeMillis) { | ||
int max = 0; | ||
if (maxmax < allUpstreams.size()) { | ||
// try inverted upstream bound | ||
Map<Set<K>, Set<K>> mapByUpstreams = new HashMap<>(); | ||
allUpstreams.forEach((k, ups) -> { | ||
mapByUpstreams.computeIfAbsent(ups, n -> new HashSet<>()).add(k); | ||
}); | ||
max = mapByUpstreams.values().stream() | ||
.mapToInt(Set::size) | ||
.max() | ||
.orElse(0); | ||
if (max >= maxmax) { | ||
return maxmax; | ||
} | ||
} | ||
long tmax = System.currentTimeMillis() + maxTimeMillis; | ||
int tries = 0; | ||
SubsetIterator iterator = new SubsetIterator(getRoots()); | ||
while (max < maxmax && iterator.hasNext()) { | ||
if (++tries % 100 == 0 && System.currentTimeMillis() < tmax) { | ||
return maxmax; | ||
} | ||
List<K> l = iterator.next(); | ||
max = Math.max(max, l.size()); | ||
} | ||
return max; | ||
} | ||
|
||
private class SubsetIterator implements Iterator<List<K>> { | ||
|
||
final List<List<K>> nexts = new ArrayList<>(); | ||
final Set<List<K>> visited = new HashSet<>(); | ||
|
||
public SubsetIterator(List<K> roots) { | ||
nexts.add(roots); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return !nexts.isEmpty(); | ||
} | ||
|
||
@Override | ||
public List<K> next() { | ||
List<K> list = nexts.remove(0); | ||
list.stream() | ||
.map(node -> ensembleWithChildrenOf(list, node)) | ||
.filter(visited::add) | ||
.forEach(nexts::add); | ||
return list; | ||
} | ||
} | ||
|
||
private List<K> getRoots() { | ||
return graph.getProjects() | ||
.filter(graph::isRoot) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Get a stream of all subset of descendants of the given nodes | ||
*/ | ||
private Stream<List<K>> childEnsembles(List<K> list) { | ||
return Stream.concat( | ||
Stream.of(list), | ||
list.parallelStream() | ||
.map(node -> ensembleWithChildrenOf(list, node)) | ||
.flatMap(this::childEnsembles)); | ||
} | ||
|
||
List<K> ensembleWithChildrenOf(List<K> list, K node) { | ||
return Stream.concat( | ||
list.stream().filter(k -> !Objects.equals(k, node)), | ||
graph.getDownstreamProjects(node) | ||
.filter(k -> allUpstreams(k).stream().noneMatch(k2 -> !Objects.equals(k2, node) && list.contains(k2)))) | ||
.distinct().collect(Collectors.toList()); | ||
} | ||
|
||
private Set<K> allUpstreams(K node) { | ||
Set<K> aups = allUpstreams.get(node); | ||
if (aups == null) { | ||
aups = Stream.concat( | ||
graph.getUpstreamProjects(node), | ||
graph.getUpstreamProjects(node).map(this::allUpstreams).flatMap(Set::stream)) | ||
.collect(Collectors.toSet()); | ||
allUpstreams.put(node, aups); | ||
} | ||
return aups; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
daemon/src/test/java/org/mvndaemon/mvnd/builder/DagWidthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* 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 org.mvndaemon.mvnd.builder; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class DagWidthTest { | ||
|
||
@Test | ||
void testSimpleGraph() { | ||
// | ||
// A B | ||
// / | \ / \ | ||
// C D E F | ||
// \/ | ||
// G | ||
Map<String, List<String>> upstreams = new HashMap<>(); | ||
upstreams.put("A", Collections.emptyList()); | ||
upstreams.put("B", Collections.emptyList()); | ||
upstreams.put("C", Collections.singletonList("A")); | ||
upstreams.put("D", Collections.singletonList("A")); | ||
upstreams.put("E", Arrays.asList("A", "B")); | ||
upstreams.put("F", Collections.singletonList("B")); | ||
upstreams.put("G", Arrays.asList("D", "E")); | ||
DependencyGraph<String> graph = new SimpleGraph<>(upstreams); | ||
|
||
assertEquals(4, new DagWidth<>(graph).getMaxWidth(12)); | ||
} | ||
|
||
@Test | ||
void testSingle() { | ||
// | ||
// A | ||
// | ||
Map<String, List<String>> upstreams = new HashMap<>(); | ||
upstreams.put("A", Collections.emptyList()); | ||
DependencyGraph<String> graph = new SimpleGraph<>(upstreams); | ||
|
||
assertEquals(1, new DagWidth<>(graph).getMaxWidth(12)); | ||
} | ||
|
||
@Test | ||
void testLinear() { | ||
// | ||
// A -> B -> C -> D | ||
// | ||
Map<String, List<String>> upstreams = new HashMap<>(); | ||
upstreams.put("A", Collections.emptyList()); | ||
upstreams.put("B", Collections.singletonList("A")); | ||
upstreams.put("C", Collections.singletonList("B")); | ||
upstreams.put("D", Collections.singletonList("C")); | ||
DependencyGraph<String> graph = new SimpleGraph<>(upstreams); | ||
|
||
assertEquals(1, new DagWidth<>(graph).getMaxWidth(12)); | ||
} | ||
|
||
@Test | ||
public void testHugeGraph() throws IOException { | ||
Map<String, List<String>> upstreams = new HashMap<>(); | ||
try (BufferedReader r = new BufferedReader( | ||
new InputStreamReader(getClass().getResourceAsStream("huge-graph.properties")))) { | ||
r.lines().forEach(l -> { | ||
int idxEq = l.indexOf(" = "); | ||
if (!l.startsWith("#") && idxEq > 0) { | ||
String k = l.substring(0, idxEq).trim(); | ||
String[] ups = l.substring(idxEq + 3).trim().split(","); | ||
List<String> list = Stream.of(ups).map(String::trim) | ||
.filter(s -> !s.isEmpty()) | ||
.collect(Collectors.toList()); | ||
upstreams.put(k, list); | ||
} | ||
}); | ||
} | ||
SimpleGraph<String> graph = new SimpleGraph<>(upstreams); | ||
|
||
DagWidth<String> w = new DagWidth<>(graph); | ||
List<String> d = w.ensembleWithChildrenOf(graph.downstreams.get("org.apache.camel:camel"), | ||
"org.apache.camel:camel-parent"); | ||
|
||
assertEquals(12, w.getMaxWidth(12)); | ||
} | ||
|
||
static class SimpleGraph<K> implements DependencyGraph<K> { | ||
|
||
final List<K> nodes; | ||
final Map<K, List<K>> upstreams; | ||
final Map<K, List<K>> downstreams; | ||
|
||
public SimpleGraph(Map<K, List<K>> upstreams) { | ||
this.upstreams = upstreams; | ||
this.nodes = Stream.concat(upstreams.keySet().stream(), upstreams.values().stream().flatMap(List::stream)) | ||
.distinct() | ||
.sorted() | ||
.collect(Collectors.toList()); | ||
this.downstreams = this.nodes.stream().collect(Collectors.toMap(k -> k, k -> new ArrayList<>())); | ||
upstreams.forEach((k, ups) -> { | ||
ups.forEach(up -> downstreams.get(up).add(k)); | ||
}); | ||
} | ||
|
||
@Override | ||
public Stream<K> getProjects() { | ||
return nodes.stream(); | ||
} | ||
|
||
@Override | ||
public Stream<K> getDownstreamProjects(K project) { | ||
return downstreams.get(project).stream(); | ||
} | ||
|
||
@Override | ||
public Stream<K> getUpstreamProjects(K project) { | ||
List<K> ups = upstreams.get(project); | ||
return Objects.requireNonNull(ups, () -> "Could not find upstreams for " + project).stream(); | ||
} | ||
|
||
@Override | ||
public boolean isRoot(K project) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
} |
Oops, something went wrong.