Search in sources :

Example 1 with FlatSearchRequest

use of org.apache.maven.index.FlatSearchRequest in project archiva by apache.

the class ArchivaIndexingTaskExecutor method executeTask.

/**
 * depending on current {@link Task} you have.
 * If {@link org.apache.archiva.scheduler.indexing.ArtifactIndexingTask.Action#FINISH} && isExecuteOnEntireRepo:
 * repository will be scanned.
 *
 * @param task
 * @throws TaskExecutionException
 */
@Override
public void executeTask(Task task) throws TaskExecutionException {
    ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
    ManagedRepository repository = indexingTask.getRepository();
    ArchivaIndexingContext archivaContext = indexingTask.getContext();
    IndexingContext context = null;
    try {
        context = archivaContext.getBaseContext(IndexingContext.class);
    } catch (UnsupportedBaseContextException e) {
        throw new TaskExecutionException("Bad repository type.", e);
    }
    if (ArtifactIndexingTask.Action.FINISH.equals(indexingTask.getAction()) && indexingTask.isExecuteOnEntireRepo()) {
        long start = System.currentTimeMillis();
        try {
            context.updateTimestamp();
            DefaultScannerListener listener = new DefaultScannerListener(context, indexerEngine, true, null);
            ScanningRequest request = new ScanningRequest(context, listener);
            ScanningResult result = scanner.scan(request);
            if (result.hasExceptions()) {
                log.error("Exceptions occured during index scan of " + context.getId());
                result.getExceptions().stream().map(e -> e.getMessage()).distinct().limit(5).forEach(s -> log.error("Message: " + s));
            }
        } catch (IOException e) {
            log.error("Error during context scan {}: {}", context.getId(), context.getIndexDirectory());
        }
        long end = System.currentTimeMillis();
        log.info("indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId(), indexingTask.isOnlyUpdate(), (end - start));
        log.debug("Finishing indexing task on repo: {}", repository.getId());
        finishIndexingTask(indexingTask, repository, context);
    } else {
        // create context if not a repo scan request
        if (!indexingTask.isExecuteOnEntireRepo()) {
            try {
                // 
                log.debug(// 
                "Creating indexing context on resource: {}", (indexingTask.getResourceFile() == null ? "none" : indexingTask.getResourceFile()));
                archivaContext = repository.getIndexingContext();
                context = archivaContext.getBaseContext(IndexingContext.class);
            } catch (UnsupportedBaseContextException e) {
                log.error("Error occurred while creating context: {}", e.getMessage());
                throw new TaskExecutionException("Error occurred while creating context: " + e.getMessage(), e);
            }
        }
        if (context == null || context.getIndexDirectory() == null) {
            throw new TaskExecutionException("Trying to index an artifact but the context is already closed");
        }
        try {
            Path artifactFile = indexingTask.getResourceFile();
            if (artifactFile == null) {
                log.debug("no artifact pass in indexing task so skip it");
            } else {
                ArtifactContext ac = artifactContextProducer.getArtifactContext(context, artifactFile.toFile());
                if (ac != null) {
                    // TODO make that configurable?
                    if (artifactFile.getFileName().toString().endsWith(".pom")) {
                        ac.getArtifactInfo().setFileExtension("pom");
                        ac.getArtifactInfo().setPackaging("pom");
                        ac.getArtifactInfo().setClassifier("pom");
                    }
                    if (indexingTask.getAction().equals(ArtifactIndexingTask.Action.ADD)) {
                        // IndexSearcher s = context.getIndexSearcher();
                        // String uinfo = ac.getArtifactInfo().getUinfo();
                        // TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
                        BooleanQuery.Builder qb = new BooleanQuery.Builder();
                        qb.add(indexer.constructQuery(MAVEN.GROUP_ID, new SourcedSearchExpression(ac.getArtifactInfo().getGroupId())), BooleanClause.Occur.MUST);
                        qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new SourcedSearchExpression(ac.getArtifactInfo().getArtifactId())), BooleanClause.Occur.MUST);
                        qb.add(indexer.constructQuery(MAVEN.VERSION, new SourcedSearchExpression(ac.getArtifactInfo().getVersion())), BooleanClause.Occur.MUST);
                        if (ac.getArtifactInfo().getClassifier() != null) {
                            qb.add(indexer.constructQuery(MAVEN.CLASSIFIER, new SourcedSearchExpression(ac.getArtifactInfo().getClassifier())), BooleanClause.Occur.MUST);
                        }
                        if (ac.getArtifactInfo().getPackaging() != null) {
                            qb.add(indexer.constructQuery(MAVEN.PACKAGING, new SourcedSearchExpression(ac.getArtifactInfo().getPackaging())), BooleanClause.Occur.MUST);
                        }
                        FlatSearchRequest flatSearchRequest = new FlatSearchRequest(qb.build(), context);
                        FlatSearchResponse flatSearchResponse = indexer.searchFlat(flatSearchRequest);
                        if (flatSearchResponse.getResults().isEmpty()) {
                            log.debug("Adding artifact '{}' to index..", ac.getArtifactInfo());
                            indexerEngine.index(context, ac);
                        } else {
                            log.debug("Updating artifact '{}' in index..", ac.getArtifactInfo());
                            // TODO check if update exists !!
                            indexerEngine.update(context, ac);
                        }
                        context.updateTimestamp();
                        context.commit();
                    } else {
                        log.debug("Removing artifact '{}' from index..", ac.getArtifactInfo());
                        indexerEngine.remove(context, ac);
                    }
                }
            }
            // close the context if not a repo scan request
            if (!indexingTask.isExecuteOnEntireRepo()) {
                log.debug("Finishing indexing task on resource file : {}", indexingTask.getResourceFile() != null ? indexingTask.getResourceFile() : " none ");
                finishIndexingTask(indexingTask, repository, context);
            }
        } catch (IOException e) {
            log.error("Error occurred while executing indexing task '{}': {}", indexingTask, e.getMessage(), e);
            throw new TaskExecutionException("Error occurred while executing indexing task '" + indexingTask + "'", e);
        }
    }
}
Also used : ArtifactContext(org.apache.maven.index.ArtifactContext) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) Task(org.apache.archiva.redback.components.taskqueue.Task) ArtifactContextProducer(org.apache.maven.index.ArtifactContextProducer) LoggerFactory(org.slf4j.LoggerFactory) Inject(javax.inject.Inject) FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) Service(org.springframework.stereotype.Service) MAVEN(org.apache.maven.index.MAVEN) IndexPackingRequest(org.apache.maven.index.packer.IndexPackingRequest) TaskExecutionException(org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException) Path(java.nio.file.Path) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) Logger(org.slf4j.Logger) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) IndexPacker(org.apache.maven.index.packer.IndexPacker) DefaultScannerListener(org.apache.maven.index.DefaultScannerListener) ScanningRequest(org.apache.maven.index.ScanningRequest) BooleanClause(org.apache.maven.index_shaded.lucene.search.BooleanClause) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) IOException(java.io.IOException) SourcedSearchExpression(org.apache.maven.index.expr.SourcedSearchExpression) Indexer(org.apache.maven.index.Indexer) ManagedRepository(org.apache.archiva.repository.ManagedRepository) TaskExecutor(org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor) IndexingContext(org.apache.maven.index.context.IndexingContext) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) IndexerEngine(org.apache.maven.index.IndexerEngine) Scanner(org.apache.maven.index.Scanner) ScanningResult(org.apache.maven.index.ScanningResult) Path(java.nio.file.Path) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) ManagedRepository(org.apache.archiva.repository.ManagedRepository) FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) ArtifactContext(org.apache.maven.index.ArtifactContext) IOException(java.io.IOException) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) TaskExecutionException(org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException) ScanningResult(org.apache.maven.index.ScanningResult) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) ScanningRequest(org.apache.maven.index.ScanningRequest) SourcedSearchExpression(org.apache.maven.index.expr.SourcedSearchExpression) DefaultScannerListener(org.apache.maven.index.DefaultScannerListener) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexingContext(org.apache.maven.index.context.IndexingContext)

