indexing simplification

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@367160 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2006-01-09 02:16:47 +00:00
parent d9fa724185
commit dfe1b9b09f
5 changed files with 53 additions and 87 deletions

View File

@ -36,19 +36,24 @@ public abstract class AbstractRepositoryIndex
private boolean indexOpen;
private IndexReader indexReader;
private IndexWriter indexWriter;
protected ArtifactRepository repository;
public AbstractRepositoryIndex( String indexPath, ArtifactRepository repository )
protected AbstractRepositoryIndex( String indexPath, ArtifactRepository repository, String[] indexFields )
throws RepositoryIndexException
{
this.repository = repository;
this.indexPath = indexPath;
open( indexPath );
try
{
validateIndex( indexFields );
}
catch ( IOException e )
{
throw new RepositoryIndexException( e );
}
}
/**
@ -96,12 +101,6 @@ public abstract class AbstractRepositoryIndex
indexWriter = null;
}
if ( indexReader != null )
{
indexReader.close();
indexReader = null;
}
indexOpen = false;
}
catch ( IOException e )
@ -110,23 +109,6 @@ public abstract class AbstractRepositoryIndex
}
}
/**
* method for opening the index directory for indexing operations
*/
protected void open( String indexPath )
throws RepositoryIndexException
{
try
{
this.indexPath = indexPath;
validateIndex();
}
catch ( IOException e )
{
throw new RepositoryIndexException( e );
}
}
public String getIndexPath()
{
return indexPath;
@ -142,50 +124,42 @@ public abstract class AbstractRepositoryIndex
return indexWriter;
}
private IndexReader getIndexReader()
throws IOException
{
if ( indexReader == null )
{
indexReader = IndexReader.open( indexPath );
}
return indexReader;
}
/**
* method for validating an index directory
*
* @param indexFields
* @throws RepositoryIndexException if the given indexPath is not valid for this type of RepositoryIndex
*/
private void validateIndex()
private void validateIndex( String[] indexFields )
throws RepositoryIndexException, IOException
{
File indexDir = new File( indexPath );
if ( IndexReader.indexExists( indexDir ) )
{
IndexReader indexReader = getIndexReader();
if ( indexReader.numDocs() > 0 )
IndexReader indexReader = IndexReader.open( indexPath );
try
{
Collection fields = indexReader.getFieldNames();
String[] indexFields = getIndexFields();
for ( int idx = 0; idx < indexFields.length; idx++ )
if ( indexReader.numDocs() > 0 )
{
if ( !fields.contains( indexFields[idx] ) )
Collection fields = indexReader.getFieldNames();
for ( int idx = 0; idx < indexFields.length; idx++ )
{
throw new RepositoryIndexException(
"The Field " + indexFields[idx] + " does not exist in " + "index path " + indexPath + "." );
if ( !fields.contains( indexFields[idx] ) )
{
throw new RepositoryIndexException(
"The Field " + indexFields[idx] + " does not exist in index " + indexPath + "." );
}
}
}
}
else
finally
{
//getLogger().info( "Skipping index field validations for empty index." );
indexReader.close();
}
}
else if ( !indexDir.exists() )
{
indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
//getLogger().info( "New index directory created in: " + indexDir.getAbsolutePath() );
}
else if ( indexDir.isDirectory() )
{

View File

@ -28,6 +28,7 @@ import org.apache.maven.repository.indexing.query.CompoundQuery;
import org.apache.maven.repository.indexing.query.CompoundQueryTerm;
import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.io.IOException;
import java.util.ArrayList;
@ -38,6 +39,7 @@ import java.util.List;
*
*/
public abstract class AbstractRepositoryIndexSearcher
extends AbstractLogEnabled
implements RepositoryIndexSearcher
{
protected RepositoryIndex index;
@ -63,43 +65,50 @@ public abstract class AbstractRepositoryIndexSearcher
public List search( Query query )
throws RepositoryIndexSearchException
{
IndexSearcher searcher;
org.apache.lucene.search.Query luceneQuery;
try
{
luceneQuery = createLuceneQuery( query );
}
catch ( ParseException e )
{
throw new RepositoryIndexSearchException( "Unable to construct query: " + e.getMessage(), e );
}
IndexSearcher searcher;
try
{
searcher = new IndexSearcher( index.getIndexPath() );
}
catch ( IOException e )
{
throw new RepositoryIndexSearchException( e.getMessage(), e );
throw new RepositoryIndexSearchException( "Unable to open index: " + e.getMessage(), e );
}
Hits hits;
List docs;
try
{
hits = searcher.search( createLuceneQuery( query ) );
Hits hits = searcher.search( luceneQuery );
docs = buildList( hits );
}
catch ( IOException e )
{
throw new RepositoryIndexSearchException( e.getMessage(), e );
throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(), e );
}
catch ( ParseException e )
finally
{
throw new RepositoryIndexSearchException( e.getMessage(), e );
try
{
searcher.close();
}
catch ( IOException e )
{
getLogger().error( "Unable to close index searcher", e );
}
}
List artifactList;
try
{
artifactList = buildList( hits );
searcher.close();
}
catch ( IOException ie )
{
throw new RepositoryIndexSearchException( ie.getMessage(), ie );
}
return artifactList;
return docs;
}
private org.apache.lucene.search.Query createLuceneQuery( String field, String value )

View File

@ -69,7 +69,7 @@ public class ArtifactRepositoryIndex
public ArtifactRepositoryIndex( String indexPath, ArtifactRepository repository, Digester digester )
throws RepositoryIndexException
{
super( indexPath, repository );
super( indexPath, repository, FIELDS );
this.digester = digester;
}
@ -88,16 +88,6 @@ public class ArtifactRepositoryIndex
return analyzer;
}
/**
* method for collecting the available index fields usable for searching
*
* @return index field names
*/
public String[] getIndexFields()
{
return FIELDS;
}
/**
* generic method for indexing
*

View File

@ -79,7 +79,7 @@ public class PomRepositoryIndex
ArtifactFactory artifactFactory )
throws RepositoryIndexException
{
super( indexPath, repository );
super( indexPath, repository, FIELDS );
this.digester = digester;
this.artifactFactory = artifactFactory;
}
@ -108,11 +108,6 @@ public class PomRepositoryIndex
}
}
public String[] getIndexFields()
{
return FIELDS;
}
public void indexPom( Model pom )
throws RepositoryIndexException
{

View File

@ -26,8 +26,6 @@ public interface RepositoryIndex
{
String ROLE = RepositoryIndex.class.getName();
String[] getIndexFields();
boolean isOpen();
void index( Object obj )