import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.index.IndexReader;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
import org.apache.maven.archiva.consumers.ConsumerException;
import org.slf4j.LoggerFactory;
import org.sonatype.nexus.index.ArtifactContext;
import org.sonatype.nexus.index.ArtifactContextProducer;
+import org.sonatype.nexus.index.ArtifactInfo;
import org.sonatype.nexus.index.DefaultArtifactContextProducer;
import org.sonatype.nexus.index.NexusIndexer;
import org.sonatype.nexus.index.context.IndexingContext;
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
+import org.sonatype.nexus.index.creator.AbstractIndexCreator;
import org.sonatype.nexus.index.creator.IndexerEngine;
import org.sonatype.nexus.index.packer.IndexPacker;
private File managedRepository;
private IndexerEngine indexerEngine;
+
+ private Set<String> uinfos;
public NexusIndexerConsumer( NexusIndexer indexer, IndexPacker indexPacker, IndexerEngine indexerEngine )
{
repositoryContent = new ManagedDefaultRepositoryContent();
repositoryContent.setRepository( repository );
+ uinfos = new HashSet<String>();
synchronized ( indexer )
{
indexDirectory, null, null, NexusIndexer.FULL_INDEX );
context.setSearchable( repository.isScanned() );
+ // read index to get all the artifacts already indexed
+ IndexReader r = context.getIndexReader();
+ for ( int i = 0; i < r.numDocs(); i++ )
+ {
+ if ( !r.isDeleted( i ) )
+ {
+ Document d = r.document( i );
+ String uinfo = d.get( ArtifactInfo.UINFO );
+
+ if ( uinfo != null )
+ {
+ uinfos.add( uinfo );
+ }
+ }
+ }
+
indexerEngine.beginIndexing( context );
}
catch ( UnsupportedExistingLuceneIndexException e )
public void processFile( String path )
throws ConsumerException
{
- File artifactFile = new File( managedRepository, path );
-
+ File artifactFile = new File( managedRepository, path );
ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile );
+
if ( artifactContext != null )
{
try
- {
- //indexer.artifactDiscovered( artifactContext, context );
+ {
+ ArtifactInfo ai = artifactContext.getArtifactInfo();
+ String uinfo = AbstractIndexCreator.getGAV(
+ ai.groupId, ai.artifactId, ai.version, ai.classifier, ai.packaging );
- indexerEngine.index( context, artifactContext );
+ // already indexed so update!
+ if ( uinfos.contains( uinfo ) )
+ {
+ indexerEngine.update( context, artifactContext );
+ }
+ else
+ {
+ indexerEngine.index( context, artifactContext );
+ }
}
catch ( IOException e )
{
final File indexLocation = new File( managedRepository, ".index" );
try
{
- indexerEngine.endIndexing( context );
-
- indexPacker.packIndex( context, indexLocation );
+ indexerEngine.endIndexing( context );
+ indexPacker.packIndex( context, indexLocation );
+ uinfos = null;
}
catch ( IOException e )
{
import org.apache.commons.io.FileUtils;
import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
// begin scan
Date now = Calendar.getInstance().getTime();
nexusIndexerConsumer.beginScan( repositoryConfig, now );
-
- // process file
nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
-
- // end scan
nexusIndexerConsumer.completeScan();
// search!
ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
assertEquals( "org.apache.archiva", artifactInfo.groupId );
assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
- assertEquals( "test-repo", artifactInfo.repository );
+ assertEquals( "test-repo", artifactInfo.repository );
}
public void testIndexerArtifactAlreadyIndexed()
throws Exception
{
-
+ // begin scan
+ Date now = Calendar.getInstance().getTime();
+ nexusIndexerConsumer.beginScan( repositoryConfig, now );
+ nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
+ nexusIndexerConsumer.completeScan();
+
+ // scan and index again
+ now = Calendar.getInstance().getTime();
+ nexusIndexerConsumer.beginScan( repositoryConfig, now );
+ nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
+ nexusIndexerConsumer.completeScan();
+
+ // search!
+ BooleanQuery q = new BooleanQuery();
+ q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
+ q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
+
+ IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
+ TopDocs topDocs = searcher.search( q, null, 10 );
+
+ assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
+ assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
+
+ // should return only 1 hit - artifact should have just been updated and not added as a separate doc
+ assertEquals( 1, topDocs.totalHits );
}
- /*public void testIndexerIndexPom()
+ /*
+ public void testIndexerIndexArtifactThenPom()
throws Exception
{
// begin scan