| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/devtools/devtools_file_system_indexer.h" | 5 #include "chrome/browser/devtools/devtools_file_system_indexer.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 130 |
| 131 Index::Index() : last_file_id_(0) { | 131 Index::Index() : last_file_id_(0) { |
| 132 index_.resize(kTrigramCount); | 132 index_.resize(kTrigramCount); |
| 133 is_normalized_.resize(kTrigramCount); | 133 is_normalized_.resize(kTrigramCount); |
| 134 std::fill(is_normalized_.begin(), is_normalized_.end(), true); | 134 std::fill(is_normalized_.begin(), is_normalized_.end(), true); |
| 135 } | 135 } |
| 136 | 136 |
| 137 Index::~Index() {} | 137 Index::~Index() {} |
| 138 | 138 |
| 139 Time Index::LastModifiedTimeForFile(const FilePath& file_path) { | 139 Time Index::LastModifiedTimeForFile(const FilePath& file_path) { |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 140 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 141 Time last_modified_time; | 141 Time last_modified_time; |
| 142 if (index_times_.find(file_path) != index_times_.end()) | 142 if (index_times_.find(file_path) != index_times_.end()) |
| 143 last_modified_time = index_times_[file_path]; | 143 last_modified_time = index_times_[file_path]; |
| 144 return last_modified_time; | 144 return last_modified_time; |
| 145 } | 145 } |
| 146 | 146 |
| 147 void Index::SetTrigramsForFile(const FilePath& file_path, | 147 void Index::SetTrigramsForFile(const FilePath& file_path, |
| 148 const vector<Trigram>& index, | 148 const vector<Trigram>& index, |
| 149 const Time& time) { | 149 const Time& time) { |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 150 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 151 FileId file_id = GetFileId(file_path); | 151 FileId file_id = GetFileId(file_path); |
| 152 vector<Trigram>::const_iterator it = index.begin(); | 152 vector<Trigram>::const_iterator it = index.begin(); |
| 153 for (; it != index.end(); ++it) { | 153 for (; it != index.end(); ++it) { |
| 154 Trigram trigram = *it; | 154 Trigram trigram = *it; |
| 155 index_[trigram].push_back(file_id); | 155 index_[trigram].push_back(file_id); |
| 156 is_normalized_[trigram] = false; | 156 is_normalized_[trigram] = false; |
| 157 } | 157 } |
| 158 index_times_[file_path] = time; | 158 index_times_[file_path] = time; |
| 159 } | 159 } |
| 160 | 160 |
| 161 vector<FilePath> Index::Search(string query) { | 161 vector<FilePath> Index::Search(string query) { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 162 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 163 const char* data = query.c_str(); | 163 const char* data = query.c_str(); |
| 164 vector<TrigramChar> trigram_chars; | 164 vector<TrigramChar> trigram_chars; |
| 165 trigram_chars.reserve(query.size()); | 165 trigram_chars.reserve(query.size()); |
| 166 for (size_t i = 0; i < query.size(); ++i) { | 166 for (size_t i = 0; i < query.size(); ++i) { |
| 167 TrigramChar trigram_char = TrigramCharForChar(data[i]); | 167 TrigramChar trigram_char = TrigramCharForChar(data[i]); |
| 168 if (trigram_char == kBinaryTrigramChar) | 168 if (trigram_char == kBinaryTrigramChar) |
| 169 trigram_char = kUndefinedTrigramChar; | 169 trigram_char = kUndefinedTrigramChar; |
| 170 trigram_chars.push_back(trigram_char); | 170 trigram_chars.push_back(trigram_char); |
| 171 } | 171 } |
| 172 vector<Trigram> trigrams; | 172 vector<Trigram> trigrams; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 196 for (; ids_it != file_ids_.end(); ++ids_it) { | 196 for (; ids_it != file_ids_.end(); ++ids_it) { |
| 197 if (trigrams.size() == 0 || | 197 if (trigrams.size() == 0 || |
| 198 file_ids.find(ids_it->second) != file_ids.end()) { | 198 file_ids.find(ids_it->second) != file_ids.end()) { |
| 199 result.push_back(ids_it->first); | 199 result.push_back(ids_it->first); |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 return result; | 202 return result; |
| 203 } | 203 } |
| 204 | 204 |
| 205 FileId Index::GetFileId(const FilePath& file_path) { | 205 FileId Index::GetFileId(const FilePath& file_path) { |
| 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 206 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 207 string file_path_str = file_path.AsUTF8Unsafe(); | 207 string file_path_str = file_path.AsUTF8Unsafe(); |
| 208 if (file_ids_.find(file_path) != file_ids_.end()) | 208 if (file_ids_.find(file_path) != file_ids_.end()) |
| 209 return file_ids_[file_path]; | 209 return file_ids_[file_path]; |
| 210 file_ids_[file_path] = ++last_file_id_; | 210 file_ids_[file_path] = ++last_file_id_; |
| 211 return last_file_id_; | 211 return last_file_id_; |
| 212 } | 212 } |
| 213 | 213 |
| 214 void Index::NormalizeVectors() { | 214 void Index::NormalizeVectors() { |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 215 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 216 for (size_t i = 0; i < kTrigramCount; ++i) { | 216 for (size_t i = 0; i < kTrigramCount; ++i) { |
| 217 if (!is_normalized_[i]) { | 217 if (!is_normalized_[i]) { |
| 218 std::sort(index_[i].begin(), index_[i].end()); | 218 std::sort(index_[i].begin(), index_[i].end()); |
| 219 if (index_[i].capacity() > index_[i].size()) | 219 if (index_[i].capacity() > index_[i].size()) |
| 220 vector<FileId>(index_[i]).swap(index_[i]); | 220 vector<FileId>(index_[i]).swap(index_[i]); |
| 221 is_normalized_[i] = true; | 221 is_normalized_[i] = true; |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 | 225 |
| 226 void Index::PrintStats() { | 226 void Index::PrintStats() { |
| 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 227 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 228 LOG(ERROR) << "Index stats:"; | 228 LOG(ERROR) << "Index stats:"; |
| 229 size_t size = 0; | 229 size_t size = 0; |
| 230 size_t maxSize = 0; | 230 size_t maxSize = 0; |
| 231 size_t capacity = 0; | 231 size_t capacity = 0; |
| 232 for (size_t i = 0; i < kTrigramCount; ++i) { | 232 for (size_t i = 0; i < kTrigramCount; ++i) { |
| 233 if (index_[i].size() > maxSize) | 233 if (index_[i].size() > maxSize) |
| 234 maxSize = index_[i].size(); | 234 maxSize = index_[i].size(); |
| 235 size += index_[i].size(); | 235 size += index_[i].size(); |
| 236 capacity += index_[i].capacity(); | 236 capacity += index_[i].capacity(); |
| 237 } | 237 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 260 BrowserThread::FILE).get()), | 260 BrowserThread::FILE).get()), |
| 261 files_indexed_(0), | 261 files_indexed_(0), |
| 262 stopped_(false) { | 262 stopped_(false) { |
| 263 current_trigrams_set_.resize(kTrigramCount); | 263 current_trigrams_set_.resize(kTrigramCount); |
| 264 current_trigrams_.reserve(kTrigramCount); | 264 current_trigrams_.reserve(kTrigramCount); |
| 265 } | 265 } |
| 266 | 266 |
| 267 DevToolsFileSystemIndexer::FileSystemIndexingJob::~FileSystemIndexingJob() {} | 267 DevToolsFileSystemIndexer::FileSystemIndexingJob::~FileSystemIndexingJob() {} |
| 268 | 268 |
| 269 void DevToolsFileSystemIndexer::FileSystemIndexingJob::Start() { | 269 void DevToolsFileSystemIndexer::FileSystemIndexingJob::Start() { |
| 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 270 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 271 BrowserThread::PostTask( | 271 BrowserThread::PostTask( |
| 272 BrowserThread::FILE, | 272 BrowserThread::FILE, |
| 273 FROM_HERE, | 273 FROM_HERE, |
| 274 Bind(&FileSystemIndexingJob::CollectFilesToIndex, this)); | 274 Bind(&FileSystemIndexingJob::CollectFilesToIndex, this)); |
| 275 } | 275 } |
| 276 | 276 |
| 277 void DevToolsFileSystemIndexer::FileSystemIndexingJob::Stop() { | 277 void DevToolsFileSystemIndexer::FileSystemIndexingJob::Stop() { |
| 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 278 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 279 BrowserThread::PostTask(BrowserThread::FILE, | 279 BrowserThread::PostTask(BrowserThread::FILE, |
| 280 FROM_HERE, | 280 FROM_HERE, |
| 281 Bind(&FileSystemIndexingJob::StopOnFileThread, this)); | 281 Bind(&FileSystemIndexingJob::StopOnFileThread, this)); |
| 282 } | 282 } |
| 283 | 283 |
| 284 void DevToolsFileSystemIndexer::FileSystemIndexingJob::StopOnFileThread() { | 284 void DevToolsFileSystemIndexer::FileSystemIndexingJob::StopOnFileThread() { |
| 285 stopped_ = true; | 285 stopped_ = true; |
| 286 } | 286 } |
| 287 | 287 |
| 288 void DevToolsFileSystemIndexer::FileSystemIndexingJob::CollectFilesToIndex() { | 288 void DevToolsFileSystemIndexer::FileSystemIndexingJob::CollectFilesToIndex() { |
| 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 289 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 290 if (stopped_) | 290 if (stopped_) |
| 291 return; | 291 return; |
| 292 if (!file_enumerator_) { | 292 if (!file_enumerator_) { |
| 293 file_enumerator_.reset( | 293 file_enumerator_.reset( |
| 294 new FileEnumerator(file_system_path_, true, FileEnumerator::FILES)); | 294 new FileEnumerator(file_system_path_, true, FileEnumerator::FILES)); |
| 295 } | 295 } |
| 296 FilePath file_path = file_enumerator_->Next(); | 296 FilePath file_path = file_enumerator_->Next(); |
| 297 if (file_path.empty()) { | 297 if (file_path.empty()) { |
| 298 BrowserThread::PostTask( | 298 BrowserThread::PostTask( |
| 299 BrowserThread::UI, | 299 BrowserThread::UI, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 310 if (current_last_modified_time > saved_last_modified_time) { | 310 if (current_last_modified_time > saved_last_modified_time) { |
| 311 file_path_times_[file_path] = current_last_modified_time; | 311 file_path_times_[file_path] = current_last_modified_time; |
| 312 } | 312 } |
| 313 BrowserThread::PostTask( | 313 BrowserThread::PostTask( |
| 314 BrowserThread::FILE, | 314 BrowserThread::FILE, |
| 315 FROM_HERE, | 315 FROM_HERE, |
| 316 Bind(&FileSystemIndexingJob::CollectFilesToIndex, this)); | 316 Bind(&FileSystemIndexingJob::CollectFilesToIndex, this)); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void DevToolsFileSystemIndexer::FileSystemIndexingJob::IndexFiles() { | 319 void DevToolsFileSystemIndexer::FileSystemIndexingJob::IndexFiles() { |
| 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 320 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 321 if (stopped_) | 321 if (stopped_) |
| 322 return; | 322 return; |
| 323 if (indexing_it_ == file_path_times_.end()) { | 323 if (indexing_it_ == file_path_times_.end()) { |
| 324 g_trigram_index.Get().NormalizeVectors(); | 324 g_trigram_index.Get().NormalizeVectors(); |
| 325 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_callback_); | 325 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_callback_); |
| 326 return; | 326 return; |
| 327 } | 327 } |
| 328 FilePath file_path = indexing_it_->first; | 328 FilePath file_path = indexing_it_->first; |
| 329 current_file_.CreateOrOpen( | 329 current_file_.CreateOrOpen( |
| 330 file_path, | 330 file_path, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 current_trigrams_set_[trigram] = true; | 386 current_trigrams_set_[trigram] = true; |
| 387 current_trigrams_.push_back(trigram); | 387 current_trigrams_.push_back(trigram); |
| 388 } | 388 } |
| 389 } | 389 } |
| 390 current_file_offset_ += bytes_read - 2; | 390 current_file_offset_ += bytes_read - 2; |
| 391 ReadFromFile(); | 391 ReadFromFile(); |
| 392 } | 392 } |
| 393 | 393 |
| 394 void DevToolsFileSystemIndexer::FileSystemIndexingJob::FinishFileIndexing( | 394 void DevToolsFileSystemIndexer::FileSystemIndexingJob::FinishFileIndexing( |
| 395 bool success) { | 395 bool success) { |
| 396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 396 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 397 CloseFile(); | 397 CloseFile(); |
| 398 if (success) { | 398 if (success) { |
| 399 FilePath file_path = indexing_it_->first; | 399 FilePath file_path = indexing_it_->first; |
| 400 g_trigram_index.Get().SetTrigramsForFile( | 400 g_trigram_index.Get().SetTrigramsForFile( |
| 401 file_path, current_trigrams_, file_path_times_[file_path]); | 401 file_path, current_trigrams_, file_path_times_[file_path]); |
| 402 } | 402 } |
| 403 ReportWorked(); | 403 ReportWorked(); |
| 404 ++indexing_it_; | 404 ++indexing_it_; |
| 405 IndexFiles(); | 405 IndexFiles(); |
| 406 } | 406 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 434 } | 434 } |
| 435 | 435 |
| 436 DevToolsFileSystemIndexer::~DevToolsFileSystemIndexer() {} | 436 DevToolsFileSystemIndexer::~DevToolsFileSystemIndexer() {} |
| 437 | 437 |
| 438 scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob> | 438 scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob> |
| 439 DevToolsFileSystemIndexer::IndexPath( | 439 DevToolsFileSystemIndexer::IndexPath( |
| 440 const string& file_system_path, | 440 const string& file_system_path, |
| 441 const TotalWorkCallback& total_work_callback, | 441 const TotalWorkCallback& total_work_callback, |
| 442 const WorkedCallback& worked_callback, | 442 const WorkedCallback& worked_callback, |
| 443 const DoneCallback& done_callback) { | 443 const DoneCallback& done_callback) { |
| 444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 444 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 445 scoped_refptr<FileSystemIndexingJob> indexing_job = | 445 scoped_refptr<FileSystemIndexingJob> indexing_job = |
| 446 new FileSystemIndexingJob(FilePath::FromUTF8Unsafe(file_system_path), | 446 new FileSystemIndexingJob(FilePath::FromUTF8Unsafe(file_system_path), |
| 447 total_work_callback, | 447 total_work_callback, |
| 448 worked_callback, | 448 worked_callback, |
| 449 done_callback); | 449 done_callback); |
| 450 indexing_job->Start(); | 450 indexing_job->Start(); |
| 451 return indexing_job; | 451 return indexing_job; |
| 452 } | 452 } |
| 453 | 453 |
| 454 void DevToolsFileSystemIndexer::SearchInPath(const string& file_system_path, | 454 void DevToolsFileSystemIndexer::SearchInPath(const string& file_system_path, |
| 455 const string& query, | 455 const string& query, |
| 456 const SearchCallback& callback) { | 456 const SearchCallback& callback) { |
| 457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 457 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 458 BrowserThread::PostTask( | 458 BrowserThread::PostTask( |
| 459 BrowserThread::FILE, | 459 BrowserThread::FILE, |
| 460 FROM_HERE, | 460 FROM_HERE, |
| 461 Bind(&DevToolsFileSystemIndexer::SearchInPathOnFileThread, | 461 Bind(&DevToolsFileSystemIndexer::SearchInPathOnFileThread, |
| 462 this, | 462 this, |
| 463 file_system_path, | 463 file_system_path, |
| 464 query, | 464 query, |
| 465 callback)); | 465 callback)); |
| 466 } | 466 } |
| 467 | 467 |
| 468 void DevToolsFileSystemIndexer::SearchInPathOnFileThread( | 468 void DevToolsFileSystemIndexer::SearchInPathOnFileThread( |
| 469 const string& file_system_path, | 469 const string& file_system_path, |
| 470 const string& query, | 470 const string& query, |
| 471 const SearchCallback& callback) { | 471 const SearchCallback& callback) { |
| 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 472 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 473 vector<FilePath> file_paths = g_trigram_index.Get().Search(query); | 473 vector<FilePath> file_paths = g_trigram_index.Get().Search(query); |
| 474 vector<string> result; | 474 vector<string> result; |
| 475 FilePath path = FilePath::FromUTF8Unsafe(file_system_path); | 475 FilePath path = FilePath::FromUTF8Unsafe(file_system_path); |
| 476 vector<FilePath>::const_iterator it = file_paths.begin(); | 476 vector<FilePath>::const_iterator it = file_paths.begin(); |
| 477 for (; it != file_paths.end(); ++it) { | 477 for (; it != file_paths.end(); ++it) { |
| 478 if (path.IsParent(*it)) | 478 if (path.IsParent(*it)) |
| 479 result.push_back(it->AsUTF8Unsafe()); | 479 result.push_back(it->AsUTF8Unsafe()); |
| 480 } | 480 } |
| 481 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, Bind(callback, result)); | 481 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, Bind(callback, result)); |
| 482 } | 482 } |
| OLD | NEW |