Example 2 with FlatSearchRequest

use of org.apache.maven.index.FlatSearchRequest in project archiva by apache.

the class ArchivaIndexingTaskExecutorTest method testPackagedIndex.

@Test
public void testPackagedIndex() throws Exception {
    Path basePath = repo.getLocalPath();
    IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
    Path packedIndexDirectory = icf.getLocalPackedIndexPath();
    Path indexerDirectory = icf.getLocalIndexPath();
    Files.list(packedIndexDirectory).filter(path -> path.getFileName().toString().startsWith("nexus-maven-repository-index")).forEach(path -> {
        try {
            System.err.println("Deleting " + path);
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
    Path artifactFile = basePath.resolve("org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar");
    ArtifactIndexingTask task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.ADD, repo.getIndexingContext());
    task.setExecuteOnEntireRepo(false);
    indexingExecutor.executeTask(task);
    task = new ArtifactIndexingTask(repo, null, ArtifactIndexingTask.Action.FINISH, repo.getIndexingContext());
    task.setExecuteOnEntireRepo(false);
    indexingExecutor.executeTask(task);
    assertTrue(Files.exists(packedIndexDirectory));
    assertTrue(Files.exists(indexerDirectory));
    // test packed index file creation
    // no more zip
    // Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
    Assertions.assertThat(Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.properties")));
    Assertions.assertThat(Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.gz")));
    assertFalse(Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.1.gz")));
    // unpack .zip index
    // unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
    DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher(packedIndexDirectory.toFile());
    IndexUpdateRequest updateRequest = new IndexUpdateRequest(getIndexingContext(), fetcher);
    // updateRequest.setLocalIndexCacheDir( indexerDirectory );
    indexUpdater.fetchAndUpdateIndex(updateRequest);
    BooleanQuery.Builder qb = new BooleanQuery.Builder();
    qb.add(indexer.constructQuery(MAVEN.GROUP_ID, new StringSearchExpression("org.apache.archiva")), BooleanClause.Occur.SHOULD);
    qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new StringSearchExpression("archiva-index-methods-jar-test")), BooleanClause.Occur.SHOULD);
    FlatSearchRequest request = new FlatSearchRequest(qb.build(), getIndexingContext());
    FlatSearchResponse response = indexer.searchFlat(request);
    assertEquals(1, response.getTotalHitsCount());
    Set<ArtifactInfo> results = response.getResults();
    ArtifactInfo artifactInfo = results.iterator().next();
    assertEquals("org.apache.archiva", artifactInfo.getGroupId());
    assertEquals("archiva-index-methods-jar-test", artifactInfo.getArtifactId());
    assertEquals("test-repo", artifactInfo.getRepository());
}
Also used : Path(java.nio.file.Path) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) RunWith(org.junit.runner.RunWith) ArtifactInfo(org.apache.maven.index.ArtifactInfo) DefaultIndexUpdater(org.apache.maven.index.updater.DefaultIndexUpdater) IndexUpdater(org.apache.maven.index.updater.IndexUpdater) Inject(javax.inject.Inject) FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) ReleaseScheme(org.apache.archiva.repository.ReleaseScheme) After(org.junit.After) MAVEN(org.apache.maven.index.MAVEN) TopDocs(org.apache.maven.index_shaded.lucene.search.TopDocs) Assertions(org.assertj.core.api.Assertions) TestCase(junit.framework.TestCase) Path(java.nio.file.Path) Before(org.junit.Before) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) Files(java.nio.file.Files) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) BooleanClause(org.apache.maven.index_shaded.lucene.search.BooleanClause) Set(java.util.Set) ArchivaSpringJUnit4ClassRunner(org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) Test(org.junit.Test) IOException(java.io.IOException) SourcedSearchExpression(org.apache.maven.index.expr.SourcedSearchExpression) Indexer(org.apache.maven.index.Indexer) ManagedRepository(org.apache.archiva.repository.ManagedRepository) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IndexingContext(org.apache.maven.index.context.IndexingContext) Paths(java.nio.file.Paths) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IndexSearcher(org.apache.maven.index_shaded.lucene.search.IndexSearcher) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) BasicManagedRepository(org.apache.archiva.repository.BasicManagedRepository) StringSearchExpression(org.apache.maven.index.expr.StringSearchExpression) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IOException(java.io.IOException) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) ArtifactInfo(org.apache.maven.index.ArtifactInfo) DefaultIndexUpdater(org.apache.maven.index.updater.DefaultIndexUpdater) StringSearchExpression(org.apache.maven.index.expr.StringSearchExpression) Test(org.junit.Test)

