You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by oc...@apache.org on 2009/01/23 05:05:03 UTC

svn commit: r736905 - in /archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src: main/java/org/apache/archiva/consumers/lucene/ test/java/org/apache/archiva/consumers/lucene/

Author: oching
Date: Thu Jan 22 20:05:02 2009
New Revision: 736905

URL: http://svn.apache.org/viewvc?rev=736905&view=rev
Log:
[MRM-749]
o just update index doc if artifact has already been indexed

Modified:
    archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java
    archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java

Modified: archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java
URL: http://svn.apache.org/viewvc/archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java?rev=736905&r1=736904&r2=736905&view=diff
==============================================================================
--- archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java (original)
+++ archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java Thu Jan 22 20:05:02 2009
@@ -24,7 +24,12 @@
 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.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 @@
     private File managedRepository;
     
     private IndexerEngine indexerEngine;
+    
+    private Set<String> uinfos;
 
     public NexusIndexerConsumer( NexusIndexer indexer, IndexPacker indexPacker, IndexerEngine indexerEngine )
     {
@@ -98,6 +107,7 @@
 
         repositoryContent = new ManagedDefaultRepositoryContent();
         repositoryContent.setRepository( repository );
+        uinfos = new HashSet<String>();
 
         synchronized ( indexer )
         {
@@ -108,6 +118,22 @@
                                                 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 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 @@
         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 )
         {

Modified: archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java
URL: http://svn.apache.org/viewvc/archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java?rev=736905&r1=736904&r2=736905&view=diff
==============================================================================
--- archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java (original)
+++ archiva/branches/archiva-nexus-indexer/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java Thu Jan 22 20:05:02 2009
@@ -26,6 +26,8 @@
 
 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 @@
         // 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 @@
         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