1 package org.apache.maven.archiva.indexer.lucene;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.index.IndexModifier;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.IndexWriter;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.index.TermEnum;
29 import org.apache.lucene.queryParser.QueryParser;
30 import org.apache.lucene.search.IndexSearcher;
31 import org.apache.lucene.search.Searchable;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
34 import org.apache.maven.archiva.indexer.RepositoryIndexException;
35 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.Iterator;
42 import java.util.List;
45 * Lucene implementation of a repository index.
47 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
49 public class LuceneRepositoryContentIndex
50 implements RepositoryContentIndex
53 * The max field length for a field in a document.
55 private static final int MAX_FIELD_LENGTH = 40000;
58 * The location of the index on the file system.
60 private File indexLocation;
63 * The Lucene Index Handlers
65 private LuceneIndexHandlers indexHandlers;
67 private ManagedRepositoryConfiguration repository;
69 public LuceneRepositoryContentIndex( ManagedRepositoryConfiguration repository, File indexDir, LuceneIndexHandlers handlers )
71 this.repository = repository;
72 this.indexLocation = indexDir;
73 this.indexHandlers = handlers;
76 public void indexRecords( Collection records )
77 throws RepositoryIndexException
79 deleteRecords( records );
81 addRecords( records );
84 public void modifyRecords( Collection records )
85 throws RepositoryIndexException
87 synchronized( repository )
89 IndexModifier indexModifier = null;
92 indexModifier = new IndexModifier( indexLocation, indexHandlers.getAnalyzer(), !exists() );
93 indexModifier.setMaxFieldLength( MAX_FIELD_LENGTH );
95 for ( Iterator i = records.iterator(); i.hasNext(); )
97 LuceneRepositoryContentRecord record = (LuceneRepositoryContentRecord) i.next();
101 Term term = new Term( LuceneDocumentMaker.PRIMARY_KEY, record.getPrimaryKey() );
103 indexModifier.deleteDocuments( term );
105 Document document = indexHandlers.getConverter().convert( record );
107 indexModifier.addDocument( document );
110 indexModifier.optimize();
112 catch ( IOException e )
114 throw new RepositoryIndexException( "Error updating index: " + e.getMessage(), e );
118 closeQuietly( indexModifier );
123 public void modifyRecord( LuceneRepositoryContentRecord record )
124 throws RepositoryIndexException
126 synchronized( repository )
128 IndexModifier indexModifier = null;
131 indexModifier = new IndexModifier( indexLocation, indexHandlers.getAnalyzer(), !exists() );
132 indexModifier.setMaxFieldLength( MAX_FIELD_LENGTH );
134 if ( record != null )
136 Term term = new Term( LuceneDocumentMaker.PRIMARY_KEY, record.getPrimaryKey() );
138 indexModifier.deleteDocuments( term );
140 Document document = indexHandlers.getConverter().convert( record );
142 indexModifier.addDocument( document );
144 indexModifier.optimize();
146 catch ( IOException e )
148 throw new RepositoryIndexException( "Error updating index: " + e.getMessage(), e );
152 closeQuietly( indexModifier );
158 private void addRecords( Collection records )
159 throws RepositoryIndexException
161 synchronized( repository )
163 IndexWriter indexWriter;
166 indexWriter = new IndexWriter( indexLocation, indexHandlers.getAnalyzer(), !exists() );
167 indexWriter.setMaxFieldLength( MAX_FIELD_LENGTH );
169 catch ( IOException e )
171 throw new RepositoryIndexException( "Unable to open index", e );
176 for ( Iterator i = records.iterator(); i.hasNext(); )
178 LuceneRepositoryContentRecord record = (LuceneRepositoryContentRecord) i.next();
180 if ( record != null )
182 Document document = indexHandlers.getConverter().convert( record );
184 indexWriter.addDocument( document );
188 indexWriter.optimize();
190 catch ( IOException e )
192 throw new RepositoryIndexException( "Failed to add an index document", e );
196 closeQuietly( indexWriter );
201 public void deleteRecords( Collection records )
202 throws RepositoryIndexException
204 synchronized( repository )
208 IndexReader indexReader = null;
211 indexReader = IndexReader.open( indexLocation );
213 for ( Iterator i = records.iterator(); i.hasNext(); )
215 LuceneRepositoryContentRecord record = (LuceneRepositoryContentRecord) i.next();
217 if ( record != null )
219 Term term = new Term( LuceneDocumentMaker.PRIMARY_KEY, record.getPrimaryKey() );
221 indexReader.deleteDocuments( term );
225 catch ( IOException e )
227 throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
231 closeQuietly( indexReader );
237 public void deleteRecord( LuceneRepositoryContentRecord record )
238 throws RepositoryIndexException
240 synchronized( repository )
244 IndexReader indexReader = null;
247 indexReader = IndexReader.open( indexLocation );
249 if ( record != null )
251 Term term = new Term( LuceneDocumentMaker.PRIMARY_KEY, record.getPrimaryKey() );
253 indexReader.deleteDocuments( term );
256 catch ( IOException e )
258 throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
262 closeQuietly( indexReader );
269 public Collection getAllRecordKeys()
270 throws RepositoryIndexException
272 return getAllFieldValues( LuceneDocumentMaker.PRIMARY_KEY );
275 private List getAllFieldValues( String fieldName )
276 throws RepositoryIndexException
278 synchronized( repository )
280 List keys = new ArrayList();
284 IndexReader indexReader = null;
285 TermEnum terms = null;
288 indexReader = IndexReader.open( indexLocation );
290 terms = indexReader.terms( new Term( fieldName, "" ) );
291 while ( fieldName.equals( terms.term().field() ) )
293 keys.add( terms.term().text() );
301 catch ( IOException e )
303 throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
307 closeQuietly( indexReader );
308 closeQuietly( terms );
315 public Searchable getSearchable()
316 throws RepositoryIndexSearchException
320 IndexSearcher searcher = new IndexSearcher( indexLocation.getAbsolutePath() );
323 catch ( IOException e )
325 throw new RepositoryIndexSearchException( "Unable to open index: " + e.getMessage(), e );
329 public boolean exists()
330 throws RepositoryIndexException
332 if ( IndexReader.indexExists( indexLocation ) )
336 else if ( !indexLocation.exists() )
340 else if ( indexLocation.isDirectory() )
342 if ( indexLocation.listFiles().length > 1 )
344 throw new RepositoryIndexException( indexLocation + " is not a valid index directory." );
353 throw new RepositoryIndexException( indexLocation + " is not a directory." );
357 public QueryParser getQueryParser()
359 return this.indexHandlers.getQueryParser();
362 public static void closeSearchable( Searchable searchable )
364 if( searchable != null )
370 catch ( IOException e )
377 private static void closeQuietly( TermEnum terms )
378 throws RepositoryIndexException
386 catch ( IOException e )
393 private static void closeQuietly( IndexWriter indexWriter )
394 throws RepositoryIndexException
398 if ( indexWriter != null )
403 catch ( IOException e )
405 // write should compain if it can't be closed, data probably not persisted
406 throw new RepositoryIndexException( e.getMessage(), e );
410 private static void closeQuietly( IndexModifier indexModifier )
412 if ( indexModifier != null )
416 indexModifier.close();
418 catch ( IOException e )
425 private static void closeQuietly( IndexReader reader )
429 if ( reader != null )
434 catch ( IOException e )
440 public File getIndexDirectory()
442 return this.indexLocation;
445 public String getId()
447 return this.indexHandlers.getId();
450 public ManagedRepositoryConfiguration getRepository()
455 public Analyzer getAnalyzer()
457 return this.indexHandlers.getAnalyzer();
460 public LuceneEntryConverter getEntryConverter()
462 return this.indexHandlers.getConverter();