PR: MRM-39

Submitted by: Maria Odea Ching

ArtifactIndexSearcher classes.  Also, added more unit test coverage

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358756 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Edwin L. Punzalan 2005-12-23 07:06:14 +00:00
parent 5e6fb5ce46
commit 398db2ddf6
6 changed files with 339 additions and 37 deletions

View File

@ -64,28 +64,25 @@ public boolean isOpen()
public void close()
throws RepositoryIndexerException
{
if ( indexOpen )
try
{
try
if ( indexWriter != null )
{
if ( indexWriter != null )
{
indexWriter.close();
indexWriter = null;
}
indexWriter.close();
indexWriter = null;
}
if ( indexReader != null )
{
indexReader.close();
indexReader = null;
}
indexOpen = false;
}
catch ( Exception e )
if ( indexReader != null )
{
throw new RepositoryIndexerException( e );
indexReader.close();
indexReader = null;
}
indexOpen = false;
}
catch ( Exception e )
{
throw new RepositoryIndexerException( e );
}
}
@ -94,10 +91,7 @@ public void open()
{
try
{
if ( !indexOpen )
{
validateIndex();
}
validateIndex();
}
catch ( Exception e )
{
@ -123,7 +117,7 @@ protected void getIndexReader()
indexReader = IndexReader.open( indexPath );
}
}
protected Analyzer getAnalyzer()
{
return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );

View File

