Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(633)

Side by Side Diff: components/bookmarks/browser/bookmark_index.cc

Issue 882823004: Omnibox: BookmarksProvider: Make Multiple Prefix Matches Work (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: restore android hack Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "components/bookmarks/browser/bookmark_index.h" 5 #include "components/bookmarks/browser/bookmark_index.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <iterator> 9 #include <iterator>
10 #include <list> 10 #include <list>
11 11
12 #include "base/i18n/case_conversion.h" 12 #include "base/i18n/case_conversion.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/stl_util.h"
14 #include "base/strings/utf_offset_string_conversions.h" 15 #include "base/strings/utf_offset_string_conversions.h"
15 #include "components/bookmarks/browser/bookmark_client.h" 16 #include "components/bookmarks/browser/bookmark_client.h"
16 #include "components/bookmarks/browser/bookmark_match.h" 17 #include "components/bookmarks/browser/bookmark_match.h"
17 #include "components/bookmarks/browser/bookmark_node.h" 18 #include "components/bookmarks/browser/bookmark_node.h"
18 #include "components/bookmarks/browser/bookmark_utils.h" 19 #include "components/bookmarks/browser/bookmark_utils.h"
19 #include "components/query_parser/snippet.h" 20 #include "components/query_parser/snippet.h"
20 #include "third_party/icu/source/common/unicode/normalizer2.h" 21 #include "third_party/icu/source/common/unicode/normalizer2.h"
21 22
22 namespace bookmarks { 23 namespace bookmarks {
23 24
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 // Extract the const Node* stored in a BookmarkClient::NodeTypedCountPair. 56 // Extract the const Node* stored in a BookmarkClient::NodeTypedCountPair.
56 struct NodeTypedCountPairExtractNodeFunctor 57 struct NodeTypedCountPairExtractNodeFunctor
57 : std::unary_function<NodeTypedCountPair, const BookmarkNode*> { 58 : std::unary_function<NodeTypedCountPair, const BookmarkNode*> {
58 const BookmarkNode* operator()(const NodeTypedCountPair& pair) const { 59 const BookmarkNode* operator()(const NodeTypedCountPair& pair) const {
59 return pair.first; 60 return pair.first;
60 } 61 }
61 }; 62 };
62 63
63 } // namespace 64 } // namespace
64 65
65 // Used when finding the set of bookmarks that match a query. Each match
66 // represents a set of terms (as an interator into the Index) matching the
67 // query as well as the set of nodes that contain those terms in their titles.
68 struct BookmarkIndex::Match {
69 // List of terms matching the query.
70 std::list<Index::const_iterator> terms;
71
72 // The set of nodes matching the terms. As an optimization this is empty
73 // when we match only one term, and is filled in when we get more than one
74 // term. We can do this as when we have only one matching term we know
75 // the set of matching nodes is terms.front()->second.
76 //
77 // Use nodes_begin() and nodes_end() to get an iterator over the set as
78 // it handles the necessary switching between nodes and terms.front().
79 NodeSet nodes;
80
81 // Returns an iterator to the beginning of the matching nodes. See
82 // description of nodes for why this should be used over nodes.begin().
83 NodeSet::const_iterator nodes_begin() const;
84
85 // Returns an iterator to the beginning of the matching nodes. See
86 // description of nodes for why this should be used over nodes.end().
87 NodeSet::const_iterator nodes_end() const;
88 };
89
90 BookmarkIndex::NodeSet::const_iterator
91 BookmarkIndex::Match::nodes_begin() const {
92 return nodes.empty() ? terms.front()->second.begin() : nodes.begin();
93 }
94
95 BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const {
96 return nodes.empty() ? terms.front()->second.end() : nodes.end();
97 }
98
99 BookmarkIndex::BookmarkIndex(BookmarkClient* client, 66 BookmarkIndex::BookmarkIndex(BookmarkClient* client,
100 const std::string& languages) 67 const std::string& languages)
101 : client_(client), 68 : client_(client),
102 languages_(languages) { 69 languages_(languages) {
103 DCHECK(client_); 70 DCHECK(client_);
104 } 71 }
105 72
106 BookmarkIndex::~BookmarkIndex() { 73 BookmarkIndex::~BookmarkIndex() {
107 } 74 }
108 75
(...skipping 27 matching lines...) Expand all
136 void BookmarkIndex::GetBookmarksMatching( 103 void BookmarkIndex::GetBookmarksMatching(
137 const base::string16& input_query, 104 const base::string16& input_query,
138 size_t max_count, 105 size_t max_count,
139 query_parser::MatchingAlgorithm matching_algorithm, 106 query_parser::MatchingAlgorithm matching_algorithm,
140 std::vector<BookmarkMatch>* results) { 107 std::vector<BookmarkMatch>* results) {
141 const base::string16 query = Normalize(input_query); 108 const base::string16 query = Normalize(input_query);
142 std::vector<base::string16> terms = ExtractQueryWords(query); 109 std::vector<base::string16> terms = ExtractQueryWords(query);
143 if (terms.empty()) 110 if (terms.empty())
144 return; 111 return;
145 112
146 Matches matches; 113 NodeSet matches;
147 for (size_t i = 0; i < terms.size(); ++i) { 114 for (size_t i = 0; i < terms.size(); ++i) {
148 if (!GetBookmarksMatchingTerm( 115 if (!GetBookmarksMatchingTerm(
149 terms[i], i == 0, matching_algorithm, &matches)) { 116 terms[i], i == 0, matching_algorithm, &matches)) {
150 return; 117 return;
151 } 118 }
152 } 119 }
153 120
154 Nodes sorted_nodes; 121 Nodes sorted_nodes;
155 SortMatches(matches, &sorted_nodes); 122 SortMatches(matches, &sorted_nodes);
156 123
157 // We use a QueryParser to fill in match positions for us. It's not the most 124 // We use a QueryParser to fill in match positions for us. It's not the most
158 // efficient way to go about this, but by the time we get here we know what 125 // efficient way to go about this, but by the time we get here we know what
159 // matches and so this shouldn't be performance critical. 126 // matches and so this shouldn't be performance critical.
160 query_parser::QueryParser parser; 127 query_parser::QueryParser parser;
161 ScopedVector<query_parser::QueryNode> query_nodes; 128 ScopedVector<query_parser::QueryNode> query_nodes;
162 parser.ParseQueryNodes(query, matching_algorithm, &query_nodes.get()); 129 parser.ParseQueryNodes(query, matching_algorithm, &query_nodes.get());
163 130
164 // The highest typed counts should be at the beginning of the results vector 131 // The highest typed counts should be at the beginning of the results vector
165 // so that the best matches will always be included in the results. The loop 132 // so that the best matches will always be included in the results. The loop
166 // that calculates result relevance in HistoryContentsProvider::ConvertResults 133 // that calculates result relevance in HistoryContentsProvider::ConvertResults
167 // will run backwards to assure higher relevance will be attributed to the 134 // will run backwards to assure higher relevance will be attributed to the
168 // best matches. 135 // best matches.
169 for (Nodes::const_iterator i = sorted_nodes.begin(); 136 for (Nodes::const_iterator i = sorted_nodes.begin();
170 i != sorted_nodes.end() && results->size() < max_count; 137 i != sorted_nodes.end() && results->size() < max_count;
171 ++i) 138 ++i)
172 AddMatchToResults(*i, &parser, query_nodes.get(), results); 139 AddMatchToResults(*i, &parser, query_nodes.get(), results);
173 } 140 }
174 141
175 void BookmarkIndex::SortMatches(const Matches& matches, 142 void BookmarkIndex::SortMatches(const NodeSet& matches,
176 Nodes* sorted_nodes) const { 143 Nodes* sorted_nodes) const {
177 NodeSet nodes; 144 sorted_nodes->reserve(matches.size());
178 for (Matches::const_iterator i = matches.begin(); i != matches.end(); ++i) {
179 #if !defined(OS_ANDROID)
180 nodes.insert(i->nodes_begin(), i->nodes_end());
181 #else
182 // Work around a bug in the implementation of std::set::insert in the STL
183 // used on android (http://crbug.com/367050).
184 for (NodeSet::const_iterator n = i->nodes_begin(); n != i->nodes_end(); ++n)
185 nodes.insert(nodes.end(), *n);
186 #endif
187 }
188 sorted_nodes->reserve(sorted_nodes->size() + nodes.size());
189 if (client_->SupportsTypedCountForNodes()) { 145 if (client_->SupportsTypedCountForNodes()) {
190 NodeTypedCountPairs node_typed_counts; 146 NodeTypedCountPairs node_typed_counts;
191 client_->GetTypedCountForNodes(nodes, &node_typed_counts); 147 client_->GetTypedCountForNodes(matches, &node_typed_counts);
192 std::sort(node_typed_counts.begin(), 148 std::sort(node_typed_counts.begin(),
193 node_typed_counts.end(), 149 node_typed_counts.end(),
194 NodeTypedCountPairSortFunctor()); 150 NodeTypedCountPairSortFunctor());
195 std::transform(node_typed_counts.begin(), 151 std::transform(node_typed_counts.begin(),
196 node_typed_counts.end(), 152 node_typed_counts.end(),
197 std::back_inserter(*sorted_nodes), 153 std::back_inserter(*sorted_nodes),
198 NodeTypedCountPairExtractNodeFunctor()); 154 NodeTypedCountPairExtractNodeFunctor());
199 } else { 155 } else {
200 sorted_nodes->insert(sorted_nodes->end(), nodes.begin(), nodes.end()); 156 sorted_nodes->insert(sorted_nodes->end(), matches.begin(), matches.end());
201 } 157 }
202 } 158 }
203 159
204 void BookmarkIndex::AddMatchToResults( 160 void BookmarkIndex::AddMatchToResults(
205 const BookmarkNode* node, 161 const BookmarkNode* node,
206 query_parser::QueryParser* parser, 162 query_parser::QueryParser* parser,
207 const query_parser::QueryNodeStarVector& query_nodes, 163 const query_parser::QueryNodeStarVector& query_nodes,
208 std::vector<BookmarkMatch>* results) { 164 std::vector<BookmarkMatch>* results) {
209 // Check that the result matches the query. The previous search 165 // Check that the result matches the query. The previous search
210 // was a simple per-word search, while the more complex matching 166 // was a simple per-word search, while the more complex matching
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 BookmarkMatch::ReplaceOffsetsInMatchPositions(url_matches, offsets); 203 BookmarkMatch::ReplaceOffsetsInMatchPositions(url_matches, offsets);
248 match.url_match_positions.swap(url_matches); 204 match.url_match_positions.swap(url_matches);
249 match.node = node; 205 match.node = node;
250 results->push_back(match); 206 results->push_back(match);
251 } 207 }
252 208
253 bool BookmarkIndex::GetBookmarksMatchingTerm( 209 bool BookmarkIndex::GetBookmarksMatchingTerm(
254 const base::string16& term, 210 const base::string16& term,
255 bool first_term, 211 bool first_term,
256 query_parser::MatchingAlgorithm matching_algorithm, 212 query_parser::MatchingAlgorithm matching_algorithm,
257 Matches* matches) { 213 NodeSet* matches) {
258 Index::const_iterator i = index_.lower_bound(term); 214 Index::const_iterator i = index_.lower_bound(term);
259 if (i == index_.end()) 215 if (i == index_.end())
260 return false; 216 return false;
261 217
262 if (!query_parser::QueryParser::IsWordLongEnoughForPrefixSearch( 218 if (!query_parser::QueryParser::IsWordLongEnoughForPrefixSearch(
263 term, matching_algorithm)) { 219 term, matching_algorithm)) {
264 // Term is too short for prefix match, compare using exact match. 220 // Term is too short for prefix match, compare using exact match.
265 if (i->first != term) 221 if (i->first != term)
266 return false; // No bookmarks with this term. 222 return false; // No bookmarks with this term.
267 223
268 if (first_term) { 224 if (first_term) {
269 Match match; 225 (*matches) = i->second;
270 match.terms.push_back(i);
271 matches->push_back(match);
272 return true; 226 return true;
273 } 227 }
274 CombineMatchesInPlace(i, matches); 228 *matches = base::STLSetIntersection<NodeSet>(i->second, *matches);
275 } else if (first_term) { 229 } else {
276 // This is the first term and we're doing a prefix match. Loop through 230 // Loop through index adding all entries that start with term to
277 // index adding all entries that start with term to matches. 231 // |prefix_matches|.
232 NodeSet tmp_prefix_matches;
233 // If this is the first term, then store the result directly in |matches|
234 // to avoid calling stl intersection (which requires a copy).
235 NodeSet* prefix_matches = first_term ? matches : &tmp_prefix_matches;
278 while (i != index_.end() && 236 while (i != index_.end() &&
279 i->first.size() >= term.size() && 237 i->first.size() >= term.size() &&
280 term.compare(0, term.size(), i->first, 0, term.size()) == 0) { 238 term.compare(0, term.size(), i->first, 0, term.size()) == 0) {
281 Match match; 239 #if !defined(OS_ANDROID)
282 match.terms.push_back(i); 240 prefix_matches->insert(i->second.begin(), i->second.end());
283 matches->push_back(match); 241 #else
242 // Work around a bug in the implementation of std::set::insert in the STL
243 // used on android (http://crbug.com/367050).
244 for (NodeSet::const_iterator n = i->second.begin(); n != i->second.end();
245 ++n)
246 prefix_matches->insert(prefix_matches->end(), *n);
247 #endif
284 ++i; 248 ++i;
285 } 249 }
286 } else { 250 if (!first_term)
287 // Prefix match and not the first term. Loop through index combining 251 *matches = base::STLSetIntersection<NodeSet>(*prefix_matches, *matches);
288 // current matches in matches with term, placing result in result.
289 Matches result;
290 while (i != index_.end() &&
291 i->first.size() >= term.size() &&
292 term.compare(0, term.size(), i->first, 0, term.size()) == 0) {
293 CombineMatches(i, *matches, &result);
294 ++i;
295 }
296 matches->swap(result);
297 } 252 }
298 return !matches->empty(); 253 return !matches->empty();
299 } 254 }
300 255
301 void BookmarkIndex::CombineMatchesInPlace(const Index::const_iterator& index_i,
302 Matches* matches) {
303 for (size_t i = 0; i < matches->size(); ) {
304 Match* match = &((*matches)[i]);
305 NodeSet intersection;
306 std::set_intersection(match->nodes_begin(), match->nodes_end(),
307 index_i->second.begin(), index_i->second.end(),
308 std::inserter(intersection, intersection.begin()));
309 if (intersection.empty()) {
310 matches->erase(matches->begin() + i);
311 } else {
312 match->terms.push_back(index_i);
313 match->nodes.swap(intersection);
314 ++i;
315 }
316 }
317 }
318
319 void BookmarkIndex::CombineMatches(const Index::const_iterator& index_i,
320 const Matches& current_matches,
321 Matches* result) {
322 for (size_t i = 0; i < current_matches.size(); ++i) {
323 const Match& match = current_matches[i];
324 NodeSet intersection;
325 std::set_intersection(match.nodes_begin(), match.nodes_end(),
326 index_i->second.begin(), index_i->second.end(),
327 std::inserter(intersection, intersection.begin()));
328 if (!intersection.empty()) {
329 result->push_back(Match());
330 Match& combined_match = result->back();
331 combined_match.terms = match.terms;
332 combined_match.terms.push_back(index_i);
333 combined_match.nodes.swap(intersection);
334 }
335 }
336 }
337
338 std::vector<base::string16> BookmarkIndex::ExtractQueryWords( 256 std::vector<base::string16> BookmarkIndex::ExtractQueryWords(
339 const base::string16& query) { 257 const base::string16& query) {
340 std::vector<base::string16> terms; 258 std::vector<base::string16> terms;
341 if (query.empty()) 259 if (query.empty())
342 return std::vector<base::string16>(); 260 return std::vector<base::string16>();
343 query_parser::QueryParser parser; 261 query_parser::QueryParser parser;
344 parser.ParseQueryWords(base::i18n::ToLower(query), 262 parser.ParseQueryWords(base::i18n::ToLower(query),
345 query_parser::MatchingAlgorithm::DEFAULT, 263 query_parser::MatchingAlgorithm::DEFAULT,
346 &terms); 264 &terms);
347 return terms; 265 return terms;
(...skipping 11 matching lines...) Expand all
359 // We can get here if the node has the same term more than once. For 277 // We can get here if the node has the same term more than once. For
360 // example, a bookmark with the title 'foo foo' would end up here. 278 // example, a bookmark with the title 'foo foo' would end up here.
361 return; 279 return;
362 } 280 }
363 i->second.erase(node); 281 i->second.erase(node);
364 if (i->second.empty()) 282 if (i->second.empty())
365 index_.erase(i); 283 index_.erase(i);
366 } 284 }
367 285
368 } // namespace bookmarks 286 } // namespace bookmarks
OLDNEW
« no previous file with comments | « components/bookmarks/browser/bookmark_index.h ('k') | components/bookmarks/browser/bookmark_index_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698