[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
This commit is contained in:
Brett Porter 2006-07-28 05:18:45 +00:00
parent cd49d0608a
commit 7c31a1f9be
4 changed files with 143 additions and 11 deletions

View 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;
}

View 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
}
}
}

View 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
{

View 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" );