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

Unified Diff: components/bookmarks/browser/bookmark_index.cc

Issue 2537223008: Add TitledUrlIndex for indexing arbitrary title/URL pairs (Closed)
Patch Set: add TypedCountSorter, typedef -> using Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: components/bookmarks/browser/bookmark_index.cc
diff --git a/components/bookmarks/browser/bookmark_index.cc b/components/bookmarks/browser/bookmark_index.cc
index 9c34e5a2ca5f821b76f0c2724f7ffb542b4d247d..88b1f37fc4a218bf888fcb3c090c4e1fc50fb23a 100644
--- a/components/bookmarks/browser/bookmark_index.cc
+++ b/components/bookmarks/browser/bookmark_index.cc
@@ -6,29 +6,21 @@
#include <stdint.h>
-#include <algorithm>
-#include <functional>
-#include <iterator>
-#include <list>
-
#include "base/i18n/case_conversion.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/utf_offset_string_conversions.h"
#include "build/build_config.h"
-#include "components/bookmarks/browser/bookmark_client.h"
#include "components/bookmarks/browser/bookmark_match.h"
-#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/bookmark_utils.h"
+#include "components/bookmarks/browser/titled_url_node.h"
+#include "components/bookmarks/browser/titled_url_node_sorter.h"
#include "components/query_parser/snippet.h"
#include "third_party/icu/source/common/unicode/normalizer2.h"
#include "third_party/icu/source/common/unicode/utypes.h"
namespace bookmarks {
-typedef BookmarkClient::NodeTypedCountPair NodeTypedCountPair;
-typedef BookmarkClient::NodeTypedCountPairs NodeTypedCountPairs;
-
namespace {
// Returns a normalized version of the UTF16 string |text|. If it fails to
@@ -54,60 +46,43 @@ base::string16 Normalize(const base::string16& text) {
unicode_normalized_text.length());
}
-// Sort functor for NodeTypedCountPairs. We sort in decreasing order of typed
-// count so that the best matches will always be added to the results.
-struct NodeTypedCountPairSortFunctor {
- bool operator()(const NodeTypedCountPair& a,
- const NodeTypedCountPair& b) const {
- return a.second > b.second;
- }
-};
-
-// Extract the const Node* stored in a BookmarkClient::NodeTypedCountPair.
-struct NodeTypedCountPairExtractNodeFunctor {
- const BookmarkNode* operator()(const NodeTypedCountPair& pair) const {
- return pair.first;
- }
-};
-
} // namespace
-BookmarkIndex::BookmarkIndex(BookmarkClient* client)
- : client_(client) {
- DCHECK(client_);
+BookmarkIndex::BookmarkIndex(std::unique_ptr<TitledUrlNodeSorter> sorter)
+ : sorter_(std::move(sorter)) {
}
BookmarkIndex::~BookmarkIndex() {
}
-void BookmarkIndex::Add(const BookmarkNode* node) {
- if (!node->is_url())
+void BookmarkIndex::Add(const TitledUrlNode* node) {
+ if (!node->GetTitledUrlNodeUrl().is_valid())
return;
std::vector<base::string16> terms =
- ExtractQueryWords(Normalize(node->GetTitle()));
+ ExtractQueryWords(Normalize(node->GetTitledUrlNodeTitle()));
for (size_t i = 0; i < terms.size(); ++i)
RegisterNode(terms[i], node);
- terms =
- ExtractQueryWords(CleanUpUrlForMatching(node->url(), nullptr));
+ terms = ExtractQueryWords(
+ CleanUpUrlForMatching(node->GetTitledUrlNodeUrl(), nullptr));
for (size_t i = 0; i < terms.size(); ++i)
RegisterNode(terms[i], node);
}
-void BookmarkIndex::Remove(const BookmarkNode* node) {
- if (!node->is_url())
+void BookmarkIndex::Remove(const TitledUrlNode* node) {
+ if (!node->GetTitledUrlNodeUrl().is_valid())
return;
std::vector<base::string16> terms =
- ExtractQueryWords(Normalize(node->GetTitle()));
+ ExtractQueryWords(Normalize(node->GetTitledUrlNodeTitle()));
for (size_t i = 0; i < terms.size(); ++i)
UnregisterNode(terms[i], node);
- terms =
- ExtractQueryWords(CleanUpUrlForMatching(node->url(), nullptr));
+ terms = ExtractQueryWords(
+ CleanUpUrlForMatching(node->GetTitledUrlNodeUrl(), nullptr));
for (size_t i = 0; i < terms.size(); ++i)
UnregisterNode(terms[i], node);
}
-void BookmarkIndex::GetBookmarksMatching(
+void BookmarkIndex::GetResultsMatching(
const base::string16& input_query,
size_t max_count,
query_parser::MatchingAlgorithm matching_algorithm,
@@ -117,15 +92,15 @@ void BookmarkIndex::GetBookmarksMatching(
if (terms.empty())
return;
- NodeSet matches;
+ TitledUrlNodeSet matches;
for (size_t i = 0; i < terms.size(); ++i) {
- if (!GetBookmarksMatchingTerm(
- terms[i], i == 0, matching_algorithm, &matches)) {
+ if (!GetResultsMatchingTerm(terms[i], i == 0, matching_algorithm,
+ &matches)) {
return;
}
}
- Nodes sorted_nodes;
+ TitledUrlNodes sorted_nodes;
SortMatches(matches, &sorted_nodes);
// We use a QueryParser to fill in match positions for us. It's not the most
@@ -140,47 +115,38 @@ void BookmarkIndex::GetBookmarksMatching(
// that calculates result relevance in HistoryContentsProvider::ConvertResults
// will run backwards to assure higher relevance will be attributed to the
// best matches.
- for (Nodes::const_iterator i = sorted_nodes.begin();
+ for (TitledUrlNodes::const_iterator i = sorted_nodes.begin();
i != sorted_nodes.end() && results->size() < max_count;
++i)
AddMatchToResults(*i, &parser, query_nodes, results);
}
-void BookmarkIndex::SortMatches(const NodeSet& matches,
- Nodes* sorted_nodes) const {
- sorted_nodes->reserve(matches.size());
- if (client_->SupportsTypedCountForNodes()) {
- NodeTypedCountPairs node_typed_counts;
- client_->GetTypedCountForNodes(matches, &node_typed_counts);
- std::sort(node_typed_counts.begin(),
- node_typed_counts.end(),
- NodeTypedCountPairSortFunctor());
- std::transform(node_typed_counts.begin(),
- node_typed_counts.end(),
- std::back_inserter(*sorted_nodes),
- NodeTypedCountPairExtractNodeFunctor());
+void BookmarkIndex::SortMatches(const TitledUrlNodeSet& matches,
+ TitledUrlNodes* sorted_nodes) const {
+ if (sorter_) {
+ sorter_->SortMatches(matches, sorted_nodes);
} else {
sorted_nodes->insert(sorted_nodes->end(), matches.begin(), matches.end());
}
}
void BookmarkIndex::AddMatchToResults(
- const BookmarkNode* node,
+ const TitledUrlNode* node,
query_parser::QueryParser* parser,
const query_parser::QueryNodeVector& query_nodes,
std::vector<BookmarkMatch>* results) {
// Check that the result matches the query. The previous search
// was a simple per-word search, while the more complex matching
// of QueryParser may filter it out. For example, the query
- // ["thi"] will match the bookmark titled [Thinking], but since
+ // ["thi"] will match the title [Thinking], but since
// ["thi"] is quoted we don't want to do a prefix match.
query_parser::QueryWordVector title_words, url_words;
const base::string16 lower_title =
- base::i18n::ToLower(Normalize(node->GetTitle()));
+ base::i18n::ToLower(Normalize(node->GetTitledUrlNodeTitle()));
parser->ExtractQueryWords(lower_title, &title_words);
base::OffsetAdjuster::Adjustments adjustments;
parser->ExtractQueryWords(
- CleanUpUrlForMatching(node->url(), &adjustments),
+ CleanUpUrlForMatching(node->GetTitledUrlNodeUrl(), &adjustments),
&url_words);
query_parser::Snippet::MatchPositions title_matches, url_matches;
for (const auto& node : query_nodes) {
@@ -193,7 +159,7 @@ void BookmarkIndex::AddMatchToResults(
query_parser::QueryParser::SortAndCoalesceMatchPositions(&url_matches);
}
BookmarkMatch match;
- if (lower_title.length() == node->GetTitle().length()) {
+ if (lower_title.length() == node->GetTitledUrlNodeTitle().length()) {
// Only use title matches if the lowercase string is the same length
// as the original string, otherwise the matches are meaningless.
// TODO(mpearson): revise match positions appropriately.
@@ -212,11 +178,11 @@ void BookmarkIndex::AddMatchToResults(
results->push_back(match);
}
-bool BookmarkIndex::GetBookmarksMatchingTerm(
+bool BookmarkIndex::GetResultsMatchingTerm(
const base::string16& term,
bool first_term,
query_parser::MatchingAlgorithm matching_algorithm,
- NodeSet* matches) {
+ TitledUrlNodeSet* matches) {
Index::const_iterator i = index_.lower_bound(term);
if (i == index_.end())
return false;
@@ -225,20 +191,21 @@ bool BookmarkIndex::GetBookmarksMatchingTerm(
term, matching_algorithm)) {
// Term is too short for prefix match, compare using exact match.
if (i->first != term)
- return false; // No bookmarks with this term.
+ return false; // No title/URL pairs with this term.
if (first_term) {
(*matches) = i->second;
return true;
}
- *matches = base::STLSetIntersection<NodeSet>(i->second, *matches);
+ *matches = base::STLSetIntersection<TitledUrlNodeSet>(i->second, *matches);
} else {
// Loop through index adding all entries that start with term to
// |prefix_matches|.
- NodeSet tmp_prefix_matches;
+ TitledUrlNodeSet tmp_prefix_matches;
// If this is the first term, then store the result directly in |matches|
// to avoid calling stl intersection (which requires a copy).
- NodeSet* prefix_matches = first_term ? matches : &tmp_prefix_matches;
+ TitledUrlNodeSet* prefix_matches =
+ first_term ? matches : &tmp_prefix_matches;
while (i != index_.end() &&
i->first.size() >= term.size() &&
term.compare(0, term.size(), i->first, 0, term.size()) == 0) {
@@ -247,14 +214,17 @@ bool BookmarkIndex::GetBookmarksMatchingTerm(
#else
// Work around a bug in the implementation of std::set::insert in the STL
// used on android (http://crbug.com/367050).
- for (NodeSet::const_iterator n = i->second.begin(); n != i->second.end();
+ for (TitledUrlNodeSet::const_iterator n = i->second.begin();
+ n != i->second.end();
++n)
prefix_matches->insert(prefix_matches->end(), *n);
#endif
++i;
}
- if (!first_term)
- *matches = base::STLSetIntersection<NodeSet>(*prefix_matches, *matches);
+ if (!first_term) {
+ *matches =
+ base::STLSetIntersection<TitledUrlNodeSet>(*prefix_matches, *matches);
+ }
}
return !matches->empty();
}
@@ -272,16 +242,16 @@ std::vector<base::string16> BookmarkIndex::ExtractQueryWords(
}
void BookmarkIndex::RegisterNode(const base::string16& term,
- const BookmarkNode* node) {
+ const TitledUrlNode* node) {
index_[term].insert(node);
}
void BookmarkIndex::UnregisterNode(const base::string16& term,
- const BookmarkNode* node) {
+ const TitledUrlNode* node) {
Index::iterator i = index_.find(term);
if (i == index_.end()) {
// We can get here if the node has the same term more than once. For
- // example, a bookmark with the title 'foo foo' would end up here.
+ // example, a node with the title 'foo foo' would end up here.
return;
}
i->second.erase(node);

Powered by Google App Engine
This is Rietveld 408576698