data: increased autocomplete performance by caching and limiting results to 100

This commit is contained in:
Eberhard Graether
2015-10-27 14:22:45 +01:00
parent cbb6fcd8e2
commit c7a2533dc4
12 changed files with 76 additions and 40 deletions
+7 -2
View File
@@ -822,7 +822,7 @@ void QtSmartSearchBox::clearLineEdit()
void QtSmartSearchBox::requestAutoCompletions() const
{
MessageSearchAutocomplete("", text().toStdString()).dispatch();
MessageSearchAutocomplete(text().toStdString()).dispatch();
}
void QtSmartSearchBox::hideAutoCompletions()
@@ -832,5 +832,10 @@ void QtSmartSearchBox::hideAutoCompletions()
std::deque<SearchMatch> QtSmartSearchBox::getMatchesForInput(const std::string& text) const
{
return std::deque<SearchMatch>(1, SearchMatch(text));
std::deque<SearchMatch> matches;
if (text.size())
{
matches.push_back(SearchMatch(text));
}
return matches;
}
@@ -27,8 +27,8 @@ void SearchController::handleMessage(MessageFind* message)
void SearchController::handleMessage(MessageSearchAutocomplete* message)
{
LOG_INFO("autocomplete string: \"" + message->word + "\"");
getView()->setAutocompletionList(m_storageAccess->getAutocompletionMatches(message->query, message->word));
LOG_INFO("autocomplete string: \"" + message->query + "\"");
getView()->setAutocompletionList(m_storageAccess->getAutocompletionMatches(message->query));
}
SearchView* SearchController::getView()
+19 -5
View File
@@ -635,7 +635,7 @@ Id Storage::onTemplateMemberFunctionSpecializationParsed(
Id specializedFunctionNodeId = addNodeHierarchyWithDistinctSignature(Node::NODE_FUNCTION, specializedFunction, false);
Id edgeId = addEdge(instantiatedFunctionNodeId, specializedFunctionNodeId, Edge::EDGE_TEMPLATE_MEMBER_SPECIALIZATION_OF, location);
return edgeId;
}
@@ -770,12 +770,26 @@ Node::NodeType Storage::getNodeTypeForNodeWithId(Id nodeId) const
return Node::intToType(m_sqliteStorage.getNodeById(nodeId).type);
}
std::vector<SearchMatch> Storage::getAutocompletionMatches(const std::string& query, const std::string& word) const
std::vector<SearchMatch> Storage::getAutocompletionMatches(const std::string& query) const
{
SearchResults tokenResults = m_tokenIndex.runFuzzySearch(word);
if (query.size() == m_cachedQuery.size() + 1 && query.find(m_cachedQuery) == 0 && m_cachedResults.size())
{
m_cachedResults = m_tokenIndex.runFuzzySearchCached(query, m_cachedResults);
}
else
{
m_cachedResults = m_tokenIndex.runFuzzySearch(query);
}
std::vector<SearchMatch> matches = SearchIndex::getMatches(tokenResults, word);
SearchMatch::log(matches, word);
m_cachedQuery = query;
std::vector<SearchMatch> matches = SearchIndex::getMatches(m_cachedResults, query);
SearchMatch::log(matches, query);
if (matches.size() > 100)
{
matches.resize(100);
}
for (SearchMatch& match : matches)
{
+4 -2
View File
@@ -134,8 +134,7 @@ public:
virtual NameHierarchy getNameHierarchyForNodeWithId(Id nodeId) const;
virtual Node::NodeType getNodeTypeForNodeWithId(Id nodeId) const;
virtual std::vector<SearchMatch> getAutocompletionMatches(
const std::string& query, const std::string& word) const;
virtual std::vector<SearchMatch> getAutocompletionMatches(const std::string& query) const;
virtual std::vector<SearchMatch> getSearchMatchesForTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
@@ -205,6 +204,9 @@ private:
TokenLocationCollection m_errorLocationCollection;
std::vector<std::string> m_errorMessages;
mutable SearchResults m_cachedResults;
mutable std::string m_cachedQuery;
};
#endif // STORAGE_H
+1 -2
View File
@@ -32,8 +32,7 @@ public:
virtual NameHierarchy getNameHierarchyForNodeWithId(Id id) const = 0;
virtual Node::NodeType getNodeTypeForNodeWithId(Id id) const = 0;
virtual std::vector<SearchMatch> getAutocompletionMatches(
const std::string& query, const std::string& word) const = 0;
virtual std::vector<SearchMatch> getAutocompletionMatches(const std::string& query) const = 0;
virtual std::vector<SearchMatch> getSearchMatchesForTokenIds(const std::vector<Id>& tokenIds) const = 0;
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const = 0;
+3 -5
View File
@@ -82,13 +82,11 @@ NameHierarchy StorageAccessProxy::getNameHierarchyForNodeWithId(Id id) const
return NameHierarchy();
}
std::vector<SearchMatch> StorageAccessProxy::getAutocompletionMatches(
const std::string& query,
const std::string& word
) const {
std::vector<SearchMatch> StorageAccessProxy::getAutocompletionMatches(const std::string& query) const
{
if (hasSubject())
{
return m_subject->getAutocompletionMatches(query, word);
return m_subject->getAutocompletionMatches(query);
}
return std::vector<SearchMatch>();
+1 -1
View File
@@ -22,7 +22,7 @@ public:
virtual NameHierarchy getNameHierarchyForNodeWithId(Id id) const;
virtual Node::NodeType getNodeTypeForNodeWithId(Id id) const;
virtual std::vector<SearchMatch> getAutocompletionMatches(const std::string& query, const std::string& word) const;
virtual std::vector<SearchMatch> getAutocompletionMatches(const std::string& query) const;
virtual std::vector<SearchMatch> getSearchMatchesForTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
+6 -1
View File
@@ -15,7 +15,7 @@ std::vector<SearchMatch> SearchIndex::getMatches(
for (SearchResultsIterator it = searchResults.begin(); it != searchResults.end(); it++)
{
SearchMatch match = it->node->fuzzyMatchData(query, it->parent);
SearchMatch match = it->node->fuzzyMatchData(query, it->parent->getParent());
result.push_back(match);
}
@@ -171,6 +171,11 @@ SearchResults SearchIndex::runFuzzySearch(const std::string& query) const
return m_root.runFuzzySearch(query);
}
SearchResults SearchIndex::runFuzzySearchCached(const std::string& query, const SearchResults& searchResults) const
{
return m_root.runFuzzySearchCached(query, searchResults);
}
std::vector<SearchMatch> SearchIndex::runFuzzySearchAndGetMatches(const std::string& query) const
{
return getMatches(runFuzzySearch(query), query);
+1
View File
@@ -40,6 +40,7 @@ public:
NameHierarchy getNameHierarchyForTokenId(Id tokenId) const;
SearchResults runFuzzySearch(const std::string& query) const;
SearchResults runFuzzySearchCached(const std::string& query, const SearchResults& searchResults) const;
std::vector<SearchMatch> runFuzzySearchAndGetMatches(const std::string& query) const;
static const std::string DELIMITER;
+29 -15
View File
@@ -162,12 +162,32 @@ SearchResults SearchNode::runFuzzySearch(const std::string& query) const
FuzzyMap m = n->fuzzyMatchRecursive(query, 0, 0, 0);
for (const std::pair<size_t, const SearchNode*>& p : m)
{
addResultsRecursive(&result, p.first, p.second);
addResultsRecursive(&result, p.first, p.second, n.get());
}
}
return result;
}
SearchResults SearchNode::runFuzzySearchCached(const std::string& query, const SearchResults& searchResults) const
{
SearchResults result;
std::set<const SearchNode*> nodes;
for (const SearchResult& r : searchResults)
{
nodes.insert(r.parent);
}
for (const SearchNode* n : nodes)
{
FuzzyMap m = n->fuzzyMatchRecursive(query, 0, 0, 0);
for (const std::pair<size_t, const SearchNode*>& p : m)
{
addResultsRecursive(&result, p.first, p.second, n);
}
}
// TODO: Currently all matches are added to the ordered set and get compared by their fullName for alphabetical
// order. This could be improved by limiting the number of items to e.g. 100.
return result;
}
@@ -177,26 +197,20 @@ SearchResults SearchNode::runFuzzySearchOnSelf(const std::string& query) const
FuzzyMap m = fuzzyMatchRecursive(query, 0, 0, 0);
for (const std::pair<size_t, const SearchNode*>& p : m)
{
addResultsRecursive(&result, p.first, p.second);
addResultsRecursive(&result, p.first, p.second, this);
}
// TODO: Currently all matches are added to the ordered set and get compared by their fullName for alphabetical
// order. This could be improved by limiting the number of items to e.g. 100.
return result;
}
void SearchNode::addResults(SearchResults* results, size_t weight, const SearchNode* node) const
{
results->insert(SearchResult(weight, node, this));
}
void SearchNode::addResultsRecursive(SearchResults* results, size_t weight, const SearchNode* node) const
{
addResults(results, weight, node);
void SearchNode::addResultsRecursive(
SearchResults* results, size_t weight, const SearchNode* node, const SearchNode* parent
) const {
results->insert(SearchResult(weight, node, parent));
for (std::shared_ptr<SearchNode> n: node->m_nodes)
{
addResultsRecursive(results, weight, n.get());
addResultsRecursive(results, weight, n.get(), parent);
}
}
+2 -2
View File
@@ -45,10 +45,10 @@ public:
const std::set<std::shared_ptr<SearchNode>>& getChildren() const;
SearchResults runFuzzySearch(const std::string& query) const;
SearchResults runFuzzySearchCached(const std::string& query, const SearchResults& searchResults) const;
SearchResults runFuzzySearchOnSelf(const std::string& query) const;
void addResults(SearchResults* results, size_t weight, const SearchNode* node) const;
void addResultsRecursive(SearchResults* results, size_t weight, const SearchNode* node) const;
void addResultsRecursive(SearchResults* results, size_t weight, const SearchNode* node, const SearchNode* parent) const;
private:
typedef std::multimap<size_t, const SearchNode*> FuzzyMap;
@@ -7,9 +7,8 @@
class MessageSearchAutocomplete: public Message<MessageSearchAutocomplete>
{
public:
MessageSearchAutocomplete(const std::string& query, const std::string& word)
MessageSearchAutocomplete(const std::string& query)
: query(query)
, word(word)
{
}
@@ -19,7 +18,6 @@ public:
}
const std::string query;
const std::string word;
};
#endif // MESSAGE_SEARCH_AUTOCOMPLETE_H