1 package org.apache.archiva.indexer.maven.merger;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.utils.FileUtils;
22 import org.apache.archiva.indexer.merger.IndexMerger;
23 import org.apache.archiva.indexer.merger.IndexMergerException;
24 import org.apache.archiva.indexer.merger.IndexMergerRequest;
25 import org.apache.archiva.indexer.merger.TemporaryGroupIndex;
26 import org.apache.commons.lang.time.StopWatch;
27 import org.apache.maven.index.NexusIndexer;
28 import org.apache.maven.index.context.IndexCreator;
29 import org.apache.maven.index.context.IndexingContext;
30 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
31 import org.apache.maven.index.packer.IndexPacker;
32 import org.apache.maven.index.packer.IndexPackingRequest;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.scheduling.annotation.Async;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import java.io.IOException;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.util.Collection;
43 import java.util.List;
44 import java.util.concurrent.CopyOnWriteArrayList;
47 * @author Olivier Lamy
50 @Service("indexMerger#default")
51 public class DefaultIndexMerger
52 implements IndexMerger
55 private Logger log = LoggerFactory.getLogger( getClass() );
57 private final NexusIndexer indexer;
59 private final IndexPacker indexPacker;
61 private final List<IndexCreator> indexCreators;
63 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<>();
65 private List<String> runningGroups = new CopyOnWriteArrayList<>();
68 public DefaultIndexMerger( NexusIndexer nexusIndexer, IndexPacker indexPacker, List<IndexCreator> indexCreators )
70 this.indexer = nexusIndexer;
71 this.indexPacker = indexPacker;
72 this.indexCreators = indexCreators;
76 public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
77 throws IndexMergerException
79 String groupId = indexMergerRequest.getGroupId();
81 if ( runningGroups.contains( groupId ) )
83 log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
87 runningGroups.add( groupId );
89 StopWatch stopWatch = new StopWatch();
93 Path mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
95 String tempRepoId = mergedIndexDirectory.getFileName().toString();
99 Path indexLocation = mergedIndexDirectory.resolve( indexMergerRequest.getMergedIndexPath() );
100 IndexingContext indexingContext =
101 indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory.toFile(), indexLocation.toFile(), null, null,
104 for ( String repoId : indexMergerRequest.getRepositoriesIds() )
106 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
107 if ( idxToMerge != null )
109 indexingContext.merge( idxToMerge.getIndexDirectory() );
113 indexingContext.optimize();
115 if ( indexMergerRequest.isPackIndex() )
117 IndexPackingRequest request = new IndexPackingRequest( indexingContext, //
118 indexingContext.acquireIndexSearcher().getIndexReader(), //
119 indexLocation.toFile() );
120 indexPacker.packIndex( request );
123 if ( indexMergerRequest.isTemporary() )
125 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
126 indexMergerRequest.getMergedIndexTtl() ) );
129 log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
130 stopWatch.getTime() );
131 return indexingContext;
133 catch ( IOException | UnsupportedExistingLuceneIndexException e )
135 throw new IndexMergerException( e.getMessage(), e );
139 runningGroups.remove( groupId );
145 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
147 if ( temporaryGroupIndex == null )
154 IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
155 if ( indexingContext != null )
157 indexer.removeIndexingContext( indexingContext, true );
159 Path directory = temporaryGroupIndex.getDirectory();
160 if ( directory != null && Files.exists(directory) )
162 FileUtils.deleteDirectory( directory );
164 temporaryGroupIndexes.remove( temporaryGroupIndex );
166 catch ( IOException e )
168 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
173 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
175 return this.temporaryGroupIndexes;