Example 3 with FlatSearchRequest

use of org.apache.maven.index.FlatSearchRequest in project archiva by apache.

the class MavenRepositorySearch method search.

private SearchResults search(SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds, List<? extends ArtifactInfoFilter> filters, List<String> selectedRepos, boolean includePoms) throws RepositorySearchException {
    try {
        FlatSearchRequest request = new FlatSearchRequest(q);
        request.setContexts(getIndexingContexts(indexingContextIds));
        if (limits != null) {
            // we apply limits only when first page asked
            if (limits.getSelectedPage() == 0) {
                request.setCount(limits.getPageSize() * (Math.max(1, limits.getSelectedPage())));
            }
        }
        FlatSearchResponse response = indexer.searchFlat(request);
        if (response == null || response.getTotalHitsCount() == 0) {
            SearchResults results = new SearchResults();
            results.setLimits(limits);
            return results;
        }
        return convertToSearchResults(response, limits, filters, selectedRepos, includePoms);
    } catch (IOException e) {
        throw new RepositorySearchException(e.getMessage(), e);
    } catch (RepositoryAdminException e) {
        throw new RepositorySearchException(e.getMessage(), e);
    }
}
Also used : FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) IOException(java.io.IOException) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) SearchResults(org.apache.archiva.indexer.search.SearchResults) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException)

