Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix search test NPE #11749

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion 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,6 +37,7 @@ public List<BibEntry> getMatches() {

if (!query.isValid()) {
LOGGER.warn("Search failed: invalid search expression");
luceneManager.close();
return Collections.emptyList();
}
List<BibEntry> matchEntries = luceneManager.search(query)
Expand Down
8 changes: 4 additions & 4 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
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,7 +155,11 @@ public SearcherManager getSearcherManager() {

@Override
public void close() {
HeadlessExecutorService.INSTANCE.execute(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,7 +314,11 @@ public SearcherManager getSearcherManager() {

@Override
public void close() {
HeadlessExecutorService.INSTANCE.execute(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,7 +62,11 @@ public SearcherManager getSearcherManager() {

@Override
public void close() {
HeadlessExecutorService.INSTANCE.execute(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
@@ -1,6 +1,7 @@
package org.jabref.logic.search;

import java.io.IOException;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -18,24 +19,29 @@
import org.jabref.preferences.FilePreferences;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

public class DatabaseSearcherTest {
private static final TaskExecutor TASK_EXECUTOR = new CurrentThreadTaskExecutor();
private BibDatabaseContext databaseContext;
private final FilePreferences filePreferences = mock(FilePreferences.class);
@TempDir
private Path indexDir;

@BeforeEach
void setUp() {
when(filePreferences.shouldFulltextIndexLinkedFiles()).thenReturn(false);
when(filePreferences.fulltextIndexLinkedFilesProperty()).thenReturn(mock(BooleanProperty.class));
databaseContext = new BibDatabaseContext();
databaseContext = spy(new BibDatabaseContext());
when(databaseContext.getFulltextIndexPath()).thenReturn(indexDir);
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

class DatabaseSearcherWithBibFilesTest {
Expand Down Expand Up @@ -77,7 +78,8 @@ private BibDatabaseContext initializeDatabaseFromPath(String testFile) throws Ex

private BibDatabaseContext initializeDatabaseFromPath(Path testFile) throws Exception {
ParserResult result = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), new DummyFileUpdateMonitor()).importDatabase(testFile);
BibDatabaseContext databaseContext = result.getDatabaseContext();
BibDatabaseContext databaseContext = spy(result.getDatabaseContext());
when(databaseContext.getFulltextIndexPath()).thenReturn(indexDir);

when(filePreferences.shouldFulltextIndexLinkedFiles()).thenReturn(true);
when(filePreferences.fulltextIndexLinkedFilesProperty()).thenReturn(new SimpleBooleanProperty(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void setUp(@TempDir Path indexDir) throws IOException {
when(context.getFileDirectories(Mockito.any())).thenReturn(Collections.singletonList(Path.of("src/test/resources/pdfs")));
when(context.getFulltextIndexPath()).thenReturn(indexDir);

this.indexer = new DefaultLinkedFilesIndexer(context, filePreferences);
this.indexer = new DefaultLinkedFilesIndexer(context, filePreferences, true);
}

@Test
Expand Down