Author: cstamas
Date: Mon Dec 13 19:10:48 2010
New Revision: 1045313

URL: http://svn.apache.org/viewvc?rev=1045313&view=rev
Log:
Identify semantics changed.

Before, the algorithm was checking just for resultSize==1 and spitting result 
only then. This obviously works in some scenarios, but is not well suited in 
envs like MRMs and they were forced to "reinvent" the same query (actually 
performing a query, doing exactly same as was happening here).

Now, identify returns a collections of AIs (never null), and it is up to 
library integrator to decide how to act upon different results, possible cases:

a) empty collection: not indentified (like before)
b) collection with one result (identified successfully, like before)
c) collection with multiple results, same GAVS, they might come from some 
aggregated reposes or ... (insert your use case here)
d) collection with multiple results, different GAVs (SHA1 not enough anymore? 
;) )

Modified:
    
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/DefaultNexusIndexer.java
    
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/NexusIndexer.java
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/AbstractRepoNexusIndexerTest.java
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus13NexusIndexerTest.java
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus3233NexusIndexerTest.java
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/NexusIndexerTest.java
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/SubPathReindexTest.java

Modified: 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/DefaultNexusIndexer.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/DefaultNexusIndexer.java?rev=1045313&r1=1045312&r2=1045313&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/DefaultNexusIndexer.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/DefaultNexusIndexer.java
 Mon Dec 13 19:10:48 2010
@@ -23,6 +23,7 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -449,19 +450,19 @@ public class DefaultNexusIndexer
     // Identification
     // 
----------------------------------------------------------------------------
 
-    public ArtifactInfo identify( Field field, String query )
+    public Collection<ArtifactInfo> identify( Field field, String query )
         throws IllegalArgumentException, IOException
     {
         return identify( constructQuery( field, query, SearchType.EXACT ) );
     }
 
-    public ArtifactInfo identify( File artifact )
+    public Collection<ArtifactInfo> identify( File artifact )
         throws IOException
     {
         return identify( artifact, indexingContexts.values() );
     }
 
-    public ArtifactInfo identify( File artifact, Collection<IndexingContext> 
contexts )
+    public Collection<ArtifactInfo> identify( File artifact, 
Collection<IndexingContext> contexts )
         throws IOException
     {
         FileInputStream is = null;
@@ -497,30 +498,27 @@ public class DefaultNexusIndexer
         }
     }
 
-    public ArtifactInfo identify( Query query )
+    public Collection<ArtifactInfo> identify( Query query )
         throws IOException
     {
         return identify( query, indexingContexts.values() );
     }
 
-    public ArtifactInfo identify( Query query, Collection<IndexingContext> 
contexts )
+    public Collection<ArtifactInfo> identify( Query query, 
Collection<IndexingContext> contexts )
         throws IOException
     {
         IteratorSearchResponse result = searcher.searchIteratorPaged( new 
IteratorSearchRequest( query ), contexts );
 
         try
         {
-            // TODO: this implementation is flakey: case a) 0 hits is okay, b) 
1 hit is okay, c1) >1 hits and all same
-            // GAVs
-            // -- okay but which source repo will be used? c2) >1 hits, and 
different GAVs --- huh?
-            if ( result.getTotalHits() > 0 )
-            {
-                return result.getResults().next();
-            }
-            else
+            ArrayList<ArtifactInfo> ais = new ArrayList<ArtifactInfo>( 
result.getTotalHits() );
+
+            for ( ArtifactInfo ai : result )
             {
-                return null;
+                ais.add( ai );
             }
+
+            return ais;
         }
         finally
         {

Modified: 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/NexusIndexer.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/NexusIndexer.java?rev=1045313&r1=1045312&r2=1045313&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/NexusIndexer.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/NexusIndexer.java
 Mon Dec 13 19:10:48 2010
@@ -317,22 +317,23 @@ public interface NexusIndexer
     // throws ParseException;
 
     // 
----------------------------------------------------------------------------
-    // Identification
+    // Identification 
+    // Since 4.0: Indexer does not make any assumptions, it is caller call to 
decide what to do with multiple results
     // 
----------------------------------------------------------------------------
 
-    ArtifactInfo identify( Field field, String query )
+    Collection<ArtifactInfo> identify( Field field, String query )
         throws IllegalArgumentException, IOException;
 
-    ArtifactInfo identify( File artifact )
+    Collection<ArtifactInfo> identify( File artifact )
         throws IOException;
 
-    ArtifactInfo identify( File artifact, Collection<IndexingContext> contexts 
)
+    Collection<ArtifactInfo> identify( File artifact, 
Collection<IndexingContext> contexts )
         throws IOException;
 
-    ArtifactInfo identify( Query query )
+    Collection<ArtifactInfo> identify( Query query )
         throws IOException;
 
-    ArtifactInfo identify( Query query, Collection<IndexingContext> contexts )
+    Collection<ArtifactInfo> identify( Query query, 
Collection<IndexingContext> contexts )
         throws IOException;
 
 }