Example 4 with FlatSearchRequest

use of org.apache.maven.index.FlatSearchRequest in project archiva by apache.

the class ArchivaIndexingTaskExecutorTest method testAddArtifactToIndex.

@Test
public void testAddArtifactToIndex() throws Exception {
    Path basePath = repo.getLocalPath();
    Path artifactFile = basePath.resolve("org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar");
    ArtifactIndexingTask task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.ADD, repo.getIndexingContext());
    indexingExecutor.executeTask(task);
    task = new ArtifactIndexingTask(repo, null, ArtifactIndexingTask.Action.FINISH, repo.getIndexingContext());
    indexingExecutor.executeTask(task);
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    queryBuilder.add(indexer.constructQuery(MAVEN.GROUP_ID, new StringSearchExpression("org.apache.archiva")), BooleanClause.Occur.SHOULD);
    queryBuilder.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new StringSearchExpression("archiva-index-methods-jar-test")), BooleanClause.Occur.SHOULD);
    BooleanQuery q = queryBuilder.build();
    FlatSearchRequest request = new FlatSearchRequest(q, getIndexingContext());
    FlatSearchResponse response = indexer.searchFlat(request);
    assertTrue(Files.exists(basePath.resolve(".indexer")));
    assertTrue(Files.exists(basePath.resolve(".index")));
    assertEquals(1, response.getTotalHitsCount());
    Set<ArtifactInfo> results = response.getResults();
    ArtifactInfo artifactInfo = results.iterator().next();
    assertEquals("org.apache.archiva", artifactInfo.getGroupId());
    assertEquals("archiva-index-methods-jar-test", artifactInfo.getArtifactId());
    assertEquals("test-repo", artifactInfo.getRepository());
}
Also used : Path(java.nio.file.Path) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) ArtifactInfo(org.apache.maven.index.ArtifactInfo) FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) StringSearchExpression(org.apache.maven.index.expr.StringSearchExpression) Test(org.junit.Test)

