PR: MRM-56

Submitted by: Maria Odea Ching

Enabled Index delete document.  Also did some refractoring and code formatting.

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@371476 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Edwin L. Punzalan 2006-01-23 07:27:58 +00:00
parent bcee846abd
commit 1e14d74abc
12 changed files with 295 additions and 168 deletions

View File

@ -18,6 +18,7 @@ package org.apache.maven.repository.indexing;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.maven.artifact.repository.ArtifactRepository;
import java.io.File;
@ -195,4 +196,26 @@ public abstract class AbstractRepositoryIndex
{
return repository;
}
/**
* @see org.apache.maven.repository.indexing.RepositoryIndex#deleteDocument(String, String)
*/
public void deleteDocument( String field, String value )
throws RepositoryIndexException, IOException
{
IndexReader indexReader = null;
try
{
indexReader = IndexReader.open( indexPath );
indexReader.delete( new Term( field, value ) );
}
catch ( IOException ie )
{
throw new RepositoryIndexException( indexPath + "is not a valid directory." );
}
finally
{
indexReader.close();
}
}
}

View File

@ -27,8 +27,8 @@ import org.apache.lucene.search.TermQuery;
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.apache.maven.repository.indexing.query.RangeQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.io.IOException;
@ -159,17 +159,17 @@ public abstract class AbstractRepositoryIndexSearcher
}
retVal = booleanQuery;
}
else if( query instanceof RangeQuery )
else if ( query instanceof RangeQuery )
{
RangeQuery rq = (RangeQuery) query;
List queries = rq.getQueries();
Iterator iter = queries.iterator();
Term begin = null, end = null;
if(queries.size() == 2)
if ( queries.size() == 2 )
{
SinglePhraseQuery qry = (SinglePhraseQuery) iter.next();
begin = new Term( qry.getField(), qry.getValue() );
qry = ( SinglePhraseQuery ) iter.next();
qry = (SinglePhraseQuery) iter.next();
end = new Term( qry.getField(), qry.getValue() );
}
retVal = new org.apache.lucene.search.RangeQuery( begin, end, rq.isInclusive() );

View File

@ -41,6 +41,8 @@ import java.util.zip.ZipFile;
public class ArtifactRepositoryIndex
extends AbstractRepositoryIndex
{
protected static final String FLD_ID = "id";
protected static final String FLD_NAME = "name";
protected static final String FLD_GROUPID = "groupId";
@ -59,13 +61,15 @@ public class ArtifactRepositoryIndex
protected static final String FLD_FILES = "files";
private static final String[] FIELDS =
{FLD_NAME, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_SHA1, FLD_MD5, FLD_CLASSES, FLD_PACKAGES, FLD_FILES};
private static final String[] FIELDS = {FLD_ID, FLD_NAME, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_SHA1,
FLD_MD5, FLD_CLASSES, FLD_PACKAGES, FLD_FILES};
private Analyzer analyzer;
private Digester digester;
protected static final String ARTIFACT_TYPE = "ARTIFACT";
/**
* Class constructor
*
@ -161,6 +165,7 @@ public class ArtifactRepositoryIndex
//@todo should some of these fields be Keyword instead of Text ?
Document doc = new Document();
doc.add( Field.Keyword( FLD_ID, ARTIFACT_TYPE + artifact.getId() ) );
doc.add( Field.Text( FLD_NAME, artifact.getFile().getName() ) );
doc.add( Field.Text( FLD_GROUPID, artifact.getGroupId() ) );
doc.add( Field.Text( FLD_ARTIFACTID, artifact.getArtifactId() ) );

View File

@ -72,9 +72,10 @@ public class DefaultRepositoryIndexingFactory
return new PomRepositoryIndexSearcher( index, artifactFactory );
}
public MetadataRepositoryIndex createMetadataRepositoryIndex( String indexPath, ArtifactRepository repository)
throws RepositoryIndexException{
return new MetadataRepositoryIndex(indexPath, repository);
public MetadataRepositoryIndex createMetadataRepositoryIndex( String indexPath, ArtifactRepository repository )
throws RepositoryIndexException
{
return new MetadataRepositoryIndex( indexPath, repository );
}
public MetadataRepositoryIndexSearcher createMetadataRepositoryIndexSearcher( MetadataRepositoryIndex index )

View File

@ -21,14 +21,14 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.Versioning;
import org.apache.maven.artifact.repository.metadata.Plugin;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Versioning;
import java.util.List;
import java.util.Iterator;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
/**
* This class indexes the metadata in the repository.
@ -36,23 +36,26 @@ import java.io.IOException;
public class MetadataRepositoryIndex
extends AbstractRepositoryIndex
{
private static final String FLD_LASTUPDATE = "lastUpdate";
protected static final String FLD_ID = "id";
private static final String FLD_PLUGINPREFIX = "pluginPrefix";
protected static final String FLD_LASTUPDATE = "lastUpdate";
private static final String FLD_METADATAPATH = "path";
protected static final String FLD_PLUGINPREFIX = "pluginPrefix";
private static final String FLD_GROUPID = "groupId";
protected static final String FLD_METADATAPATH = "path";
private static final String FLD_ARTIFACTID = "artifactId";
protected static final String FLD_GROUPID = "groupId";
private static final String FLD_VERSION = "version";
protected static final String FLD_ARTIFACTID = "artifactId";
private static final String[] FIELDS = {FLD_METADATAPATH, FLD_PLUGINPREFIX, FLD_LASTUPDATE,
FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION};
protected static final String FLD_VERSION = "version";
private static final String[] FIELDS =
{FLD_ID, FLD_METADATAPATH, FLD_PLUGINPREFIX, FLD_LASTUPDATE, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION};
/**
* Constructor
*
* @param indexPath the path to the index
* @param repository the repository where the metadata to be indexed is located
* @throws RepositoryIndexException
@ -65,6 +68,7 @@ public class MetadataRepositoryIndex
/**
* Get the field names to be used in the index
*
* @return array of strings
*/
public String[] getIndexFields()
@ -74,6 +78,7 @@ public class MetadataRepositoryIndex
/**
* Returns the analyzer used for indexing
*
* @return Analyzer object
*/
public Analyzer getAnalyzer()
@ -83,10 +88,12 @@ public class MetadataRepositoryIndex
/**
* Index the paramater object
*
* @param obj
* @throws RepositoryIndexException
*/
public void index( Object obj ) throws RepositoryIndexException
public void index( Object obj )
throws RepositoryIndexException
{
if ( obj instanceof RepositoryMetadata )
{
@ -101,10 +108,12 @@ public class MetadataRepositoryIndex
/**
* Index the contents of the specified RepositoryMetadata paramter object
*
* @param repoMetadata the metadata object to be indexed
* @throws RepositoryIndexException
*/
private void indexMetadata( RepositoryMetadata repoMetadata ) throws RepositoryIndexException
private void indexMetadata( RepositoryMetadata repoMetadata )
throws RepositoryIndexException
{
if ( !isOpen() )
{
@ -116,38 +125,40 @@ public class MetadataRepositoryIndex
//get the metadatapath: check where metadata is located, then concatenate the groupId,
// artifactId, version based on its location
Document doc = new Document();
doc.add( Field.Keyword( FLD_ID, (String) repoMetadata.getKey() ) );
String path = "";
if( repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory())
if ( repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory() )
{
path = repoMetadata.getGroupId() + "/";
}
else if(!repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory())
else if ( !repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory() )
{
path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/";
}
else if(!repoMetadata.storedInGroupDirectory() && repoMetadata.storedInArtifactVersionDirectory())
else if ( !repoMetadata.storedInGroupDirectory() && repoMetadata.storedInArtifactVersionDirectory() )
{
path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/" + repoMetadata.getBaseVersion() + "/";
path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/" +
repoMetadata.getBaseVersion() + "/";
}
//@todo use localfilename or remotefilename to get the path???
path = path + repoMetadata.getRemoteFilename();
doc.add( Field.Text( FLD_METADATAPATH, path) );
doc.add( Field.Text( FLD_METADATAPATH, path ) );
Metadata metadata = repoMetadata.getMetadata();
Versioning versioning = metadata.getVersioning();
if( versioning != null )
if ( versioning != null )
{
doc.add( Field.Text( FLD_LASTUPDATE, versioning.getLastUpdated() ) );
}
List plugins = metadata.getPlugins();
String pluginAppended = "";
for( Iterator iter = plugins.iterator(); iter.hasNext(); )
for ( Iterator iter = plugins.iterator(); iter.hasNext(); )
{
Plugin plugin = (Plugin) iter.next();
if( plugin.getPrefix() != null && !plugin.getPrefix().equals("") )
if ( plugin.getPrefix() != null && !plugin.getPrefix().equals( "" ) )
{
pluginAppended = plugin.getPrefix() + " ";
}
@ -155,11 +166,11 @@ public class MetadataRepositoryIndex
doc.add( Field.Text( FLD_PLUGINPREFIX, pluginAppended ) );
doc.add( Field.UnIndexed( FLD_GROUPID, metadata.getGroupId() ) );
if( metadata.getArtifactId() != null && !metadata.getArtifactId().equals("") )
if ( metadata.getArtifactId() != null && !metadata.getArtifactId().equals( "" ) )
{
doc.add( Field.UnIndexed( FLD_ARTIFACTID, metadata.getArtifactId() ) );
}
if( metadata.getVersion() != null && !metadata.getVersion().equals("") )
if ( metadata.getVersion() != null && !metadata.getVersion().equals( "" ) )
{
doc.add( Field.UnIndexed( FLD_VERSION, metadata.getVersion() ) );
}
@ -174,7 +185,8 @@ public class MetadataRepositoryIndex
}
}
public boolean isKeywordField( String field ){
public boolean isKeywordField( String field )
{
return false;
}
}

View File

@ -17,23 +17,26 @@ package org.apache.maven.repository.indexing;
*/
import org.apache.lucene.document.Document;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.Artifact;
import java.net.URL;
import java.io.InputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
/**
* This class searches the specified index given the search/query criteria.
*
*/
public class MetadataRepositoryIndexSearcher
extends AbstractRepositoryIndexSearcher
@ -56,17 +59,19 @@ public class MetadataRepositoryIndexSearcher
/**
* Constructor
*
* @param index the index object to be set
* @param factory
*/
public MetadataRepositoryIndexSearcher( MetadataRepositoryIndex index, ArtifactFactory factory)
public MetadataRepositoryIndexSearcher( MetadataRepositoryIndex index, ArtifactFactory factory )
{
super(index);
super( index );
artifactFactory = factory;
}
/**
* Create object to be returned by the search based on the document
*
* @param doc
* @return Object
*/
@ -85,11 +90,11 @@ public class MetadataRepositoryIndexSearcher
String tmpDir = (String) it.next();
String metadataType = "";
if( tmpDir.equals( doc.get( FLD_GROUPID ) ) )
if ( tmpDir.equals( doc.get( FLD_GROUPID ) ) )
{
metadataType = GROUP_TYPE;
}
else if( tmpDir.equals( doc.get( FLD_ARTIFACTID ) ) )
else if ( tmpDir.equals( doc.get( FLD_ARTIFACTID ) ) )
{
metadataType = ARTIFACT_TYPE;
}
@ -100,10 +105,12 @@ public class MetadataRepositoryIndexSearcher
RepositoryMetadata repoMetadata = null;
try{
repoMetadata = getMetadata(doc.get( FLD_GROUPID ), doc.get( FLD_ARTIFACTID ), doc.get( FLD_VERSION ), metadataFile, metadataType );
try
{
repoMetadata = getMetadata( doc.get( FLD_GROUPID ), doc.get( FLD_ARTIFACTID ), doc.get( FLD_VERSION ),
metadataFile, metadataType );
}
catch(Exception e)
catch ( Exception e )
{
//@todo
}
@ -122,7 +129,8 @@ public class MetadataRepositoryIndexSearcher
* @return RepositoryMetadata
* @throws Exception
*/
private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename, String metadataType)
private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,
String metadataType )
throws Exception
{
RepositoryMetadata repoMetadata = null;
@ -131,25 +139,27 @@ public class MetadataRepositoryIndexSearcher
MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
//group metadata
if( metadataType.equals( GROUP_TYPE ) )
if ( metadataType.equals( GROUP_TYPE ) )
{
url = new File( index.getRepository().getBasedir() + groupId.replace('.', '/') + "/" + filename ).toURL();
url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();
is = url.openStream();
repoMetadata = new GroupRepositoryMetadata(groupId);
repoMetadata = new GroupRepositoryMetadata( groupId );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
}
//artifact metadata
else if( metadataType.equals( ARTIFACT_TYPE ) )
else if ( metadataType.equals( ARTIFACT_TYPE ) )
{
url = new File( index.getRepository().getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + filename ).toURL();
url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" +
filename ).toURL();
is = url.openStream();
repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
}
//snapshot/version metadata
else if( metadataType.equals( SNAPSHOT_TYPE ) )
else if ( metadataType.equals( SNAPSHOT_TYPE ) )
{
url = new File( index.getRepository().getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + filename ).toURL();
url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" +
version + "/" + filename ).toURL();
is = url.openStream();
repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
@ -160,6 +170,7 @@ public class MetadataRepositoryIndexSearcher
/**
* Create artifact object.
*
* @param groupId the groupId of the artifact
* @param artifactId the artifactId of the artifact
* @param version the version of the artifact

View File

@ -47,6 +47,8 @@ import java.util.List;
public class PomRepositoryIndex
extends AbstractRepositoryIndex
{
protected static final String FLD_ID = "id";
protected static final String FLD_GROUPID = "groupId";
protected static final String FLD_ARTIFACTID = "artifactId";
@ -69,8 +71,8 @@ public class PomRepositoryIndex
protected static final String FLD_MD5 = "md5";
private static final String[] FIELDS = {FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_PACKAGING, FLD_LICENSE_URLS,
FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL};
private static final String[] FIELDS = {FLD_ID, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_PACKAGING,
FLD_LICENSE_URLS, FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL};
private Analyzer analyzer;
@ -78,8 +80,10 @@ public class PomRepositoryIndex
private ArtifactFactory artifactFactory;
private static final List KEYWORD_FIELDS = Arrays.asList(
new String[]{FLD_LICENSE_URLS, FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL} );
private static final List KEYWORD_FIELDS = Arrays.asList( new String[]{FLD_ID, FLD_LICENSE_URLS, FLD_DEPENDENCIES,
FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL} );
protected static final String POM_TYPE = "POM";
/**
* Class Constructor
@ -127,6 +131,7 @@ public class PomRepositoryIndex
}
Document doc = new Document();
doc.add( Field.Keyword( FLD_ID, POM_TYPE + pom.getId() ) );
doc.add( Field.Text( FLD_GROUPID, pom.getGroupId() ) );
doc.add( Field.Text( FLD_ARTIFACTID, pom.getArtifactId() ) );
doc.add( Field.Text( FLD_VERSION, pom.getVersion() ) );

View File

@ -65,7 +65,7 @@ public interface RepositoryIndexingFactory
*/
PomRepositoryIndexSearcher createPomRepositoryIndexSearcher( PomRepositoryIndex index );
MetadataRepositoryIndex createMetadataRepositoryIndex( String indexPath, ArtifactRepository repository)
MetadataRepositoryIndex createMetadataRepositoryIndex( String indexPath, ArtifactRepository repository )
throws RepositoryIndexException;
MetadataRepositoryIndexSearcher createMetadataRepositoryIndexSearcher( MetadataRepositoryIndex index );

View File

@ -16,11 +16,12 @@ package org.apache.maven.repository.indexing.query;
* limitations under the License.
*/
import java.util.List;
import java.util.ArrayList;
import java.util.List;
/**
* Query object that handles range queries for dates.
*
* @author Maria Odea Ching
*/
public class RangeQuery
@ -30,7 +31,7 @@ public class RangeQuery
private boolean inclusive;
public RangeQuery( boolean inclusive)
public RangeQuery( boolean inclusive )
{
this.inclusive = inclusive;
}

View File

@ -39,18 +39,6 @@ import java.util.List;
public class ArtifactRepositoryIndexingTest
extends PlexusTestCase
{
private static final String GROUPID = "groupId";
private static final String ARTIFACTID = "artifactId";
private static final String VERSION = "version";
private static final String CLASSES = "classes";
private static final String PACKAGES = "packages";
private static final String FILES = "files";
private ArtifactFactory artifactFactory;
private ArtifactRepository repository;
@ -59,11 +47,7 @@ public class ArtifactRepositoryIndexingTest
private Digester digester;
private static final String SHA1 = "sha1";
private static final String MD5 = "md5";
private static final String NAME = "name";
private static final String ARTIFACT_TYPE = "ARTIFACT";
protected void setUp()
throws Exception
@ -181,7 +165,7 @@ public class ArtifactRepositoryIndexingTest
RepositoryIndexSearcher repoSearcher = factory.createArtifactRepositoryIndexSearcher( indexer );
// search version
Query qry = new SinglePhraseQuery( VERSION, "1.0" );
Query qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_VERSION, "1.0" );
List artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@ -191,7 +175,7 @@ public class ArtifactRepositoryIndexingTest
}
// search classes
qry = new SinglePhraseQuery( CLASSES, "App" );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_CLASSES, "App" );
artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@ -201,7 +185,7 @@ public class ArtifactRepositoryIndexingTest
}
// search packages
qry = new SinglePhraseQuery( PACKAGES, "groupId" );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_PACKAGES, "groupId" );
artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@ -211,7 +195,7 @@ public class ArtifactRepositoryIndexingTest
}
// search files
qry = new SinglePhraseQuery( FILES, "pom.xml" );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_FILES, "pom.xml" );
artifacts = repoSearcher.search( qry );
assertEquals( 3, artifacts.size() );
Iterator iter = artifacts.iterator();
@ -222,7 +206,7 @@ public class ArtifactRepositoryIndexingTest
}
// search group id
qry = new SinglePhraseQuery( GROUPID, "org.apache.maven" );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
artifacts = repoSearcher.search( qry );
assertEquals( 2, artifacts.size() );
iter = artifacts.iterator();
@ -233,7 +217,7 @@ public class ArtifactRepositoryIndexingTest
}
// search artifact id
qry = new SinglePhraseQuery( ARTIFACTID, "maven-artifact" );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -243,7 +227,7 @@ public class ArtifactRepositoryIndexingTest
}
// search version
qry = new SinglePhraseQuery( VERSION, "2" );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_VERSION, "2" );
artifacts = repoSearcher.search( qry );
assertEquals( 2, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -258,7 +242,7 @@ public class ArtifactRepositoryIndexingTest
String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
qry = new SinglePhraseQuery( SHA1, sha1.trim() );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_SHA1, sha1.trim() );
artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -270,7 +254,7 @@ public class ArtifactRepositoryIndexingTest
// search md5 checksum
String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 );
qry = new SinglePhraseQuery( MD5, md5.trim() );
qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_MD5, md5.trim() );
artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -300,8 +284,8 @@ public class ArtifactRepositoryIndexingTest
// Criteria 1: required query
// ex. artifactId=maven-artifact AND groupId=org.apache.maven
Query qry1 = new SinglePhraseQuery( ARTIFACTID, "maven-artifact" );
Query qry2 = new SinglePhraseQuery( GROUPID, "org.apache.maven" );
Query qry1 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
Query qry2 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
CompoundQuery rQry = new CompoundQuery();
rQry.and( qry1 );
rQry.and( qry2 );
@ -317,7 +301,7 @@ public class ArtifactRepositoryIndexingTest
// Criteria 2: nested required query
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
// version=2.0.3
Query qry3 = new SinglePhraseQuery( VERSION, "2.0.3" );
Query qry3 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_VERSION, "2.0.3" );
CompoundQuery oQry = new CompoundQuery();
oQry.or( rQry );
oQry.or( qry3 );
@ -334,14 +318,14 @@ public class ArtifactRepositoryIndexingTest
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
// (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
Query qry4 = new SinglePhraseQuery( VERSION, "2.0.1" );
Query qry4 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_VERSION, "2.0.1" );
oQry = new CompoundQuery();
oQry.or( qry3 );
oQry.or( qry4 );
CompoundQuery oQry5 = new CompoundQuery();
Query qry9 = new SinglePhraseQuery( NAME, "maven-artifact-2.0.1.jar" );
Query qry10 = new SinglePhraseQuery( NAME, "maven-artifact" );
Query qry9 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_NAME, "maven-artifact-2.0.1.jar" );
Query qry10 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_NAME, "maven-artifact" );
oQry5.or( qry9 );
oQry5.or( qry10 );
@ -365,8 +349,8 @@ public class ArtifactRepositoryIndexingTest
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
// OR [(artifactId=sample AND groupId=test)]
CompoundQuery rQry3 = new CompoundQuery();
Query qry5 = new SinglePhraseQuery( ARTIFACTID, "sample" );
Query qry6 = new SinglePhraseQuery( GROUPID, "test" );
Query qry5 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "sample" );
Query qry6 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "test" );
rQry3.and( qry5 );
rQry3.and( qry6 );
CompoundQuery oQry2 = new CompoundQuery();
@ -389,8 +373,8 @@ public class ArtifactRepositoryIndexingTest
// [(artifactId=sample AND groupId=test)] OR
// [(artifactId=sample2 AND groupId=test)]
CompoundQuery rQry4 = new CompoundQuery();
Query qry7 = new SinglePhraseQuery( ARTIFACTID, "sample2" );
Query qry8 = new SinglePhraseQuery( GROUPID, "test" );
Query qry7 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "sample2" );
Query qry8 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "test" );
rQry4.and( qry7 );
rQry4.and( qry8 );
oQry2.and( rQry4 );
@ -406,6 +390,24 @@ public class ArtifactRepositoryIndexingTest
indexer.close();
}
public void testDeleteArtifactDocument()
throws Exception
{
createTestIndex();
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.deleteDocument( ArtifactRepositoryIndex.FLD_ID, ARTIFACT_TYPE + artifact.getId() );
RepositoryIndexSearcher repoSearcher = factory.createArtifactRepositoryIndexSearcher( indexer );
Query qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ID, ARTIFACT_TYPE + artifact.getId() );
List artifacts = repoSearcher.search( qry );
assertEquals( artifacts.size(), 0 );
}
private Artifact getArtifact( String groupId, String artifactId, String version )
throws Exception
{

View File

@ -21,7 +21,13 @@ import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.metadata.*;
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.Plugin;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Versioning;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.RangeQuery;
@ -46,10 +52,6 @@ public class MetadataRepositoryIndexingTest
private String indexPath;
private static final String FLD_LASTUPDATE = "lastUpdate";
private static final String FLD_PLUGINPREFIX = "pluginPrefix";
private static final String GROUP_TYPE = "GROUP";
private static final String ARTIFACT_TYPE = "ARTIFACT";
@ -62,15 +64,17 @@ public class MetadataRepositoryIndexingTest
/**
* Set up.
*
* @throws Exception
*/
public void setUp() throws Exception
public void setUp()
throws Exception
{
super.setUp();
File repositoryDirectory = getTestFile( "src/test/repository" );
String repoDir = repositoryDirectory.toURL().toString();
ArtifactRepositoryLayout layout = ( ArtifactRepositoryLayout ) lookup( ArtifactRepositoryLayout.ROLE, "default" );
ArtifactRepositoryFactory repoFactory = ( ArtifactRepositoryFactory ) lookup( ArtifactRepositoryFactory.ROLE );
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
indexPath = "target/index/metadata";
@ -79,9 +83,11 @@ public class MetadataRepositoryIndexingTest
/**
* Tear down.
*
* @throws Exception
*/
public void tearDown() throws Exception
public void tearDown()
throws Exception
{
repository = null;
super.tearDown();
@ -89,20 +95,25 @@ public class MetadataRepositoryIndexingTest
/**
* Create the test index.
*
* @throws Exception
*/
private void createTestIndex() throws Exception
private void createTestIndex()
throws Exception
{
RepositoryIndexingFactory factory = ( RepositoryIndexingFactory ) lookup( RepositoryIndexingFactory.ROLE );
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
RepositoryMetadata repoMetadata = getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", GROUP_TYPE );
RepositoryMetadata repoMetadata =
getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", GROUP_TYPE );
indexer.index( repoMetadata );
repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", ARTIFACT_TYPE );
repoMetadata =
getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", ARTIFACT_TYPE );
indexer.index( repoMetadata );
repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", SNAPSHOT_TYPE );
repoMetadata =
getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", SNAPSHOT_TYPE );
indexer.index( repoMetadata );
indexer.optimize();
@ -124,7 +135,8 @@ public class MetadataRepositoryIndexingTest
RepositoryIndexSearcher repoSearcher = factory.createMetadataRepositoryIndexSearcher( indexer );
// search last update
org.apache.maven.repository.indexing.query.Query qry = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212044643" );
org.apache.maven.repository.indexing.query.Query qry =
new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
List metadataList = repoSearcher.search( qry );
assertEquals( 1, metadataList.size() );
for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
@ -137,7 +149,7 @@ public class MetadataRepositoryIndexingTest
}
// search plugin prefix
qry = new SinglePhraseQuery( FLD_PLUGINPREFIX, "org.apache.maven" );
qry = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" );
metadataList = repoSearcher.search( qry );
assertEquals( 1, metadataList.size() );
for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
@ -145,7 +157,7 @@ public class MetadataRepositoryIndexingTest
RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();
Metadata metadata = repoMetadata.getMetadata();
List plugins = metadata.getPlugins();
for( Iterator it = plugins.iterator(); it.hasNext(); )
for ( Iterator it = plugins.iterator(); it.hasNext(); )
{
Plugin plugin = (Plugin) it.next();
assertEquals( "org.apache.maven", plugin.getPrefix() );
@ -153,8 +165,8 @@ public class MetadataRepositoryIndexingTest
}
// search last update using INCLUSIVE Range Query
Query qry1 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212000000" );
Query qry2 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212235959");
Query qry1 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
Query qry2 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212235959" );
RangeQuery rQry = new RangeQuery( true );
rQry.addQuery( qry1 );
rQry.addQuery( qry2 );
@ -169,8 +181,8 @@ public class MetadataRepositoryIndexingTest
}
// search last update using EXCLUSIVE Range Query
qry1 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212000000" );
qry2 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212044643");
qry1 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
qry2 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
rQry = new RangeQuery( false );
rQry.addQuery( qry1 );
rQry.addQuery( qry2 );
@ -181,33 +193,38 @@ public class MetadataRepositoryIndexingTest
indexer.close();
}
/**
* Test the exceptions thrown by MetadataRepositoryIndex.
*
* @throws Exception
*/
public void testExceptions()
throws Exception
{
//test when the object passed in the index(..) method is not a RepositoryMetadat instance
RepositoryIndexingFactory factory = ( RepositoryIndexingFactory ) lookup( RepositoryIndexingFactory.ROLE );
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
try
{
Artifact artifact = getArtifact("org.apache.maven", "maven-artifact", "2.0.1");
Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
indexer.index( artifact );
fail( "Must throw exception when the passed object is not a RepositoryMetadata object." );
}
catch( Exception e )
catch ( Exception e )
{
}
indexer.optimize();
indexer.close();
//test when the plugin prefix is blank
factory = ( RepositoryIndexingFactory ) lookup( RepositoryIndexingFactory.ROLE );
factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
try
{
RepositoryMetadata repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", GROUP_TYPE );
indexer.index( repoMetadata );
}
catch( Exception e )
catch ( Exception e )
{
}
indexer.optimize();
@ -216,15 +233,40 @@ public class MetadataRepositoryIndexingTest
//test when the index is closed
try
{
RepositoryMetadata repoMetadata = getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", GROUP_TYPE );
RepositoryMetadata repoMetadata =
getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", GROUP_TYPE );
indexer.index( repoMetadata );
fail( "Must throw exception when a metadata is added to the index while the indexer is still closed." );
}
catch( Exception e )
catch ( Exception e )
{
}
}
/**
* Test delete of document from metadata index.
*
* @throws Exception
*/
public void testDeleteMetadataDocument()
throws Exception
{
createTestIndex();
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
RepositoryMetadata repoMetadata =
getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", GROUP_TYPE );
indexer.deleteDocument( MetadataRepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
RepositoryIndexSearcher repoSearcher = factory.createMetadataRepositoryIndexSearcher( indexer );
org.apache.maven.repository.indexing.query.Query qry =
new SinglePhraseQuery( MetadataRepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
List metadataList = repoSearcher.search( qry );
assertEquals( metadataList.size(), 0 );
}
/**
* Create RepositoryMetadata object.
*
@ -236,7 +278,8 @@ public class MetadataRepositoryIndexingTest
* @return RepositoryMetadata
* @throws Exception
*/
private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename, String metadataType)
private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,
String metadataType )
throws Exception
{
RepositoryMetadata repoMetadata = null;
@ -245,25 +288,27 @@ public class MetadataRepositoryIndexingTest
MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
//group metadata
if( metadataType.equals( GROUP_TYPE ) )
if ( metadataType.equals( GROUP_TYPE ) )
{
url = new File( repository.getBasedir() + groupId.replace('.', '/') + "/" + filename ).toURL();
url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();
is = url.openStream();
repoMetadata = new GroupRepositoryMetadata(groupId);
repoMetadata = new GroupRepositoryMetadata( groupId );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
}
//artifact metadata
else if( metadataType.equals( ARTIFACT_TYPE ) )
else if ( metadataType.equals( ARTIFACT_TYPE ) )
{
url = new File( repository.getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + filename ).toURL();
url = new File(
repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + filename ).toURL();
is = url.openStream();
repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
}
//snapshot/version metadata
else if( metadataType.equals( SNAPSHOT_TYPE ) )
else if ( metadataType.equals( SNAPSHOT_TYPE ) )
{
url = new File( repository.getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + filename ).toURL();
url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + version +
"/" + filename ).toURL();
is = url.openStream();
repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
@ -275,6 +320,7 @@ public class MetadataRepositoryIndexingTest
/**
* Create artifact object.
*
* @param groupId the groupId of the artifact
* @param artifactId the artifactId of the artifact
* @param version the version of the artifact

View File

@ -450,6 +450,27 @@ public class PomRepositoryIndexingTest
indexer.close();
}
/**
* Test delete of pom document from index.
*
* @throws Exception
*/
public void testDeletePomDocument()
throws Exception
{
createTestIndex();
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
indexer.deleteDocument( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM_TYPE + pom.getId() );
RepositoryIndexSearcher repoSearcher = factory.createPomRepositoryIndexSearcher( indexer );
Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM_TYPE + pom.getId() );
List artifactList = repoSearcher.search( qry );
assertEquals( artifactList.size(), 0 );
}
private Model getPom( String groupId, String artifactId, String version )
throws Exception
{