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

Side by Side Diff: net/disk_cache/simple/simple_index_file.cc

Issue 22406005: Index initialization improvements for Simple Cache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add unit test, fix windows build. 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/disk_cache/simple/simple_index_file.h" 5 #include "net/disk_cache/simple/simple_index_file.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
11 #include "base/files/memory_mapped_file.h"
11 #include "base/hash.h" 12 #include "base/hash.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
14 #include "base/pickle.h" 15 #include "base/pickle.h"
15 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
16 #include "base/task_runner_util.h" 17 #include "base/task_runner_util.h"
17 #include "base/threading/thread_restrictions.h" 18 #include "base/threading/thread_restrictions.h"
18 #include "net/disk_cache/simple/simple_entry_format.h" 19 #include "net/disk_cache/simple/simple_entry_format.h"
19 #include "net/disk_cache/simple/simple_index.h" 20 #include "net/disk_cache/simple/simple_index.h"
20 #include "net/disk_cache/simple/simple_synchronous_entry.h" 21 #include "net/disk_cache/simple/simple_synchronous_entry.h"
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 248
248 UMA_HISTOGRAM_ENUMERATION("SimpleCache.IndexInitializeMethod", 249 UMA_HISTOGRAM_ENUMERATION("SimpleCache.IndexInitializeMethod",
249 initialize_method, INITIALIZE_METHOD_MAX); 250 initialize_method, INITIALIZE_METHOD_MAX);
250 } 251 }
251 252
252 // static 253 // static
253 void SimpleIndexFile::SyncLoadFromDisk(const base::FilePath& index_filename, 254 void SimpleIndexFile::SyncLoadFromDisk(const base::FilePath& index_filename,
254 SimpleIndexLoadResult* out_result) { 255 SimpleIndexLoadResult* out_result) {
255 out_result->Reset(); 256 out_result->Reset();
256 257
257 std::string contents; 258 base::MemoryMappedFile index_file_map;
258 if (!file_util::ReadFileToString(index_filename, &contents)) { 259 if (!index_file_map.Initialize(index_filename)) {
259 LOG(WARNING) << "Could not read Simple Index file."; 260 LOG(WARNING) << "Could not map Simple Index file.";
260 base::DeleteFile(index_filename, false); 261 base::DeleteFile(index_filename, false);
261 return; 262 return;
262 } 263 }
263 264
264 SimpleIndexFile::Deserialize(contents.data(), contents.size(), out_result); 265 SimpleIndexFile::Deserialize(
266 reinterpret_cast<const char*>(index_file_map.data()),
267 index_file_map.length(), out_result);
265 268
266 if (!out_result->did_load) 269 if (!out_result->did_load)
267 base::DeleteFile(index_filename, false); 270 base::DeleteFile(index_filename, false);
268 } 271 }
269 272
270 // static 273 // static
271 scoped_ptr<Pickle> SimpleIndexFile::Serialize( 274 scoped_ptr<Pickle> SimpleIndexFile::Serialize(
272 const SimpleIndexFile::IndexMetadata& index_metadata, 275 const SimpleIndexFile::IndexMetadata& index_metadata,
273 const SimpleIndex::EntrySet& entries) { 276 const SimpleIndex::EntrySet& entries) {
274 scoped_ptr<Pickle> pickle(new Pickle(sizeof(SimpleIndexFile::PickleHeader))); 277 scoped_ptr<Pickle> pickle(new Pickle(sizeof(SimpleIndexFile::PickleHeader)));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 if (!index_metadata.Deserialize(&pickle_it)) { 318 if (!index_metadata.Deserialize(&pickle_it)) {
316 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; 319 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index.";
317 return; 320 return;
318 } 321 }
319 322
320 if (!index_metadata.CheckIndexMetadata()) { 323 if (!index_metadata.CheckIndexMetadata()) {
321 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; 324 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index.";
322 return; 325 return;
323 } 326 }
324 327
328 #if !defined(OS_WIN)
329 // TODO(gavinp): Consider using std::unordered_map.
330 entries->resize(index_metadata.GetNumberOfEntries() + kExtraSizeForMerge);
331 #endif
325 while (entries->size() < index_metadata.GetNumberOfEntries()) { 332 while (entries->size() < index_metadata.GetNumberOfEntries()) {
326 uint64 hash_key; 333 uint64 hash_key;
327 EntryMetadata entry_metadata; 334 EntryMetadata entry_metadata;
328 if (!pickle_it.ReadUInt64(&hash_key) || 335 if (!pickle_it.ReadUInt64(&hash_key) ||
329 !entry_metadata.Deserialize(&pickle_it)) { 336 !entry_metadata.Deserialize(&pickle_it)) {
330 LOG(WARNING) << "Invalid EntryMetadata in Simple Index file."; 337 LOG(WARNING) << "Invalid EntryMetadata in Simple Index file.";
331 entries->clear(); 338 entries->clear();
332 return; 339 return;
333 } 340 }
334 SimpleIndex::InsertInEntrySet(hash_key, entry_metadata, entries); 341 SimpleIndex::InsertInEntrySet(hash_key, entry_metadata, entries);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 // static 414 // static
408 bool SimpleIndexFile::IsIndexFileStale(base::Time cache_last_modified, 415 bool SimpleIndexFile::IsIndexFileStale(base::Time cache_last_modified,
409 const base::FilePath& index_file_path) { 416 const base::FilePath& index_file_path) {
410 base::Time index_mtime; 417 base::Time index_mtime;
411 if (!simple_util::GetMTime(index_file_path, &index_mtime)) 418 if (!simple_util::GetMTime(index_file_path, &index_mtime))
412 return true; 419 return true;
413 return index_mtime < cache_last_modified; 420 return index_mtime < cache_last_modified;
414 } 421 }
415 422
416 } // namespace disk_cache 423 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698