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.IndexReader;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import java.io.File; import java.io.File;
@ -195,4 +196,26 @@ public abstract class AbstractRepositoryIndex
{ {
return repository; 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.CompoundQuery;
import org.apache.maven.repository.indexing.query.CompoundQueryTerm; import org.apache.maven.repository.indexing.query.CompoundQueryTerm;
import org.apache.maven.repository.indexing.query.Query; 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.RangeQuery;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.io.IOException; import java.io.IOException;
@ -159,17 +159,17 @@ public abstract class AbstractRepositoryIndexSearcher
} }
retVal = booleanQuery; retVal = booleanQuery;
} }
else if( query instanceof RangeQuery ) else if ( query instanceof RangeQuery )
{ {
RangeQuery rq = (RangeQuery) query; RangeQuery rq = (RangeQuery) query;
List queries = rq.getQueries(); List queries = rq.getQueries();
Iterator iter = queries.iterator(); Iterator iter = queries.iterator();
Term begin = null, end = null; Term begin = null, end = null;
if(queries.size() == 2) if ( queries.size() == 2 )
{ {
SinglePhraseQuery qry = (SinglePhraseQuery) iter.next(); SinglePhraseQuery qry = (SinglePhraseQuery) iter.next();
begin = new Term( qry.getField(), qry.getValue() ); begin = new Term( qry.getField(), qry.getValue() );
qry = ( SinglePhraseQuery ) iter.next(); qry = (SinglePhraseQuery) iter.next();
end = new Term( qry.getField(), qry.getValue() ); end = new Term( qry.getField(), qry.getValue() );
} }
retVal = new org.apache.lucene.search.RangeQuery( begin, end, rq.isInclusive() ); 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 public class ArtifactRepositoryIndex
extends AbstractRepositoryIndex extends AbstractRepositoryIndex
{ {
protected static final String FLD_ID = "id";
protected static final String FLD_NAME = "name"; protected static final String FLD_NAME = "name";
protected static final String FLD_GROUPID = "groupId"; protected static final String FLD_GROUPID = "groupId";
@ -59,13 +61,15 @@ public class ArtifactRepositoryIndex
protected static final String FLD_FILES = "files"; protected static final String FLD_FILES = "files";
private static final String[] FIELDS = private static final String[] FIELDS = {FLD_ID, FLD_NAME, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_SHA1,
{FLD_NAME, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_SHA1, FLD_MD5, FLD_CLASSES, FLD_PACKAGES, FLD_FILES}; FLD_MD5, FLD_CLASSES, FLD_PACKAGES, FLD_FILES};
private Analyzer analyzer; private Analyzer analyzer;
private Digester digester; private Digester digester;
protected static final String ARTIFACT_TYPE = "ARTIFACT";
/** /**
* Class constructor * Class constructor
* *
@ -161,6 +165,7 @@ public class ArtifactRepositoryIndex
//@todo should some of these fields be Keyword instead of Text ? //@todo should some of these fields be Keyword instead of Text ?
Document doc = new Document(); 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_NAME, artifact.getFile().getName() ) );
doc.add( Field.Text( FLD_GROUPID, artifact.getGroupId() ) ); doc.add( Field.Text( FLD_GROUPID, artifact.getGroupId() ) );
doc.add( Field.Text( FLD_ARTIFACTID, artifact.getArtifactId() ) ); doc.add( Field.Text( FLD_ARTIFACTID, artifact.getArtifactId() ) );

View File

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

View File

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

View File

@ -17,26 +17,29 @@ package org.apache.maven.repository.indexing;
*/ */
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory; 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.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.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; 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.File;
import java.io.InputStream;
import java.io.InputStreamReader; 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. * This class searches the specified index given the search/query criteria.
*
*/ */
public class MetadataRepositoryIndexSearcher public class MetadataRepositoryIndexSearcher
extends AbstractRepositoryIndexSearcher extends AbstractRepositoryIndexSearcher
{ {
private ArtifactFactory artifactFactory; private ArtifactFactory artifactFactory;
@ -56,17 +59,19 @@ public class MetadataRepositoryIndexSearcher
/** /**
* Constructor * Constructor
* @param index the index object to be set *
* @param index the index object to be set
* @param factory * @param factory
*/ */
public MetadataRepositoryIndexSearcher( MetadataRepositoryIndex index, ArtifactFactory factory) public MetadataRepositoryIndexSearcher( MetadataRepositoryIndex index, ArtifactFactory factory )
{ {
super(index); super( index );
artifactFactory = factory; artifactFactory = factory;
} }
/** /**
* Create object to be returned by the search based on the document * Create object to be returned by the search based on the document
*
* @param doc * @param doc
* @return Object * @return Object
*/ */
@ -85,11 +90,11 @@ public class MetadataRepositoryIndexSearcher
String tmpDir = (String) it.next(); String tmpDir = (String) it.next();
String metadataType = ""; String metadataType = "";
if( tmpDir.equals( doc.get( FLD_GROUPID ) ) ) if ( tmpDir.equals( doc.get( FLD_GROUPID ) ) )
{ {
metadataType = GROUP_TYPE; metadataType = GROUP_TYPE;
} }
else if( tmpDir.equals( doc.get( FLD_ARTIFACTID ) ) ) else if ( tmpDir.equals( doc.get( FLD_ARTIFACTID ) ) )
{ {
metadataType = ARTIFACT_TYPE; metadataType = ARTIFACT_TYPE;
} }
@ -100,10 +105,12 @@ public class MetadataRepositoryIndexSearcher
RepositoryMetadata repoMetadata = null; RepositoryMetadata repoMetadata = null;
try{ try
repoMetadata = getMetadata(doc.get( FLD_GROUPID ), doc.get( FLD_ARTIFACTID ), doc.get( FLD_VERSION ), metadataFile, metadataType ); {
repoMetadata = getMetadata( doc.get( FLD_GROUPID ), doc.get( FLD_ARTIFACTID ), doc.get( FLD_VERSION ),
metadataFile, metadataType );
} }
catch(Exception e) catch ( Exception e )
{ {
//@todo //@todo
} }
@ -114,15 +121,16 @@ public class MetadataRepositoryIndexSearcher
/** /**
* Create RepositoryMetadata object. * Create RepositoryMetadata object.
* *
* @param groupId the groupId to be set * @param groupId the groupId to be set
* @param artifactId the artifactId to be set * @param artifactId the artifactId to be set
* @param version the version to be set * @param version the version to be set
* @param filename the name of the metadata file * @param filename the name of the metadata file
* @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT) * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)
* @return RepositoryMetadata * @return RepositoryMetadata
* @throws Exception * @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 throws Exception
{ {
RepositoryMetadata repoMetadata = null; RepositoryMetadata repoMetadata = null;
@ -131,25 +139,27 @@ public class MetadataRepositoryIndexSearcher
MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
//group metadata //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(); is = url.openStream();
repoMetadata = new GroupRepositoryMetadata(groupId); repoMetadata = new GroupRepositoryMetadata( groupId );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
} }
//artifact metadata //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(); is = url.openStream();
repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
} }
//snapshot/version metadata //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(); is = url.openStream();
repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
@ -160,9 +170,10 @@ public class MetadataRepositoryIndexSearcher
/** /**
* Create artifact object. * Create artifact object.
* @param groupId the groupId of the artifact *
* @param groupId the groupId of the artifact
* @param artifactId the artifactId of the artifact * @param artifactId the artifactId of the artifact
* @param version the version of the artifact * @param version the version of the artifact
* @return Artifact * @return Artifact
* @throws Exception * @throws Exception
*/ */

View File

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

View File

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

View File

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

View File

@ -39,18 +39,6 @@ import java.util.List;
public class ArtifactRepositoryIndexingTest public class ArtifactRepositoryIndexingTest
extends PlexusTestCase 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 ArtifactFactory artifactFactory;
private ArtifactRepository repository; private ArtifactRepository repository;
@ -59,11 +47,7 @@ public class ArtifactRepositoryIndexingTest
private Digester digester; private Digester digester;
private static final String SHA1 = "sha1"; private static final String ARTIFACT_TYPE = "ARTIFACT";
private static final String MD5 = "md5";
private static final String NAME = "name";
protected void setUp() protected void setUp()
throws Exception throws Exception
@ -181,7 +165,7 @@ public class ArtifactRepositoryIndexingTest
RepositoryIndexSearcher repoSearcher = factory.createArtifactRepositoryIndexSearcher( indexer ); RepositoryIndexSearcher repoSearcher = factory.createArtifactRepositoryIndexSearcher( indexer );
// search version // search version
Query qry = new SinglePhraseQuery( VERSION, "1.0" ); Query qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_VERSION, "1.0" );
List artifacts = repoSearcher.search( qry ); List artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@ -191,7 +175,7 @@ public class ArtifactRepositoryIndexingTest
} }
// search classes // search classes
qry = new SinglePhraseQuery( CLASSES, "App" ); qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_CLASSES, "App" );
artifacts = repoSearcher.search( qry ); artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@ -201,7 +185,7 @@ public class ArtifactRepositoryIndexingTest
} }
// search packages // search packages
qry = new SinglePhraseQuery( PACKAGES, "groupId" ); qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_PACKAGES, "groupId" );
artifacts = repoSearcher.search( qry ); artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
@ -211,7 +195,7 @@ public class ArtifactRepositoryIndexingTest
} }
// search files // search files
qry = new SinglePhraseQuery( FILES, "pom.xml" ); qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_FILES, "pom.xml" );
artifacts = repoSearcher.search( qry ); artifacts = repoSearcher.search( qry );
assertEquals( 3, artifacts.size() ); assertEquals( 3, artifacts.size() );
Iterator iter = artifacts.iterator(); Iterator iter = artifacts.iterator();
@ -222,7 +206,7 @@ public class ArtifactRepositoryIndexingTest
} }
// search group id // search group id
qry = new SinglePhraseQuery( GROUPID, "org.apache.maven" ); qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
artifacts = repoSearcher.search( qry ); artifacts = repoSearcher.search( qry );
assertEquals( 2, artifacts.size() ); assertEquals( 2, artifacts.size() );
iter = artifacts.iterator(); iter = artifacts.iterator();
@ -233,7 +217,7 @@ public class ArtifactRepositoryIndexingTest
} }
// search artifact id // search artifact id
qry = new SinglePhraseQuery( ARTIFACTID, "maven-artifact" ); qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
artifacts = repoSearcher.search( qry ); artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); ) for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -243,7 +227,7 @@ public class ArtifactRepositoryIndexingTest
} }
// search version // search version
qry = new SinglePhraseQuery( VERSION, "2" ); qry = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_VERSION, "2" );
artifacts = repoSearcher.search( qry ); artifacts = repoSearcher.search( qry );
assertEquals( 2, artifacts.size() ); assertEquals( 2, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); ) for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -258,7 +242,7 @@ public class ArtifactRepositoryIndexingTest
String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); 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 ); artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); ) for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -270,7 +254,7 @@ public class ArtifactRepositoryIndexingTest
// search md5 checksum // search md5 checksum
String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 ); 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 ); artifacts = repoSearcher.search( qry );
assertEquals( 1, artifacts.size() ); assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); ) for ( iter = artifacts.iterator(); iter.hasNext(); )
@ -300,8 +284,8 @@ public class ArtifactRepositoryIndexingTest
// Criteria 1: required query // Criteria 1: required query
// ex. artifactId=maven-artifact AND groupId=org.apache.maven // ex. artifactId=maven-artifact AND groupId=org.apache.maven
Query qry1 = new SinglePhraseQuery( ARTIFACTID, "maven-artifact" ); Query qry1 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
Query qry2 = new SinglePhraseQuery( GROUPID, "org.apache.maven" ); Query qry2 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
CompoundQuery rQry = new CompoundQuery(); CompoundQuery rQry = new CompoundQuery();
rQry.and( qry1 ); rQry.and( qry1 );
rQry.and( qry2 ); rQry.and( qry2 );
@ -317,7 +301,7 @@ public class ArtifactRepositoryIndexingTest
// Criteria 2: nested required query // Criteria 2: nested required query
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
// version=2.0.3 // 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(); CompoundQuery oQry = new CompoundQuery();
oQry.or( rQry ); oQry.or( rQry );
oQry.or( qry3 ); oQry.or( qry3 );
@ -334,14 +318,14 @@ public class ArtifactRepositoryIndexingTest
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
// (version=2.0.3 OR version=2.0.1) // (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact) // 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 = new CompoundQuery();
oQry.or( qry3 ); oQry.or( qry3 );
oQry.or( qry4 ); oQry.or( qry4 );
CompoundQuery oQry5 = new CompoundQuery(); CompoundQuery oQry5 = new CompoundQuery();
Query qry9 = new SinglePhraseQuery( NAME, "maven-artifact-2.0.1.jar" ); Query qry9 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_NAME, "maven-artifact-2.0.1.jar" );
Query qry10 = new SinglePhraseQuery( NAME, "maven-artifact" ); Query qry10 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_NAME, "maven-artifact" );
oQry5.or( qry9 ); oQry5.or( qry9 );
oQry5.or( qry10 ); oQry5.or( qry10 );
@ -365,8 +349,8 @@ public class ArtifactRepositoryIndexingTest
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
// OR [(artifactId=sample AND groupId=test)] // OR [(artifactId=sample AND groupId=test)]
CompoundQuery rQry3 = new CompoundQuery(); CompoundQuery rQry3 = new CompoundQuery();
Query qry5 = new SinglePhraseQuery( ARTIFACTID, "sample" ); Query qry5 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "sample" );
Query qry6 = new SinglePhraseQuery( GROUPID, "test" ); Query qry6 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "test" );
rQry3.and( qry5 ); rQry3.and( qry5 );
rQry3.and( qry6 ); rQry3.and( qry6 );
CompoundQuery oQry2 = new CompoundQuery(); CompoundQuery oQry2 = new CompoundQuery();
@ -389,8 +373,8 @@ public class ArtifactRepositoryIndexingTest
// [(artifactId=sample AND groupId=test)] OR // [(artifactId=sample AND groupId=test)] OR
// [(artifactId=sample2 AND groupId=test)] // [(artifactId=sample2 AND groupId=test)]
CompoundQuery rQry4 = new CompoundQuery(); CompoundQuery rQry4 = new CompoundQuery();
Query qry7 = new SinglePhraseQuery( ARTIFACTID, "sample2" ); Query qry7 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_ARTIFACTID, "sample2" );
Query qry8 = new SinglePhraseQuery( GROUPID, "test" ); Query qry8 = new SinglePhraseQuery( ArtifactRepositoryIndex.FLD_GROUPID, "test" );
rQry4.and( qry7 ); rQry4.and( qry7 );
rQry4.and( qry8 ); rQry4.and( qry8 );
oQry2.and( rQry4 ); oQry2.and( rQry4 );
@ -406,6 +390,24 @@ public class ArtifactRepositoryIndexingTest
indexer.close(); 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 ) private Artifact getArtifact( String groupId, String artifactId, String version )
throws Exception 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.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 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.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.repository.indexing.query.Query; import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.RangeQuery; import org.apache.maven.repository.indexing.query.RangeQuery;
@ -40,16 +46,12 @@ import java.util.List;
* This class tests the MetadataRepositoryIndex. * This class tests the MetadataRepositoryIndex.
*/ */
public class MetadataRepositoryIndexingTest public class MetadataRepositoryIndexingTest
extends PlexusTestCase extends PlexusTestCase
{ {
private ArtifactRepository repository; private ArtifactRepository repository;
private String indexPath; 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 GROUP_TYPE = "GROUP";
private static final String ARTIFACT_TYPE = "ARTIFACT"; private static final String ARTIFACT_TYPE = "ARTIFACT";
@ -62,15 +64,17 @@ public class MetadataRepositoryIndexingTest
/** /**
* Set up. * Set up.
*
* @throws Exception * @throws Exception
*/ */
public void setUp() throws Exception public void setUp()
throws Exception
{ {
super.setUp(); super.setUp();
File repositoryDirectory = getTestFile( "src/test/repository" ); File repositoryDirectory = getTestFile( "src/test/repository" );
String repoDir = repositoryDirectory.toURL().toString(); String repoDir = repositoryDirectory.toURL().toString();
ArtifactRepositoryLayout layout = ( ArtifactRepositoryLayout ) lookup( ArtifactRepositoryLayout.ROLE, "default" ); ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
ArtifactRepositoryFactory repoFactory = ( ArtifactRepositoryFactory ) lookup( ArtifactRepositoryFactory.ROLE ); ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
indexPath = "target/index/metadata"; indexPath = "target/index/metadata";
@ -79,9 +83,11 @@ public class MetadataRepositoryIndexingTest
/** /**
* Tear down. * Tear down.
*
* @throws Exception * @throws Exception
*/ */
public void tearDown() throws Exception public void tearDown()
throws Exception
{ {
repository = null; repository = null;
super.tearDown(); super.tearDown();
@ -89,20 +95,25 @@ public class MetadataRepositoryIndexingTest
/** /**
* Create the test index. * Create the test index.
*
* @throws Exception * @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 ); 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 ); 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 ); 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.index( repoMetadata );
indexer.optimize(); indexer.optimize();
@ -114,9 +125,9 @@ public class MetadataRepositoryIndexingTest
* *
* @throws Exception * @throws Exception
*/ */
public void testSearch() public void testSearch()
throws Exception throws Exception
{ {
createTestIndex(); createTestIndex();
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
@ -124,7 +135,8 @@ public class MetadataRepositoryIndexingTest
RepositoryIndexSearcher repoSearcher = factory.createMetadataRepositoryIndexSearcher( indexer ); RepositoryIndexSearcher repoSearcher = factory.createMetadataRepositoryIndexSearcher( indexer );
// search last update // 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 ); List metadataList = repoSearcher.search( qry );
assertEquals( 1, metadataList.size() ); assertEquals( 1, metadataList.size() );
for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
@ -137,7 +149,7 @@ public class MetadataRepositoryIndexingTest
} }
// search plugin prefix // search plugin prefix
qry = new SinglePhraseQuery( FLD_PLUGINPREFIX, "org.apache.maven" ); qry = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" );
metadataList = repoSearcher.search( qry ); metadataList = repoSearcher.search( qry );
assertEquals( 1, metadataList.size() ); assertEquals( 1, metadataList.size() );
for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
@ -145,7 +157,7 @@ public class MetadataRepositoryIndexingTest
RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next(); RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();
Metadata metadata = repoMetadata.getMetadata(); Metadata metadata = repoMetadata.getMetadata();
List plugins = metadata.getPlugins(); List plugins = metadata.getPlugins();
for( Iterator it = plugins.iterator(); it.hasNext(); ) for ( Iterator it = plugins.iterator(); it.hasNext(); )
{ {
Plugin plugin = (Plugin) it.next(); Plugin plugin = (Plugin) it.next();
assertEquals( "org.apache.maven", plugin.getPrefix() ); assertEquals( "org.apache.maven", plugin.getPrefix() );
@ -153,8 +165,8 @@ public class MetadataRepositoryIndexingTest
} }
// search last update using INCLUSIVE Range Query // search last update using INCLUSIVE Range Query
Query qry1 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212000000" ); Query qry1 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
Query qry2 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212235959"); Query qry2 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212235959" );
RangeQuery rQry = new RangeQuery( true ); RangeQuery rQry = new RangeQuery( true );
rQry.addQuery( qry1 ); rQry.addQuery( qry1 );
rQry.addQuery( qry2 ); rQry.addQuery( qry2 );
@ -169,8 +181,8 @@ public class MetadataRepositoryIndexingTest
} }
// search last update using EXCLUSIVE Range Query // search last update using EXCLUSIVE Range Query
qry1 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212000000" ); qry1 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
qry2 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212044643"); qry2 = new SinglePhraseQuery( MetadataRepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
rQry = new RangeQuery( false ); rQry = new RangeQuery( false );
rQry.addQuery( qry1 ); rQry.addQuery( qry1 );
rQry.addQuery( qry2 ); rQry.addQuery( qry2 );
@ -179,64 +191,95 @@ public class MetadataRepositoryIndexingTest
assertEquals( metadataList.size(), 0 ); assertEquals( metadataList.size(), 0 );
indexer.close(); indexer.close();
} }
/**
* Test the exceptions thrown by MetadataRepositoryIndex.
*
* @throws Exception
*/
public void testExceptions() public void testExceptions()
throws Exception throws Exception
{ {
//test when the object passed in the index(..) method is not a RepositoryMetadat instance //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 ); indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
try 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 ); indexer.index( artifact );
fail( "Must throw exception when the passed object is not a RepositoryMetadata object." ); fail( "Must throw exception when the passed object is not a RepositoryMetadata object." );
} }
catch( Exception e ) catch ( Exception e )
{ {
} }
indexer.optimize(); indexer.optimize();
indexer.close(); indexer.close();
//test when the plugin prefix is blank //test when the plugin prefix is blank
factory = ( RepositoryIndexingFactory ) lookup( RepositoryIndexingFactory.ROLE ); factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
indexer = factory.createMetadataRepositoryIndex( indexPath, repository ); indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
try try
{ {
RepositoryMetadata repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", GROUP_TYPE ); RepositoryMetadata repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", GROUP_TYPE );
indexer.index( repoMetadata ); indexer.index( repoMetadata );
} }
catch( Exception e ) catch ( Exception e )
{ {
} }
indexer.optimize(); indexer.optimize();
indexer.close(); indexer.close();
//test when the index is closed //test when the index is closed
try 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 ); indexer.index( repoMetadata );
fail( "Must throw exception when a metadata is added to the index while the indexer is still closed." ); 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. * Create RepositoryMetadata object.
* *
* @param groupId the groupId to be set * @param groupId the groupId to be set
* @param artifactId the artifactId to be set * @param artifactId the artifactId to be set
* @param version the version to be set * @param version the version to be set
* @param filename the name of the metadata file * @param filename the name of the metadata file
* @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT) * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)
* @return RepositoryMetadata * @return RepositoryMetadata
* @throws Exception * @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 throws Exception
{ {
RepositoryMetadata repoMetadata = null; RepositoryMetadata repoMetadata = null;
@ -245,25 +288,27 @@ public class MetadataRepositoryIndexingTest
MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
//group metadata //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(); is = url.openStream();
repoMetadata = new GroupRepositoryMetadata(groupId); repoMetadata = new GroupRepositoryMetadata( groupId );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
} }
//artifact metadata //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(); is = url.openStream();
repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
} }
//snapshot/version metadata //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(); is = url.openStream();
repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
@ -275,9 +320,10 @@ public class MetadataRepositoryIndexingTest
/** /**
* Create artifact object. * Create artifact object.
* @param groupId the groupId of the artifact *
* @param groupId the groupId of the artifact
* @param artifactId the artifactId of the artifact * @param artifactId the artifactId of the artifact
* @param version the version of the artifact * @param version the version of the artifact
* @return Artifact * @return Artifact
* @throws Exception * @throws Exception
*/ */

View File

@ -450,6 +450,27 @@ public class PomRepositoryIndexingTest
indexer.close(); 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 ) private Model getPom( String groupId, String artifactId, String version )
throws Exception throws Exception
{ {