|
| 1 | +package org.eclipse.aether.internal.impl.collect; |
| 2 | + |
| 3 | +/* |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +import org.eclipse.aether.artifact.Artifact; |
| 23 | +import org.eclipse.aether.graph.DependencyNode; |
| 24 | +import org.eclipse.aether.util.artifact.ArtifactIdUtils; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.LinkedHashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | + |
| 34 | +final class DependencyResolveSkipper |
| 35 | +{ |
| 36 | + private static final Logger LOGGER = LoggerFactory.getLogger( DependencyResolveSkipper.class ); |
| 37 | + |
| 38 | + private Map<DependencyNode, DependencyResolveResult> resolveResults = new LinkedHashMap<>( 256 ); |
| 39 | + private CacheManager cacheManager = new CacheManager(); |
| 40 | + private CoordinateManager coordinateManager = new CoordinateManager(); |
| 41 | + |
| 42 | + DependencyResolveSkipper() |
| 43 | + { |
| 44 | + // enables default constructor |
| 45 | + } |
| 46 | + |
| 47 | + Map<DependencyNode, DependencyResolveResult> report() |
| 48 | + { |
| 49 | + LOGGER.trace( "Skipped {} nodes as having deeper depth.", |
| 50 | + resolveResults.entrySet().stream().filter( n -> n.getValue().skippedAsDeeper ).count() ); |
| 51 | + LOGGER.trace( "Skipped {} nodes as having version conflict.", |
| 52 | + resolveResults.entrySet().stream().filter( n -> n.getValue().skippedAsVersionConflict ).count() ); |
| 53 | + LOGGER.trace( "Resolved {} nodes.", |
| 54 | + resolveResults.entrySet().stream().filter( n -> n.getValue().resolve ).count() ); |
| 55 | + LOGGER.trace( "Force resolved {} nodes for scope selection.", |
| 56 | + resolveResults.entrySet().stream().filter( n -> n.getValue().forceResolve ).count() ); |
| 57 | + |
| 58 | + return resolveResults; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + boolean skipResolve( DependencyNode node, List<DependencyNode> parents ) |
| 63 | + { |
| 64 | + DependencyResolveResult resolveResult = new DependencyResolveResult( node ); |
| 65 | + resolveResults.put( node, resolveResult ); |
| 66 | + |
| 67 | + int depth = parents.size() + 1; |
| 68 | + coordinateManager.createCoordinate( node, depth ); |
| 69 | + |
| 70 | + if ( cacheManager.isVersionConflict( node ) ) |
| 71 | + { |
| 72 | + //skip resolving version conflict losers (omitted conflict) |
| 73 | + resolveResult.skippedAsVersionConflict = true; |
| 74 | + LOGGER.trace( "Skipped resolving node: {} as version conflict", |
| 75 | + ArtifactIdUtils.toId( node.getArtifact() ) ); |
| 76 | + } |
| 77 | + else if ( coordinateManager.isLeftmost( node, parents ) ) |
| 78 | + { |
| 79 | + /* |
| 80 | + * Force resolving the node to retain conflict paths when its coordinate is more left than last resolved. |
| 81 | + * This is because maven picks the widest scope present among conflicting dependencies. |
| 82 | + */ |
| 83 | + resolveResult.forceResolve = true; |
| 84 | + LOGGER.trace( "Force resolving node: {} for scope selection", |
| 85 | + ArtifactIdUtils.toId( node.getArtifact() ) ); |
| 86 | + } |
| 87 | + else if ( cacheManager.getWinnerDepth( node ) <= depth ) |
| 88 | + { |
| 89 | + //skip resolve if depth deeper (omitted duplicate) |
| 90 | + resolveResult.skippedAsDeeper = true; |
| 91 | + LOGGER.trace( "Skipped resolving node: {} as the node's depth is deeper than winner", |
| 92 | + ArtifactIdUtils.toId( node.getArtifact() ) ); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + resolveResult.resolve = true; |
| 97 | + LOGGER.trace( "Resolving node: {}", |
| 98 | + ArtifactIdUtils.toId( node.getArtifact() ) ); |
| 99 | + } |
| 100 | + |
| 101 | + if ( resolveResult.toResolve() ) |
| 102 | + { |
| 103 | + coordinateManager.updateLeftmost( node ); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + void cacheWithDepth( DependencyNode node, List<DependencyNode> parents ) |
| 111 | + { |
| 112 | + boolean parentForceResolve = parents.stream() |
| 113 | + .anyMatch( n -> resolveResults.containsKey( n ) && resolveResults.get( n ).forceResolve ); |
| 114 | + if ( parentForceResolve ) |
| 115 | + { |
| 116 | + LOGGER.trace( |
| 117 | + "Won't cache as current node: {} inherits from a force-resolved node and will be omitted duplicate", |
| 118 | + ArtifactIdUtils.toId( node.getArtifact() ) ); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + cacheManager.cacheWinnerDepth( node, parents.size() + 1 ); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + static final class DependencyResolveResult |
| 128 | + { |
| 129 | + DependencyNode current; |
| 130 | + boolean skippedAsVersionConflict; //omitted conflict |
| 131 | + boolean skippedAsDeeper; //omitted duplicate |
| 132 | + boolean resolve; //node to resolve (winner node) |
| 133 | + boolean forceResolve; //force resolve (duplicate code) for scope selection |
| 134 | + |
| 135 | + DependencyResolveResult( DependencyNode current ) |
| 136 | + { |
| 137 | + this.current = current; |
| 138 | + } |
| 139 | + |
| 140 | + boolean toResolve() |
| 141 | + { |
| 142 | + return resolve || forceResolve; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + static final class CacheManager |
| 147 | + { |
| 148 | + |
| 149 | + /** |
| 150 | + * artifact -> depth, only cache winners. |
| 151 | + */ |
| 152 | + private final HashMap<Artifact, Integer> winnerDepths = new HashMap<>( 256 ); |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * versionLessId -> Artifact, only cache winners |
| 157 | + */ |
| 158 | + private final Map<String, Artifact> winnerGAs = new HashMap<>( 256 ); |
| 159 | + |
| 160 | + boolean isVersionConflict( DependencyNode node ) |
| 161 | + { |
| 162 | + String ga = ArtifactIdUtils.toVersionlessId( node.getArtifact() ); |
| 163 | + if ( winnerGAs.containsKey( ga ) ) |
| 164 | + { |
| 165 | + Artifact result = winnerGAs.get( ga ); |
| 166 | + return !node.getArtifact().getVersion().equals( result.getVersion() ); |
| 167 | + } |
| 168 | + |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + void cacheWinnerDepth( DependencyNode node, int depth ) |
| 173 | + { |
| 174 | + LOGGER.trace( "Artifact {} with depth {} cached", node.getArtifact(), depth ); |
| 175 | + winnerDepths.put( node.getArtifact(), depth ); |
| 176 | + winnerGAs.put( ArtifactIdUtils.toVersionlessId( node.getArtifact() ), node.getArtifact() ); |
| 177 | + } |
| 178 | + |
| 179 | + int getWinnerDepth( DependencyNode node ) |
| 180 | + { |
| 181 | + return winnerDepths.getOrDefault( node.getArtifact(), Integer.MAX_VALUE ); |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | + static final class CoordinateManager |
| 188 | + { |
| 189 | + private final Map<Integer, AtomicInteger> sequenceGen = new HashMap<>( 256 ); |
| 190 | + |
| 191 | + /** |
| 192 | + * Dependency node -> Coordinate |
| 193 | + */ |
| 194 | + private final Map<DependencyNode, Coordinate> coordinateMap = new HashMap<>( 256 ); |
| 195 | + |
| 196 | + /** |
| 197 | + * Leftmost coordinate of given artifact |
| 198 | + */ |
| 199 | + private final Map<Artifact, Coordinate> leftmostCoordinates = new HashMap<>( 256 ); |
| 200 | + |
| 201 | + |
| 202 | + Coordinate getCoordinate( DependencyNode node ) |
| 203 | + { |
| 204 | + return coordinateMap.get( node ); |
| 205 | + } |
| 206 | + |
| 207 | + Coordinate createCoordinate( DependencyNode node, int depth ) |
| 208 | + { |
| 209 | + int seq = sequenceGen.computeIfAbsent( depth, k -> new AtomicInteger() ).incrementAndGet(); |
| 210 | + Coordinate coordinate = new Coordinate( depth, seq ); |
| 211 | + coordinateMap.put( node, coordinate ); |
| 212 | + return coordinate; |
| 213 | + } |
| 214 | + |
| 215 | + void updateLeftmost( DependencyNode current ) |
| 216 | + { |
| 217 | + leftmostCoordinates.put( current.getArtifact(), getCoordinate( current ) ); |
| 218 | + } |
| 219 | + |
| 220 | + boolean isLeftmost( DependencyNode node, List<DependencyNode> parents ) |
| 221 | + { |
| 222 | + Coordinate leftmost = leftmostCoordinates.get( node.getArtifact() ); |
| 223 | + if ( leftmost != null && leftmost.depth <= parents.size() ) |
| 224 | + { |
| 225 | + DependencyNode sameLevelNode = parents.get( leftmost.depth - 1 ); |
| 226 | + if ( getCoordinate( sameLevelNode ).sequence < leftmost.sequence ) |
| 227 | + { |
| 228 | + return true; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + return false; |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + static final class Coordinate |
| 237 | + { |
| 238 | + int depth; |
| 239 | + int sequence; |
| 240 | + |
| 241 | + Coordinate( int depth, int sequence ) |
| 242 | + { |
| 243 | + this.depth = depth; |
| 244 | + this.sequence = sequence; |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public String toString() |
| 249 | + { |
| 250 | + return "{" |
| 251 | + + "depth=" |
| 252 | + + depth |
| 253 | + + ", sequence=" |
| 254 | + + sequence |
| 255 | + + '}'; |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + |
| 260 | +} |
0 commit comments