}
public void merge() throws IndexException {
try {
long bufferSize = uriToDocumentMap.size();
SeparateOccurrencesContextSearcher searcher = new SeparateOccurrencesContextSearcher(this.mLucene);
LOG.info("Merging "+bufferSize+" resources in memory with " +searcher.getNumberOfEntries()+" resources in disk.");
int numUpdatedDocs = 0;
for(String uri: uriToDocumentMap.keySet()) {
// Get document from buffer
Document docForResource = uriToDocumentMap.get(uri);
// Merge with documents from disk if there are any
List<Document> occurrences = searcher.getOccurrences(new DBpediaResource(uri));
for (Document occurrenceDoc: occurrences) {
docForResource = mLucene.merge(occurrenceDoc, docForResource); // adds occurrence to resource
}
// If no merges were needed, add document
if (occurrences.size() == 0) {
mWriter.addDocument(docForResource);
} else { // Otherwise, update existing document.
numUpdatedDocs++;
Term uriTerm = new Term(LuceneManager.DBpediaResourceField.URI.toString(), uri);
mWriter.updateDocument(uriTerm, docForResource); //deletes everything with this uri and writes a new doc
}
}
double percent = 0.0;
if (numUpdatedDocs * bufferSize > 0) // Account for when either of these numbers is zero.
percent = new Double(numUpdatedDocs)/bufferSize;
LOG.info(String.format("Merge done (%s) resources merged).",percent));
numMerges++;
searcher.close();
} catch (IOException e) {
throw new IndexException(e);
} catch (SearchException e) {
throw new IndexException(e);
}