OLD | NEW |
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/callback.h" | 12 #include "base/file_util.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" | |
16 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
18 #include "base/time.h" | 17 #include "net/disk_cache/disk_cache.h" |
19 #include "net/base/net_export.h" | 18 #include "net/disk_cache/simple/simple_disk_format.h" |
20 | |
21 class Pickle; | |
22 class PickleIterator; | |
23 | 19 |
24 namespace base { | 20 namespace base { |
25 class TaskRunner; | 21 class TaskRunner; |
26 } | 22 } |
27 | 23 |
28 namespace disk_cache { | 24 namespace disk_cache { |
29 | 25 |
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 | |
68 // This class is not Thread-safe. | 26 // This class is not Thread-safe. |
69 class NET_EXPORT_PRIVATE SimpleIndex | 27 class SimpleIndex |
70 : public base::SupportsWeakPtr<SimpleIndex> { | 28 : public base::SupportsWeakPtr<SimpleIndex> { |
71 public: | 29 public: |
72 SimpleIndex( | 30 SimpleIndex( |
73 const scoped_refptr<base::TaskRunner>& cache_thread, | 31 const scoped_refptr<base::TaskRunner>& cache_thread, |
74 const scoped_refptr<base::TaskRunner>& io_thread, | 32 const scoped_refptr<base::TaskRunner>& io_thread, |
75 const base::FilePath& path); | 33 const base::FilePath& path); |
76 | 34 |
77 virtual ~SimpleIndex(); | 35 virtual ~SimpleIndex(); |
78 | 36 |
79 void Initialize(); | 37 void Initialize(); |
80 | 38 |
81 void Insert(const std::string& key); | 39 void Insert(const std::string& key); |
82 void Remove(const std::string& key); | 40 void Remove(const std::string& key); |
83 | 41 |
84 bool Has(const std::string& key) const; | 42 bool Has(const std::string& key) const; |
85 | 43 |
86 // Update the last used time of the entry with the given key and return true | 44 // Update the last used time of the entry with the given key and return true |
87 // iff the entry exist in the index. | 45 // iff the entry exist in the index. |
88 bool UseIfExists(const std::string& key); | 46 bool UseIfExists(const std::string& key); |
89 | 47 |
90 void WriteToDisk(); | 48 void WriteToDisk(); |
91 | 49 |
92 // Update the size (in bytes) of an entry, in the metadata stored in the | 50 // Update the size (in bytes) of an entry, in the metadata stored in the |
93 // index. This should be the total disk-file size including all streams of the | 51 // index. This should be the total disk-file size including all streams of the |
94 // entry. | 52 // entry. |
95 bool UpdateEntrySize(const std::string& key, uint64 entry_size); | 53 bool UpdateEntrySize(const std::string& key, uint64 entry_size); |
96 | 54 |
97 // TODO(felipeg): This way we are storing the hash_key twice, as the | 55 private: |
| 56 // TODO(felipeg): This way we are storing the hash_key twice (as the |
98 // hash_map::key and as a member of EntryMetadata. We could save space if we | 57 // hash_map::key and as a member of EntryMetadata. We could save space if we |
99 // use a hash_set. | 58 // use a hash_set. |
100 typedef base::hash_map<uint64, EntryMetadata> EntrySet; | 59 typedef base::hash_map<uint64, SimpleIndexFile::EntryMetadata> EntrySet; |
101 | 60 |
102 static void InsertInEntrySet(const EntryMetadata& entry_metadata, | 61 typedef base::Callback<void(scoped_ptr<EntrySet>)> MergeCallback; |
103 EntrySet* entry_set); | |
104 | 62 |
105 private: | 63 static void InsertInternal( |
106 typedef base::Callback<void(scoped_ptr<EntrySet>)> IndexCompletionCallback; | 64 EntrySet* entry_set, |
| 65 const SimpleIndexFile::EntryMetadata& entry_metadata); |
107 | 66 |
| 67 // Load index from disk. If it is corrupted, call RestoreFromDisk(). |
108 static void LoadFromDisk( | 68 static void LoadFromDisk( |
109 const base::FilePath& index_filename, | 69 const base::FilePath& index_filename, |
110 const scoped_refptr<base::TaskRunner>& io_thread, | 70 const scoped_refptr<base::TaskRunner>& io_thread, |
111 const IndexCompletionCallback& completion_callback); | 71 const MergeCallback& merge_callback); |
112 | 72 |
113 // Enumerates all entries' files on disk and regenerates the index. | 73 // Enumerates all entries' files on disk and regenerates the index. |
114 static scoped_ptr<SimpleIndex::EntrySet> RestoreFromDisk( | 74 static void RestoreFromDisk( |
115 const base::FilePath& index_filename); | 75 const base::FilePath& index_filename, |
116 | 76 const scoped_refptr<base::TaskRunner>& io_thread, |
117 static void WriteToDiskInternal(const base::FilePath& index_filename, | 77 const MergeCallback& merge_callback); |
118 scoped_ptr<Pickle> pickle); | |
119 | 78 |
120 // Must run on IO Thread. | 79 // Must run on IO Thread. |
121 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); | 80 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); |
122 | 81 |
| 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 |
123 EntrySet entries_set_; | 93 EntrySet entries_set_; |
124 uint64 cache_size_; // Total cache storage size in bytes. | 94 uint64 cache_size_; // Total cache storage size in bytes. |
125 | 95 |
126 // This stores all the hash_key of entries that are removed during | 96 // This stores all the hash_key of entries that are removed during |
127 // initialization. | 97 // initialization. |
128 base::hash_set<uint64> removed_entries_; | 98 base::hash_set<uint64> removed_entries_; |
129 bool initialized_; | 99 bool initialized_; |
130 | 100 |
131 base::FilePath index_filename_; | 101 base::FilePath index_filename_; |
132 | 102 |
133 scoped_refptr<base::TaskRunner> cache_thread_; | 103 scoped_refptr<base::TaskRunner> cache_thread_; |
134 scoped_refptr<base::TaskRunner> io_thread_; | 104 scoped_refptr<base::TaskRunner> io_thread_; |
135 | 105 |
136 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 106 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
137 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 107 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
138 base::ThreadChecker io_thread_checker_; | 108 base::ThreadChecker io_thread_checker_; |
139 }; | 109 }; |
140 | 110 |
141 } // namespace disk_cache | 111 } // namespace disk_cache |
142 | 112 |
143 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 113 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
OLD | NEW |