@ -18,6 +18,8 @@
*/
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CharTokenizer;
import org.apache.lucene.analysis.TokenStream;
@ -51,7 +53,7 @@ public TokenStream tokenStream(String fieldName, Reader reader)
return tokenStream;
}
private class VersionTokenizer
extends CharTokenizer
{
@ -60,14 +62,15 @@ public VersionTokenizer( Reader reader )
super( reader );
}
protected boolean isTokenChar(char param)
protected boolean isTokenChar( char param )
{
boolean token;
switch( param )
{
case '.': token = false; break;
case '-': token = false; break;
case '.':
case '-':
token = false; break;
default:
token = true;
}

View File

@ -101,7 +101,6 @@ public void addArtifactIndex( Artifact artifact )
{
getIndexWriter();
initBuffers();
processArtifactContents( artifact.getFile() );
//@todo should some of these fields be Keyword instead of Text ?
@ -176,6 +175,7 @@ private void removeBuffers()
private void processArtifactContents( File artifact )
throws IOException, ZipException
{
initBuffers();
ZipFile jar = new ZipFile( artifact );
for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); )
{

View File

@ -0,0 +1,178 @@
package org.apache.maven.repository.indexing;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.factory.DefaultArtifactFactory;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.versioning.VersionRange;
/**
* This class searches the index for existing artifacts that contains the
* specified query string.
*
* @author Maria Odea Ching
*/
public class ArtifactRepositorySearcher implements RepositorySearcher {
private IndexSearcher searcher;
private ArtifactRepository repository;
private ArtifactFactory factory;
private static final String NAME = "name";
private static final String GROUPID = "groupId";
private static final String ARTIFACTID = "artifactId";
private static final String VERSION = "version";
private static final String JAR_TYPE = "jar";
private static final String XML_TYPE = "xml";
private static final String POM_TYPE = "pom";
/**
* Constructor
*
* @param indexPath
* @param repository
*/
public ArtifactRepositorySearcher(String indexPath,
ArtifactRepository repository) {
this.repository = repository;
factory = new DefaultArtifactFactory();
try {
searcher = new IndexSearcher(indexPath);
} catch (IOException ie) {
ie.printStackTrace();
}
}
protected Analyzer getAnalyzer()
{
//PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new SimpleAnalyzer());
//wrapper.addAnalyzer(VERSION, new StandardAnalyzer());
//return wrapper;
return new ArtifactRepositoryIndexAnalyzer(new SimpleAnalyzer());
}
/**
* Search the artifact that contains the query string in the specified
* search field.
*
* @param queryString
* @param searchField
* @return
*/
public List searchArtifact(String queryString, String searchField) {
QueryParser parser = new QueryParser(searchField,
getAnalyzer());
Query qry = null;
List artifactList = new ArrayList();
try {
qry = parser.parse(queryString);
} catch (ParseException pe) {
pe.printStackTrace();
return artifactList;
}
try {
Hits hits = searcher.search(qry);
//System.out.println("HITS SIZE --> " + hits.length());
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
// System.out.println("===========================");
// System.out.println("NAME :: " + (String) doc.get(NAME));
// System.out.println("GROUP ID :: " + (String)
// doc.get(GROUPID));
// System.out.println("ARTIFACT ID :: " + (String)
// doc.get(ARTIFACTID));
//System.out.println("VERSION :: " + (String)
// doc.get(VERSION));
// System.out.println("SHA! :: " + (String) doc.get(SHA1));
// System.out.println("MD5 :: " + (String) doc.get(MD5));
// System.out.println("CLASSES :: " + (String)
// doc.get(CLASSES));
// System.out.println("PACKAGES :: " + (String)
// doc.get(PACKAGES));
// System.out.println("FILES :: " + (String) doc.get(FILES));
// System.out.println("===========================");
String name = (String) doc.get(NAME);
String type = "";
if ((name.substring(name.length() - 3).toLowerCase())
.equals(JAR_TYPE))
type = JAR_TYPE;
else if ((name.substring(name.length() - 3).toLowerCase())
.equals(XML_TYPE)
|| (name.substring(name.length() - 3).toLowerCase())
.equals(POM_TYPE))
type = POM_TYPE;
if (!type.equals("") && type != null) {
ArtifactHandler handler = new DefaultArtifactHandler(type);
VersionRange version = VersionRange
.createFromVersion((String) doc.get(VERSION));
Artifact artifact = new DefaultArtifact((String) doc
.get(GROUPID), (String) doc.get(ARTIFACTID),
version, "compile", type, "", handler);
/*
* Artifact artifact = factory.createArtifact((String)
* doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String)
* doc.get(VERSION), "", type);
*/
artifact.setRepository(repository);
artifact.setFile(new File(repository.getBasedir() + "/"
+ (String) doc.get(NAME)));
artifactList.add(artifact);
}
}
} catch (IOException ie) {
ie.printStackTrace();
return artifactList;
}
return artifactList;
}
}

View File

@ -0,0 +1,39 @@
package org.apache.maven.repository.indexing;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.List;
/**
* @author Maria Odea Ching
*/
public interface RepositorySearcher {
String ROLE = RepositoryIndexer.class.getName();
/**
* Search the artifact that contains the query string in the specified
* search field.
*
* @param queryString
* @param searchField
* @return
*/
public List searchArtifact(String queryString, String searchField);
}

View File

@ -18,6 +18,9 @@
*/
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
@ -29,7 +32,7 @@
/**
*
* @author Edwin Punzalan
* @author Edwin Punzalan/Maria Odea Ching
*/
public class ArtifactRepositoryIndexingTest
extends PlexusTestCase
@ -38,6 +41,15 @@ public class ArtifactRepositoryIndexingTest
protected ArtifactFactory artifactFactory;
protected ArtifactRepository repository;
protected String indexPath;
private RepositorySearcher repoSearcher;
private static final String GROUPID = "groupId";
private static final String ARTIFACTID = "artifactId";
private static final String VERSION = "version";
private static final String SHA1 = "sha1";
private static final String MD5 = "md5";
private static final String CLASSES = "classes";
private static final String PACKAGES = "packages";
private static final String FILES = "files";
protected void setUp()
throws Exception
@ -55,36 +67,112 @@ protected void setUp()
repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
indexer = (ArtifactRepositoryIndexer) factory.getArtifactRepositoryIndexer( indexPath, repository );
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
repoSearcher = new ArtifactRepositorySearcher(indexPath, repository);
}
public void testIndex()
throws Exception
{
Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.addArtifactIndex( artifact );
//indexer.optimize();
artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.addArtifactIndex( artifact );
indexer.optimize();
indexer.close();
indexer.open();
artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.addObjectIndex( artifact );
indexer.close();
}
public void testSearch() throws Exception{
//test the search GROUPID
List artifacts = repoSearcher.searchArtifact("test", GROUPID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("test", ARTIFACTID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("1.0", VERSION);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("App", CLASSES);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("groupId", PACKAGES);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("pom.xml", FILES);
assertEquals( 3, artifacts.size() );
for( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = (Artifact) iter.next();
File f = artifact.getFile();
assertNotNull( f );
assertTrue( f.exists() );
}
//search org.apache.maven jars
artifacts = repoSearcher.searchArtifact("org.apache.maven", GROUPID);
assertEquals( 2, artifacts.size() );
artifacts = repoSearcher.searchArtifact("maven-artifact", ARTIFACTID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("2", VERSION);
assertEquals( 2, artifacts.size() );
}
public void testIndexerExceptions()
throws Exception
{
//test closed index
indexer.close();
Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
try
{
indexer.close();
Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.addArtifactIndex( artifact );
fail( "Must throw exception on closed index." );
fail( "Must throw exception on add index with closed index." );
}
catch( RepositoryIndexerException e )
{
//expected
}
try
{
indexer.optimize();
fail( "Must throw exception on optimize index with closed index." );
}
catch( RepositoryIndexerException e )
{
//expected
}
indexer.open();
try
{
indexer.addObjectIndex( "should fail" );
fail( "Must throw exception on add non-Artifact object." );
}
catch( RepositoryIndexerException e )
{
//expected
}
}
protected Artifact getArtifact( String groupId, String artifactId, String version )
{
return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );