[ https://issues.apache.org/jira/browse/MRESOLVER-273?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17609999#comment-17609999 ]
ASF GitHub Bot commented on MRESOLVER-273: ------------------------------------------ michael-o commented on code in PR #194: URL: https://github.com/apache/maven-resolver/pull/194#discussion_r981133064 ########## maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/synccontext/named/GAVNameMapper.java: ########## @@ -19,39 +19,112 @@ * under the License. */ +import java.util.Collection; +import java.util.TreeSet; + +import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.metadata.Metadata; +import static java.util.Objects.requireNonNull; + /** * Artifact GAV {@link NameMapper}, uses artifact and metadata coordinates to name their corresponding locks. Is not - * considering local repository, only the artifact coordinates. + * considering local repository, only the artifact coordinates. May use custom prefixes and sufixes and separators, + * hence this instance may or may not be filesystem friendly (depends on strings used). */ -public class GAVNameMapper extends NameMapperSupport +public class GAVNameMapper implements NameMapper { + private final boolean fileSystemFriendly; + + private final String artifactPrefix; + + private final String artifactSuffix; + + private final String metadataPrefix; + + private final String metadataSuffix; + + private final String fieldSeparator; + + public GAVNameMapper( boolean fileSystemFriendly, + String artifactPrefix, String artifactSuffix, + String metadataPrefix, String metadataSuffix, + String fieldSeparator ) + { + this.fileSystemFriendly = fileSystemFriendly; + this.artifactPrefix = requireNonNull( artifactPrefix ); + this.artifactSuffix = requireNonNull( artifactSuffix ); + this.metadataPrefix = requireNonNull( metadataPrefix ); + this.metadataSuffix = requireNonNull( metadataSuffix ); + this.fieldSeparator = requireNonNull( fieldSeparator ); + } + @Override - protected String getArtifactName( Artifact artifact ) + public boolean isFileSystemFriendly() { - return "artifact:" + artifact.getGroupId() - + ":" + artifact.getArtifactId() - + ":" + artifact.getBaseVersion(); + return fileSystemFriendly; } @Override - protected String getMetadataName( Metadata metadata ) + public Collection<String> nameLocks( final RepositorySystemSession session, + final Collection<? extends Artifact> artifacts, + final Collection<? extends Metadata> metadatas ) + { + // Deadlock prevention: https://stackoverflow.com/a/16780988/696632 + // We must acquire multiple locks always in the same order! + TreeSet<String> keys = new TreeSet<>(); + if ( artifacts != null ) + { + for ( Artifact artifact : artifacts ) + { + keys.add( getArtifactName( artifact ) ); + } + } + + if ( metadatas != null ) + { + for ( Metadata metadata : metadatas ) + { + keys.add( getMetadataName( metadata ) ); + } + } + return keys; + } + + private String getArtifactName( Artifact artifact ) + { + return artifactPrefix + artifact.getGroupId() + + fieldSeparator + artifact.getArtifactId() + + fieldSeparator + artifact.getBaseVersion() + + artifactSuffix; + } + + private String getMetadataName( Metadata metadata ) { - String name = "metadata:"; + String name = metadataPrefix; if ( !metadata.getGroupId().isEmpty() ) { name += metadata.getGroupId(); if ( !metadata.getArtifactId().isEmpty() ) { - name += ":" + metadata.getArtifactId(); + name += fieldSeparator + metadata.getArtifactId(); if ( !metadata.getVersion().isEmpty() ) { - name += ":" + metadata.getVersion(); + name += fieldSeparator + metadata.getVersion(); } } } - return name; + return name + metadataSuffix; + } + + public static NameMapper gav() + { + return new GAVNameMapper( false, "artifact:", "", "metadata:", "", ":" ); + } + + public static NameMapper fileGav() + { + return new GAVNameMapper( true, "", ".lock", "", ".lock", "~" ); Review Comment: Attention, whe you obtain group level locks on metadata this can lead to very coarse locks. Why not have prefixes: `metadata~` and `artifact~? ? > Create more compact File locking layout/mapper > ---------------------------------------------- > > Key: MRESOLVER-273 > URL: https://issues.apache.org/jira/browse/MRESOLVER-273 > Project: Maven Resolver > Issue Type: Improvement > Components: Resolver > Reporter: Tamás Cservenák > Assignee: Tamás Cservenák > Priority: Major > Fix For: resolver-next > > > Current FileGAVNameMapper "mimics" loosely local reposiory layout, uses long > paths, hence (relatively) big strings etc. > More compact layout would be just to hash strings like "a:G:A:V" and > "m:G[:A[:V]]" for artifacts and metadata instead, and create 2 level deep > hashed storage. > Problem with "loose" layout is that they do end up as files in OS, while > these would be much shorter. -- This message was sent by Atlassian Jira (v8.20.10#820010)