| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_ | 5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_TITLED_URL_INDEX_H_ |
| 6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_ | 6 #define COMPONENTS_BOOKMARKS_BROWSER_TITLED_URL_INDEX_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 17 #include "components/query_parser/query_parser.h" | 17 #include "components/query_parser/query_parser.h" |
| 18 | 18 |
| 19 namespace bookmarks { | 19 namespace bookmarks { |
| 20 | 20 |
| 21 class TitledUrlNode; | 21 class TitledUrlNode; |
| 22 class TitledUrlNodeSorter; | 22 class TitledUrlNodeSorter; |
| 23 struct BookmarkMatch; | 23 struct TitledUrlMatch; |
| 24 | 24 |
| 25 // BookmarkIndex maintains an index of the titles and URLs of bookmarks for | 25 // TitledUrlIndex maintains an index of paired titles and URLs for quick lookup. |
| 26 // quick look up. BookmarkIndex is owned and maintained by BookmarkModel, you | |
| 27 // shouldn't need to interact directly with BookmarkIndex. | |
| 28 // | 26 // |
| 29 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type | 27 // TitledUrlIndex maintains the index (index_) as a map of sets. The map (type |
| 30 // Index) maps from a lower case string to the set (type NodeSet) of | 28 // Index) maps from a lower case string to the set (type TitledUrlNodeSet) of |
| 31 // TitledUrlNodes that contain that string in their title or URL. | 29 // TitledUrlNodes that contain that string in their title or URL. |
| 32 class BookmarkIndex { | 30 class TitledUrlIndex { |
| 33 public: | 31 public: |
| 34 BookmarkIndex(std::unique_ptr<TitledUrlNodeSorter> sorter); | 32 TitledUrlIndex(std::unique_ptr<TitledUrlNodeSorter> sorter); |
| 35 ~BookmarkIndex(); | 33 ~TitledUrlIndex(); |
| 36 | 34 |
| 37 // Invoked when a title/URL pair has been added to the model. | 35 // Invoked when a title/URL pair has been added to the model. |
| 38 void Add(const TitledUrlNode* node); | 36 void Add(const TitledUrlNode* node); |
| 39 | 37 |
| 40 // Invoked when a title/URL pair has been removed from the model. | 38 // Invoked when a title/URL pair has been removed from the model. |
| 41 void Remove(const TitledUrlNode* node); | 39 void Remove(const TitledUrlNode* node); |
| 42 | 40 |
| 43 // Returns up to |max_count| of matches containing each term from the text | 41 // Returns up to |max_count| of matches containing each term from the text |
| 44 // |query| in either the title or the URL. | 42 // |query| in either the title or the URL. |
| 45 void GetResultsMatching(const base::string16& query, | 43 void GetResultsMatching(const base::string16& query, |
| 46 size_t max_count, | 44 size_t max_count, |
| 47 query_parser::MatchingAlgorithm matching_algorithm, | 45 query_parser::MatchingAlgorithm matching_algorithm, |
| 48 std::vector<BookmarkMatch>* results); | 46 std::vector<TitledUrlMatch>* results); |
| 49 | 47 |
| 50 private: | 48 private: |
| 51 using TitledUrlNodes = std::vector<const TitledUrlNode*>; | 49 using TitledUrlNodes = std::vector<const TitledUrlNode*>; |
| 52 using TitledUrlNodeSet = std::set<const TitledUrlNode*>; | 50 using TitledUrlNodeSet = std::set<const TitledUrlNode*>; |
| 53 using Index = std::map<base::string16, TitledUrlNodeSet>; | 51 using Index = std::map<base::string16, TitledUrlNodeSet>; |
| 54 | 52 |
| 55 // Constructs |sorted_nodes| by taking the matches in |matches| and sorting | 53 // Constructs |sorted_nodes| by copying the matches in |matches| and sorting |
| 56 // them in decreasing order of typed count (if supported by the client) and | 54 // them. |
| 57 // deduping them. | |
| 58 void SortMatches(const TitledUrlNodeSet& matches, | 55 void SortMatches(const TitledUrlNodeSet& matches, |
| 59 TitledUrlNodes* sorted_nodes) const; | 56 TitledUrlNodes* sorted_nodes) const; |
| 60 | 57 |
| 61 // Add |node| to |results| if the node matches the query. | 58 // Add |node| to |results| if the node matches the query. |
| 62 void AddMatchToResults(const TitledUrlNode* node, | 59 void AddMatchToResults(const TitledUrlNode* node, |
| 63 query_parser::QueryParser* parser, | 60 query_parser::QueryParser* parser, |
| 64 const query_parser::QueryNodeVector& query_nodes, | 61 const query_parser::QueryNodeVector& query_nodes, |
| 65 std::vector<BookmarkMatch>* results); | 62 std::vector<TitledUrlMatch>* results); |
| 66 | 63 |
| 67 // Populates |matches| for the specified term. If |first_term| is true, this | 64 // Populates |matches| for the specified term. If |first_term| is true, this |
| 68 // is the first term in the query. Returns true if there is at least one node | 65 // is the first term in the query. Returns true if there is at least one node |
| 69 // matching the term. | 66 // matching the term. |
| 70 bool GetResultsMatchingTerm( | 67 bool GetResultsMatchingTerm( |
| 71 const base::string16& term, | 68 const base::string16& term, |
| 72 bool first_term, | 69 bool first_term, |
| 73 query_parser::MatchingAlgorithm matching_algorithm, | 70 query_parser::MatchingAlgorithm matching_algorithm, |
| 74 TitledUrlNodeSet* matches); | 71 TitledUrlNodeSet* matches); |
| 75 | 72 |
| 76 // Returns the set of query words from |query|. | 73 // Returns the set of query words from |query|. |
| 77 std::vector<base::string16> ExtractQueryWords(const base::string16& query); | 74 std::vector<base::string16> ExtractQueryWords(const base::string16& query); |
| 78 | 75 |
| 79 // Adds |node| to |index_|. | 76 // Adds |node| to |index_|. |
| 80 void RegisterNode(const base::string16& term, const TitledUrlNode* node); | 77 void RegisterNode(const base::string16& term, const TitledUrlNode* node); |
| 81 | 78 |
| 82 // Removes |node| from |index_|. | 79 // Removes |node| from |index_|. |
| 83 void UnregisterNode(const base::string16& term, const TitledUrlNode* node); | 80 void UnregisterNode(const base::string16& term, const TitledUrlNode* node); |
| 84 | 81 |
| 85 Index index_; | 82 Index index_; |
| 86 | 83 |
| 87 std::unique_ptr<TitledUrlNodeSorter> sorter_; | 84 std::unique_ptr<TitledUrlNodeSorter> sorter_; |
| 88 | 85 |
| 89 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); | 86 DISALLOW_COPY_AND_ASSIGN(TitledUrlIndex); |
| 90 }; | 87 }; |
| 91 | 88 |
| 92 } // namespace bookmarks | 89 } // namespace bookmarks |
| 93 | 90 |
| 94 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_ | 91 #endif // COMPONENTS_BOOKMARKS_BROWSER_TITLED_URL_INDEX_H_ |
| OLD | NEW |