Skip to content

Commit

Permalink
Use a correct key
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 13, 2021
1 parent 1d9aa35 commit 2b228ae
Showing 1 changed file with 46 additions and 16 deletions.
Expand Up @@ -25,6 +25,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -92,13 +93,12 @@ public class DefaultPluginVersionResolver
public PluginVersionResult resolve( PluginVersionRequest request )
throws PluginVersionResolutionException
{
ConcurrentMap<String, PluginVersionResult> cache = getCache( request.getRepositorySession().getData() );

PluginVersionResult result = resolveFromProject( request );

if ( result == null )
{
String key = getKey( request );
ConcurrentMap<Key, PluginVersionResult> cache = getCache( request.getRepositorySession().getData() );
Key key = getKey( request );
result = cache.get( key );

if ( result == null )
Expand Down Expand Up @@ -405,34 +405,64 @@ private PluginVersionResult resolveFromProject( PluginVersionRequest request, Li
}

@SuppressWarnings( "unchecked" )
private ConcurrentMap<String, PluginVersionResult> getCache( SessionData data )
private ConcurrentMap<Key, PluginVersionResult> getCache( SessionData data )
{
ConcurrentMap<String, PluginVersionResult> cache =
( ConcurrentMap<String, PluginVersionResult> ) data.get( CACHE_KEY );
ConcurrentMap<Key, PluginVersionResult> cache =
( ConcurrentMap<Key, PluginVersionResult> ) data.get( CACHE_KEY );
while ( cache == null )
{
cache = new ConcurrentHashMap<>( 256 );
if ( data.set( CACHE_KEY, null, cache ) )
{
break;
}
cache = ( ConcurrentMap<String, PluginVersionResult> ) data.get( CACHE_KEY );
cache = ( ConcurrentMap<Key, PluginVersionResult> ) data.get( CACHE_KEY );
}
return cache;
}

private static String getKey( PluginVersionRequest request )
private static Key getKey( PluginVersionRequest request )
{
StringBuilder sb = new StringBuilder();
sb.append( request.getGroupId() );
sb.append( ':' );
sb.append( request.getArtifactId() );
for ( RemoteRepository repository : request.getRepositories() )
return new Key( request.getGroupId(), request.getArtifactId(), request.getRepositories() );
}

static class Key
{
final String groupId;
final String artifactId;
final List<RemoteRepository> repositories;
final int hash;

Key( String groupId, String artifactId, List<RemoteRepository> repositories )
{
this.groupId = groupId;
this.artifactId = artifactId;
this.repositories = repositories;
this.hash = Objects.hash( groupId, artifactId, repositories );
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
Key key = ( Key ) o;
return groupId.equals( key.groupId )
&& artifactId.equals( key.artifactId )
&& repositories.equals( key.repositories );
}

@Override
public int hashCode()
{
sb.append( ':' );
sb.append( repository.getId() );
return hash;
}
return sb.toString();
}

static class Versions
Expand Down

0 comments on commit 2b228ae

Please sign in to comment.