caiwei-ebay commented on a change in pull request #158: URL: https://github.com/apache/maven-resolver/pull/158#discussion_r821488802
########## File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/collect/DependencyResolveSkipper.java ########## @@ -0,0 +1,260 @@ +package org.eclipse.aether.internal.impl.collect; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.util.artifact.ArtifactIdUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +final class DependencyResolveSkipper +{ + private static final Logger LOGGER = LoggerFactory.getLogger( DependencyResolveSkipper.class ); + + private Map<DependencyNode, DependencyResolveResult> resolveResults = new LinkedHashMap<>( 256 ); + private CacheManager cacheManager = new CacheManager(); + private CoordinateManager coordinateManager = new CoordinateManager(); + + DependencyResolveSkipper() + { + // enables default constructor + } + + Map<DependencyNode, DependencyResolveResult> report() + { + LOGGER.trace( "Skipped {} nodes as having deeper depth.", + resolveResults.entrySet().stream().filter( n -> n.getValue().skippedAsDeeper ).count() ); + LOGGER.trace( "Skipped {} nodes as having version conflict.", + resolveResults.entrySet().stream().filter( n -> n.getValue().skippedAsVersionConflict ).count() ); + LOGGER.trace( "Resolved {} nodes.", + resolveResults.entrySet().stream().filter( n -> n.getValue().resolve ).count() ); + LOGGER.trace( "Force resolved {} nodes for scope selection.", + resolveResults.entrySet().stream().filter( n -> n.getValue().forceResolve ).count() ); + + return resolveResults; + } + + + boolean skipResolve( DependencyNode node, List<DependencyNode> parents ) + { + DependencyResolveResult resolveResult = new DependencyResolveResult( node ); + resolveResults.put( node, resolveResult ); + + int depth = parents.size() + 1; + coordinateManager.createCoordinate( node, depth ); + + if ( cacheManager.isVersionConflict( node ) ) + { + //skip resolving version conflict losers (omitted conflict) + resolveResult.skippedAsVersionConflict = true; + LOGGER.trace( "Skipped resolving node: {} as version conflict", + ArtifactIdUtils.toId( node.getArtifact() ) ); + } + else if ( coordinateManager.isLeftmost( node, parents ) ) + { + /* + * Force resolving the node to retain conflict paths when its coordinate is more left than last resolved. + * This is because maven picks the widest scope present among conflicting dependencies. + */ + resolveResult.forceResolve = true; + LOGGER.trace( "Force resolving node: {} for scope selection", + ArtifactIdUtils.toId( node.getArtifact() ) ); + } + else if ( cacheManager.getWinnerDepth( node ) <= depth ) + { + //skip resolve if depth deeper (omitted duplicate) + resolveResult.skippedAsDeeper = true; + LOGGER.trace( "Skipped resolving node: {} as the node's depth is deeper than winner", + ArtifactIdUtils.toId( node.getArtifact() ) ); + } + else + { + resolveResult.resolve = true; + LOGGER.trace( "Resolving node: {}", + ArtifactIdUtils.toId( node.getArtifact() ) ); + } + + if ( resolveResult.toResolve() ) + { + coordinateManager.updateLeftmost( node ); + return false; + } + + return true; + } + + void cacheWithDepth( DependencyNode node, List<DependencyNode> parents ) + { + boolean parentForceResolve = parents.stream() + .anyMatch( n -> resolveResults.containsKey( n ) && resolveResults.get( n ).forceResolve ); + if ( parentForceResolve ) + { + LOGGER.trace( + "Won't cache as current node: {} inherits from a force-resolved node and will be omitted duplicate", + ArtifactIdUtils.toId( node.getArtifact() ) ); + } + else + { + cacheManager.cacheWinnerDepth( node, parents.size() + 1 ); + } + } + + + static final class DependencyResolveResult Review comment: Renamed. Thanks! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org