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

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

Issue 13913010: Add Cache size to the Simple Index. (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
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 fb264c510b9b7314dd5b13792df971880e45f8d8..31009f9d4130e95d7fd3a6471e347ac330770180 100644
--- a/net/disk_cache/simple/simple_index.cc
+++ b/net/disk_cache/simple/simple_index.cc
@@ -112,11 +112,15 @@ bool SimpleIndex::Initialize() {
}
void SimpleIndex::Insert(const std::string& key) {
+ // Upon insert we don't know yet the size of the entry.
+ // It will be updated later when the SynchronousEntryImpl finish and the
gavinp 2013/04/10 10:33:23 Wording suggestion: It will be updated later when
felipeg 2013/04/10 11:45:35 Done.
+ // UpdateEntrySize will be called.
InsertInternal(SimpleIndexFile::EntryMetadata(GetEntryHashForKey(key),
- base::Time::Now()));
+ base::Time::Now(), 0));
}
void SimpleIndex::Remove(const std::string& key) {
+ UpdateEntrySize(key, 0);
entries_set_.erase(GetEntryHashForKey(key));
}
@@ -132,6 +136,19 @@ bool SimpleIndex::UseIfExists(const std::string& key) {
return true;
}
+bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) {
+ EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key));
+ if (it == entries_set_.end())
+ return false;
+
+ // Update the total cache size with the new entry size.
+ cache_size_ -= it->second.entry_size;
+ cache_size_ += entry_size;
+ it->second.entry_size = entry_size;
+
+ return true;
+}
+
void SimpleIndex::InsertInternal(
const SimpleIndexFile::EntryMetadata& entry_metadata) {
entries_set_.insert(make_pair(entry_metadata.GetHashKey(), entry_metadata));
@@ -143,11 +160,13 @@ bool SimpleIndex::RestoreFromDisk() {
CloseIndexFile();
file_util::Delete(index_filename_, /* recursive = */ false);
entries_set_.clear();
- const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_0");
+ // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format.
gavinp 2013/04/10 10:33:23 COMPILE_ASSERT(kSimpleEntryFileCount == 3, file_pa
felipeg 2013/04/10 11:45:35 Done.
+ const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
gavinp 2013/04/10 10:33:23 Would: ??????????_[0-2] Be a good idea?
felipeg 2013/04/10 11:45:35 It doesn't matter, we will change it anyway
FileEnumerator enumerator(path_,
false /* recursive */,
FileEnumerator::FILES,
file_pattern);
+
for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
file_path = enumerator.Next()) {
const base::FilePath::StringType base_name = file_path.BaseName().value();
@@ -166,8 +185,18 @@ bool SimpleIndex::RestoreFromDisk() {
#endif
if (last_used_time.is_null())
last_used_time = FileEnumerator::GetLastModifiedTime(find_info);
- InsertInternal(SimpleIndexFile::EntryMetadata(hash_key, last_used_time));
+
+ int64 file_size = FileEnumerator::GetFilesize(find_info);
+ EntrySet::iterator it = entries_set_.find(hash_key);
gavinp 2013/04/10 10:33:23 Note that in the not found case (33% of enumeratio
felipeg 2013/04/10 11:45:35 it is a hash map two searches doesn't matter compa
gavinp 2013/04/10 14:49:29 Good point on the hash. Note also that every time
+ if (it == entries_set_.end()) {
+ InsertInternal(SimpleIndexFile::EntryMetadata(
+ hash_key, last_used_time, file_size));
+ } else {
+ // Summing up the total size of the entry through all the *_[0-2] files
+ it->second.entry_size += file_size;
+ }
}
+
// TODO(felipeg): Detect unrecoverable problems and return false here.
return true;
}

Powered by Google App Engine
This is Rietveld 408576698