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 bool UpdateEntrySize(const std::string& key, uint64 entry_size); | 50 bool UpdateEntrySize(const std::string& key, uint64 entry_size); |
51 | 51 |
52 private: | 52 private: |
53 // TODO(felipeg): This way we are storing the hash_key string twice (as the | 53 // TODO(felipeg): This way we are storing the hash_key string twice (as the |
54 // hash_map::key and as a member of EntryMetadata. We could save space if we | 54 // hash_map::key and as a member of EntryMetadata. We could save space if we |
55 // redefine the hash_map::operators and make the hash_map::key be part of the | 55 // redefine the hash_map::operators and make the hash_map::key be part of the |
56 // EntryMetadata itself. | 56 // EntryMetadata itself. |
57 typedef base::hash_map<std::string, SimpleIndexFile::EntryMetadata> EntrySet; | 57 typedef base::hash_map<std::string, SimpleIndexFile::EntryMetadata> EntrySet; |
58 | 58 |
59 void InsertInternal(const SimpleIndexFile::EntryMetadata& entry_metadata); | 59 static void InsertInternal( |
60 EntrySet* entry_set, | |
61 const SimpleIndexFile::EntryMetadata& entry_metadata); | |
62 | |
63 // Load index file from disk, if file is corrupted it calls RestoreFromDisk(). | |
gavinp
2013/04/10 11:45:44
Suggestion: // Load index from disk. If it is corr
felipeg
2013/04/10 14:21:45
Done.
| |
64 void LoadFromDisk(); | |
60 | 65 |
61 // Enumerates all entries' files on disk and regenerates the index. | 66 // Enumerates all entries' files on disk and regenerates the index. |
62 bool RestoreFromDisk(); | 67 void RestoreFromDisk(); |
68 | |
69 // Must run on IO Thread. | |
70 void MergeInitializingSet(); | |
63 | 71 |
64 // |out_buffer| needs to be pre-allocated. The serialized index is stored in | 72 // |out_buffer| needs to be pre-allocated. The serialized index is stored in |
65 // |out_buffer|. | 73 // |out_buffer|. |
66 void Serialize(std::string* out_buffer); | 74 void Serialize(std::string* out_buffer); |
67 | 75 |
68 bool OpenIndexFile(); | 76 bool OpenIndexFile(); |
69 bool CloseIndexFile(); | 77 bool CloseIndexFile(); |
70 | 78 |
71 static void UpdateFile(const base::FilePath& index_filename, | 79 static void UpdateFile(const base::FilePath& index_filename, |
72 const base::FilePath& temp_filename, | 80 const base::FilePath& temp_filename, |
73 scoped_ptr<std::string> buffer); | 81 scoped_ptr<std::string> buffer); |
74 | 82 |
75 const base::FilePath path_; | 83 const base::FilePath path_; |
76 | 84 |
77 EntrySet entries_set_; | 85 EntrySet entries_set_; |
78 uint64 cache_size_; // Total cache storage size in bytes. | 86 uint64 cache_size_; // Total cache storage size in bytes. |
79 | 87 |
88 // These sets are only used during initialization phase. | |
89 // They are merged back in the entries_set_ once it finishes. | |
90 EntrySet initializing_set_; | |
91 // This stores all the hash_key of entries that are removed during | |
92 // initialization. | |
93 base::hash_set<std::string> removals_set_; | |
94 bool initialized_; | |
95 | |
80 base::FilePath index_filename_; | 96 base::FilePath index_filename_; |
81 base::PlatformFile index_file_; | 97 base::PlatformFile index_file_; |
82 | 98 |
83 // We keep the thread from where Initialize() method has been called so that | 99 // We keep the thread from where Initialize() method has been called so that |
84 // we run the Cleanup method in the same thread. Usually that should be the | 100 // we run the WriteToDisk method in the same thread. Usually that should be |
85 // CacheThread. | 101 // the Cache Thread. |
86 scoped_refptr<base::TaskRunner> cache_thread_; | 102 scoped_refptr<base::TaskRunner> cache_thread_; |
103 scoped_refptr<base::TaskRunner> io_thread_; | |
87 }; | 104 }; |
88 | 105 |
89 } // namespace disk_cache | 106 } // namespace disk_cache |
90 | 107 |
91 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 108 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
OLD | NEW |