[MNG-127] move other repoman code to use new indexer

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@426403 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2006-07-28 06:41:56 +00:00
parent 7c31a1f9be
commit 8a2f6fc754
8 changed files with 189 additions and 182 deletions

View File

@ -16,6 +16,7 @@
* limitations under the License. * limitations under the License.
*/ */
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.configuration.Configuration; import org.apache.maven.repository.configuration.Configuration;
import org.apache.maven.repository.configuration.ConfigurationStore; import org.apache.maven.repository.configuration.ConfigurationStore;
@ -23,15 +24,15 @@
import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory; import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory;
import org.apache.maven.repository.discovery.ArtifactDiscoverer; import org.apache.maven.repository.discovery.ArtifactDiscoverer;
import org.apache.maven.repository.discovery.DiscovererException; import org.apache.maven.repository.discovery.DiscovererException;
import org.apache.maven.repository.discovery.MetadataDiscoverer; import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
import org.apache.maven.repository.indexing.ArtifactRepositoryIndex; import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
import org.apache.maven.repository.indexing.MetadataRepositoryIndex;
import org.apache.maven.repository.indexing.RepositoryIndex;
import org.apache.maven.repository.indexing.RepositoryIndexException; import org.apache.maven.repository.indexing.RepositoryIndexException;
import org.apache.maven.repository.indexing.RepositoryIndexingFactory; import org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory;
import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -55,7 +56,7 @@ public class IndexerTask
/** /**
* @plexus.requirement * @plexus.requirement
*/ */
private RepositoryIndexingFactory indexFactory; private RepositoryArtifactIndexFactory indexFactory;
/** /**
* @plexus.requirement * @plexus.requirement
@ -68,9 +69,9 @@ public class IndexerTask
private Map artifactDiscoverers; private Map artifactDiscoverers;
/** /**
* @plexus.requirement role="org.apache.maven.repository.discovery.MetadataDiscoverer" * @plexus.requirement role-hint="standard"
*/ */
private Map metadataDiscoverers; private RepositoryIndexRecordFactory recordFactory;
public void execute() public void execute()
throws TaskExecutionException throws TaskExecutionException
@ -112,15 +113,6 @@ private void execute( Configuration configuration, File indexPath )
getLogger().info( "Indexing " + artifacts.size() + " new artifacts" ); getLogger().info( "Indexing " + artifacts.size() + " new artifacts" );
indexArtifact( artifacts, indexPath, defaultRepository ); indexArtifact( artifacts, indexPath, defaultRepository );
} }
MetadataDiscoverer metadataDiscoverer = (MetadataDiscoverer) metadataDiscoverers.get( layoutProperty );
List metadataList =
metadataDiscoverer.discoverMetadata( defaultRepository, "indexer", blacklistedPatterns );
if ( !metadataList.isEmpty() )
{
getLogger().info( "Indexing " + metadataList.size() + " new metadata files" );
indexMetadata( metadataList, indexPath, defaultRepository );
}
} }
catch ( RepositoryIndexException e ) catch ( RepositoryIndexException e )
{ {
@ -153,8 +145,8 @@ public void executeNowIfNeeded()
try try
{ {
ArtifactRepository repository = repoFactory.createRepository( configuration ); ArtifactRepository repository = repoFactory.createRepository( configuration );
RepositoryIndex artifactIndex = indexFactory.createArtifactRepositoryIndex( indexPath, repository ); RepositoryArtifactIndex artifactIndex = indexFactory.createStandardIndex( indexPath, repository );
if ( !artifactIndex.indexExists() ) if ( !artifactIndex.exists() )
{ {
execute( configuration, indexPath ); execute( configuration, indexPath );
} }
@ -165,32 +157,16 @@ public void executeNowIfNeeded()
} }
} }
/** private void indexArtifact( List artifacts, File indexPath, ArtifactRepository repository )
* Index the artifacts in the list
*
* @param artifacts the artifacts to be indexed
* @param indexPath the path to the index file
* @param repository the repository where the artifacts are located
*/
protected void indexArtifact( List artifacts, File indexPath, ArtifactRepository repository )
throws RepositoryIndexException throws RepositoryIndexException
{ {
ArtifactRepositoryIndex artifactIndex = indexFactory.createArtifactRepositoryIndex( indexPath, repository ); RepositoryArtifactIndex artifactIndex = indexFactory.createStandardIndex( indexPath, repository );
artifactIndex.indexArtifacts( artifacts ); List records = new ArrayList();
artifactIndex.optimize(); for ( Iterator i = artifacts.iterator(); i.hasNext(); )
} {
Artifact a = (Artifact) i.next();
/** records.add( recordFactory.createRecord( a ) );
* Index the metadata in the list }
* artifactIndex.indexRecords( records );
* @param metadataList the metadata to be indexed
* @param indexPath the path to the index file
*/
protected void indexMetadata( List metadataList, File indexPath, ArtifactRepository repository )
throws RepositoryIndexException
{
MetadataRepositoryIndex metadataIndex = indexFactory.createMetadataRepositoryIndex( indexPath, repository );
metadataIndex.indexMetadata( metadataList );
metadataIndex.optimize();
} }
} }

