1 package org.apache.archiva.indexer.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.commons.io.FileUtils;
22 import org.apache.commons.lang.time.StopWatch;
23 import org.apache.maven.index.NexusIndexer;
24 import org.apache.maven.index.context.IndexCreator;
25 import org.apache.maven.index.context.IndexingContext;
26 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
27 import org.apache.maven.index.packer.IndexPacker;
28 import org.apache.maven.index.packer.IndexPackingRequest;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.scheduling.annotation.Async;
32 import org.springframework.stereotype.Service;
34 import javax.inject.Inject;
36 import java.io.IOException;
37 import java.util.Collection;
38 import java.util.List;
39 import java.util.concurrent.CopyOnWriteArrayList;
42 * @author Olivier Lamy
45 @Service("indexMerger#default")
46 public class DefaultIndexMerger
47 implements IndexMerger
50 private Logger log = LoggerFactory.getLogger( getClass() );
52 private final NexusIndexer indexer;
54 private final IndexPacker indexPacker;
56 private final List<IndexCreator> indexCreators;
58 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<>();
60 private List<String> runningGroups = new CopyOnWriteArrayList<>();
63 public DefaultIndexMerger( NexusIndexer nexusIndexer, IndexPacker indexPacker, List<IndexCreator> indexCreators )
65 this.indexer = nexusIndexer;
66 this.indexPacker = indexPacker;
67 this.indexCreators = indexCreators;
71 public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
72 throws IndexMergerException
74 String groupId = indexMergerRequest.getGroupId();
76 if ( runningGroups.contains( groupId ) )
78 log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
82 runningGroups.add( groupId );
84 StopWatch stopWatch = new StopWatch();
88 File mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
90 String tempRepoId = mergedIndexDirectory.getName();
94 File indexLocation = new File( mergedIndexDirectory, indexMergerRequest.getMergedIndexPath() );
95 IndexingContext indexingContext =
96 indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory, indexLocation, null, null,
99 for ( String repoId : indexMergerRequest.getRepositoriesIds() )
101 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
102 if ( idxToMerge != null )
104 indexingContext.merge( idxToMerge.getIndexDirectory() );
108 indexingContext.optimize();
110 if ( indexMergerRequest.isPackIndex() )
112 IndexPackingRequest request = new IndexPackingRequest( indexingContext, //
113 indexingContext.acquireIndexSearcher().getIndexReader(), //
115 indexPacker.packIndex( request );
118 if ( indexMergerRequest.isTemporary() )
120 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
121 indexMergerRequest.getMergedIndexTtl() ) );
124 log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
125 stopWatch.getTime() );
126 return indexingContext;
128 catch ( IOException | UnsupportedExistingLuceneIndexException e )
130 throw new IndexMergerException( e.getMessage(), e );
134 runningGroups.remove( groupId );
140 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
142 if ( temporaryGroupIndex == null )
149 IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
150 if ( indexingContext != null )
152 indexer.removeIndexingContext( indexingContext, true );
154 File directory = temporaryGroupIndex.getDirectory();
155 if ( directory != null && directory.exists() )
157 FileUtils.deleteDirectory( directory );
159 temporaryGroupIndexes.remove( temporaryGroupIndex );
161 catch ( IOException e )
163 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
168 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
170 return this.temporaryGroupIndexes;