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

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

Issue 14877019: Minor SimpleCacheBackend improvements. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Use sizeof("_0") - 1' Created 7 years, 6 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
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 const base::FilePath& path) 145 const base::FilePath& path)
146 : cache_size_(0), 146 : cache_size_(0),
147 max_size_(0), 147 max_size_(0),
148 high_watermark_(0), 148 high_watermark_(0),
149 low_watermark_(0), 149 low_watermark_(0),
150 eviction_in_progress_(false), 150 eviction_in_progress_(false),
151 initialized_(false), 151 initialized_(false),
152 index_filename_(path.AppendASCII("the-real-index")), 152 index_filename_(path.AppendASCII("the-real-index")),
153 cache_thread_(cache_thread), 153 cache_thread_(cache_thread),
154 io_thread_(io_thread), 154 io_thread_(io_thread),
155 app_on_background_(false) { 155 // Creating the callback once so it is reused every time
156 } 156 // write_to_disk_timer_.Start() is called.
157 write_to_disk_cb_(base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())),
158 app_on_background_(false) {}
157 159
158 SimpleIndex::~SimpleIndex() { 160 SimpleIndex::~SimpleIndex() {
159 DCHECK(io_thread_checker_.CalledOnValidThread()); 161 DCHECK(io_thread_checker_.CalledOnValidThread());
160 162
161 // Fail all callbacks waiting for the index to come up. 163 // Fail all callbacks waiting for the index to come up.
162 for (CallbackList::iterator it = to_run_when_initialized_.begin(), 164 for (CallbackList::iterator it = to_run_when_initialized_.begin(),
163 end = to_run_when_initialized_.end(); it != end; ++it) { 165 end = to_run_when_initialized_.end(); it != end; ++it) {
164 it->Run(net::ERR_ABORTED); 166 it->Run(net::ERR_ABORTED);
165 } 167 }
166 } 168 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 const uint64 hash_key = simple_util::GetEntryHashKey(key); 243 const uint64 hash_key = simple_util::GetEntryHashKey(key);
242 InsertInEntrySet( 244 InsertInEntrySet(
243 hash_key, EntryMetadata(base::Time::Now(), 0), &entries_set_); 245 hash_key, EntryMetadata(base::Time::Now(), 0), &entries_set_);
244 if (!initialized_) 246 if (!initialized_)
245 removed_entries_.erase(hash_key); 247 removed_entries_.erase(hash_key);
246 PostponeWritingToDisk(); 248 PostponeWritingToDisk();
247 } 249 }
248 250
249 void SimpleIndex::Remove(const std::string& key) { 251 void SimpleIndex::Remove(const std::string& key) {
250 DCHECK(io_thread_checker_.CalledOnValidThread()); 252 DCHECK(io_thread_checker_.CalledOnValidThread());
251 UpdateEntrySize(key, 0);
252 const uint64 hash_key = simple_util::GetEntryHashKey(key); 253 const uint64 hash_key = simple_util::GetEntryHashKey(key);
253 entries_set_.erase(hash_key); 254 EntrySet::iterator it = entries_set_.find(hash_key);
255 if (it != entries_set_.end()) {
256 UpdateEntryIteratorSize(&it, 0);
257 entries_set_.erase(it);
258 }
254 259
255 if (!initialized_) 260 if (!initialized_)
256 removed_entries_.insert(hash_key); 261 removed_entries_.insert(hash_key);
257 PostponeWritingToDisk(); 262 PostponeWritingToDisk();
258 } 263 }
259 264
260 bool SimpleIndex::Has(const std::string& key) const { 265 bool SimpleIndex::Has(const std::string& key) const {
261 DCHECK(io_thread_checker_.CalledOnValidThread()); 266 DCHECK(io_thread_checker_.CalledOnValidThread());
262 // If not initialized, always return true, forcing it to go to the disk. 267 // If not initialized, always return true, forcing it to go to the disk.
263 return !initialized_ || 268 return !initialized_ ||
264 entries_set_.count(simple_util::GetEntryHashKey(key)) != 0; 269 entries_set_.count(simple_util::GetEntryHashKey(key)) > 0;
265 } 270 }
266 271
267 bool SimpleIndex::UseIfExists(const std::string& key) { 272 bool SimpleIndex::UseIfExists(const std::string& key) {
268 DCHECK(io_thread_checker_.CalledOnValidThread()); 273 DCHECK(io_thread_checker_.CalledOnValidThread());
269 // Always update the last used time, even if it is during initialization. 274 // Always update the last used time, even if it is during initialization.
270 // It will be merged later. 275 // It will be merged later.
271 EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key)); 276 EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key));
272 if (it == entries_set_.end()) 277 if (it == entries_set_.end())
273 // If not initialized, always return true, forcing it to go to the disk. 278 // If not initialized, always return true, forcing it to go to the disk.
274 return !initialized_; 279 return !initialized_;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 base::Passed(&result)); 330 base::Passed(&result));
326 base::WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); 331 base::WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true);
327 } 332 }
328 333
329 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) { 334 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) {
330 DCHECK(io_thread_checker_.CalledOnValidThread()); 335 DCHECK(io_thread_checker_.CalledOnValidThread());
331 EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key)); 336 EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key));
332 if (it == entries_set_.end()) 337 if (it == entries_set_.end())
333 return false; 338 return false;
334 339
335 // Update the total cache size with the new entry size. 340 UpdateEntryIteratorSize(&it, entry_size);
336 DCHECK(cache_size_ - it->second.GetEntrySize() <= cache_size_);
337 cache_size_ -= it->second.GetEntrySize();
338 cache_size_ += entry_size;
339 it->second.SetEntrySize(entry_size);
340 PostponeWritingToDisk(); 341 PostponeWritingToDisk();
341 StartEvictionIfNeeded(); 342 StartEvictionIfNeeded();
342 return true; 343 return true;
343 } 344 }
344 345
345 void SimpleIndex::EvictionDone(scoped_ptr<int> result) { 346 void SimpleIndex::EvictionDone(scoped_ptr<int> result) {
346 DCHECK(io_thread_checker_.CalledOnValidThread()); 347 DCHECK(io_thread_checker_.CalledOnValidThread());
347 DCHECK(result); 348 DCHECK(result);
348 349
349 // Ignore the result of eviction. We did our best. 350 // Ignore the result of eviction. We did our best.
(...skipping 13 matching lines...) Expand all
363 entry_set->insert(std::make_pair(hash_key, entry_metadata)); 364 entry_set->insert(std::make_pair(hash_key, entry_metadata));
364 } 365 }
365 366
366 void SimpleIndex::PostponeWritingToDisk() { 367 void SimpleIndex::PostponeWritingToDisk() {
367 if (!initialized_) 368 if (!initialized_)
368 return; 369 return;
369 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs 370 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs
370 : kWriteToDiskDelayMSecs; 371 : kWriteToDiskDelayMSecs;
371 // If the timer is already active, Start() will just Reset it, postponing it. 372 // If the timer is already active, Start() will just Reset it, postponing it.
372 write_to_disk_timer_.Start( 373 write_to_disk_timer_.Start(
373 FROM_HERE, 374 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_);
374 base::TimeDelta::FromMilliseconds(delay), 375 }
375 base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); 376
377 void SimpleIndex::UpdateEntryIteratorSize(EntrySet::iterator* it,
378 uint64 entry_size) {
379 // Update the total cache size with the new entry size.
380 DCHECK(io_thread_checker_.CalledOnValidThread());
381 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize());
382 cache_size_ -= (*it)->second.GetEntrySize();
383 cache_size_ += entry_size;
384 (*it)->second.SetEntrySize(entry_size);
376 } 385 }
377 386
378 // static 387 // static
379 bool SimpleIndex::IsIndexFileStale(const base::FilePath& index_filename) { 388 bool SimpleIndex::IsIndexFileStale(const base::FilePath& index_filename) {
380 base::Time index_mtime; 389 base::Time index_mtime;
381 base::Time dir_mtime; 390 base::Time dir_mtime;
382 if (!GetMTime(index_filename.DirName(), &dir_mtime)) 391 if (!GetMTime(index_filename.DirName(), &dir_mtime))
383 return true; 392 return true;
384 if (!GetMTime(index_filename, &index_mtime)) 393 if (!GetMTime(index_filename, &index_mtime))
385 return true; 394 return true;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 using file_util::FileEnumerator; 467 using file_util::FileEnumerator;
459 LOG(INFO) << "Simple Cache Index is being restored from disk."; 468 LOG(INFO) << "Simple Cache Index is being restored from disk.";
460 469
461 file_util::Delete(index_filename, /* recursive = */ false); 470 file_util::Delete(index_filename, /* recursive = */ false);
462 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); 471 scoped_ptr<EntrySet> index_file_entries(new EntrySet());
463 472
464 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. 473 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format.
465 COMPILE_ASSERT(kSimpleEntryFileCount == 3, 474 COMPILE_ASSERT(kSimpleEntryFileCount == 3,
466 file_pattern_must_match_file_count); 475 file_pattern_must_match_file_count);
467 476
468 const int kFileSuffixLenght = std::string("_0").size(); 477 const int kFileSuffixLength = sizeof("_0") - 1;
469 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); 478 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
470 FileEnumerator enumerator(index_filename.DirName(), 479 FileEnumerator enumerator(index_filename.DirName(),
471 false /* recursive */, 480 false /* recursive */,
472 FileEnumerator::FILES, 481 FileEnumerator::FILES,
473 file_pattern); 482 file_pattern);
474 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); 483 for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
475 file_path = enumerator.Next()) { 484 file_path = enumerator.Next()) {
476 const base::FilePath::StringType base_name = file_path.BaseName().value(); 485 const base::FilePath::StringType base_name = file_path.BaseName().value();
477 // Converting to std::string is OK since we never use UTF8 wide chars in our 486 // Converting to std::string is OK since we never use UTF8 wide chars in our
478 // file names. 487 // file names.
479 const std::string hash_name(base_name.begin(), base_name.end()); 488 const std::string hash_key_string(base_name.begin(),
480 const std::string hash_key_string = 489 base_name.end() - kFileSuffixLength);
481 hash_name.substr(0, hash_name.size() - kFileSuffixLenght);
482 uint64 hash_key = 0; 490 uint64 hash_key = 0;
483 if (!simple_util::GetEntryHashKeyFromHexString( 491 if (!simple_util::GetEntryHashKeyFromHexString(
484 hash_key_string, &hash_key)) { 492 hash_key_string, &hash_key)) {
485 LOG(WARNING) << "Invalid Entry Hash Key filename while restoring " 493 LOG(WARNING) << "Invalid Entry Hash Key filename while restoring "
486 << "Simple Index from disk: " << hash_name; 494 << "Simple Index from disk: " << base_name;
487 // TODO(felipeg): Should we delete the invalid file here ? 495 // TODO(felipeg): Should we delete the invalid file here ?
488 continue; 496 continue;
489 } 497 }
490 498
491 FileEnumerator::FindInfo find_info = {}; 499 FileEnumerator::FindInfo find_info = {};
492 enumerator.GetFindInfo(&find_info); 500 enumerator.GetFindInfo(&find_info);
493 base::Time last_used_time; 501 base::Time last_used_time;
494 #if defined(OS_POSIX) 502 #if defined(OS_POSIX)
495 // For POSIX systems, a last access time is available. However, it's not 503 // For POSIX systems, a last access time is available. However, it's not
496 // guaranteed to be more accurate than mtime. It is no worse though. 504 // guaranteed to be more accurate than mtime. It is no worse though.
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 entries_set_); 616 entries_set_);
609 cache_thread_->PostTask(FROM_HERE, base::Bind( 617 cache_thread_->PostTask(FROM_HERE, base::Bind(
610 &SimpleIndex::WriteToDiskInternal, 618 &SimpleIndex::WriteToDiskInternal,
611 index_filename_, 619 index_filename_,
612 base::Passed(&pickle), 620 base::Passed(&pickle),
613 start, 621 start,
614 app_on_background_)); 622 app_on_background_));
615 } 623 }
616 624
617 } // namespace disk_cache 625 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698