View File

@ -133,21 +133,36 @@ record = new StandardArtifactIndexRecord();
artifact.getRepository().pathOf( pomArtifact ) ); artifact.getRepository().pathOf( pomArtifact ) );
if ( pomFile.exists() ) if ( pomFile.exists() )
{ {
populatePomEntries( readPom( pomArtifact, artifact.getRepository() ), record ); try
{
populatePomEntries( readPom( pomArtifact, artifact.getRepository() ), record );
}
catch ( ProjectBuildingException e )
{
getLogger().error( "Error reading POM file, not populating in index: " + e.getMessage() );
}
} }
} }
else else
{ {
Model model = readPom( artifact, artifact.getRepository() ); Model model;
try
{
model = readPom( artifact, artifact.getRepository() );
if ( !"pom".equals( model.getPackaging() ) ) if ( !"pom".equals( model.getPackaging() ) )
{ {
// Don't return a record for a POM that is does not belong on its own // Don't return a record for a POM that is does not belong on its own
record = null; record = null;
}
else
{
populatePomEntries( model, record );
}
} }
else catch ( ProjectBuildingException e )
{ {
populatePomEntries( model, record ); getLogger().error( "Error reading POM file, not populating in index: " + e.getMessage() );
} }
} }
} }
@ -172,20 +187,11 @@ private void populatePomEntries( Model pom, StandardArtifactIndexRecord record )
} }
private Model readPom( Artifact artifact, ArtifactRepository repository ) private Model readPom( Artifact artifact, ArtifactRepository repository )
throws RepositoryIndexException throws RepositoryIndexException, ProjectBuildingException
{ {
// TODO: will this pollute with local repo metadata? // TODO: will this pollute with local repo metadata?
Model model; MavenProject project = projectBuilder.buildFromRepository( artifact, Collections.EMPTY_LIST, repository );
try return project.getModel();
{
MavenProject project = projectBuilder.buildFromRepository( artifact, Collections.EMPTY_LIST, repository );
model = project.getModel();
}
catch ( ProjectBuildingException e )
{
throw new RepositoryIndexException( "Unable to read project: " + e.getMessage(), e );
}
return model;
} }
private void populateArchiveEntries( List files, StandardArtifactIndexRecord record, File artifactFile ) private void populateArchiveEntries( List files, StandardArtifactIndexRecord record, File artifactFile )

View File

