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/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 16 #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 | 17 |
20 namespace base { | 18 namespace base { |
21 class TaskRunner; | 19 class TaskRunner; |
22 } | 20 } |
23 | 21 |
24 namespace disk_cache { | 22 namespace disk_cache { |
25 | 23 |
24 class EntryMetadata { | |
25 public: | |
26 EntryMetadata(); | |
27 EntryMetadata(uint64 hash_key, | |
28 base::Time last_used_time, | |
29 uint64 entry_size); | |
30 | |
31 uint64 GetHashKey() const; | |
Philippe
2013/04/15 13:57:57
Nit: I would make this an inline getter as you did
felipeg
2013/04/15 14:39:07
Done.
| |
32 | |
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 // If we had cheese we could make a sandwitch if we had bread. But we got | |
40 // only pickles. | |
pasko-google - do not use
2013/04/15 14:23:55
thanks for the funny comment, and please remove it
felipeg
2013/04/15 15:37:04
Done.
| |
41 // Serialize the data into the provided pickle. | |
42 void Serialize(Pickle* pickle) const; | |
43 static bool DeSerialize(PickleIterator* it, | |
Philippe
2013/04/15 13:57:57
I would make DeSerialize() an instance method to m
felipeg
2013/04/15 14:39:07
Done.
| |
44 EntryMetadata* out); | |
pasko-google - do not use
2013/04/15 14:23:55
nit: fits in one line
felipeg
2013/04/15 15:37:04
Done.
felipeg
2013/04/15 15:37:04
Done.
| |
45 | |
46 // Merge two EntryMetadata instances. | |
47 // The existing current valid data in this object will prevail. | |
48 void MergeWith(const EntryMetadata& entry_metadata); | |
49 | |
50 private: | |
51 uint64 hash_key_; | |
52 | |
53 // This is the serialized format from Time::ToInternalValue(). | |
54 // If you want to make calculations/comparisons, you should use the | |
55 // base::Time() class. Use the GetLastUsedTime() method above. | |
56 int64 last_used_time_; | |
pasko-google - do not use
2013/04/15 14:23:55
since we serialize now without writing bare struct
felipeg
2013/04/15 15:37:04
Done.
| |
57 | |
58 uint64 entry_size_; // Storage size in bytes. | |
59 }; | |
60 | |
61 // TODO(felipeg): This way we are storing the hash_key twice (as the | |
Philippe
2013/04/15 13:57:57
Nit: the ')' is missing.
felipeg
2013/04/15 14:39:07
Done.
| |
62 // hash_map::key and as a member of EntryMetadata. We could save space if we | |
63 // use a hash_set. | |
64 typedef base::hash_map<uint64, EntryMetadata> EntrySet; | |
65 | |
66 void InsertInEntrySet(EntrySet* entry_set, | |
Philippe
2013/04/15 13:57:57
Nit: output parameters should be last.
Philippe
2013/04/15 13:57:57
I would make this function return whether the inse
felipeg
2013/04/15 14:39:07
Done.
| |
67 const EntryMetadata& entry_metadata); | |
68 | |
69 typedef base::Callback<void(scoped_ptr<EntrySet>)> IndexCompletionCallback; | |
70 | |
26 // This class is not Thread-safe. | 71 // This class is not Thread-safe. |
27 class SimpleIndex | 72 class SimpleIndex |
28 : public base::SupportsWeakPtr<SimpleIndex> { | 73 : public base::SupportsWeakPtr<SimpleIndex> { |
29 public: | 74 public: |
30 SimpleIndex( | 75 SimpleIndex( |
31 const scoped_refptr<base::TaskRunner>& cache_thread, | 76 const scoped_refptr<base::TaskRunner>& cache_thread, |
32 const scoped_refptr<base::TaskRunner>& io_thread, | 77 const scoped_refptr<base::TaskRunner>& io_thread, |
33 const base::FilePath& path); | 78 const base::FilePath& path); |
34 | 79 |
35 virtual ~SimpleIndex(); | 80 virtual ~SimpleIndex(); |
(...skipping 10 matching lines...) Expand all Loading... | |
46 bool UseIfExists(const std::string& key); | 91 bool UseIfExists(const std::string& key); |
47 | 92 |
48 void WriteToDisk(); | 93 void WriteToDisk(); |
49 | 94 |
50 // Update the size (in bytes) of an entry, in the metadata stored in the | 95 // 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 | 96 // index. This should be the total disk-file size including all streams of the |
52 // entry. | 97 // entry. |
53 bool UpdateEntrySize(const std::string& key, uint64 entry_size); | 98 bool UpdateEntrySize(const std::string& key, uint64 entry_size); |
54 | 99 |
55 private: | 100 private: |
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 | |
58 // use a hash_set. | |
59 typedef base::hash_map<uint64, SimpleIndexFile::EntryMetadata> EntrySet; | |
60 | |
61 typedef base::Callback<void(scoped_ptr<EntrySet>)> MergeCallback; | |
62 | |
63 static void InsertInternal( | |
64 EntrySet* entry_set, | |
65 const SimpleIndexFile::EntryMetadata& entry_metadata); | |
66 | |
67 // Load index from disk. If it is corrupted, call RestoreFromDisk(). | |
68 static void LoadFromDisk( | |
69 const base::FilePath& index_filename, | |
70 const scoped_refptr<base::TaskRunner>& io_thread, | |
71 const MergeCallback& merge_callback); | |
72 | |
73 // Enumerates all entries' files on disk and regenerates the index. | |
74 static void RestoreFromDisk( | |
75 const base::FilePath& index_filename, | |
76 const scoped_refptr<base::TaskRunner>& io_thread, | |
77 const MergeCallback& merge_callback); | |
78 | |
79 // Must run on IO Thread. | 101 // Must run on IO Thread. |
80 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); | 102 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); |
81 | 103 |
82 // |out_buffer| needs to be pre-allocated. The serialized index is stored in | 104 // |out_buffer| needs to be pre-allocated. The serialized index is stored in |
83 // |out_buffer|. | 105 // |out_buffer|. |
84 void Serialize(std::string* out_buffer); | 106 void Serialize(std::string* out_buffer); |
85 | 107 |
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_; | 108 EntrySet entries_set_; |
94 uint64 cache_size_; // Total cache storage size in bytes. | 109 uint64 cache_size_; // Total cache storage size in bytes. |
95 | 110 |
96 // This stores all the hash_key of entries that are removed during | 111 // This stores all the hash_key of entries that are removed during |
97 // initialization. | 112 // initialization. |
98 base::hash_set<uint64> removed_entries_; | 113 base::hash_set<uint64> removed_entries_; |
99 bool initialized_; | 114 bool initialized_; |
100 | 115 |
101 base::FilePath index_filename_; | 116 base::FilePath index_filename_; |
102 | 117 |
103 scoped_refptr<base::TaskRunner> cache_thread_; | 118 scoped_refptr<base::TaskRunner> cache_thread_; |
104 scoped_refptr<base::TaskRunner> io_thread_; | 119 scoped_refptr<base::TaskRunner> io_thread_; |
105 | 120 |
106 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 121 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
107 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 122 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
108 base::ThreadChecker io_thread_checker_; | 123 base::ThreadChecker io_thread_checker_; |
109 }; | 124 }; |
110 | 125 |
111 } // namespace disk_cache | 126 } // namespace disk_cache |
112 | 127 |
113 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 128 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
OLD | NEW |