Author: ifedorenko
Date: Tue Apr 23 16:54:25 2013
New Revision: 1471039

URL: http://svn.apache.org/r1471039
Log:
MINDEXER-73 Incremental-only index update support

The patch provided by Steve Carlucci <scarlu...@sonatype.com>


Modified:
    
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
    
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateRequest.java
    
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateResult.java
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DefaultIndexUpdaterTest.java

Modified: 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java?rev=1471039&r1=1471038&r2=1471039&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
 Tue Apr 23 16:54:25 2013
@@ -133,8 +133,10 @@ public class DefaultIndexUpdater
 
                     try
                     {
-                        fetchAndUpdateIndex( updateRequest, fetcher, cache );
-                        cache.commit();
+                        if( fetchAndUpdateIndex( updateRequest, fetcher, cache 
).isSuccessful() )
+                        {
+                            cache.commit();
+                        }
                     }
                     finally
                     {
@@ -154,8 +156,12 @@ public class DefaultIndexUpdater
                 if ( !updateRequest.isCacheOnly() )
                 {
                     LuceneIndexAdaptor target = new LuceneIndexAdaptor( 
updateRequest );
-                    result.setTimestamp( fetchAndUpdateIndex( updateRequest, 
fetcher, target ) );
-                    target.commit();
+                    result = fetchAndUpdateIndex( updateRequest, fetcher, 
target );
+                    
+                    if(result.isSuccessful())
+                    {
+                        target.commit();
+                    }
                 }
             }
             finally
@@ -795,10 +801,12 @@ public class DefaultIndexUpdater
             throws IOException;
     }
 
-    private Date fetchAndUpdateIndex( final IndexUpdateRequest updateRequest, 
ResourceFetcher source,
+    private IndexUpdateResult fetchAndUpdateIndex( final IndexUpdateRequest 
updateRequest, ResourceFetcher source,
                                       IndexAdaptor target )
         throws IOException
     {
+        IndexUpdateResult result = new IndexUpdateResult();
+        
         if ( !updateRequest.isForceFullUpdate() )
         {
             Properties localProperties = target.getProperties();
@@ -829,7 +837,9 @@ public class DefaultIndexUpdater
                         target.addIndexChunk( source, filename );
                     }
 
-                    return updateTimestamp;
+                    result.setTimestamp(updateTimestamp);
+                    result.setSuccessful(true);
+                    return result;
                 }
             }
             else
@@ -847,7 +857,9 @@ public class DefaultIndexUpdater
                 // checking the timestamp, if the same, nothing to do
                 if ( updateTimestamp != null && localTimestamp != null && 
!updateTimestamp.after( localTimestamp ) )
                 {
-                    return null; // index is up to date
+                    //Index is up to date
+                    result.setSuccessful(true);
+                    return result;
                 }
             }
         }
@@ -857,34 +869,43 @@ public class DefaultIndexUpdater
             target.setProperties( source );
         }
 
-        try
+        if( !updateRequest.isIncrementalOnly() )
         {
-            Date timestamp = target.setIndexFile( source, 
IndexingContext.INDEX_FILE_PREFIX + ".gz" );
-            if ( source instanceof LocalIndexCacheFetcher )
+            Date timestamp = null;
+            try
             {
-                // local cache has inverse organization compared to remote 
indexes,
-                // i.e. initial index file and delta chunks to apply on top of 
it
-                for ( String filename : ( (LocalIndexCacheFetcher) source 
).getChunks() )
+                timestamp = target.setIndexFile( source, 
IndexingContext.INDEX_FILE_PREFIX + ".gz" );
+                if ( source instanceof LocalIndexCacheFetcher )
                 {
-                    target.addIndexChunk( source, filename );
+                    // local cache has inverse organization compared to remote 
indexes,
+                    // i.e. initial index file and delta chunks to apply on 
top of it
+                    for ( String filename : ( (LocalIndexCacheFetcher) source 
).getChunks() )
+                    {
+                        target.addIndexChunk( source, filename );
+                    }
                 }
             }
-            return timestamp;
-        }
-        catch ( IOException ex )
-        {
-            // try to look for legacy index transfer format
-            try
-            {
-                return target.setIndexFile( source, 
IndexingContext.INDEX_FILE_PREFIX + ".zip" );
-            }
-            catch ( IOException ex2 )
+            catch ( IOException ex )
             {
-                getLogger().error( "Fallback to *.zip also failed: " + ex2 ); 
// do not bother with stack trace
-                
-                throw ex; // original exception more likely to be interesting
+                // try to look for legacy index transfer format
+                try
+                {
+                    timestamp = target.setIndexFile( source, 
IndexingContext.INDEX_FILE_PREFIX + ".zip" );
+                }
+                catch ( IOException ex2 )
+                {
+                    getLogger().error( "Fallback to *.zip also failed: " + ex2 
); // do not bother with stack trace
+                    
+                    throw ex; // original exception more likely to be 
interesting
+                }
             }
+            
+            result.setTimestamp(timestamp);
+            result.setSuccessful(true);
+            result.setFullUpdate(true);
         }