@ -16,25 +16,26 @@
* limitations under the License. * limitations under the License.
*/ */
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException; import org.apache.maven.repository.digest.DigesterException;
import org.apache.maven.repository.indexing.RepositoryIndex; import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
import org.apache.maven.repository.indexing.RepositoryIndexException; import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
import org.apache.maven.repository.indexing.RepositoryIndexSearchException; import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
import org.apache.maven.repository.indexing.RepositoryIndexSearchLayer; import org.apache.maven.repository.indexing.lucene.LuceneQuery;
import org.apache.maven.repository.indexing.RepositoryIndexingFactory; import org.apache.maven.repository.indexing.record.StandardArtifactIndexRecord;
import org.apache.maven.repository.indexing.SearchResult; import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
import org.apache.maven.repository.indexing.query.SingleTermQuery;
import java.io.File; import java.io.File;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Validates an artifact file for duplicates within the same groupId based from what's available in a RepositoryIndex * Validates an artifact file for duplicates within the same groupId based from what's available in a repository index.
* *
* @author Edwin Punzalan * @author Edwin Punzalan
* @plexus.component role="org.apache.maven.repository.reporting.ArtifactReportProcessor" role-hint="duplicate" * @plexus.component role="org.apache.maven.repository.reporting.ArtifactReportProcessor" role-hint="duplicate"
@ -50,41 +51,25 @@ public class DuplicateArtifactFileReportProcessor
/** /**
* @plexus.requirement * @plexus.requirement
*/ */
private RepositoryIndexingFactory indexFactory; private RepositoryArtifactIndexFactory indexFactory;
/**
* @plexus.requirement
*/
private RepositoryIndexSearchLayer searchLayer;
/** /**
* @plexus.configuration * @plexus.configuration
*/ */
private String indexDirectory; private String indexDirectory;
//@todo configurable?
private String algorithm = RepositoryIndex.FLD_MD5;
public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter, public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter,
ArtifactRepository repository ) ArtifactRepository repository )
throws ReportProcessorException throws ReportProcessorException
{ {
if ( artifact.getFile() != null ) if ( artifact.getFile() != null )
{ {
RepositoryIndex index; RepositoryArtifactIndex index = indexFactory.createStandardIndex( new File( indexDirectory ), repository );
try
{
index = indexFactory.createArtifactRepositoryIndex( new File( indexDirectory ), repository );
}
catch ( RepositoryIndexException e )
{
throw new ReportProcessorException( "Unable to create RepositoryIndex instance", e );
}
String checksum; String checksum;
try try
{ {
checksum = digester.createChecksum( artifact.getFile(), algorithm ); checksum = digester.createChecksum( artifact.getFile(), Digester.MD5 );
} }
catch ( DigesterException e ) catch ( DigesterException e )
{ {
@ -93,7 +78,8 @@ public void processArtifact( Model model, Artifact artifact, ArtifactReporter re
try try
{ {
List results = searchLayer.searchAdvanced( new SingleTermQuery( algorithm, checksum.trim() ), index ); List results = index.search( new LuceneQuery(
new TermQuery( new Term( StandardIndexRecordFields.MD5, checksum.toLowerCase() ) ) ) );
if ( results.isEmpty() ) if ( results.isEmpty() )
{ {
@ -101,23 +87,20 @@ public void processArtifact( Model model, Artifact artifact, ArtifactReporter re
} }
else else
{ {
String id = artifact.getId();
boolean hasDuplicates = false; boolean hasDuplicates = false;
for ( Iterator hits = results.iterator(); hits.hasNext(); ) for ( Iterator i = results.iterator(); i.hasNext(); )
{ {
SearchResult result = (SearchResult) hits.next(); StandardArtifactIndexRecord result = (StandardArtifactIndexRecord) i.next();
Artifact artifactMatch = result.getArtifact();
//make sure it is not the same artifact //make sure it is not the same artifact
if ( !id.equals( artifactMatch.getId() ) ) if ( !result.getFilename().equals( repository.pathOf( artifact ) ) )
{ {
//report only duplicates from the same groupId //report only duplicates from the same groupId
String groupId = artifact.getGroupId(); String groupId = artifact.getGroupId();
if ( groupId.equals( artifactMatch.getGroupId() ) ) if ( groupId.equals( result.getGroupId() ) )
{ {
hasDuplicates = true; hasDuplicates = true;
reporter.addFailure( artifact, "Found duplicate for " + artifactMatch.getId() ); reporter.addFailure( artifact, "Found duplicate for " + artifact.getId() );
} }
} }
} }

View File

@ -19,10 +19,13 @@
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
import org.apache.maven.repository.indexing.ArtifactRepositoryIndex; import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
import org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory;
import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
import java.util.Collections;
/** /**
* @author Edwin Punzalan * @author Edwin Punzalan
@ -47,39 +50,27 @@ protected void setUp()
{ {
super.setUp(); super.setUp();
Digester digester = (Digester) lookup( Digester.ROLE );
indexDirectory = getTestFile( "target/indexDirectory" ); indexDirectory = getTestFile( "target/indexDirectory" );
FileUtils.deleteDirectory( indexDirectory );
if ( !indexDirectory.exists() )
{
indexDirectory.mkdirs();
}
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "jar" ); artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "jar" );
reporter = new MockArtifactReporter(); reporter = new MockArtifactReporter();
model = new Model(); model = new Model();
ArtifactRepositoryIndex index = new ArtifactRepositoryIndex( indexDirectory, repository, digester ); RepositoryArtifactIndexFactory factory =
index.indexArtifact( artifact ); (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
index.optimize();
RepositoryArtifactIndex index = factory.createStandardIndex( indexDirectory, repository );
RepositoryIndexRecordFactory recordFactory =
(RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
index.indexRecords( Collections.singletonList( recordFactory.createRecord( artifact ) ) );
processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "duplicate" ); processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "duplicate" );
} }
protected void tearDown()
throws Exception
{
//FileUtils.deleteDirectory( indexDirectory );
processor = null;
model = null;
artifact = null;
reporter = null;
super.tearDown();
}
public void testNullArtifactFile() public void testNullArtifactFile()
throws Exception throws Exception
{ {
@ -139,10 +130,7 @@ public void testFailure()
assertEquals( "Check no failures", 1, reporter.getFailures() ); assertEquals( "Check no failures", 1, reporter.getFailures() );
} }
private Artifact createArtifact( String groupId, private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
String artifactId,
String baseVersion,
String version,
String type ) String type )
{ {
Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type ); Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );

View File

@ -9,10 +9,7 @@
<role>org.apache.maven.repository.digest.Digester</role> <role>org.apache.maven.repository.digest.Digester</role>
</requirement> </requirement>
<requirement> <requirement>
<role>org.apache.maven.repository.indexing.RepositoryIndexingFactory</role> <role>org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory</role>
</requirement>
<requirement>
<role>org.apache.maven.repository.indexing.RepositoryIndexSearchLayer</role>
</requirement> </requirement>
</requirements> </requirements>
<configuration> <configuration>

View File

@ -17,30 +17,42 @@
*/ */
import com.opensymphony.xwork.ActionSupport; import com.opensymphony.xwork.ActionSupport;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.configuration.Configuration; import org.apache.maven.repository.configuration.Configuration;
import org.apache.maven.repository.configuration.ConfigurationStore; import org.apache.maven.repository.configuration.ConfigurationStore;
import org.apache.maven.repository.configuration.ConfigurationStoreException; import org.apache.maven.repository.configuration.ConfigurationStoreException;
import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory; import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory;
import org.apache.maven.repository.indexing.ArtifactRepositoryIndex; import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
import org.apache.maven.repository.indexing.RepositoryIndexException; import org.apache.maven.repository.indexing.RepositoryIndexException;
import org.apache.maven.repository.indexing.RepositoryIndexSearchLayer; import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
import org.apache.maven.repository.indexing.RepositoryIndexingFactory; import org.apache.maven.repository.indexing.lucene.LuceneQuery;
import org.apache.maven.repository.indexing.record.StandardArtifactIndexRecord;
import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet;
/** /**
* Browse the repository. * Browse the repository.
* *
* @todo the tree part probably belongs in a browsing component, along with the methods currently in the indexer * @todo the tree part probably belongs in a browsing component, and the indexer could optimize how it retrieves the terms rather than querying everything
* @plexus.component role="com.opensymphony.xwork.Action" role-hint="browseAction" * @plexus.component role="com.opensymphony.xwork.Action" role-hint="browseAction"
*/ */
public class BrowseAction public class BrowseAction
@ -49,12 +61,7 @@ public class BrowseAction
/** /**
* @plexus.requirement * @plexus.requirement
*/ */
private RepositoryIndexingFactory factory; private RepositoryArtifactIndexFactory factory;
/**
* @plexus.requirement
*/
private RepositoryIndexSearchLayer searchLayer;
/** /**
* @plexus.requirement * @plexus.requirement
@ -79,11 +86,11 @@ public class BrowseAction
private List versions; private List versions;
public String browse() public String browse()
throws ConfigurationStoreException, RepositoryIndexException, IOException throws ConfigurationStoreException, RepositoryIndexException, IOException, RepositoryIndexSearchException
{ {
ArtifactRepositoryIndex index = getIndex(); RepositoryArtifactIndex index = getIndex();
if ( !index.indexExists() ) if ( !index.exists() )
{ {
addActionError( "The repository is not yet indexed. Please wait, and then try again." ); addActionError( "The repository is not yet indexed. Please wait, and then try again." );
return ERROR; return ERROR;
@ -97,11 +104,11 @@ public String browse()
} }
public String browseGroup() public String browseGroup()
throws ConfigurationStoreException, RepositoryIndexException, IOException throws ConfigurationStoreException, RepositoryIndexException, IOException, RepositoryIndexSearchException
{ {
ArtifactRepositoryIndex index = getIndex(); RepositoryArtifactIndex index = getIndex();
if ( !index.indexExists() ) if ( !index.exists() )
{ {
addActionError( "The repository is not yet indexed. Please wait, and then try again." ); addActionError( "The repository is not yet indexed. Please wait, and then try again." );
return ERROR; return ERROR;
@ -135,15 +142,26 @@ public String browseGroup()
this.groups = collateGroups( rootNode ); this.groups = collateGroups( rootNode );
this.artifactIds = index.getArtifacts( groupId.replaceAll( GROUP_SEPARATOR, "." ) ); String groupId = this.groupId.replaceAll( GROUP_SEPARATOR, "." );
List records = index.search(
new LuceneQuery( new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, groupId ) ) ) );
Set artifactIds = new HashSet();
for ( Iterator i = records.iterator(); i.hasNext(); )
{
StandardArtifactIndexRecord record = (StandardArtifactIndexRecord) i.next();
artifactIds.add( record.getArtifactId() );
}
this.artifactIds = new ArrayList( artifactIds );
Collections.sort( this.artifactIds );
return SUCCESS; return SUCCESS;
} }
public String browseArtifact() public String browseArtifact()
throws ConfigurationStoreException, RepositoryIndexException, IOException throws ConfigurationStoreException, RepositoryIndexException, IOException, RepositoryIndexSearchException
{ {
ArtifactRepositoryIndex index = getIndex(); RepositoryArtifactIndex index = getIndex();
if ( StringUtils.isEmpty( groupId ) ) if ( StringUtils.isEmpty( groupId ) )
{ {
@ -159,26 +177,51 @@ public String browseArtifact()
return ERROR; return ERROR;
} }
versions = index.getVersions( groupId.replaceAll( GROUP_SEPARATOR, "." ), artifactId ); String groupId = this.groupId.replaceAll( GROUP_SEPARATOR, "." );
if ( versions.isEmpty() ) BooleanQuery query = new BooleanQuery();
query.add( new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, groupId ) ),
BooleanClause.Occur.MUST );
query.add( new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, artifactId ) ),
BooleanClause.Occur.MUST );
List records = index.search( new LuceneQuery( query ) );
if ( records.isEmpty() )
{ {
// TODO: i18n // TODO: i18n
addActionError( "Could not find any artifacts with the given group and artifact ID" ); addActionError( "Could not find any artifacts with the given group and artifact ID" );
return ERROR; return ERROR;
} }
Set versions = new HashSet();
for ( Iterator i = records.iterator(); i.hasNext(); )
{
StandardArtifactIndexRecord record = (StandardArtifactIndexRecord) i.next();
versions.add( record.getVersion() );
}
this.versions = new ArrayList( versions );
Collections.sort( this.versions );
return SUCCESS; return SUCCESS;
} }
private GroupTreeNode buildGroupTree( ArtifactRepositoryIndex index ) private GroupTreeNode buildGroupTree( RepositoryArtifactIndex index )
throws IOException throws IOException, RepositoryIndexSearchException
{ {
// TODO: give action message if indexing is in progress // TODO: give action message if indexing is in progress
// TODO: this will be inefficient over a very large number of artifacts, should be cached // TODO: this will be inefficient over a very large number of artifacts, should be cached
List groups = index.enumerateGroupIds(); List records = index.search( new LuceneQuery( new MatchAllDocsQuery() ) );
Set groups = new TreeSet();
for ( Iterator i = records.iterator(); i.hasNext(); )
{
StandardArtifactIndexRecord record = (StandardArtifactIndexRecord) i.next();
groups.add( record.getGroupId() );
}
GroupTreeNode rootNode = new GroupTreeNode(); GroupTreeNode rootNode = new GroupTreeNode();
@ -227,7 +270,7 @@ private List collateGroups( GroupTreeNode rootNode )
return groups; return groups;
} }
private ArtifactRepositoryIndex getIndex() private RepositoryArtifactIndex getIndex()
throws ConfigurationStoreException, RepositoryIndexException throws ConfigurationStoreException, RepositoryIndexException
{ {
Configuration configuration = configurationStore.getConfigurationFromStore(); Configuration configuration = configurationStore.getConfigurationFromStore();
@ -235,7 +278,7 @@ private ArtifactRepositoryIndex getIndex()
ArtifactRepository repository = repositoryFactory.createRepository( configuration ); ArtifactRepository repository = repositoryFactory.createRepository( configuration );
return factory.createArtifactRepositoryIndex( indexPath, repository ); return factory.createStandardIndex( indexPath, repository );
} }
public List getGroups() public List getGroups()

View File

@ -17,17 +17,22 @@
*/ */
import com.opensymphony.xwork.ActionSupport; import com.opensymphony.xwork.ActionSupport;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.TermQuery;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.configuration.Configuration; import org.apache.maven.repository.configuration.Configuration;
import org.apache.maven.repository.configuration.ConfigurationStore; import org.apache.maven.repository.configuration.ConfigurationStore;
import org.apache.maven.repository.configuration.ConfigurationStoreException; import org.apache.maven.repository.configuration.ConfigurationStoreException;
import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory; import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory;
import org.apache.maven.repository.indexing.ArtifactRepositoryIndex; import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
import org.apache.maven.repository.indexing.RepositoryIndexException; import org.apache.maven.repository.indexing.RepositoryIndexException;
import org.apache.maven.repository.indexing.RepositoryIndexSearchException; import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
import org.apache.maven.repository.indexing.RepositoryIndexSearchLayer; import org.apache.maven.repository.indexing.lucene.LuceneQuery;
import org.apache.maven.repository.indexing.RepositoryIndexingFactory; import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
import org.apache.maven.repository.indexing.query.SingleTermQuery;
import java.io.File; import java.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -59,12 +64,7 @@ public class SearchAction
/** /**
* @plexus.requirement * @plexus.requirement
*/ */
private RepositoryIndexingFactory factory; private RepositoryArtifactIndexFactory factory;
/**
* @plexus.requirement
*/
private RepositoryIndexSearchLayer searchLayer;
/** /**
* @plexus.requirement * @plexus.requirement
@ -78,21 +78,27 @@ public class SearchAction
public String quickSearch() public String quickSearch()
throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException, throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException,
ConfigurationStoreException ConfigurationStoreException, ParseException
{ {
// TODO: give action message if indexing is in progress // TODO: give action message if indexing is in progress
assert q != null && q.length() != 0; assert q != null && q.length() != 0;
ArtifactRepositoryIndex index = getIndex(); RepositoryArtifactIndex index = getIndex();
if ( !index.indexExists() ) if ( !index.exists() )
{ {
addActionError( "The repository is not yet indexed. Please wait, and then try again." ); addActionError( "The repository is not yet indexed. Please wait, and then try again." );
return ERROR; return ERROR;
} }
searchResults = searchLayer.searchGeneral( q, index ); // TODO! this is correct, but ugly
MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{StandardIndexRecordFields.GROUPID,
StandardIndexRecordFields.ARTIFACTID, StandardIndexRecordFields.BASE_VERSION,
StandardIndexRecordFields.CLASSIFIER, StandardIndexRecordFields.CLASSES, StandardIndexRecordFields.FILES,
StandardIndexRecordFields.TYPE, StandardIndexRecordFields.PROJECT_NAME,
StandardIndexRecordFields.PROJECT_DESCRIPTION}, new StandardAnalyzer() );
searchResults = index.search( new LuceneQuery( parser.parse( q ) ) );
return SUCCESS; return SUCCESS;
} }
@ -104,20 +110,21 @@ public String findArtifact()
assert md5 != null && md5.length() != 0; assert md5 != null && md5.length() != 0;
ArtifactRepositoryIndex index = getIndex(); RepositoryArtifactIndex index = getIndex();
if ( !index.indexExists() ) if ( !index.exists() )
{ {
addActionError( "The repository is not yet indexed. Please wait, and then try again." ); addActionError( "The repository is not yet indexed. Please wait, and then try again." );
return ERROR; return ERROR;
} }
searchResults = searchLayer.searchAdvanced( new SingleTermQuery( "md5", md5 ), index ); searchResults = index.search(
new LuceneQuery( new TermQuery( new Term( StandardIndexRecordFields.MD5, md5.toLowerCase() ) ) ) );
return SUCCESS; return SUCCESS;
} }
private ArtifactRepositoryIndex getIndex() private RepositoryArtifactIndex getIndex()
throws ConfigurationStoreException, RepositoryIndexException throws ConfigurationStoreException, RepositoryIndexException
{ {
Configuration configuration = configurationStore.getConfigurationFromStore(); Configuration configuration = configurationStore.getConfigurationFromStore();
@ -125,7 +132,7 @@ private ArtifactRepositoryIndex getIndex()
ArtifactRepository repository = repositoryFactory.createRepository( configuration ); ArtifactRepository repository = repositoryFactory.createRepository( configuration );
return factory.createArtifactRepositoryIndex( indexPath, repository ); return factory.createStandardIndex( indexPath, repository );
} }
public String doInput() public String doInput()

View File

@ -1,5 +1,3 @@
<%@ taglib uri="/webwork" prefix="ww" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- <%--
~ Copyright 2005-2006 The Apache Software Foundation. ~ Copyright 2005-2006 The Apache Software Foundation.
~ ~
@ -16,6 +14,9 @@
~ limitations under the License. ~ limitations under the License.
--%> --%>
<%@ taglib uri="/webwork" prefix="ww" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html> <html>
<head> <head>
<title>Search Results</title> <title>Search Results</title>
@ -42,11 +43,17 @@
--%> --%>
</tr> </tr>
<ww:set name="searchResults" value="searchResults" /> <ww:set name="searchResults" value="searchResults" />
<c:forEach items="${searchResults}" var="result" varStatus="i"> <c:forEach items="${searchResults}" var="record" varStatus="i">
<tr class="${i.index % 2 == 0 ? "b" : "a"}"> <tr class="${i.index % 2 == 0 ? "b" : "a"}">
<td><c:out value="${result.artifact.groupId}" /></td> <td>
<td><c:out value="${result.artifact.artifactId}" /></td> <c:out value="${record.groupId}" />
<td><c:out value="${result.artifact.version}" /></td> </td>
<td>
<c:out value="${record.artifactId}" />
</td>
<td>
<c:out value="${record.version}" />
</td>
<%-- TODO: hits <%-- TODO: hits
<td> <td>
<table border="1px" width="100%" cellspacing="0"> <table border="1px" width="100%" cellspacing="0">