OLD | NEW |
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 "net/disk_cache/simple/simple_index.h" | 5 #include "net/disk_cache/simple/simple_index.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 70 } |
71 | 71 |
72 SimpleIndex::SimpleIndex( | 72 SimpleIndex::SimpleIndex( |
73 const scoped_refptr<base::TaskRunner>& cache_thread, | 73 const scoped_refptr<base::TaskRunner>& cache_thread, |
74 const scoped_refptr<base::TaskRunner>& io_thread, | 74 const scoped_refptr<base::TaskRunner>& io_thread, |
75 const base::FilePath& path) | 75 const base::FilePath& path) |
76 : cache_size_(0), | 76 : cache_size_(0), |
77 initialized_(false), | 77 initialized_(false), |
78 index_filename_(path.AppendASCII("simple-index")), | 78 index_filename_(path.AppendASCII("simple-index")), |
79 cache_thread_(cache_thread), | 79 cache_thread_(cache_thread), |
80 io_thread_(io_thread) {} | 80 io_thread_(io_thread) { |
| 81 } |
81 | 82 |
82 SimpleIndex::~SimpleIndex() { | 83 SimpleIndex::~SimpleIndex() { |
83 DCHECK(io_thread_checker_.CalledOnValidThread()); | 84 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 85 |
| 86 // Fail all callbacks waiting for the index to come up. |
| 87 for (CallbackList::iterator it = to_run_when_initialized_.begin(), |
| 88 end = to_run_when_initialized_.end(); it != end; ++it) { |
| 89 it->Run(net::ERR_ABORTED); |
| 90 } |
84 } | 91 } |
85 | 92 |
86 void SimpleIndex::Initialize() { | 93 void SimpleIndex::Initialize() { |
87 DCHECK(io_thread_checker_.CalledOnValidThread()); | 94 DCHECK(io_thread_checker_.CalledOnValidThread()); |
88 IndexCompletionCallback merge_callback = | 95 IndexCompletionCallback merge_callback = |
89 base::Bind(&SimpleIndex::MergeInitializingSet, this); | 96 base::Bind(&SimpleIndex::MergeInitializingSet, this); |
90 base::WorkerPool::PostTask(FROM_HERE, | 97 base::WorkerPool::PostTask(FROM_HERE, |
91 base::Bind(&SimpleIndex::LoadFromDisk, | 98 base::Bind(&SimpleIndex::LoadFromDisk, |
92 index_filename_, | 99 index_filename_, |
93 io_thread_, | 100 io_thread_, |
94 merge_callback), | 101 merge_callback), |
95 true); | 102 true); |
96 } | 103 } |
97 | 104 |
| 105 int SimpleIndex::ExecuteWhenReady(const net::CompletionCallback& task) { |
| 106 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 107 if (initialized_) |
| 108 io_thread_->PostTask(FROM_HERE, base::Bind(task, net::OK)); |
| 109 else |
| 110 to_run_when_initialized_.push_back(task); |
| 111 return net::ERR_IO_PENDING; |
| 112 } |
| 113 |
| 114 scoped_ptr<std::vector<uint64> > SimpleIndex::ExtractEntriesBetween( |
| 115 const base::Time initial_time, const base::Time end_time) { |
| 116 DCHECK_EQ(true, initialized_); |
| 117 const base::Time extended_end_time = |
| 118 end_time.is_null() ? base::Time::Max() : end_time; |
| 119 DCHECK(extended_end_time >= initial_time); |
| 120 scoped_ptr<std::vector<uint64> > ret_hashes(new std::vector<uint64>()); |
| 121 for (EntrySet::iterator it = entries_set_.begin(), end = entries_set_.end(); |
| 122 it != end;) { |
| 123 EntryMetadata metadata = it->second; |
| 124 base::Time entry_time = metadata.GetLastUsedTime(); |
| 125 if (initial_time <= entry_time && entry_time < extended_end_time) { |
| 126 ret_hashes->push_back(metadata.GetHashKey()); |
| 127 entries_set_.erase(it++); |
| 128 } else { |
| 129 it++; |
| 130 } |
| 131 } |
| 132 return ret_hashes.Pass(); |
| 133 } |
| 134 |
| 135 int32 SimpleIndex::GetEntryCount() const { |
| 136 // TODO(pasko): return a meaningful initial estimate before initialized. |
| 137 return entries_set_.size(); |
| 138 } |
| 139 |
98 void SimpleIndex::Insert(const std::string& key) { | 140 void SimpleIndex::Insert(const std::string& key) { |
99 DCHECK(io_thread_checker_.CalledOnValidThread()); | 141 DCHECK(io_thread_checker_.CalledOnValidThread()); |
100 // Upon insert we don't know yet the size of the entry. | 142 // Upon insert we don't know yet the size of the entry. |
101 // It will be updated later when the SimpleEntryImpl finishes opening or | 143 // It will be updated later when the SimpleEntryImpl finishes opening or |
102 // creating the new entry, and then UpdateEntrySize will be called. | 144 // creating the new entry, and then UpdateEntrySize will be called. |
103 const uint64 hash_key = simple_util::GetEntryHashKey(key); | 145 const uint64 hash_key = simple_util::GetEntryHashKey(key); |
104 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), | 146 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), |
105 &entries_set_); | 147 &entries_set_); |
106 if (!initialized_) | 148 if (!initialized_) |
107 removed_entries_.erase(hash_key); | 149 removed_entries_.erase(hash_key); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 // When Merging, existing valid data in the |current_entry| will prevail. | 306 // When Merging, existing valid data in the |current_entry| will prevail. |
265 current_entry->second.MergeWith(it->second); | 307 current_entry->second.MergeWith(it->second); |
266 cache_size_ += current_entry->second.GetEntrySize(); | 308 cache_size_ += current_entry->second.GetEntrySize(); |
267 } else { | 309 } else { |
268 InsertInEntrySet(it->second, &entries_set_); | 310 InsertInEntrySet(it->second, &entries_set_); |
269 cache_size_ += it->second.GetEntrySize(); | 311 cache_size_ += it->second.GetEntrySize(); |
270 } | 312 } |
271 } | 313 } |
272 | 314 |
273 initialized_ = true; | 315 initialized_ = true; |
| 316 |
| 317 // Run all callbacks waiting for the index to come up. |
| 318 for (CallbackList::iterator it = to_run_when_initialized_.begin(), |
| 319 end = to_run_when_initialized_.end(); it != end; ++it) { |
| 320 io_thread_->PostTask(FROM_HERE, base::Bind((*it), net::OK)); |
| 321 } |
| 322 to_run_when_initialized_.clear(); |
274 } | 323 } |
275 | 324 |
276 void SimpleIndex::WriteToDisk() { | 325 void SimpleIndex::WriteToDisk() { |
277 DCHECK(io_thread_checker_.CalledOnValidThread()); | 326 DCHECK(io_thread_checker_.CalledOnValidThread()); |
278 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), | 327 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), |
279 cache_size_); | 328 cache_size_); |
280 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, | 329 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, |
281 entries_set_); | 330 entries_set_); |
282 cache_thread_->PostTask(FROM_HERE, base::Bind( | 331 cache_thread_->PostTask(FROM_HERE, base::Bind( |
283 &SimpleIndex::WriteToDiskInternal, | 332 &SimpleIndex::WriteToDiskInternal, |
284 index_filename_, | 333 index_filename_, |
285 base::Passed(&pickle))); | 334 base::Passed(&pickle))); |
286 } | 335 } |
287 | 336 |
288 } // namespace disk_cache | 337 } // namespace disk_cache |
OLD | NEW |