summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorfelipeg@chromium.org <felipeg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-19 15:05:47 +0000
committerfelipeg@chromium.org <felipeg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-19 15:05:47 +0000
commitcaab57252c3179f2a666a7e378e630713cdfce75 (patch)
tree4d208a67c436e8665ecdeb54a1951bcb6d742f7d /net
parent1c028316196f4e71189c37ce36ead511c685c690 (diff)
downloadchromium_src-caab57252c3179f2a666a7e378e630713cdfce75.zip
chromium_src-caab57252c3179f2a666a7e378e630713cdfce75.tar.gz
chromium_src-caab57252c3179f2a666a7e378e630713cdfce75.tar.bz2
Add SimpleCache index file heuristics
We restore the index from enumerating the entries on disk when the dir mtime doesnt match the index file mtime. Using the mtime we can reliably detect the stale index on every new entry that has been added or an old entry deleted on disk, since the index file must always the last file created (renamed) in the directory. BUG=233536 Review URL: https://chromiumcodereview.appspot.com/13933029 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195197 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/disk_cache/simple/simple_entry_impl.cc9
-rw-r--r--net/disk_cache/simple/simple_index.cc29
-rw-r--r--net/disk_cache/simple/simple_index.h4
3 files changed, 37 insertions, 5 deletions
diff --git a/net/disk_cache/simple/simple_entry_impl.cc b/net/disk_cache/simple/simple_entry_impl.cc
index 7843449..b42fd6b 100644
--- a/net/disk_cache/simple/simple_entry_impl.cc
+++ b/net/disk_cache/simple/simple_entry_impl.cc
@@ -63,6 +63,13 @@ int SimpleEntryImpl::CreateEntry(SimpleIndex* index,
const std::string& key,
Entry** entry,
const CompletionCallback& callback) {
+ // We insert the entry in the index before creating the entry files in the
+ // SimpleSynchronousEntry, because this way the worse scenario is when we
+ // have the entry in the index but we don't have the created files yet, this
+ // way we never leak files. CreationOperationComplete will remove the entry
+ // from the index if the creation fails.
+ if (index)
+ index->Insert(key);
scoped_refptr<SimpleEntryImpl> new_entry =
new SimpleEntryImpl(index, path, key);
SynchronousCreationCallback sync_creation_callback =
@@ -334,8 +341,6 @@ void SimpleEntryImpl::CreationOperationComplete(
AddRef(); // Balanced in CloseInternal().
synchronous_entry_ = sync_entry;
SetSynchronousData();
- if (index_)
- index_->Insert(key_);
*out_entry = this;
completion_callback.Run(net::OK);
}
diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc
index 02c1453a..acbbf0e 100644
--- a/net/disk_cache/simple/simple_index.cc
+++ b/net/disk_cache/simple/simple_index.cc
@@ -214,6 +214,8 @@ void SimpleIndex::InsertInEntrySet(
}
void SimpleIndex::PostponeWritingToDisk() {
+ if (!initialized_)
+ return;
const base::TimeDelta file_age = base::Time::Now() - last_write_to_disk_;
if (file_age > base::TimeDelta::FromSeconds(kMaxWriteToDiskDelaySecs) &&
write_to_disk_timer_.IsRunning()) {
@@ -231,15 +233,35 @@ void SimpleIndex::PostponeWritingToDisk() {
}
// static
+bool SimpleIndex::IsIndexFileStale(const base::FilePath& index_filename) {
+ base::PlatformFileInfo dir_info;
+ base::PlatformFileInfo index_info;
+ if (!file_util::GetFileInfo(index_filename.DirName(), &dir_info))
+ return false;
+ DCHECK(dir_info.is_directory);
+ if (!file_util::GetFileInfo(index_filename, &index_info))
+ return false;
+
+ // Index file last_modified must be equal to the directory last_modified since
+ // the last operation we do is ReplaceFile in the
+ // SimpleIndexFile::WriteToDisk().
+ // If not true, we need to restore the index.
+ return index_info.last_modified >= dir_info.last_modified;
+}
+
+// static
void SimpleIndex::LoadFromDisk(
const base::FilePath& index_filename,
base::SingleThreadTaskRunner* io_thread,
const IndexCompletionCallback& completion_callback) {
- scoped_ptr<EntrySet> index_file_entries =
- SimpleIndexFile::LoadFromDisk(index_filename);
+ // TODO(felipeg): probably could load a stale index and use it for something.
+ scoped_ptr<EntrySet> index_file_entries;
+ // Only load if the index is not stale.
+ if (!SimpleIndex::IsIndexFileStale(index_filename))
+ index_file_entries = SimpleIndexFile::LoadFromDisk(index_filename);
bool force_index_flush = false;
- if (!index_file_entries.get()) {
+ if (!index_file_entries) {
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.
@@ -322,6 +344,7 @@ void SimpleIndex::WriteToDiskInternal(const base::FilePath& index_filename,
void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries,
bool force_index_flush) {
DCHECK(io_thread_checker_.CalledOnValidThread());
+ DCHECK(index_file_entries);
// First, remove the entries that are in the |removed_entries_| from both
// sets.
for (base::hash_set<uint64>::const_iterator it =
diff --git a/net/disk_cache/simple/simple_index.h b/net/disk_cache/simple/simple_index.h
index 4f3c404..fd27ff4 100644
--- a/net/disk_cache/simple/simple_index.h
+++ b/net/disk_cache/simple/simple_index.h
@@ -126,6 +126,10 @@ class NET_EXPORT_PRIVATE SimpleIndex
void PostponeWritingToDisk();
+ // Using the mtime of the file and its mtime, detects if the index file is
+ // stale.
+ static bool IsIndexFileStale(const base::FilePath& index_filename);
+
static void LoadFromDisk(
const base::FilePath& index_filename,
base::SingleThreadTaskRunner* io_thread,