mirror of https://github.com/apache/archiva.git
[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
This commit is contained in:
parent
c18e8bc723
commit
55238052e5
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="nexusRepositorySearch" class="org.apache.archiva.indexer.search.NexusRepositorySearch">
|
||||
<constructor-arg ref="nexusIndexer"/>
|
||||
<constructor-arg ref="archivaConfiguration"/>
|
||||
</bean>
|
||||
</beans>
|
|
@ -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 )
|
||||
|
|
|
@ -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 );
|
||||
|
||||
|
|
|
@ -65,6 +65,10 @@
|
|||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.sonatype.nexus</groupId>
|
||||
<artifactId>nexus-indexer</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
|
|
@ -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<String> 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<String> 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<ArtifactInfo> artifactInfos = response.getResults();
|
||||
|
||||
for ( ArtifactInfo artifactInfo : artifactInfos )
|
||||
{
|
||||
String id = artifactInfo.groupId + ":" + artifactInfo.artifactId;
|
||||
Map<String, SearchResultHit> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String> 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;
|
||||
}
|
|
@ -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 );
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -157,4 +157,14 @@ public class SearchResultHit
|
|||
{
|
||||
this.repositoryId = repositoryId;
|
||||
}
|
||||
|
||||
public void addVersion( String version )
|
||||
{
|
||||
if( versions == null )
|
||||
{
|
||||
versions = new ArrayList<String>();
|
||||
}
|
||||
|
||||
versions.add( version );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, SearchResultHit> getHitsMap()
|
||||
{
|
||||
return hits;
|
||||
}
|
||||
|
||||
public List getRepositories()
|
||||
{
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue