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 IndexModifier indexModifier = null;
90 indexModifier = new IndexModifier( indexLocation, indexHandlers.getAnalyzer(), !exists() );
91 indexModifier.setMaxFieldLength( MAX_FIELD_LENGTH );
93 for ( Iterator i = records.iterator(); i.hasNext(); )
95 LuceneRepositoryContentRecord record = (LuceneRepositoryContentRecord) i.next();
99 Term term = new Term( LuceneDocumentMaker.PRIMARY_KEY, record.getPrimaryKey() );
101 indexModifier.deleteDocuments( term );
103 Document document = indexHandlers.getConverter().convert( record );
105 indexModifier.addDocument( document );
108 indexModifier.optimize();
110 catch ( IOException e )
112 throw new RepositoryIndexException( "Error updating index: " + e.getMessage(), e );
116 closeQuietly( indexModifier );
120 public void modifyRecord( LuceneRepositoryContentRecord record )
121 throws RepositoryIndexException
123 IndexModifier indexModifier = null;
126 indexModifier = new IndexModifier( indexLocation, indexHandlers.getAnalyzer(), !exists() );
127 indexModifier.setMaxFieldLength( MAX_FIELD_LENGTH );
129 if ( record != null )
131 Term term = new Term( LuceneDocumentMaker.PRIMARY_KEY, record.getPrimaryKey() );
133 indexModifier.deleteDocuments( term );
135 Document document = indexHandlers.getConverter().convert( record );
137 indexModifier.addDocument( document );
139 indexModifier.optimize();
141 catch ( IOException e )
143 throw new RepositoryIndexException( "Error updating index: " + e.getMessage(), e );
147 closeQuietly( indexModifier );
151 private void addRecords( Collection records )
152 throws RepositoryIndexException
154 IndexWriter indexWriter;
157 indexWriter = new IndexWriter( indexLocation, indexHandlers.getAnalyzer(), !exists() );
158 indexWriter.setMaxFieldLength( MAX_FIELD_LENGTH );
160 catch ( IOException e )
162 throw new RepositoryIndexException( "Unable to open index", e );
167 for ( Iterator i = records.iterator(); i.hasNext(); )
169 LuceneRepositoryContentRecord record = (LuceneRepositoryContentRecord) i.next();
171 if ( record != null )
173 Document document = indexHandlers.getConverter().convert( record );
175 indexWriter.addDocument( document );
179 indexWriter.optimize();
181 catch ( IOException e )
183 throw new RepositoryIndexException( "Failed to add an index document", e );
187 closeQuietly( indexWriter );
191 public void deleteRecords( Collection records )
192 throws RepositoryIndexException
196 IndexReader indexReader = null;
199 indexReader = IndexReader.open( indexLocation );
201 for ( Iterator i = records.iterator(); i.hasNext(); )
203 LuceneRepositoryContentRecord record = (LuceneRepositoryContentRecord) i.next();
205 if ( record != null )
207 Term term = new Term( LuceneDocumentMaker.PRIMARY_KEY, record.getPrimaryKey() );
209 indexReader.deleteDocuments( term );
213 catch ( IOException e )
215 throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
219 closeQuietly( indexReader );
224 public Collection getAllRecordKeys()
225 throws RepositoryIndexException
227 return getAllFieldValues( LuceneDocumentMaker.PRIMARY_KEY );
230 private List getAllFieldValues( String fieldName )
231 throws RepositoryIndexException
233 List keys = new ArrayList();
237 IndexReader indexReader = null;
238 TermEnum terms = null;
241 indexReader = IndexReader.open( indexLocation );
243 terms = indexReader.terms( new Term( fieldName, "" ) );
244 while ( fieldName.equals( terms.term().field() ) )
246 keys.add( terms.term().text() );
254 catch ( IOException e )
256 throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
260 closeQuietly( indexReader );
261 closeQuietly( terms );
267 public Searchable getSearchable()
268 throws RepositoryIndexSearchException
272 IndexSearcher searcher = new IndexSearcher( indexLocation.getAbsolutePath() );
275 catch ( IOException e )
277 throw new RepositoryIndexSearchException( "Unable to open index: " + e.getMessage(), e );
281 public boolean exists()
282 throws RepositoryIndexException
284 if ( IndexReader.indexExists( indexLocation ) )
288 else if ( !indexLocation.exists() )
292 else if ( indexLocation.isDirectory() )
294 if ( indexLocation.listFiles().length > 1 )
296 throw new RepositoryIndexException( indexLocation + " is not a valid index directory." );
305 throw new RepositoryIndexException( indexLocation + " is not a directory." );
309 public QueryParser getQueryParser()
311 return this.indexHandlers.getQueryParser();
314 public static void closeSearchable( Searchable searchable )
316 if( searchable != null )
322 catch ( IOException e )
329 private static void closeQuietly( TermEnum terms )
330 throws RepositoryIndexException
338 catch ( IOException e )
345 private static void closeQuietly( IndexWriter indexWriter )
346 throws RepositoryIndexException
350 if ( indexWriter != null )
355 catch ( IOException e )
357 // write should compain if it can't be closed, data probably not persisted
358 throw new RepositoryIndexException( e.getMessage(), e );
362 private static void closeQuietly( IndexModifier indexModifier )
364 if ( indexModifier != null )
368 indexModifier.close();
370 catch ( IOException e )
377 private static void closeQuietly( IndexReader reader )
381 if ( reader != null )
386 catch ( IOException e )
392 public File getIndexDirectory()
394 return this.indexLocation;
397 public String getId()
399 return this.indexHandlers.getId();
402 public ManagedRepositoryConfiguration getRepository()
407 public Analyzer getAnalyzer()
409 return this.indexHandlers.getAnalyzer();
412 public LuceneEntryConverter getEntryConverter()
414 return this.indexHandlers.getConverter();