+        
+        return result;
     }
 
     /**

Modified: 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateRequest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateRequest.java?rev=1471039&r1=1471038&r2=1471039&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateRequest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateRequest.java
 Tue Apr 23 16:54:25 2013
@@ -40,6 +40,8 @@ public class IndexUpdateRequest
     private DocumentFilter documentFilter;
 
     private boolean forceFullUpdate;
+    
+    private boolean incrementalOnly;
 
     private File localIndexCacheDir;
 
@@ -59,6 +61,7 @@ public class IndexUpdateRequest
         this.context = context;
         this.resourceFetcher = resourceFetcher;
         this.forceFullUpdate = false;
+        this.incrementalOnly = false;
     }
 
     public IndexingContext getIndexingContext()
@@ -90,6 +93,16 @@ public class IndexUpdateRequest
     {
         return forceFullUpdate;
     }
+    
+    public boolean isIncrementalOnly()
+    {
+        return incrementalOnly;
+    }
+
+    public void setIncrementalOnly(boolean incrementalOnly)
+    {
+        this.incrementalOnly = incrementalOnly;
+    }
 
     public File getLocalIndexCacheDir()
     {

Modified: 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateResult.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateResult.java?rev=1471039&r1=1471038&r2=1471039&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateResult.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/IndexUpdateResult.java
 Tue Apr 23 16:54:25 2013
@@ -26,6 +26,15 @@ public class IndexUpdateResult
     private Date timestamp;
 
     private boolean fullUpdate;
+    
+    private boolean successful;
+    
+    public IndexUpdateResult()
+    {
+        this.timestamp = null;
+        this.fullUpdate = false;
+        this.successful = false;
+    }
 
     public Date getTimestamp()
     {
@@ -46,4 +55,14 @@ public class IndexUpdateResult
     {
         return this.fullUpdate;
     }
+
+    public boolean isSuccessful()
+    {
+        return successful;
+    }
+
+    public void setSuccessful(boolean successful)
+    {
+        this.successful = successful;
+    }
 }

Modified: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DefaultIndexUpdaterTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DefaultIndexUpdaterTest.java?rev=1471039&r1=1471038&r2=1471039&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DefaultIndexUpdaterTest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DefaultIndexUpdaterTest.java
 Tue Apr 23 16:54:25 2013
@@ -345,9 +345,10 @@ public class DefaultIndexUpdaterTest
 
         IndexUpdateRequest updateRequest = new IndexUpdateRequest( 
tempContext, mockFetcher );
 
-        updater.fetchAndUpdateIndex( updateRequest );
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
 
         mockery.assertIsSatisfied();
+        assertIndexUpdateSucceeded(updateResult);
     }
 
     public void testFullIndexUpdate()
@@ -416,9 +417,10 @@ public class DefaultIndexUpdaterTest
 
         IndexUpdateRequest updateRequest = new IndexUpdateRequest( 
tempContext, mockFetcher );
 
-        updater.fetchAndUpdateIndex( updateRequest );
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
 
         mockery.assertIsSatisfied();
+        assertIndexUpdateSucceeded(updateResult);
     }
 
     public void testIncrementalIndexUpdate()
@@ -497,10 +499,12 @@ public class DefaultIndexUpdaterTest
         // tempContext.updateTimestamp( true, contextTimestamp );
 
         IndexUpdateRequest updateRequest = new IndexUpdateRequest( 
tempContext, mockFetcher );
+        updateRequest.setIncrementalOnly(true);
 
-        updater.fetchAndUpdateIndex( updateRequest );
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
 
         mockery.assertIsSatisfied();
+        assertIndexUpdateSucceeded(updateResult);
     }
 
     public void testIncrementalIndexUpdateNoCounter()
@@ -576,9 +580,74 @@ public class DefaultIndexUpdaterTest
 
         IndexUpdateRequest updateRequest = new IndexUpdateRequest( 
tempContext, mockFetcher );
 
-        updater.fetchAndUpdateIndex( updateRequest );
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
 
         mockery.assertIsSatisfied();
+        assertIndexUpdateSucceeded(updateResult);
+    }
+    
+    public void testIncrementalOnlyIndexUpdateNoCounter()
+        throws Exception
+    {
+        Mockery mockery = new Mockery();
+
+        final String indexUrl = repositoryUrl + ".index";
+        final Date contextTimestamp = df.parse( "20081128000000.000 -0600" );
+
+        final ResourceFetcher mockFetcher = mockery.mock( 
ResourceFetcher.class );
+
+        final IndexingContext tempContext = mockery.mock( 
IndexingContext.class );
+
+        mockery.checking( new Expectations()
+        {
+            {
+                allowing( tempContext ).getIndexDirectoryFile();
+                will( new ReturnValueAction( testBasedir ) );
+
+                allowing( tempContext ).getTimestamp();
+                will( returnValue( contextTimestamp ) );
+
+                allowing( tempContext ).getId();
+                will( returnValue( repositoryId ) );
+
+                allowing( tempContext ).getIndexUpdateUrl();
+                will( returnValue( indexUrl ) );
+
+                allowing( tempContext ).getIndexCreators();
+                will( returnValue( DEFAULT_CREATORS ) );
+
+                oneOf( mockFetcher ).connect( repositoryId, indexUrl );
+
+                oneOf( mockFetcher ).retrieve( //
+                    with( IndexingContext.INDEX_REMOTE_PROPERTIES_FILE ) );
+                will( new PropertiesAction()
+                {
+                    @Override
+                    Properties getProperties()
+                    {
+                        Properties properties = new Properties();
+                        properties.setProperty( IndexingContext.INDEX_ID, 
"central" );
+                        properties.setProperty( 
IndexingContext.INDEX_TIMESTAMP, "20081129174241.859 -0600" );
+                        properties.setProperty( 
IndexingContext.INDEX_CHUNK_COUNTER, "3" );
+                        properties.setProperty( 
IndexingContext.INDEX_CHAIN_ID, "someid" );
+                        properties.setProperty( 
IndexingContext.INDEX_CHUNK_PREFIX + "0", "3" );
+                        properties.setProperty( 
IndexingContext.INDEX_CHUNK_PREFIX + "1", "2" );
+                        properties.setProperty( 
IndexingContext.INDEX_CHUNK_PREFIX + "2", "1" );
+                        return properties;
+                    }
+                } );
+
+                oneOf( mockFetcher ).disconnect();
+            }
+        } );
+
+        IndexUpdateRequest updateRequest = new IndexUpdateRequest( 
tempContext, mockFetcher );
+        updateRequest.setIncrementalOnly(true);
+
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
+
+        mockery.assertIsSatisfied();
+        assertIndexUpdateFailed(updateResult);
     }
 
     public void testIncrementalIndexUpdateNoUpdateNecessary()
@@ -663,9 +732,10 @@ public class DefaultIndexUpdaterTest
 
         IndexUpdateRequest updateRequest = new IndexUpdateRequest( 
tempContext, mockFetcher );
 
-        updater.fetchAndUpdateIndex( updateRequest );
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
 
         mockery.assertIsSatisfied();
+        assertIndexUpdateSucceeded(updateResult);
     }
 
     public void testUpdateForceFullUpdate()
@@ -751,9 +821,10 @@ public class DefaultIndexUpdaterTest
 
         updateRequest.setForceFullUpdate( true );
 
-        updater.fetchAndUpdateIndex( updateRequest );
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
 
         mockery.assertIsSatisfied();
+        assertIndexUpdateSucceeded(updateResult);
     }
 
     public void testUpdateForceFullUpdateNoGZ()
@@ -830,9 +901,10 @@ public class DefaultIndexUpdaterTest
 
         updateRequest.setForceFullUpdate( true );
 
-        updater.fetchAndUpdateIndex( updateRequest );
+        IndexUpdateResult updateResult = updater.fetchAndUpdateIndex( 
updateRequest );
 
         mockery.assertIsSatisfied();
+        assertIndexUpdateSucceeded(updateResult);
     }
 
     protected InputStream newInputStream( String path )
@@ -898,4 +970,14 @@ public class DefaultIndexUpdaterTest
             return this.file.getParentFile();
         }
     }
+    
+    private void assertIndexUpdateSucceeded(IndexUpdateResult updateResult)
+    {
+        assertTrue("Index update should have succeeded, but says it failed", 
updateResult.isSuccessful());
+    }
+    
+    private void assertIndexUpdateFailed(IndexUpdateResult updateResult)
+    {
+        assertFalse("Index update should have failed, but says it succeeded", 
updateResult.isSuccessful());
+    }
 }
\ No newline at end of file


Reply via email to