From 9b2c646f7559d01f74b3378b1c64132d0ebcfc9a Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Thu, 10 Dec 2009 11:36:51 +0000 Subject: [PATCH] [MRM-1300] move search and find artifact action over to metadata repository for XMLRPC git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@889203 13f79535-47bb-0310-9956-ffa450edef68 --- .../xmlrpc/services/SearchServiceImpl.java | 103 +++++++---------- .../services/SearchServiceImplTest.java | 105 +++++++++--------- 2 files changed, 91 insertions(+), 117 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java index c1e81e388..632821281 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java @@ -28,43 +28,36 @@ import org.apache.archiva.indexer.search.RepositorySearch; import org.apache.archiva.indexer.search.SearchResultHit; import org.apache.archiva.indexer.search.SearchResultLimits; import org.apache.archiva.indexer.search.SearchResults; +import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.model.ProjectVersionReference; +import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.MetadataResolver; import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet; import org.apache.archiva.web.xmlrpc.api.SearchService; import org.apache.archiva.web.xmlrpc.api.beans.Artifact; import org.apache.archiva.web.xmlrpc.api.beans.Dependency; import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories; -import org.apache.maven.archiva.common.utils.VersionUtil; -import org.apache.maven.archiva.database.ArchivaDAO; -import org.apache.maven.archiva.database.ArtifactDAO; -import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint; -import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint; -import org.apache.maven.archiva.model.ArchivaArtifact; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.maven.archiva.repository.content.ArtifactExtensionMapping; public class SearchServiceImpl implements SearchService { - private Logger log = LoggerFactory.getLogger( SearchServiceImpl.class ); - private RepositorySearch search; private XmlRpcUserRepositories xmlRpcUserRepositories; - private ArchivaDAO archivaDAO; - private MetadataResolver metadataResolver; - public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO, - MetadataResolver metadataResolver, RepositorySearch search ) + private MetadataRepository metadataRepository; + + public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, MetadataResolver metadataResolver, + MetadataRepository metadataRepository, RepositorySearch search ) { this.xmlRpcUserRepositories = xmlRpcUserRepositories; - this.archivaDAO = archivaDAO; this.search = search; this.metadataResolver = metadataResolver; + this.metadataRepository = metadataRepository; } @SuppressWarnings("unchecked") @@ -74,21 +67,12 @@ public class SearchServiceImpl List artifacts = new ArrayList(); List observableRepos = xmlRpcUserRepositories.getObservableRepositories(); SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); - SearchResults results = null; + SearchResults results; results = search.search( "", observableRepos, queryString, limits, null ); for ( SearchResultHit resultHit : results.getHits() ) { - // double-check all versions as done in SearchAction - final List versions = (List) archivaDAO.query( - new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(), resultHit.getArtifactId() ) ); - if ( versions != null && !versions.isEmpty() ) - { - resultHit.setVersion( null ); - resultHit.setVersions( filterTimestampedSnapshots( versions ) ); - } - List resultHitVersions = resultHit.getVersions(); if ( resultHitVersions != null ) { @@ -130,47 +114,40 @@ public class SearchServiceImpl return artifacts; } - /** - * Remove timestamped snapshots from versions - */ - private static List filterTimestampedSnapshots( List versions ) - { - final List filtered = new ArrayList(); - for ( final String version : versions ) - { - final String baseVersion = VersionUtil.getBaseVersion( version ); - if ( !filtered.contains( baseVersion ) ) - { - filtered.add( baseVersion ); - } - } - return filtered; - } - public List getArtifactByChecksum( String checksum ) throws Exception { - // 1. get ArtifactDAO from ArchivaDAO - // 2. create ArtifactsByChecksumConstraint( "queryTerm" ) - // 3. query artifacts using constraint - // 4. convert results to list of Artifact objects + List observableRepos = xmlRpcUserRepositories.getObservableRepositories(); List results = new ArrayList(); - ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO(); - - ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum ); - List artifacts = artifactDAO.queryArtifacts( constraint ); - - for ( ArchivaArtifact archivaArtifact : artifacts ) + for ( String repoId : observableRepos ) { - Artifact artifact = - new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(), - archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), - archivaArtifact.getType() ); - //archivaArtifact.getModel().getWhenGathered() ); - results.add( artifact ); + for ( ArtifactMetadata artifact : metadataRepository.getArtifactsByChecksum( repoId, checksum ) ) + { + // TODO: use a maven facet instead, for now just using the extension and classifier + String type = null; + String key = artifact.getProject() + "-" + artifact.getVersion(); + String filename = artifact.getId(); + int extIndex = filename.lastIndexOf( "." ); + if ( filename.startsWith( key ) ) + { + int i = key.length(); + char nextToken = filename.charAt( i ); + if ( nextToken == '-' ) + { + String classifier = filename.substring( i + 1, extIndex ); + String extension = filename.substring( extIndex + 1 ); + type = ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension ); + } + else if ( nextToken == '.' ) + { + type = ArtifactExtensionMapping.mapExtensionToType( filename.substring( i + 1 ) ); + } + } + results.add( new Artifact( artifact.getRepositoryId(), artifact.getNamespace(), artifact.getProject(), + artifact.getVersion(), type ) ); + } } - return results; } @@ -198,11 +175,11 @@ public class SearchServiceImpl public List getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since ) throws Exception { - List artifacts = new ArrayList(); +// List artifacts = new ArrayList(); // 1. get observable repositories // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date) - + throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" ); // return artifacts; @@ -236,8 +213,8 @@ public class SearchServiceImpl public List getDependencyTree( String groupId, String artifactId, String version ) throws Exception { - List a = new ArrayList(); - +// List a = new ArrayList(); + throw new UnsupportedOperationException( "getDependencyTree not yet implemented" ); // return a; } diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java index 34b18b483..45d3f12c6 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java @@ -31,19 +31,16 @@ import org.apache.archiva.indexer.search.SearchResultHit; import org.apache.archiva.indexer.search.SearchResultLimits; import org.apache.archiva.indexer.search.SearchResults; import org.apache.archiva.indexer.util.SearchUtil; +import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.model.ProjectVersionReference; +import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.MetadataResolver; import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet; import org.apache.archiva.web.xmlrpc.api.SearchService; import org.apache.archiva.web.xmlrpc.api.beans.Artifact; import org.apache.archiva.web.xmlrpc.api.beans.Dependency; import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories; -import org.apache.maven.archiva.database.ArchivaDAO; -import org.apache.maven.archiva.database.ArtifactDAO; -import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint; -import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint; -import org.apache.maven.archiva.model.ArchivaArtifact; import org.codehaus.plexus.spring.PlexusInSpringTestCase; import org.easymock.MockControl; import org.easymock.classextension.MockClassControl; @@ -66,14 +63,6 @@ public class SearchServiceImplTest private RepositorySearch search; - private MockControl archivaDAOControl; - - private ArchivaDAO archivaDAO; - - private MockControl artifactDAOControl; - - private ArtifactDAO artifactDAO; - private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test"; private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva"; @@ -82,6 +71,14 @@ public class SearchServiceImplTest private MetadataResolver metadataResolver; + private MockControl metadataRepositoryControl; + + private MetadataRepository metadataRepository; + + private static final String CHECKSUM = "a1b2c3aksjhdasfkdasasd"; + + private static final String TEST_REPO = "test-repo"; + @Override public void setUp() throws Exception @@ -89,10 +86,6 @@ public class SearchServiceImplTest userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class ); userRepos = (XmlRpcUserRepositories) userReposControl.getMock(); - archivaDAOControl = MockControl.createControl( ArchivaDAO.class ); - archivaDAOControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); - archivaDAO = (ArchivaDAO) archivaDAOControl.getMock(); - searchControl = MockControl.createControl( RepositorySearch.class ); searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); search = (RepositorySearch) searchControl.getMock(); @@ -100,13 +93,14 @@ public class SearchServiceImplTest metadataResolverControl = MockControl.createControl( MetadataResolver.class ); metadataResolver = (MetadataResolver) metadataResolverControl.getMock(); - artifactDAOControl = MockControl.createControl( ArtifactDAO.class ); - artifactDAO = (ArtifactDAO) artifactDAOControl.getMock(); + metadataRepositoryControl = MockControl.createControl( MetadataRepository.class ); + metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock(); - searchService = new SearchServiceImpl( userRepos, archivaDAO, metadataResolver, search ); + searchService = new SearchServiceImpl( userRepos, metadataResolver, metadataRepository, search ); } // MRM-1230 + public void testQuickSearchModelPackagingIsUsed() throws Exception { @@ -133,10 +127,6 @@ public class SearchServiceImplTest searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), results ); - archivaDAOControl.expectAndReturn( archivaDAO.query( - new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ), - null ); - ProjectVersionMetadata model = new ProjectVersionMetadata(); model.setId( "1.0" ); MavenProjectFacet facet = new MavenProjectFacet(); @@ -150,14 +140,14 @@ public class SearchServiceImplTest userReposControl.replay(); searchControl.replay(); metadataResolverControl.replay(); - archivaDAOControl.replay(); + metadataRepositoryControl.replay(); List artifacts = searchService.quickSearch( "archiva" ); userReposControl.verify(); searchControl.verify(); metadataResolverControl.verify(); - archivaDAOControl.verify(); + metadataRepositoryControl.verify(); assertNotNull( artifacts ); assertEquals( 1, artifacts.size() ); @@ -197,10 +187,6 @@ public class SearchServiceImplTest searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), results ); - archivaDAOControl.expectAndReturn( archivaDAO.query( - new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ), - null ); - metadataResolverControl.expectAndReturn( metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ), null ); @@ -214,14 +200,14 @@ public class SearchServiceImplTest userReposControl.replay(); searchControl.replay(); metadataResolverControl.replay(); - archivaDAOControl.replay(); + metadataRepositoryControl.replay(); List artifacts = searchService.quickSearch( "archiva" ); userReposControl.verify(); searchControl.verify(); metadataResolverControl.verify(); - archivaDAOControl.verify(); + metadataRepositoryControl.verify(); assertNotNull( artifacts ); assertEquals( 1, artifacts.size() ); @@ -255,10 +241,6 @@ public class SearchServiceImplTest results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit ); - archivaDAOControl.expectAndReturn( archivaDAO.query( - new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ), - null ); - SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), @@ -276,14 +258,14 @@ public class SearchServiceImplTest userReposControl.replay(); searchControl.replay(); - archivaDAOControl.replay(); + metadataRepositoryControl.replay(); metadataResolverControl.replay(); List artifacts = searchService.quickSearch( "archiva" ); userReposControl.verify(); searchControl.verify(); - archivaDAOControl.verify(); + metadataRepositoryControl.verify(); metadataResolverControl.verify(); assertNotNull( artifacts ); @@ -327,30 +309,31 @@ public class SearchServiceImplTest public void testGetArtifactByChecksum() throws Exception { + userReposControl.expectAndReturn( userRepos.getObservableRepositories(), + Collections.singletonList( TEST_REPO ) ); + Date whenGathered = new Date(); - ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" ); - List artifacts = new ArrayList(); - ArchivaArtifact artifact = - new ArchivaArtifact( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "", "jar", "test-repo" ); - artifact.getModel().setWhenGathered( whenGathered ); - artifacts.add( artifact ); + ArtifactMetadata artifact = createArtifact( whenGathered ); + metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, CHECKSUM ), + Collections.singletonList( artifact ) ); - archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO ); - artifactDAO.queryArtifacts( constraint ); - artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER ); - artifactDAOControl.setReturnValue( artifacts ); + metadataRepositoryControl.replay(); + userReposControl.replay(); - archivaDAOControl.replay(); - artifactDAOControl.replay(); + List results = searchService.getArtifactByChecksum( CHECKSUM ); - List results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" ); - - archivaDAOControl.verify(); - artifactDAOControl.verify(); + metadataRepositoryControl.verify(); + userReposControl.verify(); assertNotNull( results ); assertEquals( 1, results.size() ); + Artifact result = results.get( 0 ); + assertEquals( ARCHIVA_TEST_GROUP_ID, result.getGroupId() ); + assertEquals( ARCHIVA_TEST_ARTIFACT_ID, result.getArtifactId() ); + assertEquals( "1.0", result.getVersion() ); + assertEquals( "jar", result.getType() ); + assertEquals( TEST_REPO, result.getRepositoryId() ); } public void testGetArtifactVersionsArtifactExists() @@ -553,4 +536,18 @@ public class SearchServiceImplTest assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() ); } + + private ArtifactMetadata createArtifact( Date whenGathered ) + { + String version = "1.0"; + ArtifactMetadata artifactMetadata = new ArtifactMetadata(); + artifactMetadata.setVersion( version ); + artifactMetadata.setId( ARCHIVA_TEST_ARTIFACT_ID + "-" + version + ".jar" ); + artifactMetadata.setProject( ARCHIVA_TEST_ARTIFACT_ID ); + artifactMetadata.setNamespace( ARCHIVA_TEST_GROUP_ID ); + artifactMetadata.setMd5( CHECKSUM ); + artifactMetadata.setWhenGathered( whenGathered ); + artifactMetadata.setRepositoryId( TEST_REPO ); + return artifactMetadata; + } } \ No newline at end of file