From d4d1c3526449ed7bd5594f363222f02ad7dde68a Mon Sep 17 00:00:00 2001 From: Benjamin Gamard Date: Tue, 5 Feb 2019 15:37:46 +0100 Subject: [PATCH] Closes #260: Re-index all if the Lucene directory is unusable --- .../docs/core/model/context/AppContext.java | 4 +--- .../util/indexing/LuceneIndexingHandler.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java b/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java index 9cc433a3..0b191b82 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java +++ b/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java @@ -89,9 +89,7 @@ public class AppContext { } indexingHandler.startUp(); } catch (Exception e) { - log.error("Error starting the indexing handler, rebuilding the index: " + e.getMessage()); - RebuildIndexAsyncEvent rebuildIndexAsyncEvent = new RebuildIndexAsyncEvent(); - asyncEventBus.post(rebuildIndexAsyncEvent); + log.error("Error starting the indexing handler", e); } // Start file service diff --git a/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java b/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java index 948e18fb..178a9f96 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java +++ b/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java @@ -9,6 +9,8 @@ import com.sismics.docs.core.constant.PermType; import com.sismics.docs.core.dao.ConfigDao; import com.sismics.docs.core.dao.criteria.DocumentCriteria; import com.sismics.docs.core.dao.dto.DocumentDto; +import com.sismics.docs.core.event.RebuildIndexAsyncEvent; +import com.sismics.docs.core.model.context.AppContext; import com.sismics.docs.core.model.jpa.Config; import com.sismics.docs.core.model.jpa.Document; import com.sismics.docs.core.model.jpa.File; @@ -42,7 +44,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.sql.Timestamp; import java.util.*; @@ -83,6 +87,25 @@ public class LuceneIndexingHandler implements IndexingHandler { @Override public void startUp() throws Exception { + try { + initLucene(); + } catch (Exception e) { + // An error occurred initializing Lucene, the index is out of date or broken, delete everything + log.info("Unable to initialize Lucene, cleaning up the index: " + e.getMessage()); + Path luceneDirectory = DirectoryUtil.getLuceneDirectory(); + Files.walk(luceneDirectory) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(java.io.File::delete); + + // Re-initialize and schedule a full reindex + initLucene(); + RebuildIndexAsyncEvent rebuildIndexAsyncEvent = new RebuildIndexAsyncEvent(); + AppContext.getInstance().getAsyncEventBus().post(rebuildIndexAsyncEvent); + } + } + + private void initLucene() throws Exception { ConfigDao configDao = new ConfigDao(); Config luceneStorageConfig = configDao.getById(ConfigType.LUCENE_DIRECTORY_STORAGE); String luceneStorage = luceneStorageConfig == null ? null : luceneStorageConfig.getValue();