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