mirror of https://github.com/apache/archiva.git
[MRM-127] create indexer based on index records
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@425609 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
16305b2cc0
commit
0d68e24a2e
|
@ -0,0 +1,46 @@
|
||||||
|
package org.apache.maven.repository.indexing;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maintain an artifact index on the repository.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
*/
|
||||||
|
public interface RepositoryArtifactIndex
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Indexes the artifacts found within the specified list of index records. If the artifacts are already in the
|
||||||
|
* repository they are updated.
|
||||||
|
*
|
||||||
|
* @param records the artifacts to index
|
||||||
|
* @throws RepositoryIndexException if there is a problem indexing the records
|
||||||
|
*/
|
||||||
|
void indexRecords( List records )
|
||||||
|
throws RepositoryIndexException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the index already exists.
|
||||||
|
*
|
||||||
|
* @return true if the index already exists
|
||||||
|
* @throws RepositoryIndexException if the index location is not valid
|
||||||
|
*/
|
||||||
|
boolean exists()
|
||||||
|
throws RepositoryIndexException;
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package org.apache.maven.repository.indexing;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtain an index instance.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
*/
|
||||||
|
public interface RepositoryArtifactIndexFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Plexus role.
|
||||||
|
*/
|
||||||
|
String ROLE = RepositoryArtifactIndexFactory.class.getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to create an instance of the standard index.
|
||||||
|
*
|
||||||
|
* @param indexPath the path where the index will be created/updated
|
||||||
|
* @param repository the repository where the indexed artifacts are located
|
||||||
|
* @return the index instance
|
||||||
|
*/
|
||||||
|
RepositoryArtifactIndex createStandardIndex( File indexPath, ArtifactRepository repository );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to create an instance of the minimal index.
|
||||||
|
*
|
||||||
|
* @param indexPath the path where the index will be created/updated
|
||||||
|
* @param repository the repository where the indexed artifacts are located
|
||||||
|
* @return the index instance
|
||||||
|
*/
|
||||||
|
RepositoryArtifactIndex createMinimalIndex( File indexPath, ArtifactRepository repository );
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package org.apache.maven.repository.indexing.lucene;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.maven.repository.indexing.record.RepositoryIndexRecord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts repository records to Lucene documents.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
*/
|
||||||
|
public interface LuceneIndexRecordConverter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Convert an index record to a Lucene document.
|
||||||
|
*
|
||||||
|
* @param record the record
|
||||||
|
* @return the document
|
||||||
|
*/
|
||||||
|
Document convert( RepositoryIndexRecord record );
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package org.apache.maven.repository.indexing.lucene;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.maven.repository.indexing.record.RepositoryIndexRecord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the minimal index record to a Lucene document.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
*/
|
||||||
|
public class LuceneMinimalIndexRecordConverter
|
||||||
|
implements LuceneIndexRecordConverter
|
||||||
|
{
|
||||||
|
public Document convert( RepositoryIndexRecord record )
|
||||||
|
{
|
||||||
|
// TODO: implement!
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,191 @@
|
||||||
|
package org.apache.maven.repository.indexing.lucene;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
|
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.lucene.document.Field;
|
||||||
|
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 org.apache.maven.repository.indexing.RepositoryArtifactIndex;
|
||||||
|
import org.apache.maven.repository.indexing.RepositoryIndexException;
|
||||||
|
import org.apache.maven.repository.indexing.record.RepositoryIndexRecord;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lucene implementation of a repository index.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
*/
|
||||||
|
public class LuceneRepositoryArtifactIndex
|
||||||
|
implements RepositoryArtifactIndex
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The location of the index on the file system.
|
||||||
|
*/
|
||||||
|
private File indexLocation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert repository records to Lucene documents.
|
||||||
|
*/
|
||||||
|
private LuceneIndexRecordConverter converter;
|
||||||
|
|
||||||
|
private static final String FLD_PK = "pk";
|
||||||
|
|
||||||
|
public LuceneRepositoryArtifactIndex( File indexPath, ArtifactRepository repository,
|
||||||
|
LuceneIndexRecordConverter converter )
|
||||||
|
{
|
||||||
|
this.indexLocation = indexPath;
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void indexRecords( List records )
|
||||||
|
throws RepositoryIndexException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
deleteRecords( records );
|
||||||
|
}
|
||||||
|
catch ( IOException e )
|
||||||
|
{
|
||||||
|
throw new RepositoryIndexException( "Failed to delete an index document", e );
|
||||||
|
}
|
||||||
|
|
||||||
|
addRecords( records );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addRecords( List records )
|
||||||
|
throws RepositoryIndexException
|
||||||
|
{
|
||||||
|
IndexWriter indexWriter;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
indexWriter = new IndexWriter( indexLocation, getAnalyzer(), !exists() );
|
||||||
|
}
|
||||||
|
catch ( IOException e )
|
||||||
|
{
|
||||||
|
throw new RepositoryIndexException( "Unable to open index", e );
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for ( Iterator i = records.iterator(); i.hasNext(); )
|
||||||
|
{
|
||||||
|
RepositoryIndexRecord record = (RepositoryIndexRecord) i.next();
|
||||||
|
|
||||||
|
Document document = converter.convert( record );
|
||||||
|
document.add( new Field( FLD_PK, record.getPrimaryKey(), Field.Store.NO, Field.Index.UN_TOKENIZED ) );
|
||||||
|
|
||||||
|
indexWriter.addDocument( document );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( IOException e )
|
||||||
|
{
|
||||||
|
throw new RepositoryIndexException( "Failed to add an index document", e );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
close( indexWriter );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void close( IndexWriter indexWriter )
|
||||||
|
throws RepositoryIndexException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if ( indexWriter != null )
|
||||||
|
{
|
||||||
|
indexWriter.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( IOException e )
|
||||||
|
{
|
||||||
|
throw new RepositoryIndexException( e.getMessage(), e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Analyzer getAnalyzer()
|
||||||
|
{
|
||||||
|
// TODO: investigate why changed in original!
|
||||||
|
return new StandardAnalyzer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteRecords( List records )
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
if ( exists() )
|
||||||
|
{
|
||||||
|
IndexReader indexReader = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
indexReader = IndexReader.open( indexLocation );
|
||||||
|
|
||||||
|
for ( Iterator artifacts = records.iterator(); artifacts.hasNext(); )
|
||||||
|
{
|
||||||
|
RepositoryIndexRecord record = (RepositoryIndexRecord) artifacts.next();
|
||||||
|
|
||||||
|
Term term = new Term( FLD_PK, record.getPrimaryKey() );
|
||||||
|
|
||||||
|
indexReader.deleteDocuments( term );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if ( indexReader != null )
|
||||||
|
{
|
||||||
|
indexReader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists()
|
||||||
|
throws RepositoryIndexException
|
||||||
|
{
|
||||||
|
if ( IndexReader.indexExists( indexLocation ) )
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if ( !indexLocation.exists() )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if ( indexLocation.isDirectory() )
|
||||||
|
{
|
||||||
|
if ( indexLocation.listFiles().length > 1 )
|
||||||
|
{
|
||||||
|
throw new RepositoryIndexException( indexLocation + " is not a valid index directory." );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new RepositoryIndexException( indexLocation + " is not a directory." );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package org.apache.maven.repository.indexing.lucene;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||||
|
import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
|
||||||
|
import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for Lucene artifact index instances.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
* @plexus.component role="org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory" role-hint="lucene"
|
||||||
|
*/
|
||||||
|
public class LuceneRepositoryArtifactIndexFactory
|
||||||
|
implements RepositoryArtifactIndexFactory
|
||||||
|
{
|
||||||
|
public RepositoryArtifactIndex createStandardIndex( File indexPath, ArtifactRepository repository )
|
||||||
|
{
|
||||||
|
return new LuceneRepositoryArtifactIndex( indexPath, repository, new LuceneStandardIndexRecordConverter() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public RepositoryArtifactIndex createMinimalIndex( File indexPath, ArtifactRepository repository )
|
||||||
|
{
|
||||||
|
return new LuceneRepositoryArtifactIndex( indexPath, repository, new LuceneMinimalIndexRecordConverter() );
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,115 @@
|
||||||
|
package org.apache.maven.repository.indexing.lucene;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.document.DateTools;
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.lucene.document.Field;
|
||||||
|
import org.apache.lucene.document.NumberTools;
|
||||||
|
import org.apache.maven.repository.indexing.record.RepositoryIndexRecord;
|
||||||
|
import org.apache.maven.repository.indexing.record.StandardArtifactIndexRecord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the standard index record to a Lucene document.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
* @todo use enum for field names
|
||||||
|
*/
|
||||||
|
public class LuceneStandardIndexRecordConverter
|
||||||
|
implements LuceneIndexRecordConverter
|
||||||
|
{
|
||||||
|
private static final String FLD_FILENAME = "filename";
|
||||||
|
|
||||||
|
private static final String FLD_GROUPID = "groupId";
|
||||||
|
|
||||||
|
private static final String FLD_ARTIFACTID = "artifactId";
|
||||||
|
|
||||||
|
private static final String FLD_VERSION = "version";
|
||||||
|
|
||||||
|
private static final String FLD_TYPE = "type";
|
||||||
|
|
||||||
|
private static final String FLD_CLASSIFIER = "classifier";
|
||||||
|
|
||||||
|
private static final String FLD_PACKAGING = "packaging";
|
||||||
|
|
||||||
|
private static final String FLD_REPOSITORY = "repo";
|
||||||
|
|
||||||
|
private static final String FLD_LAST_MODIFIED = "lastModified";
|
||||||
|
|
||||||
|
private static final String FLD_FILE_SIZE = "fileSize";
|
||||||
|
|
||||||
|
private static final String FLD_MD5 = "md5";
|
||||||
|
|
||||||
|
private static final String FLD_SHA1 = "sha1";
|
||||||
|
|
||||||
|
private static final String FLD_CLASSES = "classes";
|
||||||
|
|
||||||
|
private static final String FLD_PACKAGES = "packages";
|
||||||
|
|
||||||
|
private static final String FLD_PLUGINPREFIX = "pluginPrefix";
|
||||||
|
|
||||||
|
private static final String FLD_FILES = "files";
|
||||||
|
|
||||||
|
public Document convert( RepositoryIndexRecord record )
|
||||||
|
{
|
||||||
|
StandardArtifactIndexRecord standardIndexRecord = (StandardArtifactIndexRecord) record;
|
||||||
|
|
||||||
|
Document document = new Document();
|
||||||
|
addTokenizedField( document, FLD_FILENAME, standardIndexRecord.getFilename() );
|
||||||
|
addTokenizedField( document, FLD_GROUPID, standardIndexRecord.getGroupId() );
|
||||||
|
addTokenizedField( document, FLD_ARTIFACTID, standardIndexRecord.getArtifactId() );
|
||||||
|
addTokenizedField( document, FLD_VERSION, standardIndexRecord.getVersion() );
|
||||||
|
addUntokenizedField( document, FLD_TYPE, standardIndexRecord.getType() );
|
||||||
|
addTokenizedField( document, FLD_CLASSIFIER, standardIndexRecord.getClassifier() );
|
||||||
|
addUntokenizedField( document, FLD_PACKAGING, standardIndexRecord.getPackaging() );
|
||||||
|
addTokenizedField( document, FLD_REPOSITORY, standardIndexRecord.getRepository() );
|
||||||
|
addUntokenizedField( document, FLD_LAST_MODIFIED, DateTools.timeToString( standardIndexRecord.getLastModified(),
|
||||||
|
DateTools.Resolution.SECOND ) );
|
||||||
|
addUntokenizedField( document, FLD_FILE_SIZE, NumberTools.longToString( standardIndexRecord.getSize() ) );
|
||||||
|
addUntokenizedField( document, FLD_MD5, standardIndexRecord.getMd5Checksum() );
|
||||||
|
addUntokenizedField( document, FLD_SHA1, standardIndexRecord.getSha1Checksum() );
|
||||||
|
addTokenizedField( document, FLD_CLASSES, standardIndexRecord.getClasses() );
|
||||||
|
addTokenizedField( document, FLD_PACKAGES, standardIndexRecord.getPackages() );
|
||||||
|
addTokenizedField( document, FLD_FILES, standardIndexRecord.getFiles() );
|
||||||
|
addTokenizedField( document, FLD_PLUGINPREFIX, standardIndexRecord.getPluginPrefix() );
|
||||||
|
/* TODO: add later
|
||||||
|
document.add( Field.Keyword( FLD_LICENSE_URLS, "" ) );
|
||||||
|
document.add( Field.Keyword( FLD_DEPENDENCIES, "" ) );
|
||||||
|
document.add( Field.Keyword( FLD_PLUGINS_REPORT, "" ) );
|
||||||
|
document.add( Field.Keyword( FLD_PLUGINS_BUILD, "" ) );
|
||||||
|
*/
|
||||||
|
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addUntokenizedField( Document document, String name, String value )
|
||||||
|
{
|
||||||
|
if ( value != null )
|
||||||
|
{
|
||||||
|
document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addTokenizedField( Document document, String name, String value )
|
||||||
|
{
|
||||||
|
if ( value != null )
|
||||||
|
{
|
||||||
|
document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -159,4 +159,9 @@ public class MinimalArtifactIndexRecord
|
||||||
return "Filename: " + filename + "; checksum: " + md5Checksum + "; size: " + size + "; lastModified: " +
|
return "Filename: " + filename + "; checksum: " + md5Checksum + "; size: " + size + "; lastModified: " +
|
||||||
new Date( lastModified ) + "; classes: " + classes;
|
new Date( lastModified ) + "; classes: " + classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getPrimaryKey()
|
||||||
|
{
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,4 +23,10 @@ package org.apache.maven.repository.indexing.record;
|
||||||
*/
|
*/
|
||||||
public interface RepositoryIndexRecord
|
public interface RepositoryIndexRecord
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Get the primary key used to identify the record uniquely in the index.
|
||||||
|
*
|
||||||
|
* @return the primary key
|
||||||
|
*/
|
||||||
|
String getPrimaryKey();
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,6 +69,16 @@ public class StandardArtifactIndexRecord
|
||||||
*/
|
*/
|
||||||
private String repository;
|
private String repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The packaging specified in the POM for this artifact.
|
||||||
|
*/
|
||||||
|
private String packaging;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The plugin prefix specified in the metadata if the artifact is a plugin.
|
||||||
|
*/
|
||||||
|
private String pluginPrefix;
|
||||||
|
|
||||||
public void setSha1Checksum( String sha1Checksum )
|
public void setSha1Checksum( String sha1Checksum )
|
||||||
{
|
{
|
||||||
this.sha1Checksum = sha1Checksum;
|
this.sha1Checksum = sha1Checksum;
|
||||||
|
@ -170,6 +180,14 @@ public class StandardArtifactIndexRecord
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if ( packaging != null ? !packaging.equals( that.packaging ) : that.packaging != null )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ( pluginPrefix != null ? !pluginPrefix.equals( that.pluginPrefix ) : that.pluginPrefix != null )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -186,6 +204,73 @@ public class StandardArtifactIndexRecord
|
||||||
result = 31 * result + ( packages != null ? packages.hashCode() : 0 );
|
result = 31 * result + ( packages != null ? packages.hashCode() : 0 );
|
||||||
result = 31 * result + ( files != null ? files.hashCode() : 0 );
|
result = 31 * result + ( files != null ? files.hashCode() : 0 );
|
||||||
result = 31 * result + ( repository != null ? repository.hashCode() : 0 );
|
result = 31 * result + ( repository != null ? repository.hashCode() : 0 );
|
||||||
|
result = 31 * result + ( packaging != null ? packaging.hashCode() : 0 );
|
||||||
|
result = 31 * result + ( pluginPrefix != null ? pluginPrefix.hashCode() : 0 );
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSha1Checksum()
|
||||||
|
{
|
||||||
|
return sha1Checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGroupId()
|
||||||
|
{
|
||||||
|
return groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getArtifactId()
|
||||||
|
{
|
||||||
|
return artifactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVersion()
|
||||||
|
{
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClassifier()
|
||||||
|
{
|
||||||
|
return classifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType()
|
||||||
|
{
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPackages()
|
||||||
|
{
|
||||||
|
return packages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFiles()
|
||||||
|
{
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRepository()
|
||||||
|
{
|
||||||
|
return repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPackaging()
|
||||||
|
{
|
||||||
|
return packaging;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPluginPrefix()
|
||||||
|
{
|
||||||
|
return pluginPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPackaging( String packaging )
|
||||||
|
{
|
||||||
|
this.packaging = packaging;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPluginPrefix( String pluginPrefix )
|
||||||
|
{
|
||||||
|
this.pluginPrefix = pluginPrefix;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,8 +89,14 @@ public class StandardArtifactIndexRecordFactory
|
||||||
record.setSize( file.length() );
|
record.setSize( file.length() );
|
||||||
record.setRepository( artifact.getRepository().getId() );
|
record.setRepository( artifact.getRepository().getId() );
|
||||||
/* TODO! these come from the POM and metadata, so probably part of an update record method instead
|
/* TODO! these come from the POM and metadata, so probably part of an update record method instead
|
||||||
|
// remember to test parent & inheritence
|
||||||
record.setPluginPrefix( pluginPrefix );
|
record.setPluginPrefix( pluginPrefix );
|
||||||
record.setPackaging( packaging );
|
record.setPackaging( packaging );
|
||||||
|
record.setProjectName( name );
|
||||||
|
record.setProjectDescription( description );
|
||||||
|
record.setInceptionYear( year );
|
||||||
|
*/
|
||||||
|
/* TODO: fields for later
|
||||||
indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() );
|
indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() );
|
||||||
indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() );
|
indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() );
|
||||||
record.setDependencies( dependencies );
|
record.setDependencies( dependencies );
|
||||||
|
|
|
@ -0,0 +1,306 @@
|
||||||
|
package org.apache.maven.repository.indexing.lucene;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2005-2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.lucene.index.IndexReader;
|
||||||
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.maven.artifact.Artifact;
|
||||||
|
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.repository.indexing.RepositoryArtifactIndex;
|
||||||
|
import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
|
||||||
|
import org.apache.maven.repository.indexing.RepositoryIndexException;
|
||||||
|
import org.apache.maven.repository.indexing.record.RepositoryIndexRecord;
|
||||||
|
import org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory;
|
||||||
|
import org.codehaus.plexus.PlexusTestCase;
|
||||||
|
import org.codehaus.plexus.util.FileUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the Lucene implementation of the artifact index.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
*/
|
||||||
|
public class LuceneRepositoryArtifactIndexTest
|
||||||
|
extends PlexusTestCase
|
||||||
|
{
|
||||||
|
private RepositoryArtifactIndex index;
|
||||||
|
|
||||||
|
private ArtifactRepository repository;
|
||||||
|
|
||||||
|
private ArtifactFactory artifactFactory;
|
||||||
|
|
||||||
|
private File indexLocation;
|
||||||
|
|
||||||
|
private RepositoryIndexRecordFactory recordFactory;
|
||||||
|
|
||||||
|
protected void setUp()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
super.setUp();
|
||||||
|
|
||||||
|
recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
|
||||||
|
|
||||||
|
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
|
||||||
|
|
||||||
|
ArtifactRepositoryFactory repositoryFactory =
|
||||||
|
(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
|
||||||
|
|
||||||
|
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
|
||||||
|
|
||||||
|
File file = getTestFile( "src/test/managed-repository" );
|
||||||
|
repository =
|
||||||
|
repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
|
||||||
|
|
||||||
|
RepositoryArtifactIndexFactory factory =
|
||||||
|
(RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
|
||||||
|
|
||||||
|
indexLocation = getTestFile( "target/test-index" );
|
||||||
|
|
||||||
|
FileUtils.deleteDirectory( indexLocation );
|
||||||
|
|
||||||
|
// TODO: test minimal one in same way
|
||||||
|
index = factory.createStandardIndex( indexLocation, repository );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIndexExists()
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
assertFalse( "check index doesn't exist", index.exists() );
|
||||||
|
|
||||||
|
// create empty directory
|
||||||
|
indexLocation.mkdirs();
|
||||||
|
assertFalse( "check index doesn't exist even if directory does", index.exists() );
|
||||||
|
|
||||||
|
// create index, with no records
|
||||||
|
createEmptyIndex();
|
||||||
|
assertTrue( "check index is considered to exist", index.exists() );
|
||||||
|
|
||||||
|
// Test non-directory
|
||||||
|
FileUtils.deleteDirectory( indexLocation );
|
||||||
|
indexLocation.createNewFile();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
index.exists();
|
||||||
|
fail( "Index operation should fail as the location is not valid" );
|
||||||
|
}
|
||||||
|
catch ( RepositoryIndexException e )
|
||||||
|
{
|
||||||
|
// great
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
indexLocation.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddRecordNoIndex()
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
Artifact artifact = createArtifact( "test-jar" );
|
||||||
|
|
||||||
|
RepositoryIndexRecord record = recordFactory.createRecord( artifact );
|
||||||
|
index.indexRecords( Collections.singletonList( record ) );
|
||||||
|
|
||||||
|
IndexReader reader = IndexReader.open( indexLocation );
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Document document = reader.document( 0 );
|
||||||
|
assertEquals( "Check document", "test-jar", document.getField( "artifactId" ).stringValue() );
|
||||||
|
assertEquals( "Check index size", 1, reader.numDocs() );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddRecordExistingEmptyIndex()
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
createEmptyIndex();
|
||||||
|
|
||||||
|
Artifact artifact = createArtifact( "test-jar" );
|
||||||
|
|
||||||
|
RepositoryIndexRecord record = recordFactory.createRecord( artifact );
|
||||||
|
index.indexRecords( Collections.singletonList( record ) );
|
||||||
|
|
||||||
|
IndexReader reader = IndexReader.open( indexLocation );
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Document document = reader.document( 0 );
|
||||||
|
assertEquals( "Check document", "test-jar", document.getField( "artifactId" ).stringValue() );
|
||||||
|
assertEquals( "Check index size", 1, reader.numDocs() );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public void testUpdateRecordWithPomMetadata()
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
createEmptyIndex();
|
||||||
|
|
||||||
|
Artifact artifact = createArtifact( "test-plugin" );
|
||||||
|
|
||||||
|
RepositoryIndexRecord record = recordFactory.createRecord( artifact );
|
||||||
|
index.indexRecords( Collections.singletonList( record ) );
|
||||||
|
|
||||||
|
// TODO: index again, with the POM metadata! Make sure a value in the first one is not present, and that is tested for
|
||||||
|
|
||||||
|
IndexReader reader = IndexReader.open( indexLocation );
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Document document = reader.document( 0 );
|
||||||
|
assertEquals( "Check document", "test-plugin", document.getField( "artifactId" ).stringValue() );
|
||||||
|
assertEquals( "Check document", "jar", document.getField( "type" ).stringValue() );
|
||||||
|
assertEquals( "Check document", "maven-plugin", document.getField( "packaging" ).stringValue() );
|
||||||
|
assertEquals( "Check index size", 1, reader.numDocs() );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
public void testAddPomRecord()
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
createEmptyIndex();
|
||||||
|
|
||||||
|
Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
|
||||||
|
|
||||||
|
RepositoryIndexRecord record = recordFactory.createRecord( artifact );
|
||||||
|
index.indexRecords( Collections.singletonList( record ) );
|
||||||
|
|
||||||
|
IndexReader reader = IndexReader.open( indexLocation );
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Document document = reader.document( 0 );
|
||||||
|
assertEquals( "Check document", "test-pom", document.getField( "artifactId" ).stringValue() );
|
||||||
|
assertEquals( "Check document", "pom", document.getField( "type" ).stringValue() );
|
||||||
|
// assertEquals( "Check document", "pom", document.getField( "packaging" ).stringValue() ); // TODO!
|
||||||
|
assertEquals( "Check index size", 1, reader.numDocs() );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public void testUpdateRecordWithRepoMetadata()
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
createEmptyIndex();
|
||||||
|
|
||||||
|
Artifact artifact = createArtifact( "test-plugin" );
|
||||||
|
|
||||||
|
RepositoryIndexRecord record = recordFactory.createRecord( artifact );
|
||||||
|
index.indexRecords( Collections.singletonList( record ) );
|
||||||
|
|
||||||
|
// TODO: index again, with the repo metadata!
|
||||||
|
|
||||||
|
IndexReader reader = IndexReader.open( indexLocation );
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Document document = reader.document( 0 );
|
||||||
|
assertEquals( "Check document", "test-plugin", document.getField( "artifactId" ).stringValue() );
|
||||||
|
assertEquals( "Check document", "maven-plugin", document.getField( "packaging" ).stringValue() );
|
||||||
|
assertEquals( "Check document", "plugin", document.getField( "pluginPrefix" ).stringValue() );
|
||||||
|
assertEquals( "Check index size", 1, reader.numDocs() );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testUpdateRecordWithArtifactData()
|
||||||
|
throws IOException, RepositoryIndexException
|
||||||
|
{
|
||||||
|
createEmptyIndex();
|
||||||
|
|
||||||
|
// TODO: index with the repo/POM metadata!
|
||||||
|
|
||||||
|
Artifact artifact = createArtifact( "test-plugin" );
|
||||||
|
|
||||||
|
RepositoryIndexRecord record = recordFactory.createRecord( artifact );
|
||||||
|
index.indexRecords( Collections.singletonList( record ) );
|
||||||
|
|
||||||
|
IndexReader reader = IndexReader.open( indexLocation );
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Document document = reader.document( 0 );
|
||||||
|
assertEquals( "Check document", "test-plugin", document.getField( "artifactId" ).stringValue() );
|
||||||
|
assertEquals( "Check document", "maven-plugin", document.getField( "packaging" ).stringValue() );
|
||||||
|
assertEquals( "Check document", "plugin", document.getField( "pluginPrefix" ).stringValue() );
|
||||||
|
assertEquals( "Check index size", 1, reader.numDocs() );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
private Artifact createArtifact( String artifactId )
|
||||||
|
{
|
||||||
|
return createArtifact( artifactId, "1.0", "jar" );
|
||||||
|
}
|
||||||
|
|
||||||
|
private Artifact createArtifact( String artifactId, String version, String type )
|
||||||
|
{
|
||||||
|
Artifact artifact =
|
||||||
|
artifactFactory.createBuildArtifact( "org.apache.maven.repository.record", artifactId, version, type );
|
||||||
|
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
|
||||||
|
artifact.setRepository( repository );
|
||||||
|
return artifact;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createEmptyIndex()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
createIndex( Collections.EMPTY_LIST );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createIndex( List docments )
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
IndexWriter writer = new IndexWriter( indexLocation, new StandardAnalyzer(), true );
|
||||||
|
for ( Iterator i = docments.iterator(); i.hasNext(); )
|
||||||
|
{
|
||||||
|
Document document = (Document) i.next();
|
||||||
|
writer.addDocument( document );
|
||||||
|
}
|
||||||
|
writer.optimize();
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,8 @@ import java.io.File;
|
||||||
* Test the minimal artifact index record.
|
* Test the minimal artifact index record.
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
* @todo test packaging!
|
||||||
|
* @todo test pluginPrefix!
|
||||||
*/
|
*/
|
||||||
public class StandardArtifactIndexRecordFactoryTest
|
public class StandardArtifactIndexRecordFactoryTest
|
||||||
extends PlexusTestCase
|
extends PlexusTestCase
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.apache.maven.repository.record</groupId>
|
||||||
|
<artifactId>test-pom</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<name>Maven Repository Manager Test POM</name>
|
||||||
|
<inceptionYear>2005</inceptionYear>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
</project>
|
Loading…
Reference in New Issue