| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "chrome/browser/bookmarks/bookmark_index.h" | 5 #include "chrome/browser/bookmarks/bookmark_index.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model.h" | 11 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_utils.h" | 12 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 13 #include "chrome/browser/history/history_database.h" |
| 13 #include "chrome/browser/history/query_parser.h" | 14 #include "chrome/browser/history/query_parser.h" |
| 15 #include "chrome/browser/profile.h" |
| 14 | 16 |
| 15 BookmarkIndex::NodeSet::const_iterator | 17 BookmarkIndex::NodeSet::const_iterator |
| 16 BookmarkIndex::Match::nodes_begin() const { | 18 BookmarkIndex::Match::nodes_begin() const { |
| 17 return nodes.empty() ? terms.front()->second.begin() : nodes.begin(); | 19 return nodes.empty() ? terms.front()->second.begin() : nodes.begin(); |
| 18 } | 20 } |
| 19 | 21 |
| 20 BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const { | 22 BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const { |
| 21 return nodes.empty() ? terms.front()->second.end() : nodes.end(); | 23 return nodes.empty() ? terms.front()->second.end() : nodes.end(); |
| 22 } | 24 } |
| 23 | 25 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 45 std::vector<std::wstring> terms = ExtractQueryWords(query); | 47 std::vector<std::wstring> terms = ExtractQueryWords(query); |
| 46 if (terms.empty()) | 48 if (terms.empty()) |
| 47 return; | 49 return; |
| 48 | 50 |
| 49 Matches matches; | 51 Matches matches; |
| 50 for (size_t i = 0; i < terms.size(); ++i) { | 52 for (size_t i = 0; i < terms.size(); ++i) { |
| 51 if (!GetBookmarksWithTitleMatchingTerm(terms[i], i == 0, &matches)) | 53 if (!GetBookmarksWithTitleMatchingTerm(terms[i], i == 0, &matches)) |
| 52 return; | 54 return; |
| 53 } | 55 } |
| 54 | 56 |
| 57 NodeTypedCountPairs node_typed_counts; |
| 58 SortMatches(matches, &node_typed_counts); |
| 59 |
| 55 // We use a QueryParser to fill in match positions for us. It's not the most | 60 // We use a QueryParser to fill in match positions for us. It's not the most |
| 56 // efficient way to go about this, but by the time we get here we know what | 61 // efficient way to go about this, but by the time we get here we know what |
| 57 // matches and so this shouldn't be performance critical. | 62 // matches and so this shouldn't be performance critical. |
| 58 QueryParser parser; | 63 QueryParser parser; |
| 59 ScopedVector<QueryNode> query_nodes; | 64 ScopedVector<QueryNode> query_nodes; |
| 60 parser.ParseQuery(query, &query_nodes.get()); | 65 parser.ParseQuery(query, &query_nodes.get()); |
| 61 for (size_t i = 0; i < matches.size() && results->size() < max_count; ++i) { | 66 |
| 62 AddMatchToResults(matches[i], max_count, &parser, query_nodes.get(), | 67 // The highest typed counts should be at the beginning of the results vector |
| 63 results); | 68 // so that the best matches will always be included in the results. The loop |
| 69 // that calculates result relevance in HistoryContentsProvider::ConvertResults |
| 70 // will run backwards to assure higher relevance will be attributed to the |
| 71 // best matches. |
| 72 for (NodeTypedCountPairs::const_iterator i = node_typed_counts.begin(); |
| 73 i != node_typed_counts.end() && results->size() < max_count; ++i) |
| 74 AddMatchToResults(i->first, &parser, query_nodes.get(), results); |
| 75 } |
| 76 |
| 77 void BookmarkIndex::SortMatches(const Matches& matches, |
| 78 NodeTypedCountPairs* node_typed_counts) const { |
| 79 HistoryService* const history_service = profile_ ? |
| 80 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; |
| 81 |
| 82 history::URLDatabase* url_db = history_service ? |
| 83 history_service->in_memory_database() : NULL; |
| 84 |
| 85 for (Matches::const_iterator i = matches.begin(); i != matches.end(); ++i) |
| 86 ExtractBookmarkNodePairs(url_db, *i, node_typed_counts); |
| 87 |
| 88 std::sort(node_typed_counts->begin(), node_typed_counts->end(), |
| 89 &NodeTypedCountPairSortFunc); |
| 90 } |
| 91 |
| 92 void BookmarkIndex::ExtractBookmarkNodePairs( |
| 93 history::URLDatabase* url_db, |
| 94 const Match& match, |
| 95 NodeTypedCountPairs* node_typed_counts) const { |
| 96 |
| 97 for (NodeSet::const_iterator i = match.nodes_begin(); |
| 98 i != match.nodes_end(); ++i) { |
| 99 history::URLRow url; |
| 100 if (url_db) |
| 101 url_db->GetRowForURL((*i)->GetURL(), &url); |
| 102 NodeTypedCountPair pair(*i, url.typed_count()); |
| 103 node_typed_counts->push_back(pair); |
| 64 } | 104 } |
| 65 } | 105 } |
| 66 | 106 |
| 67 void BookmarkIndex::AddMatchToResults( | 107 void BookmarkIndex::AddMatchToResults( |
| 68 const Match& match, | 108 const BookmarkNode* node, |
| 69 size_t max_count, | |
| 70 QueryParser* parser, | 109 QueryParser* parser, |
| 71 const std::vector<QueryNode*>& query_nodes, | 110 const std::vector<QueryNode*>& query_nodes, |
| 72 std::vector<bookmark_utils::TitleMatch>* results) { | 111 std::vector<bookmark_utils::TitleMatch>* results) { |
| 73 for (NodeSet::const_iterator i = match.nodes_begin(); | 112 bookmark_utils::TitleMatch title_match; |
| 74 i != match.nodes_end() && results->size() < max_count; ++i) { | 113 // Check that the result matches the query. The previous search |
| 75 bookmark_utils::TitleMatch title_match; | 114 // was a simple per-word search, while the more complex matching |
| 76 // Check that the result matches the query. The previous search | 115 // of QueryParser may filter it out. For example, the query |
| 77 // was a simple per-word search, while the more complex matching | 116 // ["thi"] will match the bookmark titled [Thinking], but since |
| 78 // of QueryParser may filter it out. For example, the query | 117 // ["thi"] is quoted we don't want to do a prefix match. |
| 79 // ["thi"] will match the bookmark titled [Thinking], but since | 118 if (parser->DoesQueryMatch(node->GetTitle(), query_nodes, |
| 80 // ["thi"] is quoted we don't want to do a prefix match. | 119 &(title_match.match_positions))) { |
| 81 if (parser->DoesQueryMatch((*i)->GetTitle(), query_nodes, | 120 title_match.node = node; |
| 82 &(title_match.match_positions))) { | 121 results->push_back(title_match); |
| 83 title_match.node = *i; | |
| 84 results->push_back(title_match); | |
| 85 } | |
| 86 } | 122 } |
| 87 } | 123 } |
| 88 | 124 |
| 89 bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const std::wstring& term, | 125 bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const std::wstring& term, |
| 90 bool first_term, | 126 bool first_term, |
| 91 Matches* matches) { | 127 Matches* matches) { |
| 92 Index::const_iterator i = index_.lower_bound(term); | 128 Index::const_iterator i = index_.lower_bound(term); |
| 93 if (i == index_.end()) | 129 if (i == index_.end()) |
| 94 return false; | 130 return false; |
| 95 | 131 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 Index::iterator i = index_.find(term); | 230 Index::iterator i = index_.find(term); |
| 195 if (i == index_.end()) { | 231 if (i == index_.end()) { |
| 196 // We can get here if the node has the same term more than once. For | 232 // We can get here if the node has the same term more than once. For |
| 197 // example, a bookmark with the title 'foo foo' would end up here. | 233 // example, a bookmark with the title 'foo foo' would end up here. |
| 198 return; | 234 return; |
| 199 } | 235 } |
| 200 i->second.erase(node); | 236 i->second.erase(node); |
| 201 if (i->second.empty()) | 237 if (i->second.empty()) |
| 202 index_.erase(i); | 238 index_.erase(i); |
| 203 } | 239 } |
| OLD | NEW |