Index: components/bookmarks/browser/titled_url_index.h |
diff --git a/components/bookmarks/browser/titled_url_index.h b/components/bookmarks/browser/titled_url_index.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..97397174f8c7f742df86cc45077f2acee33dffe0 |
--- /dev/null |
+++ b/components/bookmarks/browser/titled_url_index.h |
@@ -0,0 +1,89 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_BOOKMARKS_BROWSER_TITLED_URL_INDEX_H_ |
+#define COMPONENTS_BOOKMARKS_BROWSER_TITLED_URL_INDEX_H_ |
+ |
+#include <stddef.h> |
+ |
+#include <map> |
+#include <set> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/macros.h" |
+#include "base/strings/string16.h" |
+#include "components/bookmarks/browser/titled_url_node.h" |
+#include "components/query_parser/query_parser.h" |
+ |
+namespace bookmarks { |
+ |
+struct TitledUrlMatch; |
+ |
+// TitledUrlIndex maintains an index of paired titles and URLs for quick lookup. |
+// |
+// TitledUrlIndex stores the index (index_) as a map of sets. The map (type |
+// Index) maps from a lower case string to the set (type NodeSet) of |
+// TitledUrlNodes that contain that string in their title or URL. |
+class TitledUrlIndex { |
sky
2016/12/01 20:24:51
Can you convert BookmarkIndex in place and then re
mattreynolds
2016/12/01 23:33:18
Done.
|
+ public: |
+ TitledUrlIndex(); |
+ ~TitledUrlIndex(); |
+ |
+ // Invoked when a title/URL pair has been added to the model. |
+ void Add(const TitledUrlNode* node); |
+ |
+ // Invoked when a title/URL pair has been removed from the model. |
+ void Remove(const TitledUrlNode* node); |
+ |
+ // Returns up to |max_count| of matches containing each term from the text |
+ // |query| in either the title or the URL. |
+ void GetResultsMatching(const base::string16& query, |
+ size_t max_count, |
+ query_parser::MatchingAlgorithm matching_algorithm, |
+ std::vector<TitledUrlMatch>* results); |
+ |
+ protected: |
+ typedef std::vector<const TitledUrlNode*> Nodes; |
+ typedef std::set<const TitledUrlNode*> NodeSet; |
+ typedef std::map<base::string16, NodeSet> Index; |
+ |
+ private: |
+ // Constructs |sorted_nodes| by copying the matches in |matches|. If the order |
+ // of matches is relevant, override this method to specify the sorting and |
+ // deduplication behavior. The default implementation performs no sorting. |
+ virtual void SortMatches(const NodeSet& matches, Nodes* sorted_nodes) const; |
+ |
+ // Add |node| to |results| if the node matches the query. |
+ void AddMatchToResults(const TitledUrlNode* node, |
+ query_parser::QueryParser* parser, |
+ const query_parser::QueryNodeVector& query_nodes, |
+ std::vector<TitledUrlMatch>* results); |
+ |
+ // Populates |matches| for the specified term. If |first_term| is true, this |
+ // is the first term in the query. Returns true if there is at least one node |
+ // matching the term. |
+ bool GetResultsMatchingTerm( |
+ const base::string16& term, |
+ bool first_term, |
+ query_parser::MatchingAlgorithm matching_algorithm, |
+ NodeSet* matches); |
+ |
+ // Returns the set of query words from |query|. |
+ std::vector<base::string16> ExtractQueryWords(const base::string16& query); |
+ |
+ // Adds |node| to |index_|. |
+ void RegisterNode(const base::string16& term, const TitledUrlNode* node); |
+ |
+ // Removes |node| from |index_|. |
+ void UnregisterNode(const base::string16& term, const TitledUrlNode* node); |
+ |
+ Index index_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TitledUrlIndex); |
+}; |
+ |
+} // namespace bookmarks |
+ |
+#endif // COMPONENTS_BOOKMARKS_BROWSER_TITLED_URL_INDEX_H_ |