Skip to content

Commit

Permalink
Searching in background task
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb committed Oct 5, 2024
1 parent c127aaf commit b2c5428
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ public AbstractGroup resultConverter(ButtonType button) {
Optional<IndexManager> indexManager = stateManager.getIndexManager(currentDatabase);
if (indexManager.isPresent()) {
SearchGroup searchGroup = (SearchGroup) resultingGroup;
// TODO: search should be done in a background thread
searchGroup.setMatchedEntries(indexManager.get().search(searchGroup.getSearchQuery()).getMatchedEntries());
}
} else if (typeAutoProperty.getValue()) {
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,9 @@ public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager state
databaseContext.getMetaData().groupsBinding().addListener(new WeakInvalidationListener(onInvalidatedGroup));
} else if (groupNode.getGroup() instanceof SearchGroup searchGroup) {
stateManager.getIndexManager(databaseContext).ifPresent(indexManager -> {
BackgroundTask.wrap(() -> {
searchGroup.setMatchedEntries(indexManager.search(searchGroup.getSearchQuery()).getMatchedEntries());
}).onSuccess(success -> {
refreshGroup();
databaseContext.getMetaData().groupsBinding().invalidate();
}).executeWith(taskExecutor);
searchGroup.setMatchedEntries(indexManager.search(searchGroup.getSearchQuery()).getMatchedEntries());
refreshGroup();
databaseContext.getMetaData().groupsBinding().invalidate();
});
}

Expand Down Expand Up @@ -541,12 +538,9 @@ class SearchIndexListener {
public void listen(IndexStartedEvent event) {
if (groupNode.getGroup() instanceof SearchGroup searchGroup) {
stateManager.getIndexManager(databaseContext).ifPresent(indexManager -> {
BackgroundTask.wrap(() -> {
searchGroup.setMatchedEntries(indexManager.search(searchGroup.getSearchQuery()).getMatchedEntries());
}).onSuccess(success -> {
refreshGroup();
databaseContext.getMetaData().groupsBinding().invalidate();
}).executeWith(taskExecutor);
searchGroup.setMatchedEntries(indexManager.search(searchGroup.getSearchQuery()).getMatchedEntries());
refreshGroup();
databaseContext.getMetaData().groupsBinding().invalidate();
});
}
}
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/org/jabref/logic/search/IndexManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.jabref.logic.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
Expand All @@ -14,6 +18,7 @@
import org.jabref.logic.search.retrieval.BibFieldsSearcher;
import org.jabref.logic.search.retrieval.LinkedFilesSearcher;
import org.jabref.logic.util.BackgroundTask;
import org.jabref.logic.util.HeadlessExecutorService;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -221,11 +226,22 @@ public AutoCloseable blockLinkedFileIndexer() {
}

public SearchResults search(SearchQuery query) {
SearchResults searchResults = new SearchResults();
searchResults.mergeSearchResults(bibFieldsSearcher.search(query));
List<Callable<SearchResults>> tasks = new ArrayList<>();
tasks.add(() -> bibFieldsSearcher.search(query));

if (query.getSearchFlags().contains(SearchFlags.FULLTEXT)) {
searchResults.mergeSearchResults(linkedFilesSearcher.search(query));
tasks.add(() -> linkedFilesSearcher.search(query));
}

List<Future<SearchResults>> futures = HeadlessExecutorService.INSTANCE.executeAll(tasks);

SearchResults searchResults = new SearchResults();
for (Future<SearchResults> future : futures) {
try {
searchResults.mergeSearchResults(future.get());
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("Error while searching", e);
}
}
query.setSearchResults(searchResults);
return searchResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jabref.logic.search.query.SearchQueryConversion;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.search.query.SearchQuery;
import org.jabref.model.search.query.SearchResult;
Expand Down Expand Up @@ -37,11 +38,10 @@ private static SearchQuery createBooleanQueryForEntry(BibEntry entry, SearchQuer
}

public SearchResults search(SearchQuery searchQuery) {
String sqlQuery = searchQuery.getSqlQuery(tableName);
if (!searchQuery.isValid()) {
LOGGER.error("Invalid SQL query: {}", sqlQuery);
return new SearchResults();
}
String sqlQuery = SearchQueryConversion.searchToSql(tableName, searchQuery.getSearchExpression());
LOGGER.debug("Searching in bib fields with query: {}", sqlQuery);
SearchResults searchResults = new SearchResults();
try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.jabref.logic.FilePreferences;
import org.jabref.logic.search.LuceneIndexer;
import org.jabref.logic.search.query.SearchQueryConversion;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
Expand Down Expand Up @@ -46,10 +47,14 @@ public LinkedFilesSearcher(BibDatabaseContext databaseContext, LuceneIndexer lin
}

public SearchResults search(SearchQuery searchQuery) {
Query luceneQuery = searchQuery.getLuceneQuery();
if (!searchQuery.isValid()) {
return new SearchResults();
}

Query luceneQuery = SearchQueryConversion.searchToLucene(searchQuery.getSearchExpression());
EnumSet<SearchFlags> searchFlags = searchQuery.getSearchFlags();
boolean shouldSearchInLinkedFiles = searchFlags.contains(SearchFlags.FULLTEXT) && filePreferences.shouldFulltextIndexLinkedFiles();
if (!shouldSearchInLinkedFiles || !searchQuery.isValid()) {
if (!shouldSearchInLinkedFiles) {
return new SearchResults();
}

Expand Down
17 changes: 0 additions & 17 deletions src/main/java/org/jabref/model/search/query/SearchQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
import org.jabref.model.search.SearchFlags;

import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.apache.lucene.search.Query;

public class SearchQuery {

private final String searchExpression;
private final EnumSet<SearchFlags> searchFlags;

private boolean isValidExpression;
private String sqlQuery;
private Query luceneQuery;
private SearchResults searchResults;

public SearchQuery(String searchExpression, EnumSet<SearchFlags> searchFlags) {
Expand All @@ -30,20 +27,6 @@ public SearchQuery(String searchExpression, EnumSet<SearchFlags> searchFlags) {
}
}

public String getSqlQuery(String table) {
if (sqlQuery == null) {
sqlQuery = SearchQueryConversion.searchToSql(table, searchExpression);
}
return sqlQuery;
}

public Query getLuceneQuery() {
if (luceneQuery == null) {
luceneQuery = SearchQueryConversion.searchToLucene(searchExpression);
}
return luceneQuery;
}

public String getSearchExpression() {
return searchExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.jabref.model.entry.BibEntry;

public class SearchResults {

private final Map<String, List<SearchResult>> searchResults = new HashMap<>();
private final Map<String, List<SearchResult>> searchResults = new ConcurrentHashMap<>();

public void mergeSearchResults(SearchResults additionalResults) {
this.searchResults.putAll(additionalResults.searchResults);
Expand Down

0 comments on commit b2c5428

Please sign in to comment.