Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: net/disk_cache/simple/simple_index.cc

Issue 14295013: Simple Cache: DoomEntriesBetween() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: first round of comments Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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)
gavinp 2013/04/18 06:11:35 This for loop needs braces.
pasko-google - do not use 2013/04/18 09:29:54 Not required though. And I had an impression that
89 it->Run(net::ERR_ABORTED);
84 } 90 }
85 91
86 void SimpleIndex::Initialize() { 92 void SimpleIndex::Initialize() {
87 DCHECK(io_thread_checker_.CalledOnValidThread()); 93 DCHECK(io_thread_checker_.CalledOnValidThread());
88 IndexCompletionCallback merge_callback = 94 IndexCompletionCallback merge_callback =
89 base::Bind(&SimpleIndex::MergeInitializingSet, this); 95 base::Bind(&SimpleIndex::MergeInitializingSet, this);
90 base::WorkerPool::PostTask(FROM_HERE, 96 base::WorkerPool::PostTask(FROM_HERE,
91 base::Bind(&SimpleIndex::LoadFromDisk, 97 base::Bind(&SimpleIndex::LoadFromDisk,
92 index_filename_, 98 index_filename_,
93 io_thread_, 99 io_thread_,
94 merge_callback), 100 merge_callback),
95 true); 101 true);
96 } 102 }
97 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 to_run_when_initialized_.push_back(task);
110 return net::ERR_IO_PENDING;
111 }
112
113 std::set<uint64>* SimpleIndex::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>();
gavinp 2013/04/18 06:11:35 Can we make ret_hashes a scoped_ptr? And it might
pasko-google - do not use 2013/04/18 09:29:54 Sure, I understand it may feel more cozy. Done.
120 for (EntrySet::iterator it = entries_set_.begin(), end = entries_set_.end();
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 {
128 it++;
129 }
130 }
131 return ret_hashes;
132 }
133
134 int32 SimpleIndex::GetEntryCount() const {
135 // TODO(pasko): return a meaningful initial estimate before initialized.
136 return entries_set_.size();
137 }
138
98 void SimpleIndex::Insert(const std::string& key) { 139 void SimpleIndex::Insert(const std::string& key) {
99 DCHECK(io_thread_checker_.CalledOnValidThread()); 140 DCHECK(io_thread_checker_.CalledOnValidThread());
100 // Upon insert we don't know yet the size of the entry. 141 // Upon insert we don't know yet the size of the entry.
101 // It will be updated later when the SimpleEntryImpl finishes opening or 142 // It will be updated later when the SimpleEntryImpl finishes opening or
102 // creating the new entry, and then UpdateEntrySize will be called. 143 // creating the new entry, and then UpdateEntrySize will be called.
103 const uint64 hash_key = simple_util::GetEntryHashKey(key); 144 const uint64 hash_key = simple_util::GetEntryHashKey(key);
104 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), 145 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0),
105 &entries_set_); 146 &entries_set_);
106 if (!initialized_) 147 if (!initialized_)
107 removed_entries_.erase(hash_key); 148 removed_entries_.erase(hash_key);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 // When Merging, existing valid data in the |current_entry| will prevail. 305 // When Merging, existing valid data in the |current_entry| will prevail.
265 current_entry->second.MergeWith(it->second); 306 current_entry->second.MergeWith(it->second);
266 cache_size_ += current_entry->second.GetEntrySize(); 307 cache_size_ += current_entry->second.GetEntrySize();
267 } else { 308 } else {
268 InsertInEntrySet(it->second, &entries_set_); 309 InsertInEntrySet(it->second, &entries_set_);
269 cache_size_ += it->second.GetEntrySize(); 310 cache_size_ += it->second.GetEntrySize();
270 } 311 }
271 } 312 }
272 313
273 initialized_ = true; 314 initialized_ = true;
315
316 // Run all callbacks waiting for the index to come up.
317 for (CallbackList::iterator it = to_run_when_initialized_.begin(),
318 end = to_run_when_initialized_.end(); it != end; ++it) {
319 io_thread_->PostTask(FROM_HERE, base::Bind((*it), net::OK));
320 }
321 to_run_when_initialized_.clear();
274 } 322 }
275 323
276 void SimpleIndex::WriteToDisk() { 324 void SimpleIndex::WriteToDisk() {
277 DCHECK(io_thread_checker_.CalledOnValidThread()); 325 DCHECK(io_thread_checker_.CalledOnValidThread());
278 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), 326 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(),
279 cache_size_); 327 cache_size_);
280 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, 328 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata,
281 entries_set_); 329 entries_set_);
282 cache_thread_->PostTask(FROM_HERE, base::Bind( 330 cache_thread_->PostTask(FROM_HERE, base::Bind(
283 &SimpleIndex::WriteToDiskInternal, 331 &SimpleIndex::WriteToDiskInternal,
284 index_filename_, 332 index_filename_,
285 base::Passed(&pickle))); 333 base::Passed(&pickle)));
286 } 334 }
287 335
288 } // namespace disk_cache 336 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698