From 81a30ff445045ce6baf99bd0a3ebf3188bc7b431 Mon Sep 17 00:00:00 2001 From: Loay Ghreeb Date: Wed, 11 Sep 2024 15:57:41 +0300 Subject: [PATCH] Add `waitOnClose` to the LuceneManager constructor --- src/main/java/org/jabref/gui/LibraryTab.java | 2 +- .../org/jabref/logic/search/DatabaseSearcher.java | 6 +++--- .../org/jabref/logic/search/LuceneManager.java | 15 ++++----------- .../logic/search/indexing/BibFieldsIndexer.java | 15 ++++++++------- .../indexing/DefaultLinkedFilesIndexer.java | 15 ++++++++------- .../indexing/ReadOnlyLinkedFilesIndexer.java | 15 ++++++++------- .../org/jabref/model/search/LuceneIndexer.java | 2 -- 7 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index d17a0a1871e..76a1cb1547a 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -316,7 +316,7 @@ private void onDatabaseLoadingSucceed(ParserResult result) { } public void createLuceneManager() { - luceneManager = new LuceneManager(bibDatabaseContext, taskExecutor, preferencesService.getFilePreferences()); + luceneManager = new LuceneManager(bibDatabaseContext, taskExecutor, preferencesService.getFilePreferences(), false); stateManager.setLuceneManager(bibDatabaseContext, luceneManager); } diff --git a/src/main/java/org/jabref/logic/search/DatabaseSearcher.java b/src/main/java/org/jabref/logic/search/DatabaseSearcher.java index 6ca9e070770..c3587501236 100644 --- a/src/main/java/org/jabref/logic/search/DatabaseSearcher.java +++ b/src/main/java/org/jabref/logic/search/DatabaseSearcher.java @@ -26,7 +26,7 @@ public class DatabaseSearcher { public DatabaseSearcher(SearchQuery query, BibDatabaseContext databaseContext, TaskExecutor taskExecutor, FilePreferences filePreferences) throws IOException { this.databaseContext = databaseContext; this.query = Objects.requireNonNull(query); - this.luceneManager = new LuceneManager(databaseContext, taskExecutor, filePreferences); + this.luceneManager = new LuceneManager(databaseContext, taskExecutor, filePreferences, true); } /** @@ -37,7 +37,7 @@ public List getMatches() { if (!query.isValid()) { LOGGER.warn("Search failed: invalid search expression"); - luceneManager.closeAndWait(); + luceneManager.close(); return Collections.emptyList(); } List matchEntries = luceneManager.search(query) @@ -45,7 +45,7 @@ public List getMatches() { .stream() .map(entryId -> databaseContext.getDatabase().getEntryById(entryId)) .toList(); - luceneManager.closeAndWait(); + luceneManager.close(); return BibDatabases.purgeEmptyEntries(matchEntries); } } diff --git a/src/main/java/org/jabref/logic/search/LuceneManager.java b/src/main/java/org/jabref/logic/search/LuceneManager.java index f5ab561e503..0eb1492654c 100644 --- a/src/main/java/org/jabref/logic/search/LuceneManager.java +++ b/src/main/java/org/jabref/logic/search/LuceneManager.java @@ -39,21 +39,21 @@ public class LuceneManager { private final LuceneIndexer linkedFilesIndexer; private final LuceneSearcher luceneSearcher; - public LuceneManager(BibDatabaseContext databaseContext, TaskExecutor executor, FilePreferences preferences) { + public LuceneManager(BibDatabaseContext databaseContext, TaskExecutor executor, FilePreferences preferences, boolean waitOnClose) { this.taskExecutor = executor; this.databaseContext = databaseContext; this.shouldIndexLinkedFiles = preferences.fulltextIndexLinkedFilesProperty(); this.preferencesListener = (observable, oldValue, newValue) -> bindToPreferences(newValue); this.shouldIndexLinkedFiles.addListener(preferencesListener); - this.bibFieldsIndexer = new BibFieldsIndexer(databaseContext); + this.bibFieldsIndexer = new BibFieldsIndexer(databaseContext, waitOnClose); LuceneIndexer indexer; try { - indexer = new DefaultLinkedFilesIndexer(databaseContext, preferences); + indexer = new DefaultLinkedFilesIndexer(databaseContext, preferences, waitOnClose); } catch (IOException e) { LOGGER.debug("Error initializing linked files index - using read only index"); - indexer = new ReadOnlyLinkedFilesIndexer(databaseContext); + indexer = new ReadOnlyLinkedFilesIndexer(databaseContext, waitOnClose); } linkedFilesIndexer = indexer; @@ -210,13 +210,6 @@ public void close() { databaseContext.getDatabase().postEvent(new IndexClosedEvent()); } - public void closeAndWait() { - bibFieldsIndexer.closeAndWait(); - shouldIndexLinkedFiles.removeListener(preferencesListener); - linkedFilesIndexer.closeAndWait(); - databaseContext.getDatabase().postEvent(new IndexClosedEvent()); - } - public AutoCloseable blockLinkedFileIndexer() { LOGGER.debug("Blocking linked files indexer"); isLinkedFilesIndexerBlocked.set(true); diff --git a/src/main/java/org/jabref/logic/search/indexing/BibFieldsIndexer.java b/src/main/java/org/jabref/logic/search/indexing/BibFieldsIndexer.java index 7811859be3f..ddfb5ab7966 100644 --- a/src/main/java/org/jabref/logic/search/indexing/BibFieldsIndexer.java +++ b/src/main/java/org/jabref/logic/search/indexing/BibFieldsIndexer.java @@ -29,13 +29,15 @@ public class BibFieldsIndexer implements LuceneIndexer { private static final Logger LOGGER = LoggerFactory.getLogger(BibFieldsIndexer.class); private final BibDatabaseContext databaseContext; + private final boolean waitOnClose; private final String libraryName; private final Directory indexDirectory; private IndexWriter indexWriter; private SearcherManager searcherManager; - public BibFieldsIndexer(BibDatabaseContext databaseContext) { + public BibFieldsIndexer(BibDatabaseContext databaseContext, boolean waitOnClose) { this.databaseContext = databaseContext; + this.waitOnClose = waitOnClose; this.libraryName = databaseContext.getDatabasePath().map(path -> path.getFileName().toString()).orElseGet(() -> "unsaved"); IndexWriterConfig config = new IndexWriterConfig(SearchFieldConstants.LATEX_AWARE_NGRAM_ANALYZER); @@ -153,12 +155,11 @@ public SearcherManager getSearcherManager() { @Override public void close() { - HeadlessExecutorService.INSTANCE.execute(this::closeIndex); - } - - @Override - public void closeAndWait() { - HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex); + if (waitOnClose) { + HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex); + } else { + HeadlessExecutorService.INSTANCE.execute(this::closeIndex); + } } private void closeIndex() { diff --git a/src/main/java/org/jabref/logic/search/indexing/DefaultLinkedFilesIndexer.java b/src/main/java/org/jabref/logic/search/indexing/DefaultLinkedFilesIndexer.java index f7dbfb21614..0d1312e0cb5 100644 --- a/src/main/java/org/jabref/logic/search/indexing/DefaultLinkedFilesIndexer.java +++ b/src/main/java/org/jabref/logic/search/indexing/DefaultLinkedFilesIndexer.java @@ -51,6 +51,7 @@ public class DefaultLinkedFilesIndexer implements LuceneIndexer { private final BibDatabaseContext databaseContext; private final FilePreferences filePreferences; + private final boolean waitOnClose; private final String libraryName; private final Directory indexDirectory; private final IndexWriter indexWriter; @@ -58,9 +59,10 @@ public class DefaultLinkedFilesIndexer implements LuceneIndexer { private Path indexDirectoryPath; private Map indexedFiles; - public DefaultLinkedFilesIndexer(BibDatabaseContext databaseContext, FilePreferences filePreferences) throws IOException { + public DefaultLinkedFilesIndexer(BibDatabaseContext databaseContext, FilePreferences filePreferences, boolean waitOnClose) throws IOException { this.databaseContext = databaseContext; this.filePreferences = filePreferences; + this.waitOnClose = waitOnClose; this.libraryName = databaseContext.getDatabasePath().map(path -> path.getFileName().toString()).orElseGet(() -> "untitled"); this.indexedFiles = new ConcurrentHashMap<>(); @@ -312,12 +314,11 @@ public SearcherManager getSearcherManager() { @Override public void close() { - HeadlessExecutorService.INSTANCE.execute(this::closeIndex); - } - - @Override - public void closeAndWait() { - HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex); + if (waitOnClose) { + HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex); + } else { + HeadlessExecutorService.INSTANCE.execute(this::closeIndex); + } } private void closeIndex() { diff --git a/src/main/java/org/jabref/logic/search/indexing/ReadOnlyLinkedFilesIndexer.java b/src/main/java/org/jabref/logic/search/indexing/ReadOnlyLinkedFilesIndexer.java index aa94960d52a..fd77bcae91a 100644 --- a/src/main/java/org/jabref/logic/search/indexing/ReadOnlyLinkedFilesIndexer.java +++ b/src/main/java/org/jabref/logic/search/indexing/ReadOnlyLinkedFilesIndexer.java @@ -17,10 +17,12 @@ public class ReadOnlyLinkedFilesIndexer implements LuceneIndexer { private static final Logger LOGGER = LoggerFactory.getLogger(ReadOnlyLinkedFilesIndexer.class); + private final boolean waitOnClose; private Directory indexDirectory; private SearcherManager searcherManager; - public ReadOnlyLinkedFilesIndexer(BibDatabaseContext databaseContext) { + public ReadOnlyLinkedFilesIndexer(BibDatabaseContext databaseContext, boolean waitOnClose) { + this.waitOnClose = waitOnClose; try { indexDirectory = FSDirectory.open(databaseContext.getFulltextIndexPath()); searcherManager = new SearcherManager(indexDirectory, null); @@ -60,12 +62,11 @@ public SearcherManager getSearcherManager() { @Override public void close() { - HeadlessExecutorService.INSTANCE.execute(this::closeIndex); - } - - @Override - public void closeAndWait() { - HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex); + if (waitOnClose) { + HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex); + } else { + HeadlessExecutorService.INSTANCE.execute(this::closeIndex); + } } private void closeIndex() { diff --git a/src/main/java/org/jabref/model/search/LuceneIndexer.java b/src/main/java/org/jabref/model/search/LuceneIndexer.java index 2b71016e935..1f0dfbd6edb 100644 --- a/src/main/java/org/jabref/model/search/LuceneIndexer.java +++ b/src/main/java/org/jabref/model/search/LuceneIndexer.java @@ -23,6 +23,4 @@ public interface LuceneIndexer { SearcherManager getSearcherManager(); void close(); - - void closeAndWait(); }