Filter search results in background thread

This commit is contained in:
Alexey Kulish 2018-03-26 18:35:53 +03:00
parent fd4d24099f
commit 2ebca53e25
3 changed files with 63 additions and 20 deletions

View file

@ -42,6 +42,7 @@ public class SearchUICore {
private static final int TIMEOUT_BETWEEN_CHARS = 700;
private static final int TIMEOUT_BEFORE_SEARCH = 50;
private static final int TIMEOUT_BEFORE_FILTER = 20;
private static final Log LOG = PlatformUtil.getLog(SearchUICore.class);
private SearchPhrase phrase;
private SearchResultCollection currentSearchResult;
@ -364,14 +365,19 @@ public class SearchUICore {
searchSettings = settings;
}
private List<SearchResult> filterCurrentResults(List<SearchResult> rr, SearchPhrase phrase) {
private void filterCurrentResults(SearchPhrase phrase, ResultMatcher<SearchResult> matcher) {
if (matcher == null) {
return;
}
List<SearchResult> l = currentSearchResult.searchResults;
for (SearchResult r : l) {
if (filterOneResult(r, phrase)) {
rr.add(r);
matcher.publish(r);
}
if (matcher.isCancelled()) {
return;
}
}
return rr;
}
private boolean filterOneResult(SearchResult object, SearchPhrase phrase) {
@ -401,14 +407,6 @@ public class SearchUICore {
if (debugMode) {
LOG.info("Prepare search <" + phrase + ">");
}
SearchResultCollection quickRes = new SearchResultCollection(phrase);
if (debugMode) {
LOG.info("Filtering current data <" + phrase + "> Results=" + currentSearchResult.searchResults.size());
}
filterCurrentResults(quickRes.searchResults, phrase);
if (debugMode) {
LOG.info("Current data filtered <" + phrase + "> Results=" + quickRes.searchResults.size());
}
singleThreadedExecutor.submit(new Runnable() {
@Override
@ -417,7 +415,7 @@ public class SearchUICore {
if (onSearchStart != null) {
onSearchStart.run();
}
SearchResultMatcher rm = new SearchResultMatcher(matcher, phrase, request, requestNumber, totalLimit);
final SearchResultMatcher rm = new SearchResultMatcher(matcher, phrase, request, requestNumber, totalLimit);
if (debugMode) {
LOG.info("Starting search <" + phrase.toString() + ">");
}
@ -425,11 +423,13 @@ public class SearchUICore {
if (debugMode) {
LOG.info("Search started <" + phrase.toString() + ">");
}
if (TIMEOUT_BETWEEN_CHARS > 0 && delayedExecution) {
if (delayedExecution) {
long startTime = System.currentTimeMillis();
if (debugMode) {
LOG.info("Wait for next char <" + phrase.toString() + ">");
}
boolean filtered = false;
while (System.currentTimeMillis() - startTime <= TIMEOUT_BETWEEN_CHARS) {
if (rm.isCancelled()) {
if (debugMode) {
@ -437,9 +437,36 @@ public class SearchUICore {
}
return;
}
Thread.sleep(TIMEOUT_BEFORE_SEARCH);
Thread.sleep(TIMEOUT_BEFORE_FILTER);
if (!filtered) {
final SearchResultCollection quickRes = new SearchResultCollection(phrase);
if (debugMode) {
LOG.info("Filtering current data <" + phrase + "> Results=" + currentSearchResult.searchResults.size());
}
filterCurrentResults(phrase, new ResultMatcher<SearchResult>() {
@Override
public boolean publish(SearchResult object) {
quickRes.searchResults.add(object);
return true;
}
@Override
public boolean isCancelled() {
return rm.isCancelled();
}
});
if (debugMode) {
LOG.info("Current data filtered <" + phrase + "> Results=" + quickRes.searchResults.size());
}
if (!rm.isCancelled()) {
currentSearchResult = quickRes;
rm.filterFinished(phrase);
}
filtered = true;
}
}
} else if (TIMEOUT_BEFORE_SEARCH > 0) {
} else {
Thread.sleep(TIMEOUT_BEFORE_SEARCH);
}
if (rm.isCancelled()) {
@ -477,7 +504,7 @@ public class SearchUICore {
}
}
});
return quickRes;
return null;
}
@ -598,6 +625,14 @@ public class SearchUICore {
}
}
public void filterFinished(SearchPhrase phrase) {
if (matcher != null) {
SearchResult sr = new SearchResult(phrase);
sr.objectType = ObjectType.FILTER_FINISHED;
matcher.publish(sr);
}
}
public void searchFinished(SearchPhrase phrase) {
if (matcher != null) {
SearchResult sr = new SearchResult(phrase);

View file

@ -16,6 +16,7 @@ public enum ObjectType {
REGION(true),
SEARCH_STARTED(false),
FILTER_FINISHED(false),
SEARCH_FINISHED(false),
SEARCH_API_FINISHED(false),
SEARCH_API_REGION_FINISHED(false),

View file

@ -1530,7 +1530,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
private void runCoreSearchInternal(String text, boolean showQuickResult, boolean searchMore,
final SearchResultListener resultListener) {
SearchResultCollection c = searchUICore.search(text, showQuickResult, new ResultMatcher<SearchResult>() {
searchUICore.search(text, showQuickResult, new ResultMatcher<SearchResult>() {
SearchResultCollection regionResultCollection = null;
SearchCoreAPI regionResultApi = null;
List<SearchResult> results = new ArrayList<>();
@ -1580,6 +1580,16 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
}
});
break;
case FILTER_FINISHED:
if (resultListener != null) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
resultListener.publish(searchUICore.getCurrentSearchResult(), false);
}
});
}
break;
case SEARCH_API_FINISHED:
final SearchCoreAPI searchApi = (SearchCoreAPI) object.object;
final List<SearchResult> apiResults;
@ -1627,9 +1637,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
updateSearchResult(null, false);
}
}
if (showQuickResult) {
updateSearchResult(c, false);
}
}
private void showLocationToolbar() {