Modified: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/AbstractRepoNexusIndexerTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/AbstractRepoNexusIndexerTest.java?rev=1045313&r1=1045312&r2=1045313&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/AbstractRepoNexusIndexerTest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/AbstractRepoNexusIndexerTest.java
 Mon Dec 13 19:10:48 2010
@@ -390,8 +390,12 @@ public abstract class AbstractRepoNexusI
     public void testIdentify()
         throws Exception
     {
-        ArtifactInfo ai = nexusIndexer.identify( MAVEN.SHA1, 
"4d2db265eddf1576cb9d896abc90c7ba46b48d87" );
+        Collection<ArtifactInfo> ais = nexusIndexer.identify( MAVEN.SHA1, 
"4d2db265eddf1576cb9d896abc90c7ba46b48d87" );
+        
+        assertEquals( 1, ais.size() );
 
+        ArtifactInfo ai = ais.iterator().next();
+        
         assertNotNull( ai );
 
         assertEquals( "qdox", ai.groupId );
@@ -404,7 +408,11 @@ public abstract class AbstractRepoNexusI
 
         File artifact = new File( repo, "qdox/qdox/1.5/qdox-1.5.jar" );
 
-        ai = nexusIndexer.identify( artifact );
+        ais = nexusIndexer.identify( artifact );
+        
+        assertEquals( 1, ais.size() );
+        
+        ai = ais.iterator().next();
 
         assertNotNull( "Can't identify qdox-1.5.jar", ai );
 

Modified: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus13NexusIndexerTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus13NexusIndexerTest.java?rev=1045313&r1=1045312&r2=1045313&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus13NexusIndexerTest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus13NexusIndexerTest.java
 Mon Dec 13 19:10:48 2010
@@ -237,7 +237,11 @@ public class Nexus13NexusIndexerTest
     public void testIdentify()
         throws Exception
     {
-        ArtifactInfo ai = nexusIndexer.identify( MAVEN.SHA1, 
"c8a2ef9d92a4b857eae0f36c2e01481787c5cbf8" );
+        Collection<ArtifactInfo> ais = nexusIndexer.identify( MAVEN.SHA1, 
"c8a2ef9d92a4b857eae0f36c2e01481787c5cbf8" );
+
+        assertEquals( 1, ais.size() );
+
+        ArtifactInfo ai = ais.iterator().next();
 
         assertNotNull( ai );
 
@@ -253,7 +257,11 @@ public class Nexus13NexusIndexerTest
             new File( repo,
                 
"cisco/infra/dft/maven-dma-mgmt-plugin/1.0-SNAPSHOT/maven-dma-mgmt-plugin-1.0-20080409.022326-2.jar"
 );
 
-        ai = nexusIndexer.identify( artifact );
+        ais = nexusIndexer.identify( artifact );
+        
+        assertEquals( 1, ais.size() );
+
+        ai = ais.iterator().next();
 
         assertNotNull( ai );
 

Modified: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus3233NexusIndexerTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus3233NexusIndexerTest.java?rev=1045313&r1=1045312&r2=1045313&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus3233NexusIndexerTest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/Nexus3233NexusIndexerTest.java
 Mon Dec 13 19:10:48 2010
@@ -19,9 +19,7 @@
 package org.apache.maven.index;
 
 import java.io.File;
-
-import org.apache.maven.index.ArtifactInfo;
-import org.apache.maven.index.NexusIndexer;
+import java.util.Collection;
 
 /** http://issues.sonatype.org/browse/NEXUS-3233 */
 public class Nexus3233NexusIndexerTest
@@ -42,7 +40,11 @@ public class Nexus3233NexusIndexerTest
         throws Exception
     {
         // POM1
-        ArtifactInfo ai = nexusIndexer.identify( MAVEN.SHA1, 
"741ea3998e6db3ce202d8b88aa53889543f050cc" );
+        Collection<ArtifactInfo> ais = nexusIndexer.identify( MAVEN.SHA1, 
"741ea3998e6db3ce202d8b88aa53889543f050cc" );
+
+        assertEquals( 1, ais.size() );
+
+        ArtifactInfo ai = ais.iterator().next();
 
         assertNotNull( ai );
 
@@ -53,7 +55,11 @@ public class Nexus3233NexusIndexerTest
         assertEquals( "1.0-SNAPSHOT", ai.version );
 
         // POM2
-        ai = nexusIndexer.identify( MAVEN.SHA1, 
"efb52d4ef65452b4e575fc2e7709595915775857" );
+        ais = nexusIndexer.identify( MAVEN.SHA1, 
"efb52d4ef65452b4e575fc2e7709595915775857" );
+
+        assertEquals( 1, ais.size() );
+
+        ai = ais.iterator().next();
 
         assertNotNull( ai );
 

Modified: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/NexusIndexerTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/NexusIndexerTest.java?rev=1045313&r1=1045312&r2=1045313&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/NexusIndexerTest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/NexusIndexerTest.java
 Mon Dec 13 19:10:48 2010
@@ -214,7 +214,11 @@ public class NexusIndexerTest
         bq.add( c, Occur.MUST_NOT );
 
         // invoking the old method (was present since day 1), that will return 
the match only and if only there is 1 hit
-        ArtifactInfo ai = indexer.identify( bq, Collections.singletonList( 
context ) );
+        Collection<ArtifactInfo> ais = indexer.identify( bq, 
Collections.singletonList( context ) );
+
+        assertEquals( 1, ais.size() );
+
+        ArtifactInfo ai = ais.iterator().next();
 
         // null means not "identified", so we want non-null response
         assertTrue( ai != null );
@@ -438,7 +442,11 @@ public class NexusIndexerTest
 
         // Search using SHA1 to find qdox 1.5
 
-        ArtifactInfo ai = nexus.identify( MAVEN.SHA1, 
"4d2db265eddf1576cb9d896abc90c7ba46b48d87" );
+        Collection<ArtifactInfo> ais = nexus.identify( MAVEN.SHA1, 
"4d2db265eddf1576cb9d896abc90c7ba46b48d87" );
+
+        assertEquals( 1, ais.size() );
+
+        ArtifactInfo ai = ais.iterator().next();
 
         assertNotNull( ai );
 
@@ -454,7 +462,11 @@ public class NexusIndexerTest
 
         File artifact = new File( getBasedir(), 
"src/test/repo/qdox/qdox/1.5/qdox-1.5.jar" );
 
-        ai = nexus.identify( artifact );
+        ais = nexus.identify( artifact );
+
+        assertEquals( 1, ais.size() );
+
+        ai = ais.iterator().next();
 
         assertNotNull( ai );
 

Modified: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/SubPathReindexTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/SubPathReindexTest.java?rev=1045313&r1=1045312&r2=1045313&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/SubPathReindexTest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/SubPathReindexTest.java
 Mon Dec 13 19:10:48 2010
@@ -19,6 +19,7 @@
 package org.apache.maven.index;
 
 import java.io.File;
+import java.util.Collection;
 import java.util.Set;
 
 public class SubPathReindexTest
@@ -75,22 +76,22 @@ public class SubPathReindexTest
     public void testIdentify()
         throws Exception
     {
-        ArtifactInfo ai;
+        Collection<ArtifactInfo> ais;
         File artifact;
 
         // Using a file: this one should be unknown
         artifact = new File( repo, "qdox/qdox/1.5/qdox-1.5.jar" );
 
-        ai = nexusIndexer.identify( artifact );
+        ais = nexusIndexer.identify( artifact );
 
-        assertNull( "Should not be able to identify it!", ai );
+        assertTrue( "Should not be able to identify it!", ais.isEmpty() );
 
         // Using a file: this one should be known
         artifact = new File( repo, 
"org/slf4j/slf4j-api/1.4.2/slf4j-api-1.4.2.jar" );
 
-        ai = nexusIndexer.identify( artifact );
+        ais = nexusIndexer.identify( artifact );
 
-        assertNotNull( "Should not be able to identify it!", ai );
+        assertEquals( "Should not be able to identify it!", 1, ais.size() );
     }
 
 }
\ No newline at end of file


Reply via email to