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 |
(...skipping 11 matching lines...) Expand all Loading... | |
22 } | 22 } |
23 | 23 |
24 namespace disk_cache { | 24 namespace disk_cache { |
25 | 25 |
26 // This class is not Thread-safe. | 26 // This class is not Thread-safe. |
27 class SimpleIndex | 27 class SimpleIndex |
28 : public base::SupportsWeakPtr<SimpleIndex> { | 28 : public base::SupportsWeakPtr<SimpleIndex> { |
29 public: | 29 public: |
30 SimpleIndex( | 30 SimpleIndex( |
31 const scoped_refptr<base::TaskRunner>& cache_thread, | 31 const scoped_refptr<base::TaskRunner>& cache_thread, |
32 const scoped_refptr<base::TaskRunner>& io_thread, | |
32 const base::FilePath& path); | 33 const base::FilePath& path); |
33 | 34 |
34 virtual ~SimpleIndex(); | 35 virtual ~SimpleIndex(); |
35 | 36 |
36 // Should be called on CacheThread. | 37 void Initialize(); |
37 bool Initialize(); | |
38 | 38 |
39 void Insert(const std::string& key); | 39 void Insert(const std::string& key); |
40 void Remove(const std::string& key); | 40 void Remove(const std::string& key); |
41 | 41 |
42 bool Has(const std::string& key) const; | 42 bool Has(const std::string& key) const; |
43 | 43 |
44 // 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 |
45 // iff the entry exist in the index. | 45 // iff the entry exist in the index. |
46 bool UseIfExists(const std::string& key); | 46 bool UseIfExists(const std::string& key); |
47 | 47 |
48 void Cleanup(); | 48 void WriteToDisk(); |
49 | 49 |
50 // 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 |
51 // 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 |
52 // entry. | 52 // entry. |
53 bool UpdateEntrySize(const std::string& key, uint64 entry_size); | 53 bool UpdateEntrySize(const std::string& key, uint64 entry_size); |
54 | 54 |
55 private: | 55 private: |
56 // TODO(felipeg): This way we are storing the hash_key string twice (as the | 56 // TODO(felipeg): This way we are storing the hash_key string twice (as the |
57 // 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 |
58 // redefine the hash_map::operators and make the hash_map::key be part of the | 58 // redefine the hash_map::operators and make the hash_map::key be part of the |
59 // EntryMetadata itself. | 59 // EntryMetadata itself. |
60 typedef base::hash_map<std::string, SimpleIndexFile::EntryMetadata> EntrySet; | 60 typedef base::hash_map<std::string, SimpleIndexFile::EntryMetadata> EntrySet; |
61 | 61 |
62 void InsertInternal(const SimpleIndexFile::EntryMetadata& entry_metadata); | 62 typedef base::Callback<void(scoped_ptr<EntrySet>)> MergeCallback; |
63 | |
64 static void InsertInternal( | |
65 EntrySet* entry_set, | |
66 const SimpleIndexFile::EntryMetadata& entry_metadata); | |
67 | |
68 // Load index from disk. If it is corrupted, call RestoreFromDisk(). | |
69 static void LoadFromDisk( | |
70 const base::FilePath& index_filename, | |
71 const scoped_refptr<base::TaskRunner>& io_thread, | |
72 const MergeCallback& merge_callback); | |
63 | 73 |
64 // Enumerates all entries' files on disk and regenerates the index. | 74 // Enumerates all entries' files on disk and regenerates the index. |
65 bool RestoreFromDisk(); | 75 static void RestoreFromDisk( |
76 const base::FilePath& index_filename, | |
77 const scoped_refptr<base::TaskRunner>& io_thread, | |
78 const MergeCallback& merge_callback); | |
79 | |
80 // Must run on IO Thread. | |
81 void MergeInitializingSet(scoped_ptr<EntrySet> initializing_set); | |
66 | 82 |
67 // |out_buffer| needs to be pre-allocated. The serialized index is stored in | 83 // |out_buffer| needs to be pre-allocated. The serialized index is stored in |
68 // |out_buffer|. | 84 // |out_buffer|. |
69 void Serialize(std::string* out_buffer); | 85 void Serialize(std::string* out_buffer); |
70 | 86 |
71 bool OpenIndexFile(); | 87 bool OpenIndexFile(); |
72 bool CloseIndexFile(); | 88 bool CloseIndexFile(); |
73 | 89 |
74 static void UpdateFile(const base::FilePath& index_filename, | 90 static void UpdateFile(const base::FilePath& index_filename, |
75 const base::FilePath& temp_filename, | 91 const base::FilePath& temp_filename, |
76 scoped_ptr<std::string> buffer); | 92 scoped_ptr<std::string> buffer); |
77 | 93 |
78 const base::FilePath path_; | |
79 | |
80 EntrySet entries_set_; | 94 EntrySet entries_set_; |
81 uint64 cache_size_; // Total cache storage size in bytes. | 95 uint64 cache_size_; // Total cache storage size in bytes. |
82 | 96 |
97 // This stores all the hash_key of entries that are removed during | |
98 // initialization. | |
99 base::hash_set<std::string> removals_set_; | |
gavinp
2013/04/11 09:56:32
I like: removed_entries_ better for a name.
felipeg
2013/04/11 11:25:45
Done.
| |
100 bool initialized_; | |
101 | |
83 base::FilePath index_filename_; | 102 base::FilePath index_filename_; |
84 base::PlatformFile index_file_; | |
85 | 103 |
86 // We keep the thread from where Initialize() method has been called so that | |
87 // we run the Cleanup method in the same thread. Usually that should be the | |
88 // CacheThread. | |
89 scoped_refptr<base::TaskRunner> cache_thread_; | 104 scoped_refptr<base::TaskRunner> cache_thread_; |
105 scoped_refptr<base::TaskRunner> io_thread_; | |
106 | |
107 // All nonstatic SimpleEntryImpl methods should always be called on the IO | |
108 // thread, in all cases. |io_thread_checker_| documents and enforces this. | |
109 base::ThreadChecker io_thread_checker_; | |
90 }; | 110 }; |
91 | 111 |
92 } // namespace disk_cache | 112 } // namespace disk_cache |
93 | 113 |
94 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 114 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
OLD | NEW |