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