protected IStatus run(IProgressMonitor monitor) {
Logger logger = LoggerFactory.getLogger(Indexer.class);
if (logger.isDebugEnabled())
logger.debug("Purging indexes"); //$NON-NLS-1$
long start = System.currentTimeMillis();
FileLocker lock = new FileLocker(lockFile);
SolrQuery query = findAllQuery();
try {
if (!lock.tryLock()) {
if (logger.isInfoEnabled()) {
logger.info("Search index purge: another process is currently running");
}
schedule(DEFAULT_DELAY * 2);
return Status.OK_STATUS;
}
QueryResponse solrResponse = this.server.query(query);
SolrDocumentList result = solrResponse.getResults();
long numFound = result.getNumFound();
long processed = 0;
List<String> listIds = new ArrayList<String>();
if (numFound > processed) {
while (true) {
checkCanceled(monitor);
markStaleIndexes(result, listIds);
processed += PAGE_SIZE;
if (processed >= numFound)
break;
query.setParam(CommonParams.START, Long.toString(processed));
solrResponse = this.server.query(query);
result = solrResponse.getResults();
// New indexes may have been added, perhaps
numFound = result.getNumFound();
}
}
checkCanceled(monitor);
if (listIds.size() > 0) {
this.server.deleteById(listIds);
this.server.commit();
}
if (logger.isDebugEnabled())
logger.debug("\tPurged: " + listIds.size()); //$NON-NLS-1$
} catch (OperationCanceledException e) {
//ignore and fall through
} catch (Exception e) {
handleIndexingFailure(e);
} finally {
if (lock.isValid()) {
lock.release();
}
}
long duration = System.currentTimeMillis() - start;
if (logger.isDebugEnabled())
logger.debug("Purge job took " + duration + "ms"); //$NON-NLS-1$ //$NON-NLS-2$