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

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

Issue 22927018: Avoid fragmenting the heap too much while reconstructing the index. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows build (part 2) Created 7 years, 4 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_file.h ('k') | net/disk_cache/simple/simple_index_file_posix.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/simple/simple_index_file.cc
diff --git a/net/disk_cache/simple/simple_index_file.cc b/net/disk_cache/simple/simple_index_file.cc
index 7bcea7cdfa86255360a7416d4eff5731ca24ae7c..0136be1c5a063049617d2abf628bcec61968c0da 100644
--- a/net/disk_cache/simple/simple_index_file.cc
+++ b/net/disk_cache/simple/simple_index_file.cc
@@ -7,7 +7,6 @@
#include <vector>
#include "base/file_util.h"
-#include "base/files/file_enumerator.h"
#include "base/files/memory_mapped_file.h"
#include "base/hash.h"
#include "base/logging.h"
@@ -22,11 +21,17 @@
#include "net/disk_cache/simple/simple_util.h"
#include "third_party/zlib/zlib.h"
-
+namespace disk_cache {
namespace {
+const int kEntryFilesHashLength = 16;
+const int kEntryFilesSuffixLength = 2;
+
const uint64 kMaxEntiresInIndex = 100000000;
+const char kIndexFileName[] = "the-real-index";
+const char kTempIndexFileName[] = "temp-index";
+
uint32 CalculatePickleCRC(const Pickle& pickle) {
return crc32(crc32(0, Z_NULL, 0),
reinterpret_cast<const Bytef*>(pickle.payload()),
@@ -67,9 +72,54 @@ void WriteToDiskInternal(const base::FilePath& index_filename,
}
}
-} // namespace
+// Called for each cache directory traversal iteration.
+void ProcessEntryFile(SimpleIndex::EntrySet* entries,
+ const base::FilePath& file_path) {
+ static const size_t kEntryFilesLength =
+ kEntryFilesHashLength + kEntryFilesSuffixLength;
+ // Converting to std::string is OK since we never use UTF8 wide chars in our
+ // file names.
+ const base::FilePath::StringType base_name = file_path.BaseName().value();
+ const std::string file_name(base_name.begin(), base_name.end());
+ if (file_name.size() != kEntryFilesLength)
+ return;
+ const base::StringPiece hash_string(
+ file_name.begin(), file_name.begin() + kEntryFilesHashLength);
+ uint64 hash_key = 0;
+ if (!simple_util::GetEntryHashKeyFromHexString(hash_string, &hash_key)) {
+ LOG(WARNING) << "Invalid entry hash key filename while restoring index from"
+ << " disk: " << file_name;
+ return;
+ }
-namespace disk_cache {
+ base::PlatformFileInfo file_info;
+ if (!file_util::GetFileInfo(file_path, &file_info)) {
+ LOG(ERROR) << "Could not get file info for " << file_path.value();
+ return;
+ }
+ base::Time last_used_time;
+#if defined(OS_POSIX)
+ // For POSIX systems, a last access time is available. However, it's not
+ // guaranteed to be more accurate than mtime. It is no worse though.
+ last_used_time = file_info.last_accessed;
+#endif
+ if (last_used_time.is_null())
+ last_used_time = file_info.last_modified;
+
+ int64 file_size = file_info.size;
+ SimpleIndex::EntrySet::iterator it = entries->find(hash_key);
+ if (it == entries->end()) {
+ SimpleIndex::InsertInEntrySet(
+ hash_key,
+ EntryMetadata(last_used_time, file_size),
+ entries);
+ } else {
+ // Summing up the total size of the entry through all the *_[0-2] files
+ it->second.SetEntrySize(it->second.GetEntrySize() + file_size);
+ }
+}
+
+} // namespace
SimpleIndexLoadResult::SimpleIndexLoadResult() : did_load(false),
flush_required(false) {
@@ -84,11 +134,6 @@ void SimpleIndexLoadResult::Reset() {
entries.clear();
}
-// static
-const char SimpleIndexFile::kIndexFileName[] = "the-real-index";
-// static
-const char SimpleIndexFile::kTempIndexFileName[] = "temp-index";
-
SimpleIndexFile::IndexMetadata::IndexMetadata() :
magic_number_(kSimpleIndexMagicNumber),
version_(kSimpleVersion),
@@ -350,7 +395,6 @@ void SimpleIndexFile::SyncRestoreFromDisk(
const base::FilePath& index_file_path,
SimpleIndexLoadResult* out_result) {
LOG(INFO) << "Simple Cache Index is being restored from disk.";
-
base::DeleteFile(index_file_path, /* recursive = */ false);
out_result->Reset();
SimpleIndex::EntrySet* entries = &out_result->entries;
@@ -359,53 +403,13 @@ void SimpleIndexFile::SyncRestoreFromDisk(
COMPILE_ASSERT(kSimpleEntryFileCount == 3,
file_pattern_must_match_file_count);
- const int kFileSuffixLength = sizeof("_0") - 1;
- const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
- base::FileEnumerator enumerator(cache_directory,
- false /* recursive */,
- base::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();
- // Converting to std::string is OK since we never use UTF8 wide chars in our
- // file names.
- const std::string hash_key_string(base_name.begin(),
- base_name.end() - kFileSuffixLength);
- uint64 hash_key = 0;
- if (!simple_util::GetEntryHashKeyFromHexString(
- hash_key_string, &hash_key)) {
- LOG(WARNING) << "Invalid Entry Hash Key filename while restoring "
- << "Simple Index from disk: " << base_name;
- // TODO(felipeg): Should we delete the invalid file here ?
- continue;
- }
-
- base::FileEnumerator::FileInfo info = enumerator.GetInfo();
- base::Time last_used_time;
-#if defined(OS_POSIX)
- // For POSIX systems, a last access time is available. However, it's not
- // guaranteed to be more accurate than mtime. It is no worse though.
- last_used_time = base::Time::FromTimeT(info.stat().st_atime);
-#endif
- if (last_used_time.is_null())
- last_used_time = info.GetLastModifiedTime();
-
- int64 file_size = info.GetSize();
- SimpleIndex::EntrySet::iterator it = entries->find(hash_key);
- if (it == entries->end()) {
- SimpleIndex::InsertInEntrySet(
- hash_key,
- EntryMetadata(last_used_time, file_size),
- entries);
- } else {
- // Summing up the total size of the entry through all the *_[0-2] files
- it->second.SetEntrySize(it->second.GetEntrySize() + file_size);
- }
+ const bool did_succeed = TraverseCacheDirectory(
+ cache_directory, base::Bind(&ProcessEntryFile, entries));
+ if (!did_succeed) {
+ LOG(ERROR) << "Could not reconstruct index from disk";
+ return;
}
-
out_result->did_load = true;
-
// 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.
out_result->flush_required = true;
« no previous file with comments | « net/disk_cache/simple/simple_index_file.h ('k') | net/disk_cache/simple/simple_index_file_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698