1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// Copyright (c) 2010 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.
#include "chrome/browser/bookmarks/bookmark_index.h"
#include <algorithm>
#include <iterator>
#include <list>
#include "app/l10n_util.h"
#include "base/string16.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/history/history_database.h"
#include "chrome/browser/history/query_parser.h"
#include "chrome/browser/profile.h"
// Used when finding the set of bookmarks that match a query. Each match
// represents a set of terms (as an interator into the Index) matching the
// query as well as the set of nodes that contain those terms in their titles.
struct BookmarkIndex::Match {
// List of terms matching the query.
std::list<Index::const_iterator> terms;
// The set of nodes matching the terms. As an optimization this is empty
// when we match only one term, and is filled in when we get more than one
// term. We can do this as when we have only one matching term we know
// the set of matching nodes is terms.front()->second.
//
// Use nodes_begin() and nodes_end() to get an iterator over the set as
// it handles the necessary switching between nodes and terms.front().
NodeSet nodes;
// Returns an iterator to the beginning of the matching nodes. See
// description of nodes for why this should be used over nodes.begin().
NodeSet::const_iterator nodes_begin() const;
// Returns an iterator to the beginning of the matching nodes. See
// description of nodes for why this should be used over nodes.end().
NodeSet::const_iterator nodes_end() const;
};
BookmarkIndex::NodeSet::const_iterator
BookmarkIndex::Match::nodes_begin() const {
return nodes.empty() ? terms.front()->second.begin() : nodes.begin();
}
BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const {
return nodes.empty() ? terms.front()->second.end() : nodes.end();
}
BookmarkIndex::BookmarkIndex(Profile* profile) : profile_(profile) {
}
BookmarkIndex::~BookmarkIndex() {
}
void BookmarkIndex::Add(const BookmarkNode* node) {
if (!node->is_url())
return;
std::vector<string16> terms = ExtractQueryWords(node->GetTitle());
for (size_t i = 0; i < terms.size(); ++i)
RegisterNode(terms[i], node);
}
void BookmarkIndex::Remove(const BookmarkNode* node) {
if (!node->is_url())
return;
std::vector<string16> terms = ExtractQueryWords(node->GetTitle());
for (size_t i = 0; i < terms.size(); ++i)
UnregisterNode(terms[i], node);
}
void BookmarkIndex::GetBookmarksWithTitlesMatching(
const string16& query,
size_t max_count,
std::vector<bookmark_utils::TitleMatch>* results) {
std::vector<string16> terms = ExtractQueryWords(query);
if (terms.empty())
return;
Matches matches;
for (size_t i = 0; i < terms.size(); ++i) {
if (!GetBookmarksWithTitleMatchingTerm(terms[i], i == 0, &matches))
return;
}
NodeTypedCountPairs node_typed_counts;
SortMatches(matches, &node_typed_counts);
// We use a QueryParser to fill in match positions for us. It's not the most
// efficient way to go about this, but by the time we get here we know what
// matches and so this shouldn't be performance critical.
QueryParser parser;
ScopedVector<QueryNode> query_nodes;
parser.ParseQuery(query, &query_nodes.get());
// The highest typed counts should be at the beginning of the results vector
// so that the best matches will always be included in the results. The loop
// that calculates result relevance in HistoryContentsProvider::ConvertResults
// will run backwards to assure higher relevance will be attributed to the
// best matches.
for (NodeTypedCountPairs::const_iterator i = node_typed_counts.begin();
i != node_typed_counts.end() && results->size() < max_count; ++i)
AddMatchToResults(i->first, &parser, query_nodes.get(), results);
}
void BookmarkIndex::SortMatches(const Matches& matches,
NodeTypedCountPairs* node_typed_counts) const {
HistoryService* const history_service = profile_ ?
profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
history::URLDatabase* url_db = history_service ?
history_service->InMemoryDatabase() : NULL;
for (Matches::const_iterator i = matches.begin(); i != matches.end(); ++i)
ExtractBookmarkNodePairs(url_db, *i, node_typed_counts);
std::sort(node_typed_counts->begin(), node_typed_counts->end(),
&NodeTypedCountPairSortFunc);
}
void BookmarkIndex::ExtractBookmarkNodePairs(
history::URLDatabase* url_db,
const Match& match,
NodeTypedCountPairs* node_typed_counts) const {
for (NodeSet::const_iterator i = match.nodes_begin();
i != match.nodes_end(); ++i) {
history::URLRow url;
if (url_db)
url_db->GetRowForURL((*i)->GetURL(), &url);
NodeTypedCountPair pair(*i, url.typed_count());
node_typed_counts->push_back(pair);
}
}
void BookmarkIndex::AddMatchToResults(
const BookmarkNode* node,
QueryParser* parser,
const std::vector<QueryNode*>& query_nodes,
std::vector<bookmark_utils::TitleMatch>* results) {
bookmark_utils::TitleMatch title_match;
// 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"] is quoted we don't want to do a prefix match.
if (parser->DoesQueryMatch(node->GetTitle(), query_nodes,
&(title_match.match_positions))) {
title_match.node = node;
results->push_back(title_match);
}
}
bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const string16& term,
bool first_term,
Matches* matches) {
Index::const_iterator i = index_.lower_bound(term);
if (i == index_.end())
return false;
if (!QueryParser::IsWordLongEnoughForPrefixSearch(term)) {
// Term is too short for prefix match, compare using exact match.
if (i->first != term)
return false; // No bookmarks with this term.
if (first_term) {
Match match;
match.terms.push_back(i);
matches->push_back(match);
return true;
}
CombineMatchesInPlace(i, matches);
} else if (first_term) {
// This is the first term and we're doing a prefix match. Loop through
// index adding all entries that start with term to matches.
while (i != index_.end() &&
i->first.size() >= term.size() &&
term.compare(0, term.size(), i->first, 0, term.size()) == 0) {
Match match;
match.terms.push_back(i);
matches->push_back(match);
++i;
}
} else {
// Prefix match and not the first term. Loop through index combining
// current matches in matches with term, placing result in result.
Matches result;
while (i != index_.end() &&
i->first.size() >= term.size() &&
term.compare(0, term.size(), i->first, 0, term.size()) == 0) {
CombineMatches(i, *matches, &result);
++i;
}
matches->swap(result);
}
return !matches->empty();
}
void BookmarkIndex::CombineMatchesInPlace(const Index::const_iterator& index_i,
Matches* matches) {
for (size_t i = 0; i < matches->size(); ) {
Match* match = &((*matches)[i]);
NodeSet intersection;
std::set_intersection(match->nodes_begin(), match->nodes_end(),
index_i->second.begin(), index_i->second.end(),
std::inserter(intersection, intersection.begin()));
if (intersection.empty()) {
matches->erase(matches->begin() + i);
} else {
match->terms.push_back(index_i);
match->nodes.swap(intersection);
++i;
}
}
}
void BookmarkIndex::CombineMatches(const Index::const_iterator& index_i,
const Matches& current_matches,
Matches* result) {
for (size_t i = 0; i < current_matches.size(); ++i) {
const Match& match = current_matches[i];
NodeSet intersection;
std::set_intersection(match.nodes_begin(), match.nodes_end(),
index_i->second.begin(), index_i->second.end(),
std::inserter(intersection, intersection.begin()));
if (!intersection.empty()) {
result->push_back(Match());
Match& combined_match = result->back();
combined_match.terms = match.terms;
combined_match.terms.push_back(index_i);
combined_match.nodes.swap(intersection);
}
}
}
std::vector<string16> BookmarkIndex::ExtractQueryWords(const string16& query) {
std::vector<string16> terms;
if (query.empty())
return std::vector<string16>();
QueryParser parser;
// TODO: use ICU normalization.
parser.ExtractQueryWords(l10n_util::ToLower(query), &terms);
return terms;
}
void BookmarkIndex::RegisterNode(const string16& term,
const BookmarkNode* node) {
if (std::find(index_[term].begin(), index_[term].end(), node) !=
index_[term].end()) {
// We've already added node for term.
return;
}
index_[term].insert(node);
}
void BookmarkIndex::UnregisterNode(const string16& term,
const BookmarkNode* 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.
return;
}
i->second.erase(node);
if (i->second.empty())
index_.erase(i);
}
|