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()); |
84 | 85 |
86 // Fail all callbacks waiting for the index to come up. | |
87 for (CallbackList::iterator it = initialized_waitlist_.begin(), | |
88 end = initialized_waitlist_.end(); it != end; ++it) | |
89 it->Run(net::ERR_ABORTED); | |
85 } | 90 } |
86 | 91 |
87 void SimpleIndex::Initialize() { | 92 void SimpleIndex::Initialize() { |
88 DCHECK(io_thread_checker_.CalledOnValidThread()); | 93 DCHECK(io_thread_checker_.CalledOnValidThread()); |
89 IndexCompletionCallback merge_callback = | 94 IndexCompletionCallback merge_callback = |
90 base::Bind(&SimpleIndex::MergeInitializingSet, AsWeakPtr()); | 95 base::Bind(&SimpleIndex::MergeInitializingSet, AsWeakPtr()); |
91 base::WorkerPool::PostTask(FROM_HERE, | 96 base::WorkerPool::PostTask(FROM_HERE, |
92 base::Bind(&SimpleIndex::LoadFromDisk, | 97 base::Bind(&SimpleIndex::LoadFromDisk, |
93 index_filename_, | 98 index_filename_, |
94 io_thread_, | 99 io_thread_, |
95 merge_callback), | 100 merge_callback), |
96 true); | 101 true); |
97 } | 102 } |
98 | 103 |
104 int SimpleIndex::ExecuteWhenReady(const net::CompletionCallback& task) { | |
105 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
106 if (initialized_) | |
107 io_thread_->PostTask(FROM_HERE, base::Bind(task, net::OK)); | |
108 else | |
109 initialized_waitlist_.push_back(task); | |
gavinp
2013/04/17 07:41:18
I think we probably want UMA on the size of this l
pasko-google - do not use
2013/04/17 19:47:52
May I please to it in a followup CL?
| |
110 return net::ERR_IO_PENDING; | |
111 } | |
112 | |
113 std::set<uint64>* SimpleIndex::PullEntriesBetween( | |
felipeg
2013/04/17 16:35:04
I would call it RemoveEntriesBetween
or just Remov
pasko-google - do not use
2013/04/17 19:47:52
Followed Gavin's suggestion: ExtractEntriesBetween
| |
114 const base::Time initial_time, const base::Time end_time) { | |
115 DCHECK_EQ(true, initialized_); | |
116 const base::Time extended_end_time = | |
117 end_time.is_null() ? base::Time::Max() : end_time; | |
118 DCHECK(extended_end_time >= initial_time); | |
119 std::set<uint64>* ret_hashes = new std::set<uint64>(); | |
120 for (EntrySet::iterator it = entries_set_.begin(), end = entries_set_.end(); | |
gavinp
2013/04/17 07:41:18
Why take the copy of end?
pasko-google - do not use
2013/04/17 19:47:52
This is a premature optimization: in the hope that
gavinp
2013/04/18 06:11:35
It's fine, and plenty readable. I do think it's pr
pasko-google - do not use
2013/04/18 09:29:54
you probably mean common subexpression elimination
| |
121 it != end;) { | |
122 EntryMetadata metadata = it->second; | |
123 base::Time entry_time = metadata.GetLastUsedTime(); | |
124 if (initial_time <= entry_time && entry_time < extended_end_time) { | |
125 ret_hashes->insert(metadata.GetHashKey()); | |
126 entries_set_.erase(it++); | |
127 } else | |
gavinp
2013/04/17 07:41:18
Style: } else {
pasko-google - do not use
2013/04/17 19:47:52
that is good, I like that, did not know that the g
gavinp
2013/04/18 06:11:35
Google style says braces are OK if you want them,
| |
128 it++; | |
129 } | |
130 return ret_hashes; | |
131 } | |
132 | |
133 int32 SimpleIndex::GetEntryCount() const { | |
134 DCHECK(initialized_); | |
gavinp
2013/04/17 07:41:18
This is going to make us crash in debug builds a l
pasko-google - do not use
2013/04/17 19:47:52
had the same thought very soon after uploading. Sa
| |
135 return entries_set_.size(); | |
136 } | |
137 | |
99 void SimpleIndex::Insert(const std::string& key) { | 138 void SimpleIndex::Insert(const std::string& key) { |
100 DCHECK(io_thread_checker_.CalledOnValidThread()); | 139 DCHECK(io_thread_checker_.CalledOnValidThread()); |
101 // Upon insert we don't know yet the size of the entry. | 140 // Upon insert we don't know yet the size of the entry. |
102 // It will be updated later when the SimpleEntryImpl finishes opening or | 141 // It will be updated later when the SimpleEntryImpl finishes opening or |
103 // creating the new entry, and then UpdateEntrySize will be called. | 142 // creating the new entry, and then UpdateEntrySize will be called. |
104 const uint64 hash_key = simple_util::GetEntryHashKey(key); | 143 const uint64 hash_key = simple_util::GetEntryHashKey(key); |
105 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), | 144 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), |
106 &entries_set_); | 145 &entries_set_); |
107 if (!initialized_) | 146 if (!initialized_) |
108 removed_entries_.erase(hash_key); | 147 removed_entries_.erase(hash_key); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 // When Merging, existing valid data in the |current_entry| will prevail. | 304 // When Merging, existing valid data in the |current_entry| will prevail. |
266 current_entry->second.MergeWith(it->second); | 305 current_entry->second.MergeWith(it->second); |
267 cache_size_ += current_entry->second.GetEntrySize(); | 306 cache_size_ += current_entry->second.GetEntrySize(); |
268 } else { | 307 } else { |
269 InsertInEntrySet(it->second, &entries_set_); | 308 InsertInEntrySet(it->second, &entries_set_); |
270 cache_size_ += it->second.GetEntrySize(); | 309 cache_size_ += it->second.GetEntrySize(); |
271 } | 310 } |
272 } | 311 } |
273 | 312 |
274 initialized_ = true; | 313 initialized_ = true; |
314 | |
315 // Run all callbacks waiting for the index to come up. | |
316 for (CallbackList::iterator it = initialized_waitlist_.begin(), | |
317 end = initialized_waitlist_.end(); it != end; ++it) { | |
318 io_thread_->PostTask(FROM_HERE, base::Bind((*it), net::OK)); | |
319 } | |
320 initialized_waitlist_.clear(); | |
275 } | 321 } |
276 | 322 |
277 void SimpleIndex::WriteToDisk() { | 323 void SimpleIndex::WriteToDisk() { |
278 DCHECK(io_thread_checker_.CalledOnValidThread()); | 324 DCHECK(io_thread_checker_.CalledOnValidThread()); |
279 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), | 325 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), |
280 cache_size_); | 326 cache_size_); |
281 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, | 327 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, |
282 entries_set_); | 328 entries_set_); |
283 cache_thread_->PostTask(FROM_HERE, base::Bind( | 329 cache_thread_->PostTask(FROM_HERE, base::Bind( |
284 &SimpleIndex::WriteToDiskInternal, | 330 &SimpleIndex::WriteToDiskInternal, |
285 index_filename_, | 331 index_filename_, |
286 base::Passed(&pickle))); | 332 base::Passed(&pickle))); |
287 } | 333 } |
288 | 334 |
289 } // namespace disk_cache | 335 } // namespace disk_cache |
OLD | NEW |