1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-15 10:46:26 +00:00

create a single index writer for Lucene + Closes #231

This commit is contained in:
Benjamin Gamard
2018-04-11 12:38:03 +02:00
parent b265b8b1e0
commit 748659e78e
5 changed files with 28 additions and 70 deletions

View File

@@ -62,6 +62,11 @@ public class LuceneIndexingHandler implements IndexingHandler {
*/
private DirectoryReader directoryReader;
/**
* Index writer.
*/
private IndexWriter indexWriter;
@Override
public boolean accept() {
// Embedded Lucene can always start
@@ -84,6 +89,12 @@ public class LuceneIndexingHandler implements IndexingHandler {
directory = new SimpleFSDirectory(luceneDirectory, NoLockFactory.INSTANCE);
}
// Create an index writer
IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
config.setCommitOnClose(true);
config.setMergeScheduler(new SerialMergeScheduler());
indexWriter = new IndexWriter(directory, config);
// Check index version and rebuild it if necessary
if (DirectoryReader.indexExists(directory)) {
log.info("Checking index health and version");
@@ -111,6 +122,13 @@ public class LuceneIndexingHandler implements IndexingHandler {
log.error("Error closing the index reader", e);
}
}
if (indexWriter != null) {
try {
indexWriter.close();
} catch (IOException e) {
log.error("Error closing the index writer, index may be corrupt", e);
}
}
if (directory != null) {
try {
directory.close();
@@ -469,42 +487,16 @@ public class LuceneIndexingHandler implements IndexingHandler {
* @param runnable Runnable
*/
private void handle(LuceneRunnable runnable) {
// Standard analyzer
IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
// Automatically commit when closing this writer
config.setCommitOnClose(true);
// Merge sequentially, because Lucene writing is already done asynchronously
config.setMergeScheduler(new SerialMergeScheduler());
// Creating index writer
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, config);
} catch (IOException e) {
log.error("Cannot create IndexWriter", e);
}
try {
runnable.run(indexWriter);
} catch (Exception e) {
log.error("Error in running index writing transaction", e);
try {
if (indexWriter != null) {
indexWriter.rollback();
}
} catch (IOException e1) {
log.error("Cannot rollback index writing transaction", e1);
}
log.error("Error in running index writing", e);
}
try {
if (indexWriter != null) {
indexWriter.close();
}
indexWriter.commit();
} catch (IOException e) {
log.error("Cannot commit and close IndexWriter", e);
log.error("Cannot commit index writer", e);
}
}