]> source.dussan.org Git - archiva.git/commitdiff
[MRM-749]
authorMaria Odea B. Ching <oching@apache.org>
Fri, 23 Jan 2009 04:05:02 +0000 (04:05 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Fri, 23 Jan 2009 04:05:02 +0000 (04:05 +0000)
o just update index doc if artifact has already been indexed

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@736905 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java
archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java

index 7176878a4bacd608b88eb76fdbc4a24e939facd2..b9c45a10185df69cba307c83ea24ba1b77cc8501 100644 (file)
@@ -24,7 +24,12 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.index.IndexReader;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
 import org.apache.maven.archiva.consumers.ConsumerException;
@@ -34,10 +39,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonatype.nexus.index.ArtifactContext;
 import org.sonatype.nexus.index.ArtifactContextProducer;
+import org.sonatype.nexus.index.ArtifactInfo;
 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
 import org.sonatype.nexus.index.NexusIndexer;
 import org.sonatype.nexus.index.context.IndexingContext;
 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
+import org.sonatype.nexus.index.creator.AbstractIndexCreator;
 import org.sonatype.nexus.index.creator.IndexerEngine;
 import org.sonatype.nexus.index.packer.IndexPacker;
 
@@ -65,6 +72,8 @@ public class NexusIndexerConsumer
     private File managedRepository;
     
     private IndexerEngine indexerEngine;
+    
+    private Set<String> uinfos;
 
     public NexusIndexerConsumer( NexusIndexer indexer, IndexPacker indexPacker, IndexerEngine indexerEngine )
     {
@@ -98,6 +107,7 @@ public class NexusIndexerConsumer
 
         repositoryContent = new ManagedDefaultRepositoryContent();
         repositoryContent.setRepository( repository );
+        uinfos = new HashSet<String>();
 
         synchronized ( indexer )
         {
@@ -108,6 +118,22 @@ public class NexusIndexerConsumer
                                                 indexDirectory, null, null, NexusIndexer.FULL_INDEX );
                 context.setSearchable( repository.isScanned() );
                 
+                // read index to get all the artifacts already indexed
+                IndexReader r = context.getIndexReader();                
+                for ( int i = 0; i < r.numDocs(); i++ )
+                {
+                    if ( !r.isDeleted( i ) )
+                    {
+                        Document d = r.document( i );          
+                        String uinfo = d.get( ArtifactInfo.UINFO );
+          
+                        if ( uinfo != null )
+                        {
+                            uinfos.add( uinfo );
+                        }
+                    }
+                }
+                
                 indexerEngine.beginIndexing( context );
             }
             catch ( UnsupportedExistingLuceneIndexException e )
@@ -124,16 +150,26 @@ public class NexusIndexerConsumer
     public void processFile( String path )
         throws ConsumerException
     {
-        File artifactFile = new File( managedRepository, path );
-        
+        File artifactFile = new File( managedRepository, path );        
         ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile );
+        
         if ( artifactContext != null )
         {
             try
-            {
-                //indexer.artifactDiscovered( artifactContext, context );
+            {                
+                ArtifactInfo ai = artifactContext.getArtifactInfo();                
+                String uinfo = AbstractIndexCreator.getGAV(
+                    ai.groupId, ai.artifactId, ai.version, ai.classifier, ai.packaging );
                 
-                indexerEngine.index( context, artifactContext );
+                // already indexed so update!
+                if ( uinfos.contains( uinfo ) )
+                {
+                    indexerEngine.update( context, artifactContext );
+                }
+                else
+                {
+                    indexerEngine.index( context, artifactContext );
+                }    
             }
             catch ( IOException e )
             {
@@ -147,9 +183,9 @@ public class NexusIndexerConsumer
         final File indexLocation = new File( managedRepository, ".index" );
         try
         {
-            indexerEngine.endIndexing( context );
-            
-            indexPacker.packIndex( context, indexLocation );
+            indexerEngine.endIndexing( context );            
+            indexPacker.packIndex( context, indexLocation );            
+            uinfos = null;
         }
         catch ( IOException e )
         {
index 94f43f682d3335800523da29ec952adeb2ec8aa2..ab07d0a5d1b04f86792b91606d6d8e882c0aae9c 100644 (file)
@@ -26,6 +26,8 @@ import java.util.Set;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.search.BooleanClause.Occur;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
@@ -96,11 +98,7 @@ public class NexusIndexerConsumerTest
         // begin scan
         Date now = Calendar.getInstance().getTime();
         nexusIndexerConsumer.beginScan( repositoryConfig, now );
-        
-        // process file
         nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
-        
-        // end scan
         nexusIndexerConsumer.completeScan();
         
         // search!
@@ -120,16 +118,41 @@ public class NexusIndexerConsumerTest
         ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
         assertEquals( "org.apache.archiva", artifactInfo.groupId );
         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
-        assertEquals( "test-repo", artifactInfo.repository );   
+        assertEquals( "test-repo", artifactInfo.repository );  
     }
     
     public void testIndexerArtifactAlreadyIndexed()
         throws Exception
     {
-    
+        // begin scan
+        Date now = Calendar.getInstance().getTime();
+        nexusIndexerConsumer.beginScan( repositoryConfig, now );
+        nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
+        nexusIndexerConsumer.completeScan();
+        
+        // scan and index again
+        now = Calendar.getInstance().getTime();
+        nexusIndexerConsumer.beginScan( repositoryConfig, now );
+        nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );        
+        nexusIndexerConsumer.completeScan();
+        
+        // search!
+        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 );
+        
+        IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
+        TopDocs topDocs = searcher.search( q, null, 10 );
+        
+        assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
+        assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
+        
+        // should return only 1 hit - artifact should have just been updated and not added as a separate doc
+        assertEquals( 1, topDocs.totalHits );
     }
     
-    /*public void testIndexerIndexPom()
+    /*
+    public void testIndexerIndexArtifactThenPom()
         throws Exception
     {        
         // begin scan