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 Collection getAllRecordKeys()
238 throws RepositoryIndexException
240 return getAllFieldValues( LuceneDocumentMaker.PRIMARY_KEY );
243 private List getAllFieldValues( String fieldName )
244 throws RepositoryIndexException
246 synchronized( repository )
248 List keys = new ArrayList();
252 IndexReader indexReader = null;
253 TermEnum terms = null;
256 indexReader = IndexReader.open( indexLocation );
258 terms = indexReader.terms( new Term( fieldName, "" ) );
259 while ( fieldName.equals( terms.term().field() ) )
261 keys.add( terms.term().text() );
269 catch ( IOException e )
271 throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
275 closeQuietly( indexReader );
276 closeQuietly( terms );
283 public Searchable getSearchable()
284 throws RepositoryIndexSearchException
288 IndexSearcher searcher = new IndexSearcher( indexLocation.getAbsolutePath() );
291 catch ( IOException e )
293 throw new RepositoryIndexSearchException( "Unable to open index: " + e.getMessage(), e );
297 public boolean exists()
298 throws RepositoryIndexException
300 if ( IndexReader.indexExists( indexLocation ) )
304 else if ( !indexLocation.exists() )
308 else if ( indexLocation.isDirectory() )
310 if ( indexLocation.listFiles().length > 1 )
312 throw new RepositoryIndexException( indexLocation + " is not a valid index directory." );
321 throw new RepositoryIndexException( indexLocation + " is not a directory." );
325 public QueryParser getQueryParser()
327 return this.indexHandlers.getQueryParser();
330 public static void closeSearchable( Searchable searchable )
332 if( searchable != null )
338 catch ( IOException e )
345 private static void closeQuietly( TermEnum terms )
346 throws RepositoryIndexException
354 catch ( IOException e )
361 private static void closeQuietly( IndexWriter indexWriter )
362 throws RepositoryIndexException
366 if ( indexWriter != null )
371 catch ( IOException e )
373 // write should compain if it can't be closed, data probably not persisted
374 throw new RepositoryIndexException( e.getMessage(), e );
378 private static void closeQuietly( IndexModifier indexModifier )
380 if ( indexModifier != null )
384 indexModifier.close();
386 catch ( IOException e )
393 private static void closeQuietly( IndexReader reader )
397 if ( reader != null )
402 catch ( IOException e )
408 public File getIndexDirectory()
410 return this.indexLocation;
413 public String getId()
415 return this.indexHandlers.getId();
418 public ManagedRepositoryConfiguration getRepository()
423 public Analyzer getAnalyzer()
425 return this.indexHandlers.getAnalyzer();
428 public LuceneEntryConverter getEntryConverter()
430 return this.indexHandlers.getConverter();