[MRM-749]

o apply pagination in nexus search
o add test for pagination


git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@738779 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2009-01-29 07:13:15 +00:00
parent 4d70f30499
commit 78a944daf3
3 changed files with 96 additions and 9 deletions

View File

@ -25,6 +25,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.archiva.indexer.util.SearchUtil;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
@ -70,7 +72,6 @@ public class NexusRepositorySearch
// 1. construct query for:
// - regular search
// - searching within search results
// 2. consider pagination
// 3. multiple repositories
BooleanQuery q = new BooleanQuery();
@ -91,7 +92,7 @@ public class NexusRepositorySearch
return new SearchResults();
}
return convertToSearchResults( response );
return convertToSearchResults( response, limits );
}
catch ( IndexContextInInconsistentStateException e )
{
@ -157,16 +158,14 @@ public class NexusRepositorySearch
}
}
private SearchResults convertToSearchResults( FlatSearchResponse response )
private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
{
// TODO: paginate!
SearchResults results = new SearchResults();
Set<ArtifactInfo> artifactInfos = response.getResults();
for ( ArtifactInfo artifactInfo : artifactInfos )
{
String id = artifactInfo.groupId + ":" + artifactInfo.artifactId;
String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
Map<String, SearchResultHit> hitsMap = results.getHitsMap();
SearchResultHit hit = hitsMap.get( id );
@ -192,7 +191,50 @@ public class NexusRepositorySearch
results.setTotalHits( results.getHitsMap().size() );
if( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
{
return results;
}
else
{
return paginate( limits, results );
}
}
private SearchResults paginate( SearchResultLimits limits, SearchResults results )
{
SearchResults paginated = new SearchResults();
int fetchCount = limits.getPageSize();
int offset = ( limits.getSelectedPage() * limits.getPageSize() );
// Goto offset.
if ( offset <= results.getTotalHits() )
{
// only process if the offset is within the hit count.
for ( int i = 0; i < fetchCount; i++ )
{
// Stop fetching if we are past the total # of available hits.
if ( offset + i > results.getTotalHits() )
{
break;
}
SearchResultHit hit = results.getHits().get( ( offset + i ) - 1 );
if( hit != null )
{
String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
paginated.addHit( id, hit );
}
else
{
break;
}
}
}
paginated.setTotalHits( paginated.getHitsMap().size() );
return paginated;
}
}

View File

@ -59,4 +59,9 @@ public class SearchUtil
return qString;
}
public static String getHitId( String groupId, String artifactId )
{
return groupId + ":" + artifactId;
}
}

View File

@ -29,6 +29,7 @@ import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
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.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.MockControl;
@ -176,6 +177,45 @@ public class NexusRepositorySearchTest
//TODO: search for class & package names
}
public void testQuickSearchWithPagination()
throws Exception
{
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO );
// page 1
SearchResultLimits limits = new SearchResultLimits( 1 );
limits.setPageSize( 1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay();
SearchResults results = search.search( "user", selectedRepos, "org", limits );
archivaConfigControl.verify();
assertNotNull( results );
assertEquals( 1, results.getTotalHits() );
archivaConfigControl.reset();
// page 2
limits = new SearchResultLimits( 2 );
limits.setPageSize( 1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay();
results = search.search( "user", selectedRepos, "org", limits );
archivaConfigControl.verify();
assertNotNull( results );
assertEquals( 1, results.getTotalHits() );
}
public void testArtifactFoundInMultipleRepositories()
throws Exception
{