| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 const size_t kTrigramCount = | 47 const size_t kTrigramCount = |
| 48 kTrigramCharacterCount * kTrigramCharacterCount * kTrigramCharacterCount; | 48 kTrigramCharacterCount * kTrigramCharacterCount * kTrigramCharacterCount; |
| 49 const int kMaxReadLength = 10 * 1024; | 49 const int kMaxReadLength = 10 * 1024; |
| 50 const TrigramChar kUndefinedTrigramChar = -1; | 50 const TrigramChar kUndefinedTrigramChar = -1; |
| 51 const TrigramChar kBinaryTrigramChar = -2; | 51 const TrigramChar kBinaryTrigramChar = -2; |
| 52 const Trigram kUndefinedTrigram = -1; | 52 const Trigram kUndefinedTrigram = -1; |
| 53 | 53 |
| 54 class Index { | 54 class Index { |
| 55 public: | 55 public: |
| 56 Index(); | 56 Index(); |
| 57 // Index is only instantiated as a leak LazyInstance, so the destructor is |
| 58 // never called. |
| 59 ~Index() = delete; |
| 60 |
| 57 Time LastModifiedTimeForFile(const FilePath& file_path); | 61 Time LastModifiedTimeForFile(const FilePath& file_path); |
| 58 void SetTrigramsForFile(const FilePath& file_path, | 62 void SetTrigramsForFile(const FilePath& file_path, |
| 59 const vector<Trigram>& index, | 63 const vector<Trigram>& index, |
| 60 const Time& time); | 64 const Time& time); |
| 61 vector<FilePath> Search(string query); | 65 vector<FilePath> Search(string query); |
| 62 void PrintStats(); | |
| 63 void NormalizeVectors(); | 66 void NormalizeVectors(); |
| 64 | 67 |
| 65 private: | 68 private: |
| 66 ~Index(); | |
| 67 | |
| 68 FileId GetFileId(const FilePath& file_path); | 69 FileId GetFileId(const FilePath& file_path); |
| 69 | 70 |
| 70 typedef map<FilePath, FileId> FileIdsMap; | 71 typedef map<FilePath, FileId> FileIdsMap; |
| 71 FileIdsMap file_ids_; | 72 FileIdsMap file_ids_; |
| 72 FileId last_file_id_; | 73 FileId last_file_id_; |
| 73 // The index in this vector is the trigram id. | 74 // The index in this vector is the trigram id. |
| 74 vector<vector<FileId> > index_; | 75 vector<vector<FileId> > index_; |
| 75 typedef map<FilePath, Time> IndexedFilesMap; | 76 typedef map<FilePath, Time> IndexedFilesMap; |
| 76 IndexedFilesMap index_times_; | 77 IndexedFilesMap index_times_; |
| 77 vector<bool> is_normalized_; | 78 vector<bool> is_normalized_; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 trigram_chars[index + 2]; | 132 trigram_chars[index + 2]; |
| 132 return trigram; | 133 return trigram; |
| 133 } | 134 } |
| 134 | 135 |
| 135 Index::Index() : last_file_id_(0) { | 136 Index::Index() : last_file_id_(0) { |
| 136 index_.resize(kTrigramCount); | 137 index_.resize(kTrigramCount); |
| 137 is_normalized_.resize(kTrigramCount); | 138 is_normalized_.resize(kTrigramCount); |
| 138 std::fill(is_normalized_.begin(), is_normalized_.end(), true); | 139 std::fill(is_normalized_.begin(), is_normalized_.end(), true); |
| 139 } | 140 } |
| 140 | 141 |
| 141 Index::~Index() {} | |
| 142 | |
| 143 Time Index::LastModifiedTimeForFile(const FilePath& file_path) { | 142 Time Index::LastModifiedTimeForFile(const FilePath& file_path) { |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 143 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 145 Time last_modified_time; | 144 Time last_modified_time; |
| 146 if (index_times_.find(file_path) != index_times_.end()) | 145 if (index_times_.find(file_path) != index_times_.end()) |
| 147 last_modified_time = index_times_[file_path]; | 146 last_modified_time = index_times_[file_path]; |
| 148 return last_modified_time; | 147 return last_modified_time; |
| 149 } | 148 } |
| 150 | 149 |
| 151 void Index::SetTrigramsForFile(const FilePath& file_path, | 150 void Index::SetTrigramsForFile(const FilePath& file_path, |
| 152 const vector<Trigram>& index, | 151 const vector<Trigram>& index, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 for (size_t i = 0; i < kTrigramCount; ++i) { | 219 for (size_t i = 0; i < kTrigramCount; ++i) { |
| 221 if (!is_normalized_[i]) { | 220 if (!is_normalized_[i]) { |
| 222 std::sort(index_[i].begin(), index_[i].end()); | 221 std::sort(index_[i].begin(), index_[i].end()); |
| 223 if (index_[i].capacity() > index_[i].size()) | 222 if (index_[i].capacity() > index_[i].size()) |
| 224 vector<FileId>(index_[i]).swap(index_[i]); | 223 vector<FileId>(index_[i]).swap(index_[i]); |
| 225 is_normalized_[i] = true; | 224 is_normalized_[i] = true; |
| 226 } | 225 } |
| 227 } | 226 } |
| 228 } | 227 } |
| 229 | 228 |
| 230 void Index::PrintStats() { | |
| 231 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 232 LOG(ERROR) << "Index stats:"; | |
| 233 size_t size = 0; | |
| 234 size_t maxSize = 0; | |
| 235 size_t capacity = 0; | |
| 236 for (size_t i = 0; i < kTrigramCount; ++i) { | |
| 237 if (index_[i].size() > maxSize) | |
| 238 maxSize = index_[i].size(); | |
| 239 size += index_[i].size(); | |
| 240 capacity += index_[i].capacity(); | |
| 241 } | |
| 242 LOG(ERROR) << " - total trigram count: " << size; | |
| 243 LOG(ERROR) << " - max file count per trigram: " << maxSize; | |
| 244 LOG(ERROR) << " - total vectors capacity " << capacity; | |
| 245 size_t total_index_size = | |
| 246 capacity * sizeof(FileId) + sizeof(vector<FileId>) * kTrigramCount; | |
| 247 LOG(ERROR) << " - estimated total index size " << total_index_size; | |
| 248 } | |
| 249 | |
| 250 typedef Callback<void(bool, const vector<bool>&)> IndexerCallback; | 229 typedef Callback<void(bool, const vector<bool>&)> IndexerCallback; |
| 251 | 230 |
| 252 } // namespace | 231 } // namespace |
| 253 | 232 |
| 254 DevToolsFileSystemIndexer::FileSystemIndexingJob::FileSystemIndexingJob( | 233 DevToolsFileSystemIndexer::FileSystemIndexingJob::FileSystemIndexingJob( |
| 255 const FilePath& file_system_path, | 234 const FilePath& file_system_path, |
| 256 const TotalWorkCallback& total_work_callback, | 235 const TotalWorkCallback& total_work_callback, |
| 257 const WorkedCallback& worked_callback, | 236 const WorkedCallback& worked_callback, |
| 258 const DoneCallback& done_callback) | 237 const DoneCallback& done_callback) |
| 259 : file_system_path_(file_system_path), | 238 : file_system_path_(file_system_path), |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 vector<FilePath> file_paths = g_trigram_index.Get().Search(query); | 456 vector<FilePath> file_paths = g_trigram_index.Get().Search(query); |
| 478 vector<string> result; | 457 vector<string> result; |
| 479 FilePath path = FilePath::FromUTF8Unsafe(file_system_path); | 458 FilePath path = FilePath::FromUTF8Unsafe(file_system_path); |
| 480 vector<FilePath>::const_iterator it = file_paths.begin(); | 459 vector<FilePath>::const_iterator it = file_paths.begin(); |
| 481 for (; it != file_paths.end(); ++it) { | 460 for (; it != file_paths.end(); ++it) { |
| 482 if (path.IsParent(*it)) | 461 if (path.IsParent(*it)) |
| 483 result.push_back(it->AsUTF8Unsafe()); | 462 result.push_back(it->AsUTF8Unsafe()); |
| 484 } | 463 } |
| 485 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, Bind(callback, result)); | 464 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, Bind(callback, result)); |
| 486 } | 465 } |
| OLD | NEW |