From 0d68e24a2ea3dcef3f3f31632fadd4ecc931bba5 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Wed, 26 Jul 2006 04:24:36 +0000 Subject: [PATCH] [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 --- .../indexing/RepositoryArtifactIndex.java | 46 +++ .../RepositoryArtifactIndexFactory.java | 52 +++ .../lucene/LuceneIndexRecordConverter.java | 36 +++ .../LuceneMinimalIndexRecordConverter.java | 35 ++ .../lucene/LuceneRepositoryArtifactIndex.java | 191 +++++++++++ .../LuceneRepositoryArtifactIndexFactory.java | 43 +++ .../LuceneStandardIndexRecordConverter.java | 115 +++++++ .../record/MinimalArtifactIndexRecord.java | 5 + .../record/RepositoryIndexRecord.java | 6 + .../record/StandardArtifactIndexRecord.java | 85 +++++ .../StandardArtifactIndexRecordFactory.java | 6 + .../LuceneRepositoryArtifactIndexTest.java | 306 ++++++++++++++++++ ...tandardArtifactIndexRecordFactoryTest.java | 2 + .../record/test-pom/1.0/test-pom-1.0.pom | 10 + 14 files changed, 938 insertions(+) create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndexFactory.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneIndexRecordConverter.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalIndexRecordConverter.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexFactory.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneStandardIndexRecordConverter.java create mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexTest.java create mode 100644 maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-pom/1.0/test-pom-1.0.pom diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java new file mode 100644 index 000000000..63f7aa9ea --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java @@ -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 Brett Porter + */ +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; +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndexFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndexFactory.java new file mode 100644 index 000000000..89ec2da27 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndexFactory.java @@ -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 Brett Porter + */ +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 ); +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneIndexRecordConverter.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneIndexRecordConverter.java new file mode 100644 index 000000000..aa85d6814 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneIndexRecordConverter.java @@ -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 Brett Porter + */ +public interface LuceneIndexRecordConverter +{ + /** + * Convert an index record to a Lucene document. + * + * @param record the record + * @return the document + */ + Document convert( RepositoryIndexRecord record ); +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalIndexRecordConverter.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalIndexRecordConverter.java new file mode 100644 index 000000000..2631151eb --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalIndexRecordConverter.java @@ -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 Brett Porter + */ +public class LuceneMinimalIndexRecordConverter + implements LuceneIndexRecordConverter +{ + public Document convert( RepositoryIndexRecord record ) + { + // TODO: implement! + return null; + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java new file mode 100644 index 000000000..6307636c2 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java @@ -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 Brett Porter + */ +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." ); + } + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexFactory.java new file mode 100644 index 000000000..336d72909 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexFactory.java @@ -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 Brett Porter + * @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() ); + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneStandardIndexRecordConverter.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneStandardIndexRecordConverter.java new file mode 100644 index 000000000..0ce7ea418 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneStandardIndexRecordConverter.java @@ -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 Brett Porter + * @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 ) ); + } + } + +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java index 9784a2adf..8513f807e 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/MinimalArtifactIndexRecord.java @@ -159,4 +159,9 @@ public class MinimalArtifactIndexRecord return "Filename: " + filename + "; checksum: " + md5Checksum + "; size: " + size + "; lastModified: " + new Date( lastModified ) + "; classes: " + classes; } + + public String getPrimaryKey() + { + return filename; + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java index 472e546c7..10c99f1a3 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/RepositoryIndexRecord.java @@ -23,4 +23,10 @@ package org.apache.maven.repository.indexing.record; */ public interface RepositoryIndexRecord { + /** + * Get the primary key used to identify the record uniquely in the index. + * + * @return the primary key + */ + String getPrimaryKey(); } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java index 30aad6504..10ffc3132 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecord.java @@ -69,6 +69,16 @@ public class StandardArtifactIndexRecord */ 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 ) { this.sha1Checksum = sha1Checksum; @@ -170,6 +180,14 @@ public class StandardArtifactIndexRecord { 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; } @@ -186,6 +204,73 @@ public class StandardArtifactIndexRecord result = 31 * result + ( packages != null ? packages.hashCode() : 0 ); result = 31 * result + ( files != null ? files.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; } + + 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; + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java index ab78e2c4c..22a8689e3 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java @@ -89,8 +89,14 @@ public class StandardArtifactIndexRecordFactory record.setSize( file.length() ); record.setRepository( artifact.getRepository().getId() ); /* 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.setPackaging( packaging ); + record.setProjectName( name ); + record.setProjectDescription( description ); + record.setInceptionYear( year ); + */ +/* TODO: fields for later indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); record.setDependencies( dependencies ); diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexTest.java new file mode 100644 index 000000000..4d9a87224 --- /dev/null +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndexTest.java @@ -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 Brett Porter + */ +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(); + } +} diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java index 898bbd123..f48a6949d 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java @@ -29,6 +29,8 @@ import java.io.File; * Test the minimal artifact index record. * * @author Brett Porter + * @todo test packaging! + * @todo test pluginPrefix! */ public class StandardArtifactIndexRecordFactoryTest extends PlexusTestCase diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-pom/1.0/test-pom-1.0.pom b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-pom/1.0/test-pom-1.0.pom new file mode 100644 index 000000000..9ffc00b52 --- /dev/null +++ b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-pom/1.0/test-pom-1.0.pom @@ -0,0 +1,10 @@ + + 4.0.0 + org.apache.maven.repository.record + test-pom + 1.0 + Maven Repository Manager Test POM + 2005 + pom +