Skip to content

Commit

Permalink
Caching update information in reports to increase performance
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Oct 10, 2022
1 parent 3e1278d commit 0c66cc5
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.codehaus.mojo.versions.api;


/*
* 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 java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;

import org.apache.commons.lang3.tuple.Pair;

/**
* Utility providing a cached {@link ArtifactVersions#getOldestUpdate(Optional)} API
*/
public class ArtifactVersionsCache
{
private BiFunction<AbstractVersionDetails, Optional<Segment>, ?> cachedFunction;

private Map<Pair<? extends AbstractVersionDetails, Optional<Segment>>, Object> updateCache = new HashMap<>();

/**
* Constructs a new instance given the concrete function for obtaining the details
*
* @param cachedFunction reference to the function computing the required information
*/
public ArtifactVersionsCache( BiFunction<AbstractVersionDetails, Optional<Segment>, ?>
cachedFunction )
{
this.cachedFunction = cachedFunction;
}

/**
* Returns the required information for the given {@link ArtifactVersions} object and the given update scope.
* If the information is already present in cache, the cached version is returned. Otherwise,
* the {@code artifactVersions} object is queried and the response is cached.
*
* @param artifactVersions {@linkplain ArtifactVersions} object referring to the given dependency
* @param updateScope update scope
* @return last retrieved update information
*/
@SuppressWarnings( "unchecked" )
public <V extends AbstractVersionDetails, R> R get( V artifactVersions,
Optional<Segment> updateScope )
{
return (R) updateCache.computeIfAbsent( Pair.of( artifactVersions, updateScope ),
pair -> cachedFunction.apply( pair.getLeft(), pair.getRight() ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.apache.maven.doxia.sink.SinkEventAttributes;
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.versions.api.AbstractVersionDetails;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.ArtifactVersionsCache;
import org.codehaus.mojo.versions.api.ReportRenderer;
import org.codehaus.plexus.i18n.I18N;

Expand Down Expand Up @@ -56,6 +58,12 @@ public abstract class AbstractVersionsReportRenderer<T> extends VersionsReportRe
*/
protected T model;

protected ArtifactVersionsCache oldestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getOldestUpdate );

protected ArtifactVersionsCache allUpdatesCache
= new ArtifactVersionsCache( AbstractVersionDetails::getAllUpdates );

/**
* Constructor to be called by the dependency injection framework
* @param i18n i18n object to be injected
Expand Down Expand Up @@ -319,26 +327,26 @@ protected void renderDependencyDetailTable( Dependency artifact, ArtifactVersion
sink.text( getText( "report.status" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
ArtifactVersion[] versions = details.getAllUpdates( empty() );
if ( details.getOldestUpdate( of( SUBINCREMENTAL ) ) != null )
ArtifactVersion[] versions = allUpdatesCache.get( details, empty() );
if ( oldestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
sink.text( getText( "report.otherUpdatesAvailable" ) );
}
else if ( details.getOldestUpdate( of( INCREMENTAL ) ) != null )
else if ( oldestUpdateCache.get( details, of( INCREMENTAL ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
sink.text( getText( "report.incrementalUpdatesAvailable" ) );
}
else if ( details.getOldestUpdate( of( MINOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MINOR ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
sink.text( getText( "report.minorUpdatesAvailable" ) );
}
else if ( details.getOldestUpdate( of( MAJOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MAJOR ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,19 @@ protected OverviewStats computeOverviewStats()
OverviewStats stats = new OverviewStats();
model.getAllUpdates().values().forEach( details ->
{
if ( details.getOldestUpdate( of( SUBINCREMENTAL ) ) != null )
if ( oldestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
stats.incrementAny();
}
else if ( details.getOldestUpdate( of( INCREMENTAL ) ) != null )
else if ( oldestUpdateCache.get( details, of( INCREMENTAL ) ) != null )
{
stats.incrementIncremental();
}
else if ( details.getOldestUpdate( of( MINOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MINOR ) ) != null )
{
stats.incrementMinor();
}
else if ( details.getOldestUpdate( of( MAJOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MAJOR ) ) != null )
{
stats.incrementMajor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.versions.PluginUpdatesDetails;
import org.codehaus.mojo.versions.api.AbstractVersionDetails;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.ArtifactVersionsCache;
import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel;
import org.codehaus.plexus.i18n.I18N;

Expand All @@ -46,6 +48,9 @@
*/
public class PluginUpdatesReportRenderer extends AbstractVersionsReportRenderer<PluginUpdatesModel>
{
protected ArtifactVersionsCache newestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getNewestUpdate );

public PluginUpdatesReportRenderer( I18N i18n, Sink sink, Locale locale, String bundleName,
PluginUpdatesModel model )
{
Expand Down Expand Up @@ -149,19 +154,19 @@ protected PluginOverviewStats computeOverviewStats()
PluginOverviewStats stats = new PluginOverviewStats();
model.getAllUpdates().values().forEach( details ->
{
if ( details.getOldestUpdate( of( SUBINCREMENTAL ) ) != null )
if ( oldestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
stats.incrementAny();
}
else if ( details.getOldestUpdate( of( INCREMENTAL ) ) != null )
else if ( oldestUpdateCache.get( details, of( INCREMENTAL ) ) != null )
{
stats.incrementIncremental();
}
else if ( details.getOldestUpdate( of( MINOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MINOR ) ) != null )
{
stats.incrementMinor();
}
else if ( details.getOldestUpdate( of( MAJOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MAJOR ) ) != null )
{
stats.incrementMajor();
}
Expand Down Expand Up @@ -228,37 +233,37 @@ protected void renderSummaryTableRow( Dependency artifact, PluginUpdatesDetails
sink.tableCell_();

sink.tableCell();
if ( details.getNewestUpdate( of( SUBINCREMENTAL ) ) != null )
if ( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
safeBold();
sink.text( details.getNewestUpdate( of( SUBINCREMENTAL ) ).toString() );
sink.text( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ).toString() );
safeBold_();
}
sink.tableCell_();

sink.tableCell();
if ( details.getNewestUpdate( of( INCREMENTAL ) ) != null )
if ( newestUpdateCache.get( details, of( INCREMENTAL ) ) != null )
{
safeBold();
sink.text( details.getNewestUpdate( of( INCREMENTAL ) ).toString() );
sink.text( newestUpdateCache.get( details, of( INCREMENTAL ) ).toString() );
safeBold_();
}
sink.tableCell_();

sink.tableCell();
if ( details.getNewestUpdate( of( MINOR ) ) != null )
if ( newestUpdateCache.get( details, of( MINOR ) ) != null )
{
safeBold();
sink.text( details.getNewestUpdate( of( MINOR ) ).toString() );
sink.text( newestUpdateCache.get( details, of( MINOR ) ).toString() );
safeBold_();
}
sink.tableCell_();

sink.tableCell();
if ( details.getNewestUpdate( of( MAJOR ) ) != null )
if ( newestUpdateCache.get( details, of( MAJOR ) ) != null )
{
safeBold();
sink.text( details.getNewestUpdate( of( MAJOR ) ).toString() );
sink.text( newestUpdateCache.get( details, of( MAJOR ) ).toString() );
safeBold_();
}
sink.tableCell_();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,19 +394,19 @@ protected OverviewStats computeOverviewStats()
OverviewStats stats = new OverviewStats();
model.getAllUpdates().values().forEach( details ->
{
if ( details.getOldestUpdate( of( SUBINCREMENTAL ) ) != null )
if ( oldestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
stats.incrementAny();
}
else if ( details.getOldestUpdate( of( INCREMENTAL ) ) != null )
else if ( oldestUpdateCache.get( details, of( INCREMENTAL ) ) != null )
{
stats.incrementIncremental();
}
else if ( details.getOldestUpdate( of( MINOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MINOR ) ) != null )
{
stats.incrementMinor();
}
else if ( details.getOldestUpdate( of( MAJOR ) ) != null )
else if ( oldestUpdateCache.get( details, of( MAJOR ) ) != null )
{
stats.incrementMajor();
}
Expand Down

0 comments on commit 0c66cc5

Please sign in to comment.