From: Brett Porter Date: Fri, 28 Jul 2006 05:18:45 +0000 (+0000) Subject: [MRM-127] add delete record method X-Git-Tag: archiva-0.9-alpha-1~732 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7c31a1f9bea0ac58c9b6760044eb61790ea5bece;p=archiva.git [MRM-127] add delete record method git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@426389 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java index ee2f7d205..19bfe3d1a 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryArtifactIndex.java @@ -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; } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java index eb26d388d..6cca91b88 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/lucene/LuceneRepositoryArtifactIndex.java @@ -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 + } + } } diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalArtifactIndexTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalArtifactIndexTest.java index 110a741e2..013913419 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalArtifactIndexTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneMinimalArtifactIndexTest.java @@ -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 { diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneStandardArtifactIndexTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneStandardArtifactIndexTest.java index fc8e0da17..c364f9514 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneStandardArtifactIndexTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/lucene/LuceneStandardArtifactIndexTest.java @@ -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" );