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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 const base::FilePath& path) | 84 const base::FilePath& path) |
85 : cache_size_(0), | 85 : cache_size_(0), |
86 initialized_(false), | 86 initialized_(false), |
87 index_filename_(path.AppendASCII("simple-index")), | 87 index_filename_(path.AppendASCII("simple-index")), |
88 cache_thread_(cache_thread), | 88 cache_thread_(cache_thread), |
89 io_thread_(io_thread) { | 89 io_thread_(io_thread) { |
90 } | 90 } |
91 | 91 |
92 SimpleIndex::~SimpleIndex() { | 92 SimpleIndex::~SimpleIndex() { |
93 DCHECK(io_thread_checker_.CalledOnValidThread()); | 93 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 94 |
| 95 // Fail all callbacks waiting for the index to come up. |
| 96 for (CallbackList::iterator it = to_run_when_initialized_.begin(), |
| 97 end = to_run_when_initialized_.end(); it != end; ++it) { |
| 98 it->Run(net::ERR_ABORTED); |
| 99 } |
94 } | 100 } |
95 | 101 |
96 void SimpleIndex::Initialize() { | 102 void SimpleIndex::Initialize() { |
97 DCHECK(io_thread_checker_.CalledOnValidThread()); | 103 DCHECK(io_thread_checker_.CalledOnValidThread()); |
98 IndexCompletionCallback merge_callback = | 104 IndexCompletionCallback merge_callback = |
99 base::Bind(&SimpleIndex::MergeInitializingSet, AsWeakPtr()); | 105 base::Bind(&SimpleIndex::MergeInitializingSet, AsWeakPtr()); |
100 base::WorkerPool::PostTask(FROM_HERE, | 106 base::WorkerPool::PostTask(FROM_HERE, |
101 base::Bind(&SimpleIndex::LoadFromDisk, | 107 base::Bind(&SimpleIndex::LoadFromDisk, |
102 index_filename_, | 108 index_filename_, |
103 io_thread_, | 109 io_thread_, |
104 merge_callback), | 110 merge_callback), |
105 true); | 111 true); |
106 } | 112 } |
107 | 113 |
| 114 int SimpleIndex::ExecuteWhenReady(const net::CompletionCallback& task) { |
| 115 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 116 if (initialized_) |
| 117 io_thread_->PostTask(FROM_HERE, base::Bind(task, net::OK)); |
| 118 else |
| 119 to_run_when_initialized_.push_back(task); |
| 120 return net::ERR_IO_PENDING; |
| 121 } |
| 122 |
| 123 scoped_ptr<std::vector<uint64> > SimpleIndex::RemoveEntriesBetween( |
| 124 const base::Time initial_time, const base::Time end_time) { |
| 125 DCHECK_EQ(true, initialized_); |
| 126 const base::Time extended_end_time = |
| 127 end_time.is_null() ? base::Time::Max() : end_time; |
| 128 DCHECK(extended_end_time >= initial_time); |
| 129 scoped_ptr<std::vector<uint64> > ret_hashes(new std::vector<uint64>()); |
| 130 for (EntrySet::iterator it = entries_set_.begin(), end = entries_set_.end(); |
| 131 it != end;) { |
| 132 EntryMetadata metadata = it->second; |
| 133 base::Time entry_time = metadata.GetLastUsedTime(); |
| 134 if (initial_time <= entry_time && entry_time < extended_end_time) { |
| 135 ret_hashes->push_back(metadata.GetHashKey()); |
| 136 entries_set_.erase(it++); |
| 137 } else { |
| 138 it++; |
| 139 } |
| 140 } |
| 141 return ret_hashes.Pass(); |
| 142 } |
| 143 |
| 144 int32 SimpleIndex::GetEntryCount() const { |
| 145 // TODO(pasko): return a meaningful initial estimate before initialized. |
| 146 return entries_set_.size(); |
| 147 } |
| 148 |
108 void SimpleIndex::Insert(const std::string& key) { | 149 void SimpleIndex::Insert(const std::string& key) { |
109 DCHECK(io_thread_checker_.CalledOnValidThread()); | 150 DCHECK(io_thread_checker_.CalledOnValidThread()); |
110 // Upon insert we don't know yet the size of the entry. | 151 // Upon insert we don't know yet the size of the entry. |
111 // It will be updated later when the SimpleEntryImpl finishes opening or | 152 // It will be updated later when the SimpleEntryImpl finishes opening or |
112 // creating the new entry, and then UpdateEntrySize will be called. | 153 // creating the new entry, and then UpdateEntrySize will be called. |
113 const uint64 hash_key = simple_util::GetEntryHashKey(key); | 154 const uint64 hash_key = simple_util::GetEntryHashKey(key); |
114 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), | 155 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), |
115 &entries_set_); | 156 &entries_set_); |
116 if (!initialized_) | 157 if (!initialized_) |
117 removed_entries_.erase(hash_key); | 158 removed_entries_.erase(hash_key); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 } | 347 } |
307 } | 348 } |
308 last_write_to_disk_ = base::Time::Now(); | 349 last_write_to_disk_ = base::Time::Now(); |
309 initialized_ = true; | 350 initialized_ = true; |
310 removed_entries_.clear(); | 351 removed_entries_.clear(); |
311 | 352 |
312 // The actual IO is asynchronous, so calling WriteToDisk() shouldn't slow down | 353 // The actual IO is asynchronous, so calling WriteToDisk() shouldn't slow down |
313 // much the merge. | 354 // much the merge. |
314 if (force_index_flush) | 355 if (force_index_flush) |
315 WriteToDisk(); | 356 WriteToDisk(); |
| 357 |
| 358 // Run all callbacks waiting for the index to come up. |
| 359 for (CallbackList::iterator it = to_run_when_initialized_.begin(), |
| 360 end = to_run_when_initialized_.end(); it != end; ++it) { |
| 361 io_thread_->PostTask(FROM_HERE, base::Bind((*it), net::OK)); |
| 362 } |
| 363 to_run_when_initialized_.clear(); |
316 } | 364 } |
317 | 365 |
318 void SimpleIndex::WriteToDisk() { | 366 void SimpleIndex::WriteToDisk() { |
319 DCHECK(io_thread_checker_.CalledOnValidThread()); | 367 DCHECK(io_thread_checker_.CalledOnValidThread()); |
320 if (!initialized_) | 368 if (!initialized_) |
321 return; | 369 return; |
322 last_write_to_disk_ = base::Time::Now(); | 370 last_write_to_disk_ = base::Time::Now(); |
323 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), | 371 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), |
324 cache_size_); | 372 cache_size_); |
325 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, | 373 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, |
326 entries_set_); | 374 entries_set_); |
327 cache_thread_->PostTask(FROM_HERE, base::Bind( | 375 cache_thread_->PostTask(FROM_HERE, base::Bind( |
328 &SimpleIndex::WriteToDiskInternal, | 376 &SimpleIndex::WriteToDiskInternal, |
329 index_filename_, | 377 index_filename_, |
330 base::Passed(&pickle))); | 378 base::Passed(&pickle))); |
331 } | 379 } |
332 | 380 |
333 } // namespace disk_cache | 381 } // namespace disk_cache |
OLD | NEW |