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

Side by Side Diff: components/drive/search_metadata.cc

Issue 1321553003: Files.app: split query into AND conditioned keywords in metadata search. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move import of scoped_vector. Created 5 years, 3 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/drive/search_metadata.h" 5 #include "components/drive/search_metadata.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <queue> 8 #include <queue>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/i18n/string_search.h" 11 #include "base/i18n/string_search.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h" 17 #include "base/time/time.h"
15 #include "components/drive/drive_api_util.h" 18 #include "components/drive/drive_api_util.h"
16 #include "components/drive/file_system_core_util.h" 19 #include "components/drive/file_system_core_util.h"
17 #include "net/base/escape.h" 20 #include "net/base/escape.h"
18 21
19 namespace drive { 22 namespace drive {
20 namespace internal { 23 namespace internal {
21 24
22 namespace { 25 namespace {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 137
135 private: 138 private:
136 ResourceMetadata* metadata_; 139 ResourceMetadata* metadata_;
137 140
138 // local ID to is_hidden map. 141 // local ID to is_hidden map.
139 std::map<std::string, bool> is_hiding_child_; 142 std::map<std::string, bool> is_hiding_child_;
140 }; 143 };
141 144
142 // Used to implement SearchMetadata. 145 // Used to implement SearchMetadata.
143 // Adds entry to the result when appropriate. 146 // Adds entry to the result when appropriate.
144 // In particular, if |query| is non-null, only adds files with the name matching 147 // In particular, if size of |queries| is larger than 0, only adds files with
145 // the query. 148 // the name matching the query.
146 FileError MaybeAddEntryToResult( 149 FileError MaybeAddEntryToResult(
147 ResourceMetadata* resource_metadata, 150 ResourceMetadata* resource_metadata,
148 ResourceMetadata::Iterator* it, 151 ResourceMetadata::Iterator* it,
149 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents* query, 152 const ScopedVector<
153 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>& queries,
150 const SearchMetadataPredicate& predicate, 154 const SearchMetadataPredicate& predicate,
151 size_t at_most_num_matches, 155 size_t at_most_num_matches,
152 HiddenEntryClassifier* hidden_entry_classifier, 156 HiddenEntryClassifier* hidden_entry_classifier,
153 ScopedPriorityQueue<ResultCandidate, ResultCandidateComparator>* 157 ScopedPriorityQueue<ResultCandidate, ResultCandidateComparator>*
154 result_candidates) { 158 result_candidates) {
155 DCHECK_GE(at_most_num_matches, result_candidates->size()); 159 DCHECK_GE(at_most_num_matches, result_candidates->size());
156 160
157 const ResourceEntry& entry = it->GetValue(); 161 const ResourceEntry& entry = it->GetValue();
158 162
159 // If the candidate set is already full, and this |entry| is old, do nothing. 163 // If the candidate set is already full, and this |entry| is old, do nothing.
160 // We perform this check first in order to avoid the costly find-and-highlight 164 // We perform this check first in order to avoid the costly find-and-highlight
161 // or FilePath lookup as much as possible. 165 // or FilePath lookup as much as possible.
162 if (result_candidates->size() == at_most_num_matches && 166 if (result_candidates->size() == at_most_num_matches &&
163 !CompareByTimestamp(entry, result_candidates->top()->entry)) 167 !CompareByTimestamp(entry, result_candidates->top()->entry))
164 return FILE_ERROR_OK; 168 return FILE_ERROR_OK;
165 169
166 // Add |entry| to the result if the entry is eligible for the given 170 // Add |entry| to the result if the entry is eligible for the given
167 // |options| and matches the query. The base name of the entry must 171 // |options| and matches the query. The base name of the entry must
168 // contain |query| to match the query. 172 // contain |query| to match the query.
169 std::string highlighted; 173 std::string highlighted;
170 if (!predicate.Run(entry) || 174 if (!predicate.Run(entry) ||
171 (query && !FindAndHighlight(entry.base_name(), query, &highlighted))) 175 (queries.size() > 0 &&
176 !FindAndHighlight(entry.base_name(), queries, &highlighted)))
172 return FILE_ERROR_OK; 177 return FILE_ERROR_OK;
173 178
174 // Hidden entry should not be returned. 179 // Hidden entry should not be returned.
175 bool hidden = false; 180 bool hidden = false;
176 FileError error = hidden_entry_classifier->IsHidden(entry, &hidden); 181 FileError error = hidden_entry_classifier->IsHidden(entry, &hidden);
177 if (error != FILE_ERROR_OK || hidden) 182 if (error != FILE_ERROR_OK || hidden)
178 return error; 183 return error;
179 184
180 // Make space for |entry| when appropriate. 185 // Make space for |entry| when appropriate.
181 if (result_candidates->size() == at_most_num_matches) 186 if (result_candidates->size() == at_most_num_matches)
182 result_candidates->pop(); 187 result_candidates->pop();
183 result_candidates->push(new ResultCandidate(it->GetID(), entry, highlighted)); 188 result_candidates->push(new ResultCandidate(it->GetID(), entry, highlighted));
184 return FILE_ERROR_OK; 189 return FILE_ERROR_OK;
185 } 190 }
186 191
187 // Implements SearchMetadata(). 192 // Implements SearchMetadata().
188 FileError SearchMetadataOnBlockingPool(ResourceMetadata* resource_metadata, 193 FileError SearchMetadataOnBlockingPool(ResourceMetadata* resource_metadata,
189 const std::string& query_text, 194 const std::string& query_text,
190 const SearchMetadataPredicate& predicate, 195 const SearchMetadataPredicate& predicate,
191 int at_most_num_matches, 196 int at_most_num_matches,
192 MetadataSearchResultVector* results) { 197 MetadataSearchResultVector* results) {
193 ScopedPriorityQueue<ResultCandidate, 198 ScopedPriorityQueue<ResultCandidate,
194 ResultCandidateComparator> result_candidates; 199 ResultCandidateComparator> result_candidates;
195 200
196 // Prepare data structure for searching. 201 // Prepare data structure for searching.
197 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents query( 202 std::vector<base::string16> keywords =
198 base::UTF8ToUTF16(query_text)); 203 base::SplitString(base::UTF8ToUTF16(query_text),
204 base::StringPiece16(base::kWhitespaceUTF16),
205 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
206
207 ScopedVector<base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>
208 queries;
209 for (const auto& keyword : keywords) {
210 queries.push_back(
211 new base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents(
212 keyword));
213 }
199 214
200 // Prepare an object to filter out hidden entries. 215 // Prepare an object to filter out hidden entries.
201 ResourceEntry mydrive; 216 ResourceEntry mydrive;
202 FileError error = resource_metadata->GetResourceEntryByPath( 217 FileError error = resource_metadata->GetResourceEntryByPath(
203 util::GetDriveMyDriveRootPath(), &mydrive); 218 util::GetDriveMyDriveRootPath(), &mydrive);
204 if (error != FILE_ERROR_OK) 219 if (error != FILE_ERROR_OK)
205 return error; 220 return error;
206 HiddenEntryClassifier hidden_entry_classifier(resource_metadata, 221 HiddenEntryClassifier hidden_entry_classifier(resource_metadata,
207 mydrive.local_id()); 222 mydrive.local_id());
208 223
209 // Iterate over entries. 224 // Iterate over entries.
210 scoped_ptr<ResourceMetadata::Iterator> it = resource_metadata->GetIterator(); 225 scoped_ptr<ResourceMetadata::Iterator> it = resource_metadata->GetIterator();
211 for (; !it->IsAtEnd(); it->Advance()) { 226 for (; !it->IsAtEnd(); it->Advance()) {
212 FileError error = MaybeAddEntryToResult( 227 FileError error = MaybeAddEntryToResult(
213 resource_metadata, it.get(), query_text.empty() ? NULL : &query, 228 resource_metadata, it.get(), queries, predicate, at_most_num_matches,
214 predicate, at_most_num_matches, &hidden_entry_classifier, 229 &hidden_entry_classifier, &result_candidates);
215 &result_candidates);
216 if (error != FILE_ERROR_OK) 230 if (error != FILE_ERROR_OK)
217 return error; 231 return error;
218 } 232 }
219 233
220 // Prepare the result. 234 // Prepare the result.
221 for (; !result_candidates.empty(); result_candidates.pop()) { 235 for (; !result_candidates.empty(); result_candidates.pop()) {
222 const ResultCandidate& candidate = *result_candidates.top(); 236 const ResultCandidate& candidate = *result_candidates.top();
223 // The path field of entries in result_candidates are empty at this point, 237 // The path field of entries in result_candidates are empty at this point,
224 // because we don't want to run the expensive metadata DB look up except for 238 // because we don't want to run the expensive metadata DB look up except for
225 // the final results. Hence, here we fill the part. 239 // the final results. Hence, here we fill the part.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 } else { 315 } else {
302 return entry.file_specific_info().cache_state().is_present(); 316 return entry.file_specific_info().cache_state().is_present();
303 } 317 }
304 } 318 }
305 319
306 return true; 320 return true;
307 } 321 }
308 322
309 bool FindAndHighlight( 323 bool FindAndHighlight(
310 const std::string& text, 324 const std::string& text,
311 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents* query, 325 const ScopedVector<
326 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>& queries,
312 std::string* highlighted_text) { 327 std::string* highlighted_text) {
313 DCHECK(query);
314 DCHECK(highlighted_text); 328 DCHECK(highlighted_text);
315 highlighted_text->clear(); 329 highlighted_text->clear();
316 330
317 base::string16 text16 = base::UTF8ToUTF16(text); 331 // Empty query does not match with anything.
332 if (text.empty())
hashimoto 2015/09/07 06:17:09 The comment says "empty query", but here the empti
yawano 2015/09/08 10:27:21 We should have checked empty query. Fixed and adde
333 return false;
334
335 // Check text matches with all queries.
318 size_t match_start = 0; 336 size_t match_start = 0;
319 size_t match_length = 0; 337 size_t match_length = 0;
320 if (!query->Search(text16, &match_start, &match_length))
321 return false;
322 338
323 base::string16 pre = text16.substr(0, match_start); 339 base::string16 text16 = base::UTF8ToUTF16(text);
324 base::string16 match = text16.substr(match_start, match_length); 340 std::vector<bool> highlights(text16.size(), false);
325 base::string16 post = text16.substr(match_start + match_length); 341 for (auto* query : queries) {
326 highlighted_text->append(net::EscapeForHTML(base::UTF16ToUTF8(pre))); 342 if (!query->Search(text16, &match_start, &match_length))
327 highlighted_text->append("<b>"); 343 return false;
328 highlighted_text->append(net::EscapeForHTML(base::UTF16ToUTF8(match))); 344
329 highlighted_text->append("</b>"); 345 size_t match_end = match_start + match_length;
330 highlighted_text->append(net::EscapeForHTML(base::UTF16ToUTF8(post))); 346 for (size_t i = match_start; i < match_end; ++i) {
347 highlights[i] = true;
348 }
hashimoto 2015/09/07 06:17:09 nit: You can do std::fill(highlights.begin() + mat
yawano 2015/09/08 10:27:21 Done.
349 }
350
351 // Generate highlighted text.
352 bool highlighting = false;
353 size_t start = 0;
hashimoto 2015/09/07 06:17:09 nit: |start| sounds a bit ambiguous. How about a m
yawano 2015/09/08 10:27:21 Done.
354
355 for (size_t i = 0; i < text16.size(); ++i) {
356 if (highlighting == highlights[i])
357 continue;
358
359 // Append [start, i).
360 if (i > start) {
hashimoto 2015/09/07 06:17:09 Do we need this check? IIUC this is only needed if
yawano 2015/09/08 10:27:21 Done.
361 highlighted_text->append(net::EscapeForHTML(
362 base::UTF16ToUTF8(text16.substr(start, i - start))));
363 }
364
365 highlighted_text->append(highlighting ? "</b>" : "<b>");
hashimoto 2015/09/07 06:17:09 I found this line a bit difficult to understand. H
yawano 2015/09/08 10:27:21 Done.
366
367 start = i;
368 highlighting = highlights[i];
369 }
370
371 DCHECK_GE(text16.size(), start);
372 highlighted_text->append(net::EscapeForHTML(
373 base::UTF16ToUTF8(text16.substr(start, text16.size() - start))));
374
375 if (highlighting)
376 highlighted_text->append("</b>");
377
331 return true; 378 return true;
332 } 379 }
333 380
334 } // namespace internal 381 } // namespace internal
335 } // namespace drive 382 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698