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

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: syncing 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') | net/disk_cache/simple/simple_synchronous_entry.h » ('j') | 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 60ada0d8c0a62dd5d3ad1ed948916284496d9a62..cff8b011c6e3bce20f0b7b756fcb3d32b1312939 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 SimpleEntryImpl finishes opening or
+ // creating the new entry, and then 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,16 @@ 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.
+ COMPILE_ASSERT(kSimpleEntryFileCount == 3,
+ file_pattern_must_match_file_count);
+ const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
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 +188,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);
+ 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;
}
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | net/disk_cache/simple/simple_synchronous_entry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698