Skip to content

Commit

Permalink
Do not use the ReactorModelCache to store the mapping between GA<->So…
Browse files Browse the repository at this point in the history
…urce
  • Loading branch information
gnodet authored and rfscholte committed Dec 4, 2020
1 parent 459ba0a commit 50e8beb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import org.apache.maven.building.Source;
import org.apache.maven.model.building.ModelCache;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -39,8 +37,6 @@ class ReactorModelCache

private final Map<Object, Object> models = new ConcurrentHashMap<>( 256 );

private final Map<GACacheKey, Set<Source>> mappedSources = new ConcurrentHashMap<>( 64 );

@Override
public Object get( String groupId, String artifactId, String version, String tag )
{
Expand All @@ -52,30 +48,6 @@ public void put( String groupId, String artifactId, String version, String tag,
{
models.put( new GavCacheKey( groupId, artifactId, version, tag ), data );
}

@Override
public Source get( String groupId, String artifactId )
{
Set<Source> sources = mappedSources.get( new GACacheKey( groupId, artifactId ) );
if ( sources == null )
{
return null;
}
else
{
return sources.stream().reduce( ( a, b ) ->
{
throw new IllegalStateException( "No unique Source for " + groupId + ':' + artifactId
+ ": " + a.getLocation() + " and " + b.getLocation() );
} ).orElse( null );
}
}

@Override
public void put( String groupId, String artifactId, Source source )
{
mappedSources.computeIfAbsent( new GACacheKey( groupId, artifactId ), k -> new HashSet<>() ).add( source );
}

@Override
public Object get( Source source, String tag )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;
import javax.inject.Named;
Expand Down Expand Up @@ -696,7 +699,12 @@ private Model readFileModel( ModelBuildingRequest request,
intoCache( request.getModelCache(), modelSource, ModelCacheTag.FILE, model );
if ( modelSource instanceof FileModelSource )
{
intoCache( request.getModelCache(), getGroupId( model ), model.getArtifactId(), modelSource );
if ( request.getTransformerContextBuilder() instanceof DefaultTransformerContextBuilder )
{
DefaultTransformerContextBuilder contextBuilder =
(DefaultTransformerContextBuilder) request.getTransformerContextBuilder();
contextBuilder.putSource( getGroupId( model ), model.getArtifactId(), modelSource );
}
}

return model;
Expand Down Expand Up @@ -1543,14 +1551,6 @@ private <T> void intoCache( ModelCache modelCache, String groupId, String artifa
}
}

private <T> void intoCache( ModelCache modelCache, String groupId, String artifactId, Source source )
{
if ( modelCache != null )
{
modelCache.put( groupId, artifactId, source );
}
}

private <T> void intoCache( ModelCache modelCache, Source source, ModelCacheTag<T> tag, T data )
{
if ( modelCache != null )
Expand All @@ -1572,16 +1572,6 @@ private static <T> T fromCache( ModelCache modelCache, String groupId, String ar
}
return null;
}

private static Source fromCache( ModelCache modelCache, String groupId, String artifactId )
{
if ( modelCache != null )
{
return modelCache.get( groupId, artifactId );
}
return null;
}


private static <T> T fromCache( ModelCache modelCache, Source source, ModelCacheTag<T> tag )
{
Expand Down Expand Up @@ -1808,12 +1798,15 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
private class DefaultTransformerContextBuilder implements TransformerContextBuilder
{
private final DefaultTransformerContext context = new DefaultTransformerContext();


private final Map<DefaultTransformerContext.GAKey, Set<Source>> mappedSources
= new ConcurrentHashMap<>( 64 );

/**
* If an interface could be extracted, DefaultModelProblemCollector should be ModelProblemCollectorExt
*
* @param request
* @param problems
* @param collector
* @return
*/
@Override
Expand Down Expand Up @@ -1845,7 +1838,7 @@ public Model getRawModel( Path path )

private Model findRawModel( String groupId, String artifactId )
{
Source source = fromCache( request.getModelCache(), groupId, artifactId );
Source source = getSource( groupId, artifactId );
if ( source != null )
{
try
Expand Down Expand Up @@ -1897,6 +1890,27 @@ private Model findRawModel( Path p )
public TransformerContext build()
{
return context;
}
}

public Source getSource( String groupId, String artifactId )
{
Set<Source> sources = mappedSources.get( new DefaultTransformerContext.GAKey( groupId, artifactId ) );
if ( sources == null )
{
return null;
}
return sources.stream().reduce( ( a, b ) ->
{
throw new IllegalStateException( "No unique Source for " + groupId + ':' + artifactId
+ ": " + a.getLocation() + " and " + b.getLocation() );
} ).orElse( null );
}

public void putSource( String groupId, String artifactId, Source source )
{
mappedSources.computeIfAbsent( new DefaultTransformerContext.GAKey( groupId, artifactId ),
k -> new HashSet<>() ).add( source );
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,6 @@ default Object get( Source path, String tag )
// only useful for ReactorModelCache
return null;
}

/**
*
* @param groupId The groupId, must not be {@code null}.
* @param artifactId The artifactId, must not be {@code null}.
* @param source the source matching
* @throws IllegalArgumentException if the groupId + artifactId was already added before
*/
default void put( String groupId, String artifactId, Source source )
{
// only useful for ReactorModelCache
}

/**
*
* @param groupId The groupId, must not be {@code null}.
* @param artifactId The artifactId, must not be {@code null}.
* @return The requested source or {@code null} if none was present in the cache.
*/
default Source get( String groupId, String artifactId )
{
// only useful for ReactorModelCache
return null;
}

/**
* Puts the specified data into the cache.
Expand Down

0 comments on commit 50e8beb

Please sign in to comment.