*/
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;
}
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 );
}
return new StandardAnalyzer();
}
- private void deleteRecords( Collection records )
- throws IOException, RepositoryIndexException
+ public void deleteRecords( Collection records )
+ throws RepositoryIndexException
{
if ( exists() )
{
}
}
}
+ catch ( IOException e )
+ {
+ throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e );
+ }
finally
{
if ( indexReader != null )
{
- indexReader.close();
+ closeQuietly( indexReader );
}
}
}
// ignore
}
}
+
+ private static void closeQuietly( IndexReader reader )
+ {
+ try
+ {
+ if ( reader != null )
+ {
+ reader.close();
+ }
+ }
+ catch ( IOException e )
+ {
+ // ignore
+ }
+ }
}
}
}
+ 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
{
}
}
+ 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" );