]> source.dussan.org Git - archiva.git/blob
0e0cd90598fd3f4a6a7f92fcdc1ced207d040740
[archiva.git] /
1 package org.apache.archiva.indexer.maven.merger;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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;
37
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;
45
46 /**
47  * @author Olivier Lamy
48  * @since 1.4-M2
49  */
50 @Service("indexMerger#default")
51 public class DefaultIndexMerger
52     implements IndexMerger
53 {
54
55     private Logger log = LoggerFactory.getLogger( getClass() );
56
57     private final NexusIndexer indexer;
58
59     private final IndexPacker indexPacker;
60
61     private final List<IndexCreator> indexCreators;
62
63     private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<>();
64
65     private List<String> runningGroups = new CopyOnWriteArrayList<>();
66
67     @Inject
68     public DefaultIndexMerger( NexusIndexer nexusIndexer, IndexPacker indexPacker, List<IndexCreator> indexCreators )
69     {
70         this.indexer = nexusIndexer;
71         this.indexPacker = indexPacker;
72         this.indexCreators = indexCreators;
73     }
74
75     @Override
76     public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
77         throws IndexMergerException
78     {
79         String groupId = indexMergerRequest.getGroupId();
80
81         if ( runningGroups.contains( groupId ) )
82         {
83             log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
84             return null;
85         }
86
87         runningGroups.add( groupId );
88
89         StopWatch stopWatch = new StopWatch();
90         stopWatch.reset();
91         stopWatch.start();
92
93         Path mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
94
95         String tempRepoId = mergedIndexDirectory.getFileName().toString();
96
97         try
98         {
99             Path indexLocation = mergedIndexDirectory.resolve( indexMergerRequest.getMergedIndexPath() );
100             IndexingContext indexingContext =
101                 indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory.toFile(), indexLocation.toFile(), null, null,
102                                             indexCreators );
103
104             for ( String repoId : indexMergerRequest.getRepositoriesIds() )
105             {
106                 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
107                 if ( idxToMerge != null )
108                 {
109                     indexingContext.merge( idxToMerge.getIndexDirectory() );
110                 }
111             }
112
113             indexingContext.optimize();
114
115             if ( indexMergerRequest.isPackIndex() )
116             {
117                 IndexPackingRequest request = new IndexPackingRequest( indexingContext, //
118                                                                        indexingContext.acquireIndexSearcher().getIndexReader(), //
119                                                                        indexLocation.toFile() );
120                 indexPacker.packIndex( request );
121             }
122
123             if ( indexMergerRequest.isTemporary() )
124             {
125                 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
126                                                                     indexMergerRequest.getMergedIndexTtl() ) );
127             }
128             stopWatch.stop();
129             log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
130                       stopWatch.getTime() );
131             return indexingContext;
132         }
133         catch ( IOException | UnsupportedExistingLuceneIndexException e )
134         {
135             throw new IndexMergerException( e.getMessage(), e );
136         }
137         finally
138         {
139             runningGroups.remove( groupId );
140         }
141     }
142
143     @Async
144     @Override
145     public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
146     {
147         if ( temporaryGroupIndex == null )
148         {
149             return;
150         }
151
152         try
153         {
154             IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
155             if ( indexingContext != null )
156             {
157                 indexer.removeIndexingContext( indexingContext, true );
158             }
159             Path directory = temporaryGroupIndex.getDirectory();
160             if ( directory != null && Files.exists(directory) )
161             {
162                 FileUtils.deleteDirectory( directory );
163             }
164             temporaryGroupIndexes.remove( temporaryGroupIndex );
165         }
166         catch ( IOException e )
167         {
168             log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
169         }
170     }
171
172     @Override
173     public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
174     {
175         return this.temporaryGroupIndexes;
176     }
177 }