use of org.apache.maven.index.context.IndexingContext in project archiva by apache.
the class MavenIndexManagerTest method removeArtifactsFromIndex.
@Test
public void removeArtifactsFromIndex() throws Exception {
ArchivaIndexingContext ctx = createTestContext();
Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
List<URI> uriList = new ArrayList<>();
uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
mavenIndexManager.addArtifactsToIndex(ctx, uriList);
IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
String term = "org.apache.archiva";
Query q = new BooleanQuery.Builder().add(queryCreator.constructQuery(MAVEN.GROUP_ID, new UserInputSearchExpression(term)), BooleanClause.Occur.SHOULD).build();
assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
uriList.remove(0);
mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
}
use of org.apache.maven.index.context.IndexingContext in project archiva by apache.
the class DefaultIndexMerger method buildMergedIndex.
@Override
public IndexingContext buildMergedIndex(IndexMergerRequest indexMergerRequest) throws IndexMergerException {
String groupId = indexMergerRequest.getGroupId();
if (runningGroups.contains(groupId)) {
log.info("skip build merge remote indexes for id: '{}' as already running", groupId);
return null;
}
runningGroups.add(groupId);
StopWatch stopWatch = new StopWatch();
stopWatch.reset();
stopWatch.start();
Path mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
String tempRepoId = mergedIndexDirectory.getFileName().toString();
try {
Path indexLocation = mergedIndexDirectory.resolve(indexMergerRequest.getMergedIndexPath());
List<IndexingContext> members = indexMergerRequest.getRepositoriesIds().stream().map(id -> repositoryRegistry.getRepository(id)).filter(repo -> repo.getType().equals(RepositoryType.MAVEN)).map(repo -> {
try {
return repo.getIndexingContext().getBaseContext(IndexingContext.class);
} catch (UnsupportedBaseContextException e) {
return null;
// Ignore
}
}).filter(Objects::nonNull).collect(Collectors.toList());
ContextMemberProvider memberProvider = new StaticContextMemberProvider(members);
IndexingContext mergedCtx = indexer.createMergedIndexingContext(tempRepoId, tempRepoId, mergedIndexDirectory.toFile(), indexLocation.toFile(), true, memberProvider);
mergedCtx.optimize();
if (indexMergerRequest.isPackIndex()) {
IndexPackingRequest request = new //
IndexPackingRequest(//
mergedCtx, //
mergedCtx.acquireIndexSearcher().getIndexReader(), indexLocation.toFile());
indexPacker.packIndex(request);
}
if (indexMergerRequest.isTemporary()) {
temporaryGroupIndexes.add(new TemporaryGroupIndex(mergedIndexDirectory, tempRepoId, groupId, indexMergerRequest.getMergedIndexTtl()));
temporaryContextes.add(mergedCtx);
}
stopWatch.stop();
log.info("merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(), stopWatch.getTime());
return mergedCtx;
} catch (IOException e) {
throw new IndexMergerException(e.getMessage(), e);
} finally {
runningGroups.remove(groupId);
}
}
use of org.apache.maven.index.context.IndexingContext in project archiva by apache.
the class AbstractMavenRepositorySearch method createIndex.
protected void createIndex(String repository, List<Path> filesToBeIndexed, boolean scan, Path indexDir) throws Exception {
Repository rRepo = repositoryRegistry.getRepository(repository);
IndexCreationFeature icf = rRepo.getFeature(IndexCreationFeature.class).get();
IndexingContext context = rRepo.getIndexingContext().getBaseContext(IndexingContext.class);
if (context != null) {
context.close(true);
}
Path repoDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir()).resolve("target").resolve("repos").resolve(repository);
Path indexerDirectory = repoDir.resolve(".indexer");
if (Files.exists(indexerDirectory)) {
FileUtils.deleteDirectory(indexerDirectory);
}
assertFalse(Files.exists(indexerDirectory));
Path lockFile = repoDir.resolve(".indexer/write.lock");
if (Files.exists(lockFile)) {
Files.delete(lockFile);
}
assertFalse(Files.exists(lockFile));
Path repo = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository);
assertTrue(Files.exists(repo));
org.apache.commons.io.FileUtils.copyDirectory(repo.toFile(), repoDir.toFile());
if (indexDir == null) {
Path indexDirectory = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString(System.currentTimeMillis()));
indexDirectory.toFile().deleteOnExit();
FileUtils.deleteDirectory(indexDirectory);
icf.setIndexPath(indexDirectory.toUri());
} else {
icf.setIndexPath(indexDir.toUri());
}
context = rRepo.getIndexingContext().getBaseContext(IndexingContext.class);
// context.getIndexWriter().setRAMBufferSizeMB( 1 );
for (Path artifactFile : filesToBeIndexed) {
assertTrue("file not exists " + artifactFile, Files.exists(artifactFile));
ArtifactContext ac = artifactContextProducer.getArtifactContext(context, artifactFile.toFile());
if (artifactFile.toString().endsWith(".pom")) {
ac.getArtifactInfo().setFileExtension("pom");
ac.getArtifactInfo().setPackaging("pom");
ac.getArtifactInfo().setClassifier("pom");
}
indexer.addArtifactToIndex(ac, context);
context.updateTimestamp(true);
}
if (scan) {
DefaultScannerListener listener = new DefaultScannerListener(context, indexerEngine, true, new ArtifactScanListener());
ScanningRequest req = new ScanningRequest(context, listener);
scanner.scan(req);
context.commit();
}
// force flushing
context.commit();
// context.getIndexWriter().commit();
context.setSearchable(true);
}
use of org.apache.maven.index.context.IndexingContext in project archiva by apache.
the class ArchivaIndexManagerMock method removeArtifactsFromIndex.
@Override
public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<URI> artifactReference) throws IndexUpdateFailedException {
final URI ctxUri = context.getPath();
executeUpdateFunction(context, indexingContext -> {
Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
try {
indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
} catch (IOException e) {
log.error("IOException while removing artifact {}", e.getMessage(), e);
throw new IndexUpdateFailedException("Error occured while removing artifact from index of " + context.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""));
}
});
}
use of org.apache.maven.index.context.IndexingContext in project archiva by apache.
the class ArchivaIndexManagerMock method move.
@Override
public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
if (context == null) {
return null;
}
if (context.supports(IndexingContext.class)) {
try {
Path newPath = getIndexPath(repo);
IndexingContext ctx = context.getBaseContext(IndexingContext.class);
Path oldPath = ctx.getIndexDirectoryFile().toPath();
if (oldPath.equals(newPath)) {
// Nothing to do, if path does not change
return context;
}
if (!Files.exists(oldPath)) {
return createContext(repo);
} else if (context.isEmpty()) {
context.close();
return createContext(repo);
} else {
context.close(false);
Files.move(oldPath, newPath);
return createContext(repo);
}
} catch (IOException e) {
log.error("IOException while moving index directory {}", e.getMessage(), e);
throw new IndexCreationFailedException("Could not recreated the index.", e);
} catch (UnsupportedBaseContextException e) {
throw new IndexCreationFailedException("The given context, is not a maven context.");
}
} else {
throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
}
}
Aggregations