Skip to content

Commit

Permalink
Add waitOnClose to the LuceneManager constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb committed Sep 11, 2024
1 parent ea7a373 commit 81a30ff
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/logic/search/DatabaseSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -37,15 +37,15 @@ public List<BibEntry> getMatches() {

if (!query.isValid()) {
LOGGER.warn("Search failed: invalid search expression");
luceneManager.closeAndWait();
luceneManager.close();
return Collections.emptyList();
}
List<BibEntry> matchEntries = luceneManager.search(query)
.getMatchedEntries()
.stream()
.map(entryId -> databaseContext.getDatabase().getEntryById(entryId))
.toList();
luceneManager.closeAndWait();
luceneManager.close();
return BibDatabases.purgeEmptyEntries(matchEntries);
}
}
15 changes: 4 additions & 11 deletions src/main/java/org/jabref/logic/search/LuceneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ 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;
private final SearcherManager searcherManager;
private Path indexDirectoryPath;
private Map<String, Long> 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<>();

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/jabref/model/search/LuceneIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ public interface LuceneIndexer {
SearcherManager getSearcherManager();

void close();

void closeAndWait();
}

0 comments on commit 81a30ff

Please sign in to comment.