From 55238052e5327e1dd9124d85d8d95a8a0190e71e Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Fri, 23 Jan 2009 08:33:35 +0000 Subject: [PATCH] [MRM-749] o add new implementation of search which uses Nexus Indexer for searching the nexus index o remove IndexingContext in nexus indexer consumer once the scanning is complete git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@736971 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/main/resources/spring-context.xml | 10 ++ .../lucene/NexusIndexerConsumer.java | 20 ++- .../lucene/NexusIndexerConsumerTest.java | 5 + .../archiva-base/archiva-indexer/pom.xml | 4 + .../indexer/search/NexusRepositorySearch.java | 170 ++++++++++++++++++ .../indexer/search/RepositorySearch.java | 51 ++++++ .../search/RepositorySearchException.java | 44 +++++ .../archiva/indexer/search/SearchFields.java | 107 +++++++++++ .../indexer/search/SearchResultHit.java | 10 ++ .../archiva/indexer/search/SearchResults.java | 11 ++ .../search/NexusRepositorySearchTest.java | 76 ++++++++ 11 files changed, 502 insertions(+), 6 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-configuration/src/main/resources/spring-context.xml create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearch.java create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearchException.java create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/SearchFields.java create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/resources/spring-context.xml b/archiva-modules/archiva-base/archiva-configuration/src/main/resources/spring-context.xml new file mode 100644 index 000000000..ccb973ca4 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/resources/spring-context.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java index b9c45a101..c5172e0ef 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java @@ -63,8 +63,6 @@ public class NexusIndexerConsumer private final IndexPacker indexPacker; - private ManagedRepositoryConfiguration repository; - private ManagedDefaultRepositoryContent repositoryContent; private IndexingContext context; @@ -100,10 +98,19 @@ public class NexusIndexerConsumer public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered ) throws ConsumerException - { - this.repository = repository; + { managedRepository = new File( repository.getLocation() ); - File indexDirectory = new File( managedRepository, ".indexer" ); + String indexDir = repository.getIndexDir(); + + File indexDirectory = null; + if( indexDir != null && !"".equals( indexDir ) ) + { + indexDirectory = new File( managedRepository, repository.getIndexDir() ); + } + else + { + indexDirectory = new File( managedRepository, ".indexer" ); + } repositoryContent = new ManagedDefaultRepositoryContent(); repositoryContent.setRepository( repository ); @@ -184,7 +191,8 @@ public class NexusIndexerConsumer try { indexerEngine.endIndexing( context ); - indexPacker.packIndex( context, indexLocation ); + indexPacker.packIndex( context, indexLocation ); + indexer.removeIndexingContext( context, false ); uinfos = null; } catch ( IOException e ) diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java index 16dbfd874..7b3ec1441 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java @@ -36,6 +36,7 @@ import org.sonatype.nexus.index.ArtifactInfo; import org.sonatype.nexus.index.FlatSearchRequest; import org.sonatype.nexus.index.FlatSearchResponse; import org.sonatype.nexus.index.NexusIndexer; +import org.sonatype.nexus.index.context.IndexingContext; import org.sonatype.nexus.index.creator.IndexerEngine; import org.sonatype.nexus.index.packer.IndexPacker; @@ -106,6 +107,10 @@ public class NexusIndexerConsumerTest q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD ); q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD ); + IndexingContext context = nexusIndexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(), new File( repositoryConfig.getLocation() ), + new File( repositoryConfig.getLocation(), ".indexer" ), null, null, NexusIndexer.FULL_INDEX ); + context.setSearchable( true ); + FlatSearchRequest request = new FlatSearchRequest( q ); FlatSearchResponse response = nexusIndexer.searchFlat( request ); diff --git a/archiva-modules/archiva-base/archiva-indexer/pom.xml b/archiva-modules/archiva-base/archiva-indexer/pom.xml index 13b1b2b92..a5eb3227d 100644 --- a/archiva-modules/archiva-base/archiva-indexer/pom.xml +++ b/archiva-modules/archiva-base/archiva-indexer/pom.xml @@ -65,6 +65,10 @@ commons-io commons-io + + org.sonatype.nexus + nexus-indexer + diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java new file mode 100644 index 000000000..330b8b976 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java @@ -0,0 +1,170 @@ +package org.apache.archiva.indexer.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.lucene.search.BooleanQuery; +import org.apache.maven.archiva.configuration.ArchivaConfiguration; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.indexer.search.SearchResultHit; +import org.apache.maven.archiva.indexer.search.SearchResultLimits; +import org.apache.maven.archiva.indexer.search.SearchResults; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonatype.nexus.index.ArtifactInfo; +import org.sonatype.nexus.index.FlatSearchRequest; +import org.sonatype.nexus.index.FlatSearchResponse; +import org.sonatype.nexus.index.NexusIndexer; +import org.sonatype.nexus.index.context.IndexContextInInconsistentStateException; +import org.sonatype.nexus.index.context.IndexingContext; +import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; + +/** + * RepositorySearch implementation which uses the Nexus Indexer for searching. + */ +public class NexusRepositorySearch + implements RepositorySearch +{ + private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class ); + + private NexusIndexer indexer; + + private ArchivaConfiguration archivaConfig; + + public NexusRepositorySearch( NexusIndexer indexer, ArchivaConfiguration archivaConfig ) + { + this.indexer = indexer; + this.archivaConfig = archivaConfig; + } + + public SearchResults search( String principal, List selectedRepos, String term, SearchResultLimits limits ) + throws RepositorySearchException + { + addIndexingContexts( selectedRepos ); + + // TODO: + // 1. construct query for: + // - regular search + // - searching within search results + // 2. consider pagination + + BooleanQuery q = new BooleanQuery(); + // q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD ); + // q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD ); + + try + { + FlatSearchRequest request = new FlatSearchRequest( q ); + FlatSearchResponse response = indexer.searchFlat( request ); + + return convertToSearchResults( response ); + } + catch ( IndexContextInInconsistentStateException e ) + { + throw new RepositorySearchException( e ); + } + catch ( IOException e ) + { + throw new RepositorySearchException( e ); + } + } + + public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits ) + throws RepositorySearchException + { + // TODO Auto-generated method stub + return null; + } + + private void addIndexingContexts( List selectedRepos ) + { + for( String repo : selectedRepos ) + { + try + { + ManagedRepositoryConfiguration repoConfig = archivaConfig.getConfiguration().findManagedRepositoryById( repo ); + String indexDir = repoConfig.getIndexDir(); + File indexDirectory = null; + if( indexDir != null && !"".equals( indexDir ) ) + { + indexDirectory = new File( repoConfig.getLocation(), repoConfig.getIndexDir() ); + } + else + { + indexDirectory = new File( repoConfig.getLocation(), ".indexer" ); + } + + IndexingContext context = + indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ), + indexDirectory, null, null, NexusIndexer.FULL_INDEX ); + context.setSearchable( repoConfig.isScanned() ); + } + catch ( UnsupportedExistingLuceneIndexException e ) + { + // skip repository + log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() ); + continue; + } + catch ( IOException e ) + { + // skip repository + log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() ); + continue; + } + } + } + + private SearchResults convertToSearchResults( FlatSearchResponse response ) + { + SearchResults results = new SearchResults(); + Set artifactInfos = response.getResults(); + + for ( ArtifactInfo artifactInfo : artifactInfos ) + { + String id = artifactInfo.groupId + ":" + artifactInfo.artifactId; + Map hitsMap = results.getHitsMap(); + + SearchResultHit hit = hitsMap.get( id ); + if ( hit != null ) + { + hit.addVersion( artifactInfo.version ); + } + else + { + hit = new SearchResultHit(); + hit.setArtifactId( artifactInfo.artifactId ); + hit.setGroupId( artifactInfo.groupId ); + hit.setRepositoryId( artifactInfo.repository ); + hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname ); + hit.addVersion( artifactInfo.version ); + } + + results.addHit( id, hit ); + } + + return results; + } + +} diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearch.java new file mode 100644 index 000000000..407ca3802 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearch.java @@ -0,0 +1,51 @@ +package org.apache.archiva.indexer.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.maven.archiva.indexer.search.SearchResultLimits; +import org.apache.maven.archiva.indexer.search.SearchResults; + +public interface RepositorySearch +{ + /** + * Quick search. + * + * @param principal + * @param selectedRepos + * @param term + * @param limits + * @return + */ + SearchResults search( String principal, List selectedRepos, String term, SearchResultLimits limits ) + throws RepositorySearchException; + + /** + * Advanced search. + * + * @param principal + * @param searchFields + * @param limits + * @return + */ + SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits ) + throws RepositorySearchException; +} diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearchException.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearchException.java new file mode 100644 index 000000000..364cce2c2 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/RepositorySearchException.java @@ -0,0 +1,44 @@ +package org.apache.archiva.indexer.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +public class RepositorySearchException + extends Exception +{ + public RepositorySearchException() + { + super(); + } + + public RepositorySearchException( String msg ) + { + super( msg ); + } + + public RepositorySearchException( Throwable e ) + { + super( e ); + } + + public RepositorySearchException( String msg, Throwable e ) + { + super( msg, e ); + } +} diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/SearchFields.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/SearchFields.java new file mode 100644 index 000000000..8d6c15897 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/SearchFields.java @@ -0,0 +1,107 @@ +package org.apache.archiva.indexer.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +public class SearchFields +{ + private String groupId; + + private String artifactId; + + private String version; + + private String packaging; + + private String className; + + private String packageName; + + private String repositoryId; + + public String getGroupId() + { + return groupId; + } + + public void setGroupId( String groupId ) + { + this.groupId = groupId; + } + + public String getArtifactId() + { + return artifactId; + } + + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + public String getVersion() + { + return version; + } + + public void setVersion( String version ) + { + this.version = version; + } + + public String getPackaging() + { + return packaging; + } + + public void setPackaging( String packaging ) + { + this.packaging = packaging; + } + + public String getClassName() + { + return className; + } + + public void setClassName( String className ) + { + this.className = className; + } + + public String getPackageName() + { + return packageName; + } + + public void setPackageName( String packageName ) + { + this.packageName = packageName; + } + + public String getRepositoryId() + { + return repositoryId; + } + + public void setRepositoryId( String repositoryId ) + { + this.repositoryId = repositoryId; + } +} diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java index ba3bca500..b1e707e54 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java @@ -157,4 +157,14 @@ public class SearchResultHit { this.repositoryId = repositoryId; } + + public void addVersion( String version ) + { + if( versions == null ) + { + versions = new ArrayList(); + } + + versions.add( version ); + } } diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java index ecb01fbcf..2c961d0d8 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResults.java @@ -51,6 +51,12 @@ public class SearchResults /* do nothing */ } + // for new RepositorySearch + public void addHit( String id, SearchResultHit hit ) + { + hits.put( id, hit ); + } + public void addHit( LuceneRepositoryContentRecord record ) { if ( record instanceof FileContentRecord ) @@ -149,6 +155,11 @@ public class SearchResults { return new ArrayList( hits.values() ); } + + public Map getHitsMap() + { + return hits; + } public List getRepositories() { diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java new file mode 100644 index 000000000..cc08bc834 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java @@ -0,0 +1,76 @@ +package org.apache.archiva.indexer.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.archiva.configuration.ArchivaConfiguration; +import org.codehaus.plexus.spring.PlexusInSpringTestCase; +import org.sonatype.nexus.index.NexusIndexer; + +public class NexusRepositorySearchTest + extends PlexusInSpringTestCase +{ + private RepositorySearch search; + + private ArchivaConfiguration archivaConfig; + + private NexusIndexer indexer; + + @Override + protected void setUp() + throws Exception + { + super.setUp(); + + indexer = ( NexusIndexer )lookup( NexusIndexer.class ); + + search = new NexusRepositorySearch( indexer, archivaConfig ); + } + + public void testQuickSearch() + throws Exception + { + + } + + public void testNoIndexFound() + throws Exception + { + + } + + public void testSearchWithinSearchResults() + throws Exception + { + + } + + public void testAdvancedSearch() + throws Exception + { + + } + + public void testPagination() + throws Exception + { + + } + +} -- 2.39.5