Example 5 with FlatSearchRequest

use of org.apache.maven.index.FlatSearchRequest in project archiva by apache.

the class ArchivaIndexingTaskExecutorTest method testRemoveArtifactFromIndex.

@Test
public void testRemoveArtifactFromIndex() throws Exception {
    Path basePath = repo.getLocalPath();
    Path artifactFile = basePath.resolve("org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar");
    ArtifactIndexingTask task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.ADD, repo.getIndexingContext());
    // add artifact to index
    indexingExecutor.executeTask(task);
    BooleanQuery.Builder qb = new BooleanQuery.Builder();
    qb.add(indexer.constructQuery(MAVEN.GROUP_ID, new SourcedSearchExpression("org.apache.archiva")), BooleanClause.Occur.SHOULD);
    // q.add(
    // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
    // Occur.SHOULD );
    IndexingContext ctx = repo.getIndexingContext().getBaseContext(IndexingContext.class);
    FlatSearchRequest flatSearchRequest = new FlatSearchRequest(qb.build(), ctx);
    FlatSearchResponse response = indexer.searchFlat(flatSearchRequest);
    assertTrue(Files.exists(basePath.resolve(".indexer")));
    assertTrue(Files.exists(basePath.resolve(".index")));
    // should return 1 hit
    assertEquals(1, response.getTotalHitsCount());
    // remove added artifact from index
    task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.DELETE, repo.getIndexingContext());
    indexingExecutor.executeTask(task);
    task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.FINISH, repo.getIndexingContext());
    indexingExecutor.executeTask(task);
    qb = new BooleanQuery.Builder();
    qb.add(indexer.constructQuery(MAVEN.GROUP_ID, new SourcedSearchExpression("org.apache.archiva")), BooleanClause.Occur.SHOULD);
    qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new SourcedSearchExpression("archiva-index-methods-jar-test")), BooleanClause.Occur.SHOULD);
    assertTrue(Files.exists(basePath.resolve(".indexer")));
    assertTrue(Files.exists(basePath.resolve(".index")));
    flatSearchRequest = new FlatSearchRequest(qb.build(), getIndexingContext());
    response = indexer.searchFlat(flatSearchRequest);
    // artifact should have been removed from the index!
    // .totalHits );
    assertEquals(0, response.getTotalHitsCount());
// TODO: test it was removed from the packaged index also
}
Also used : Path(java.nio.file.Path) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) SourcedSearchExpression(org.apache.maven.index.expr.SourcedSearchExpression) FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexingContext(org.apache.maven.index.context.IndexingContext) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) Test(org.junit.Test)

Aggregations

FlatSearchRequest (org.apache.maven.index.FlatSearchRequest)6 FlatSearchResponse (org.apache.maven.index.FlatSearchResponse)6 BooleanQuery (org.apache.maven.index_shaded.lucene.search.BooleanQuery)5 Path (java.nio.file.Path)4 IOException (java.io.IOException)3 ArchivaIndexingContext (org.apache.archiva.indexer.ArchivaIndexingContext)3 IndexingContext (org.apache.maven.index.context.IndexingContext)3 SourcedSearchExpression (org.apache.maven.index.expr.SourcedSearchExpression)3 StringSearchExpression (org.apache.maven.index.expr.StringSearchExpression)3 Test (org.junit.Test)3 Inject (javax.inject.Inject)2 UnsupportedBaseContextException (org.apache.archiva.indexer.UnsupportedBaseContextException)2 ManagedRepository (org.apache.archiva.repository.ManagedRepository)2 IndexCreationFeature (org.apache.archiva.repository.features.IndexCreationFeature)2 ArtifactInfo (org.apache.maven.index.ArtifactInfo)2 Indexer (org.apache.maven.index.Indexer)2 MAVEN (org.apache.maven.index.MAVEN)2 BooleanClause (org.apache.maven.index_shaded.lucene.search.BooleanClause)2 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1