Index: client/lib/search.dart |
diff --git a/client/lib/search.dart b/client/lib/search.dart |
index dd300dde9a7802f24826c3d49ec955d2c6ba1246..08bf642fbd4f753689ed6e4fda01490d50cb7fe5 100644 |
--- a/client/lib/search.dart |
+++ b/client/lib/search.dart |
@@ -10,9 +10,15 @@ |
*/ |
library search; |
+import 'dart:async'; |
+import 'package:web_ui/web_ui.dart'; |
+ |
/** Search Index */ |
Map<String, String> index = {}; |
+/** Search query. */ |
+@observable String searchQuery = ""; |
+ |
class SearchResult implements Comparable { |
/** Qualified name of this search result references. */ |
@@ -55,15 +61,22 @@ Map<String, int> value = { |
* A score is given to each potential search result based off how likely it is |
* the appropriate qualified name to return for the search query. |
*/ |
-List<SearchResult> lookupSearchResults(String searchQuery, int maxResults) { |
- |
+List<SearchResult> lookupSearchResults(String query, int maxResults) { |
+ |
+ var stopwatch = new Stopwatch()..start(); |
+ |
var scoredResults = <SearchResult>[]; |
var resultSet = new Set<String>(); |
- var queryList = searchQuery.trim().toLowerCase().split(' '); |
+ var queryList = query.trim().toLowerCase().split(' '); |
queryList.forEach((q) => resultSet.addAll(index.keys.where((e) => |
e.toLowerCase().contains(q)))); |
- for (var r in resultSet) { |
+ for (var r in resultSet) { |
+ /// If it is taking too long to compute the search results, time out and |
+ /// return an empty list of results. |
+ if (stopwatch.elapsedMilliseconds > 500) { |
+ return []; |
+ } |
int score = 0; |
var lowerCaseResult = r.toLowerCase(); |
var type = index[r]; |
@@ -123,6 +136,11 @@ List<SearchResult> lookupSearchResults(String searchQuery, int maxResults) { |
scoredResults.add(new SearchResult(r, type, score)); |
} |
+ /// If it is taking too long to compute the search results, time out and |
+ /// return an empty list of results. |
+ if (stopwatch.elapsedMilliseconds > 500) { |
+ return []; |
+ } |
scoredResults.sort(); |
updatePositions(scoredResults); |
if (scoredResults.length > maxResults) { |
@@ -136,4 +154,4 @@ void updatePositions(List<SearchResult> list) { |
for(int i = 0; i < list.length; i++) { |
list[i].position = i; |
} |
-} |
+} |