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

Unified Diff: net/disk_cache/simple/simple_index.cc

Issue 14230009: Write the Simple Cache Index file to disk once in a while (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/simple/simple_index.cc
diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc
index c87b3c8a7019ca72061ae9e8d34686006ac6164b..0cf51e3fdc1306e3937287f4700aa9ab6c0ec4d1 100644
--- a/net/disk_cache/simple/simple_index.cc
+++ b/net/disk_cache/simple/simple_index.cc
@@ -19,6 +19,17 @@
#include "net/disk_cache/simple/simple_index_file.h"
#include "net/disk_cache/simple/simple_util.h"
+namespace {
+
+// How many seconds we delay writing the index to disk since the last cache
+// operation has happened.
+const int kWriteToDiskDelaySecs = 20;
+
+// WriteToDisk at lest every 5 minutes.
Philippe 2013/04/17 15:39:18 Nit: least.
+const int kMaxWriteToDiskDelaySecs = 300;
+
+} // namespace
+
namespace disk_cache {
EntryMetadata::EntryMetadata() :
@@ -27,7 +38,6 @@ EntryMetadata::EntryMetadata() :
entry_size_(0)
{}
-
EntryMetadata::EntryMetadata(uint64 hash_key,
base::Time last_used_time,
uint64 entry_size) :
@@ -77,7 +87,8 @@ SimpleIndex::SimpleIndex(
initialized_(false),
index_filename_(path.AppendASCII("simple-index")),
cache_thread_(cache_thread),
- io_thread_(io_thread) {}
+ io_thread_(io_thread) {
+}
SimpleIndex::~SimpleIndex() {
DCHECK(io_thread_checker_.CalledOnValidThread());
@@ -106,6 +117,7 @@ void SimpleIndex::Insert(const std::string& key) {
&entries_set_);
if (!initialized_)
removed_entries_.erase(hash_key);
+ PostponeWritingToDisk();
}
void SimpleIndex::Remove(const std::string& key) {
@@ -116,6 +128,7 @@ void SimpleIndex::Remove(const std::string& key) {
if (!initialized_)
removed_entries_.insert(hash_key);
+ PostponeWritingToDisk();
}
bool SimpleIndex::Has(const std::string& key) const {
@@ -134,6 +147,7 @@ bool SimpleIndex::UseIfExists(const std::string& key) {
// If not initialized, always return true, forcing it to go to the disk.
return !initialized_;
it->second.SetLastUsedTime(base::Time::Now());
+ PostponeWritingToDisk();
return true;
}
@@ -147,7 +161,7 @@ bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) {
cache_size_ -= it->second.GetEntrySize();
cache_size_ += entry_size;
it->second.SetEntrySize(entry_size);
-
+ PostponeWritingToDisk();
return true;
}
@@ -160,6 +174,23 @@ void SimpleIndex::InsertInEntrySet(
std::make_pair(entry_metadata.GetHashKey(), entry_metadata));
}
+void SimpleIndex::PostponeWritingToDisk() {
+ const base::TimeDelta file_age = base::Time::Now() - last_write_to_disk_;
+ if (file_age > base::TimeDelta::FromSeconds(kMaxWriteToDiskDelaySecs) &&
+ write_to_disk_timer_.IsRunning()) {
+ // If the index file is too old and there is a timer programmed to run a
+ // WriteToDisk soon, we don't postpone it, so we always WriteToDisk
+ // approximately every kMaxWriteToDiskDelaySecs.
+ return;
+ }
+
+ // If the timer is already active, Start() will just Reset it, postponing it.
+ write_to_disk_timer_.Start(
+ FROM_HERE,
+ base::TimeDelta::FromSeconds(kWriteToDiskDelaySecs),
+ base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr()));
+}
+
// static
void SimpleIndex::LoadFromDisk(
const base::FilePath& index_filename,
@@ -168,12 +199,18 @@ void SimpleIndex::LoadFromDisk(
scoped_ptr<EntrySet> index_file_entries =
SimpleIndexFile::LoadFromDisk(index_filename);
- if (!index_file_entries.get())
- index_file_entries = SimpleIndex::RestoreFromDisk(index_filename);
+ bool force_index_flush = false;
+ if (!index_file_entries.get()) {
+ index_file_entries = SimpleIndex::RestoreFromDisk(index_filename);
+ // When we restore from disk we write the merged index file to disk right
+ // away, this might save us from having to restore again next time.
+ force_index_flush = true;
+ }
io_thread->PostTask(FROM_HERE,
base::Bind(completion_callback,
- base::Passed(&index_file_entries)));
+ base::Passed(&index_file_entries),
+ force_index_flush));
}
// static
@@ -243,8 +280,8 @@ void SimpleIndex::WriteToDiskInternal(const base::FilePath& index_filename,
SimpleIndexFile::WriteToDisk(index_filename, *pickle);
}
-void SimpleIndex::MergeInitializingSet(
- scoped_ptr<EntrySet> index_file_entries) {
+void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries,
+ bool force_index_flush) {
DCHECK(io_thread_checker_.CalledOnValidThread());
// First, remove the entries that are in the |removed_entries_| from both
// sets.
@@ -270,12 +307,21 @@ void SimpleIndex::MergeInitializingSet(
cache_size_ += it->second.GetEntrySize();
}
}
-
+ last_write_to_disk_ = base::Time::Now();
pasko-google - do not use 2013/04/17 15:38:16 it is already in SimpleIndex::WriteToDisk(), updat
felipeg 2013/04/17 16:00:51 Done.
initialized_ = true;
+ removed_entries_.clear();
+
+ // The actual IO is asynchronous, so calling WriteToDisk() shouldn't slow down
+ // much the merge.
+ if (force_index_flush)
+ WriteToDisk();
}
void SimpleIndex::WriteToDisk() {
DCHECK(io_thread_checker_.CalledOnValidThread());
+ if (!initialized_)
+ return;
+ last_write_to_disk_ = base::Time::Now();
Philippe 2013/04/17 15:39:18 Do you need this given that you already set the fi
pasko-google - do not use 2013/04/17 15:51:12 this one should be used instead of the one on line
felipeg 2013/04/17 16:00:51 Done.
SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(),
cache_size_);
scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata,
« 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