]> source.dussan.org Git - archiva.git/commitdiff
[MRM-127] add delete record method
authorBrett Porter <brett@apache.org>
Fri, 28 Jul 2006 05:18:45 +0000 (05:18 +0000)
committerBrett Porter <brett@apache.org>
Fri, 28 Jul 2006 05:18:45 +0000 (05:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@426389 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalArtifactIndexTest.java
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneStandardArtifactIndexTest.java

index ee2f7d20546f1196a8182f7ea22f17db7b66709f..19bfe3d1af1fdf1c2f6a4c853c4c78ed128af1e4 100644 (file)
@@ -57,4 +57,12 @@ public interface RepositoryArtifactIndex
      */
     boolean exists()
         throws RepositoryIndexException;
+
+    /**
+     * Delete records from the index. Simply ignore the request any did not exist.
+     *
+     * @param records the records to delete
+     */
+    void deleteRecords( Collection records )
+        throws RepositoryIndexException;
 }
index eb26d388dffddb2a9db801bc1fc5f3fb4e6d3f9a..6cca91b88414b0263608ee0f08690fe26e1e96ff 100644 (file)
@@ -68,14 +68,7 @@ public class LuceneRepositoryArtifactIndex
     public void indexRecords( Collection records )
         throws RepositoryIndexException
     {
-        try
-        {
-            deleteRecords( records );
-        }
-        catch ( IOException e )
-        {
-            throw new RepositoryIndexException( "Failed to delete an index document", e );
-        }
+        deleteRecords( records );
 
         addRecords( records );
     }
@@ -141,8 +134,8 @@ public class LuceneRepositoryArtifactIndex
         return new StandardAnalyzer();
     }
 
-    private void deleteRecords( Collection records )
-        throws IOException, RepositoryIndexException
+    public void deleteRecords( Collection records )
+        throws RepositoryIndexException
     {
         if ( exists() )
         {
@@ -163,11 +156,15 @@ public class LuceneRepositoryArtifactIndex
                     }
                 }
             }
+            catch ( IOException e )
+            {
+                throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
+            }
             finally
             {
                 if ( indexReader != null )
                 {
-                    indexReader.close();
+                    closeQuietly( indexReader );
                 }
             }
         }
@@ -259,4 +256,19 @@ public class LuceneRepositoryArtifactIndex
             // ignore
         }
     }
+
+    private static void closeQuietly( IndexReader reader )
+    {
+        try
+        {
+            if ( reader != null )
+            {
+                reader.close();
+            }
+        }
+        catch ( IOException e )
+        {
+            // ignore
+        }
+    }
 }
index 110a741e2215b8b0d46a134d3bc0dd9eb2b08166..0139134190a2cb0d3e55604eb49607b9069a8424 100644 (file)
@@ -195,6 +195,62 @@ public class LuceneMinimalArtifactIndexTest
         }
     }
 
+    public void testDeleteRecordInIndex()
+        throws IOException, RepositoryIndexException
+    {
+        createEmptyIndex();
+
+        Artifact artifact = createArtifact( "test-jar" );
+
+        RepositoryIndexRecord record = recordFactory.createRecord( artifact );
+        index.indexRecords( Collections.singletonList( record ) );
+
+        index.deleteRecords( Collections.singletonList( record ) );
+
+        IndexReader reader = IndexReader.open( indexLocation );
+        try
+        {
+            assertEquals( "No documents", 0, reader.numDocs() );
+        }
+        finally
+        {
+            reader.close();
+        }
+    }
+
+    public void testDeleteRecordNotInIndex()
+        throws IOException, RepositoryIndexException
+    {
+        createEmptyIndex();
+
+        Artifact artifact = createArtifact( "test-jar" );
+
+        RepositoryIndexRecord record = recordFactory.createRecord( artifact );
+
+        index.deleteRecords( Collections.singletonList( record ) );
+
+        IndexReader reader = IndexReader.open( indexLocation );
+        try
+        {
+            assertEquals( "No documents", 0, reader.numDocs() );
+        }
+        finally
+        {
+            reader.close();
+        }
+    }
+
+    public void testDeleteRecordNoIndex()
+        throws IOException, RepositoryIndexException
+    {
+        Artifact artifact = createArtifact( "test-jar" );
+
+        RepositoryIndexRecord record = recordFactory.createRecord( artifact );
+        index.deleteRecords( Collections.singleton( record ) );
+
+        assertFalse( index.exists() );
+    }
+
     public void testAddPomRecord()
         throws IOException, RepositoryIndexException
     {
index fc8e0da175788fa6e15ccd31baedc243d4eaa03a..c364f9514d1926407c964a3eb8c75ac0c7d13db4 100644 (file)
@@ -241,6 +241,62 @@ public class LuceneStandardArtifactIndexTest
         }
     }
 
+    public void testDeleteRecordInIndex()
+        throws IOException, RepositoryIndexException
+    {
+        createEmptyIndex();
+
+        Artifact artifact = createArtifact( "test-jar" );
+
+        RepositoryIndexRecord record = recordFactory.createRecord( artifact );
+        index.indexRecords( Collections.singletonList( record ) );
+
+        index.deleteRecords( Collections.singletonList( record ) );
+
+        IndexReader reader = IndexReader.open( indexLocation );
+        try
+        {
+            assertEquals( "No documents", 0, reader.numDocs() );
+        }
+        finally
+        {
+            reader.close();
+        }
+    }
+
+    public void testDeleteRecordNotInIndex()
+        throws IOException, RepositoryIndexException
+    {
+        createEmptyIndex();
+
+        Artifact artifact = createArtifact( "test-jar" );
+
+        RepositoryIndexRecord record = recordFactory.createRecord( artifact );
+
+        index.deleteRecords( Collections.singletonList( record ) );
+
+        IndexReader reader = IndexReader.open( indexLocation );
+        try
+        {
+            assertEquals( "No documents", 0, reader.numDocs() );
+        }
+        finally
+        {
+            reader.close();
+        }
+    }
+
+    public void testDeleteRecordNoIndex()
+        throws IOException, RepositoryIndexException
+    {
+        Artifact artifact = createArtifact( "test-jar" );
+
+        RepositoryIndexRecord record = recordFactory.createRecord( artifact );
+        index.deleteRecords( Collections.singleton( record ) );
+
+        assertFalse( index.exists() );
+    }
+
     private Artifact createArtifact( String artifactId )
     {
         return createArtifact( artifactId, "1.0", "jar" );