File location = File.createTempFile(indexId, null, getTempDirectory());
DirSupport.delete(location.toPath());
DirSupport.mkdir(location.toPath());
final DefaultIndexingContext temporary = new DefaultIndexingContext(indexId, //
repository.getId(), //
getRepositoryLocalStorageAsFile(repository), // repository local storage
openFSDirectory(location), //
null, // repository url
null, // repository update url
indexCreators, true);
log.debug("Created temporary indexing context " + location + " for repository " + repository.getId());
try {
runnable.run(temporary);
temporary.updateTimestamp(true);
exclusiveSingle(repository, new Runnable()
{
@Override
public void run(IndexingContext target)
throws IOException
{
// TODO igorf guard against concurrent configuration changes
// it is possible that Repository and/or target IndexingContext configuration have changed
// and temporary context is populated based contains old/stale configuration
// need to detect when this happens based on target timestamp for example and skip replace
if (target != null) {
target.replace(temporary.getIndexDirectory());
}
else {
log.warn("Could not perform index operation on repository {}", repository.getId(),
new Exception());
}
}
});
}
finally {
temporary.close(false);
DirSupport.deleteIfExists(location.toPath());
}
}