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

Side by Side Diff: net/disk_cache/simple/simple_index.h

Issue 14263005: Refactor our SimpleIndex file format and serialization to use Pickle instead of the previously bugg… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: to commit again, since the last dcommit failed 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/simple/simple_entry_impl.h ('k') | net/disk_cache/simple/simple_index.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/file_util.h" 12 #include "base/callback.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/hash_tables.h" 14 #include "base/hash_tables.h"
15 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
17 #include "net/disk_cache/disk_cache.h" 18 #include "base/time.h"
18 #include "net/disk_cache/simple/simple_disk_format.h" 19 #include "net/base/net_export.h"
20
21 class Pickle;
22 class PickleIterator;
19 23
20 namespace base { 24 namespace base {
21 class TaskRunner; 25 class TaskRunner;
22 } 26 }
23 27
24 namespace disk_cache { 28 namespace disk_cache {
25 29
30 class NET_EXPORT_PRIVATE EntryMetadata {
31 public:
32 EntryMetadata();
33 EntryMetadata(uint64 hash_key,
34 base::Time last_used_time,
35 uint64 entry_size);
36
37 uint64 GetHashKey() const { return hash_key_; }
38 base::Time GetLastUsedTime() const;
39 void SetLastUsedTime(const base::Time& last_used_time);
40
41 uint64 GetEntrySize() const { return entry_size_; }
42 void SetEntrySize(uint64 entry_size) { entry_size_ = entry_size; }
43
44 // Serialize the data into the provided pickle.
45 void Serialize(Pickle* pickle) const;
46 bool Deserialize(PickleIterator* it);
47
48 // Merge two EntryMetadata instances.
49 // The existing current valid data in |this| will prevail.
50 void MergeWith(const EntryMetadata& entry_metadata);
51
52 private:
53 friend class SimpleIndexFileTest;
54
55 // When adding new members here, you should update the Serialize() and
56 // Deserialize() methods.
57 uint64 hash_key_;
58
59 // This is the serialized format from Time::ToInternalValue().
60 // If you want to make calculations/comparisons, you should use the
61 // base::Time() class. Use the GetLastUsedTime() method above.
62 // TODO(felipeg): Use Time() here.
63 int64 last_used_time_;
64
65 uint64 entry_size_; // Storage size in bytes.
66 };
67
26 // This class is not Thread-safe. 68 // This class is not Thread-safe.
27 class SimpleIndex 69 class NET_EXPORT_PRIVATE SimpleIndex
28 : public base::SupportsWeakPtr<SimpleIndex> { 70 : public base::SupportsWeakPtr<SimpleIndex> {
29 public: 71 public:
30 SimpleIndex( 72 SimpleIndex(
31 const scoped_refptr<base::TaskRunner>& cache_thread, 73 const scoped_refptr<base::TaskRunner>& cache_thread,
32 const scoped_refptr<base::TaskRunner>& io_thread, 74 const scoped_refptr<base::TaskRunner>& io_thread,
33 const base::FilePath& path); 75 const base::FilePath& path);
34 76
35 virtual ~SimpleIndex(); 77 virtual ~SimpleIndex();
36 78
37 void Initialize(); 79 void Initialize();
38 80
39 void Insert(const std::string& key); 81 void Insert(const std::string& key);
40 void Remove(const std::string& key); 82 void Remove(const std::string& key);
41 83
42 bool Has(const std::string& key) const; 84 bool Has(const std::string& key) const;
43 85
44 // Update the last used time of the entry with the given key and return true 86 // Update the last used time of the entry with the given key and return true
45 // iff the entry exist in the index. 87 // iff the entry exist in the index.
46 bool UseIfExists(const std::string& key); 88 bool UseIfExists(const std::string& key);
47 89
48 void WriteToDisk(); 90 void WriteToDisk();
49 91
50 // Update the size (in bytes) of an entry, in the metadata stored in the 92 // Update the size (in bytes) of an entry, in the metadata stored in the
51 // index. This should be the total disk-file size including all streams of the 93 // index. This should be the total disk-file size including all streams of the
52 // entry. 94 // entry.
53 bool UpdateEntrySize(const std::string& key, uint64 entry_size); 95 bool UpdateEntrySize(const std::string& key, uint64 entry_size);
54 96
55 private: 97 // TODO(felipeg): This way we are storing the hash_key twice, as the
56 // TODO(felipeg): This way we are storing the hash_key twice (as the
57 // hash_map::key and as a member of EntryMetadata. We could save space if we 98 // hash_map::key and as a member of EntryMetadata. We could save space if we
58 // use a hash_set. 99 // use a hash_set.
59 typedef base::hash_map<uint64, SimpleIndexFile::EntryMetadata> EntrySet; 100 typedef base::hash_map<uint64, EntryMetadata> EntrySet;
60 101
61 typedef base::Callback<void(scoped_ptr<EntrySet>)> MergeCallback; 102 static void InsertInEntrySet(const EntryMetadata& entry_metadata,
103 EntrySet* entry_set);
62 104
63 static void InsertInternal( 105 private:
64 EntrySet* entry_set, 106 typedef base::Callback<void(scoped_ptr<EntrySet>)> IndexCompletionCallback;
65 const SimpleIndexFile::EntryMetadata& entry_metadata);
66 107
67 // Load index from disk. If it is corrupted, call RestoreFromDisk().
68 static void LoadFromDisk( 108 static void LoadFromDisk(
69 const base::FilePath& index_filename, 109 const base::FilePath& index_filename,
70 const scoped_refptr<base::TaskRunner>& io_thread, 110 const scoped_refptr<base::TaskRunner>& io_thread,
71 const MergeCallback& merge_callback); 111 const IndexCompletionCallback& completion_callback);
72 112
73 // Enumerates all entries' files on disk and regenerates the index. 113 // Enumerates all entries' files on disk and regenerates the index.
74 static void RestoreFromDisk( 114 static scoped_ptr<SimpleIndex::EntrySet> RestoreFromDisk(
75 const base::FilePath& index_filename, 115 const base::FilePath& index_filename);
76 const scoped_refptr<base::TaskRunner>& io_thread, 116
77 const MergeCallback& merge_callback); 117 static void WriteToDiskInternal(const base::FilePath& index_filename,
118 scoped_ptr<Pickle> pickle);
78 119
79 // Must run on IO Thread. 120 // Must run on IO Thread.
80 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); 121 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries);
81 122
82 // |out_buffer| needs to be pre-allocated. The serialized index is stored in
83 // |out_buffer|.
84 void Serialize(std::string* out_buffer);
85
86 bool OpenIndexFile();
87 bool CloseIndexFile();
88
89 static void UpdateFile(const base::FilePath& index_filename,
90 const base::FilePath& temp_filename,
91 scoped_ptr<std::string> buffer);
92
93 EntrySet entries_set_; 123 EntrySet entries_set_;
94 uint64 cache_size_; // Total cache storage size in bytes. 124 uint64 cache_size_; // Total cache storage size in bytes.
95 125
96 // This stores all the hash_key of entries that are removed during 126 // This stores all the hash_key of entries that are removed during
97 // initialization. 127 // initialization.
98 base::hash_set<uint64> removed_entries_; 128 base::hash_set<uint64> removed_entries_;
99 bool initialized_; 129 bool initialized_;
100 130
101 base::FilePath index_filename_; 131 base::FilePath index_filename_;
102 132
103 scoped_refptr<base::TaskRunner> cache_thread_; 133 scoped_refptr<base::TaskRunner> cache_thread_;
104 scoped_refptr<base::TaskRunner> io_thread_; 134 scoped_refptr<base::TaskRunner> io_thread_;
105 135
106 // All nonstatic SimpleEntryImpl methods should always be called on the IO 136 // All nonstatic SimpleEntryImpl methods should always be called on the IO
107 // thread, in all cases. |io_thread_checker_| documents and enforces this. 137 // thread, in all cases. |io_thread_checker_| documents and enforces this.
108 base::ThreadChecker io_thread_checker_; 138 base::ThreadChecker io_thread_checker_;
109 }; 139 };
110 140
111 } // namespace disk_cache 141 } // namespace disk_cache
112 142
113 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ 143 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_entry_impl.h ('k') | net/disk_cache/simple/simple_index.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698