From: Martin Stockhammer Date: Mon, 13 Nov 2017 17:25:00 +0000 (+0100) Subject: Implementing artifact add and removal in IndexManager X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f4a2623aa0ea1b715af01208a7ab576c71d0f1fc;p=archiva.git Implementing artifact add and removal in IndexManager --- diff --git a/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java b/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java index 30cd3d804..7e6d3c57f 100644 --- a/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java +++ b/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java @@ -42,12 +42,7 @@ import org.apache.archiva.repository.UnsupportedRepositoryTypeException; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.RemoteIndexFeature; import org.apache.commons.lang.StringUtils; -import org.apache.maven.index.DefaultScannerListener; -import org.apache.maven.index.Indexer; -import org.apache.maven.index.IndexerEngine; -import org.apache.maven.index.Scanner; -import org.apache.maven.index.ScanningRequest; -import org.apache.maven.index.ScanningResult; +import org.apache.maven.index.*; import org.apache.maven.index.context.IndexCreator; import org.apache.maven.index.context.IndexingContext; import org.apache.maven.index.packer.IndexPacker; @@ -83,12 +78,19 @@ import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentSkipListSet; +import java.util.stream.Collectors; /** - * Maven implementation of index manager + * Maven implementation of index manager. + * The index manager is a singleton, so we try to make sure, that index operations are not running + * parallel by synchronizing on the index path. + * A update operation waits for parallel running methods to finish before starting, but after a certain + * time of retries a IndexUpdateFailedException is thrown. */ @Service( "archivaIndexManager#maven" ) public class MavenIndexManager implements ArchivaIndexManager @@ -123,6 +125,9 @@ public class MavenIndexManager implements ArchivaIndexManager @Inject private IndexUpdater indexUpdater; + @Inject + private ArtifactContextProducer artifactContextProducer; + private ConcurrentSkipListSet activeContexts = new ConcurrentSkipListSet<>( ); private static final int WAIT_TIME = 100; @@ -151,6 +156,10 @@ public class MavenIndexManager implements ArchivaIndexManager void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException; } + /* + * This method is used to do some actions around the update execution code. And to make sure, that no other + * method is running on the same index. + */ private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException { IndexingContext indexingContext = null; @@ -374,14 +383,33 @@ public class MavenIndexManager implements ArchivaIndexManager } @Override - public void addArtifactToIndex( ArchivaIndexingContext context, ArtifactReference artifactReference ) throws IndexUpdateFailedException + public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection artifactReference ) throws IndexUpdateFailedException { - + executeUpdateFunction(context, indexingContext -> { + Collection artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList()); + try { + indexer.addArtifactsToIndex(artifacts, indexingContext); + } catch (IOException e) { + log.error("IOException while adding artifact {}", e.getMessage(), e); + throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId() + + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : "")); + } + }); } @Override - public void removeArtifactFromIndex( ArchivaIndexingContext context, ArtifactReference artifactReference ) throws IndexUpdateFailedException + public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection artifactReference ) throws IndexUpdateFailedException { + executeUpdateFunction(context, indexingContext -> { + Collection artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList()); + try { + indexer.deleteArtifactsFromIndex(artifacts, indexingContext); + } catch (IOException e) { + log.error("IOException while removing artifact {}", e.getMessage(), e); + throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId() + + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : "")); + } + }); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java index b4bf63e5b..96e578042 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java @@ -25,6 +25,8 @@ import org.apache.archiva.repository.RepositoryType; import java.io.IOException; import java.net.URI; +import java.nio.file.Path; +import java.util.Collection; public interface ArchivaIndexManager { @@ -50,18 +52,18 @@ public interface ArchivaIndexManager { void update(ArchivaIndexingContext context, URI remoteUpdateUri, boolean fullUpdate) throws IndexUpdateFailedException; /** - * Adds a artifact to the index. + * Adds a list of artifacts to the index. * @param context * @param artifactReference */ - void addArtifactToIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) throws IndexUpdateFailedException; + void addArtifactsToIndex(ArchivaIndexingContext context, Collection artifactReference) throws IndexUpdateFailedException; /** - * Removes a artifact from the index. + * Removes a list of artifacts from the index. * @param context * @param artifactReference */ - void removeArtifactFromIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) throws IndexUpdateFailedException; + void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection artifactReference) throws IndexUpdateFailedException; /** diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java index 2e9ee171c..931c60ac6 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java @@ -25,6 +25,8 @@ import org.apache.archiva.repository.RepositoryType; import org.springframework.stereotype.Service; import java.net.URI; +import java.nio.file.Path; +import java.util.Collection; @Service("indexManager#none") public class GenericIndexManager implements ArchivaIndexManager { @@ -45,12 +47,12 @@ public class GenericIndexManager implements ArchivaIndexManager { } @Override - public void addArtifactToIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) { + public void addArtifactsToIndex(ArchivaIndexingContext context, Collection artifactReference) { } @Override - public void removeArtifactFromIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) { + public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection artifactReference) { }