OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 // See net/disk_cache/disk_cache.h for the public interface of the cache. | 5 // See net/disk_cache/disk_cache.h for the public interface of the cache. |
6 | 6 |
7 #ifndef NET_DISK_CACHE_BACKEND_IMPL_H_ | 7 #ifndef NET_DISK_CACHE_BACKEND_IMPL_H_ |
8 #define NET_DISK_CACHE_BACKEND_IMPL_H_ | 8 #define NET_DISK_CACHE_BACKEND_IMPL_H_ |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
12 #include "base/timer.h" | 12 #include "base/timer.h" |
13 #include "net/disk_cache/block_files.h" | 13 #include "net/disk_cache/block_files.h" |
14 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
15 #include "net/disk_cache/eviction.h" | 15 #include "net/disk_cache/eviction.h" |
16 #include "net/disk_cache/in_flight_backend_io.h" | |
17 #include "net/disk_cache/rankings.h" | 16 #include "net/disk_cache/rankings.h" |
18 #include "net/disk_cache/stats.h" | 17 #include "net/disk_cache/stats.h" |
19 #include "net/disk_cache/trace.h" | 18 #include "net/disk_cache/trace.h" |
20 | 19 |
21 namespace disk_cache { | 20 namespace disk_cache { |
22 | 21 |
23 enum BackendFlags { | 22 enum BackendFlags { |
24 kNone = 0, | 23 kNone = 0, |
25 kMask = 1, // A mask (for the index table) was specified. | 24 kMask = 1, // A mask (for the index table) was specified. |
26 kMaxSize = 1 << 1, // A maximum size was provided. | 25 kMaxSize = 1 << 1, // A maximum size was provided. |
27 kUnitTestMode = 1 << 2, // We are modifying the behavior for testing. | 26 kUnitTestMode = 1 << 2, // We are modifying the behavior for testing. |
28 kUpgradeMode = 1 << 3, // This is the upgrade tool (dump). | 27 kUpgradeMode = 1 << 3, // This is the upgrade tool (dump). |
29 kNewEviction = 1 << 4, // Use of new eviction was specified. | 28 kNewEviction = 1 << 4, // Use of new eviction was specified. |
30 kNoRandom = 1 << 5, // Don't add randomness to the behavior. | 29 kNoRandom = 1 << 5, // Don't add randomness to the behavior. |
31 kNoLoadProtection = 1 << 6 // Don't act conservatively under load. | 30 kNoLoadProtection = 1 << 6 // Don't act conservatively under load. |
32 }; | 31 }; |
33 | 32 |
34 // This class implements the Backend interface. An object of this | 33 // This class implements the Backend interface. An object of this |
35 // class handles the operations of the cache for a particular profile. | 34 // class handles the operations of the cache for a particular profile. |
36 class BackendImpl : public Backend { | 35 class BackendImpl : public Backend { |
37 friend class Eviction; | 36 friend class Eviction; |
38 public: | 37 public: |
39 BackendImpl(const FilePath& path, base::MessageLoopProxy* cache_thread) | 38 BackendImpl(const FilePath& path, base::MessageLoopProxy* cache_thread) |
40 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), | 39 : path_(path), block_files_(path), mask_(0), max_size_(0), |
41 path_(path), block_files_(path), mask_(0), max_size_(0), | |
42 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(0), | 40 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(0), |
43 init_(false), restarted_(false), unit_test_(false), read_only_(false), | 41 init_(false), restarted_(false), unit_test_(false), read_only_(false), |
44 new_eviction_(false), first_timer_(true), done_(true, false), | 42 new_eviction_(false), first_timer_(true), |
45 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} | 43 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} |
46 // mask can be used to limit the usable size of the hash table, for testing. | 44 // mask can be used to limit the usable size of the hash table, for testing. |
47 BackendImpl(const FilePath& path, uint32 mask, | 45 BackendImpl(const FilePath& path, uint32 mask, |
48 base::MessageLoopProxy* cache_thread) | 46 base::MessageLoopProxy* cache_thread) |
49 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), | 47 : path_(path), block_files_(path), mask_(mask), max_size_(0), |
50 path_(path), block_files_(path), mask_(mask), max_size_(0), | |
51 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(kMask), | 48 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(kMask), |
52 init_(false), restarted_(false), unit_test_(false), read_only_(false), | 49 init_(false), restarted_(false), unit_test_(false), read_only_(false), |
53 new_eviction_(false), first_timer_(true), done_(true, false), | 50 new_eviction_(false), first_timer_(true), |
54 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} | 51 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} |
55 ~BackendImpl(); | 52 ~BackendImpl(); |
56 | 53 |
57 // Returns a new backend with the desired flags. See the declaration of | 54 // Returns a new backend with the desired flags. See the declaration of |
58 // CreateCacheBackend(). | 55 // CreateCacheBackend(). |
59 static int CreateBackend(const FilePath& full_path, bool force, | 56 static int CreateBackend(const FilePath& full_path, bool force, |
60 int max_bytes, net::CacheType type, | 57 int max_bytes, net::CacheType type, |
61 uint32 flags, base::MessageLoopProxy* thread, | 58 uint32 flags, base::MessageLoopProxy* thread, |
62 Backend** backend, CompletionCallback* callback); | 59 Backend** backend, CompletionCallback* callback); |
63 | 60 |
64 // Performs general initialization for this current instance of the cache. | 61 // Performs general initialization for this current instance of the cache. |
65 bool Init(); // Deprecated. | 62 bool Init(); |
66 int Init(CompletionCallback* callback); | |
67 int SyncInit(); | |
68 | |
69 // Performs final cleanup on destruction. | |
70 void CleanupCache(); | |
71 | 63 |
72 // Backend interface. | 64 // Backend interface. |
73 virtual int32 GetEntryCount() const; | 65 virtual int32 GetEntryCount() const; |
74 virtual int OpenEntry(const std::string& key, Entry** entry, | 66 virtual int OpenEntry(const std::string& key, Entry** entry, |
75 CompletionCallback* callback); | 67 CompletionCallback* callback); |
76 virtual int CreateEntry(const std::string& key, Entry** entry, | 68 virtual int CreateEntry(const std::string& key, Entry** entry, |
77 CompletionCallback* callback); | 69 CompletionCallback* callback); |
78 virtual int DoomEntry(const std::string& key, CompletionCallback* callback); | 70 virtual int DoomEntry(const std::string& key, CompletionCallback* callback); |
79 virtual int DoomAllEntries(CompletionCallback* callback); | 71 virtual int DoomAllEntries(CompletionCallback* callback); |
80 virtual int DoomEntriesBetween(const base::Time initial_time, | 72 virtual int DoomEntriesBetween(const base::Time initial_time, |
81 const base::Time end_time, | 73 const base::Time end_time, |
82 CompletionCallback* callback); | 74 CompletionCallback* callback); |
83 virtual int DoomEntriesSince(const base::Time initial_time, | 75 virtual int DoomEntriesSince(const base::Time initial_time, |
84 CompletionCallback* callback); | 76 CompletionCallback* callback); |
85 virtual int OpenNextEntry(void** iter, Entry** next_entry, | 77 virtual int OpenNextEntry(void** iter, Entry** next_entry, |
86 CompletionCallback* callback); | 78 CompletionCallback* callback); |
87 virtual void EndEnumeration(void** iter); | 79 virtual void EndEnumeration(void** iter); |
88 virtual void GetStats(StatsItems* stats); | 80 virtual void GetStats(StatsItems* stats); |
89 | 81 |
90 // Synchronous implementation of the asynchronous interface. | |
91 int SyncOpenEntry(const std::string& key, Entry** entry); | |
92 int SyncCreateEntry(const std::string& key, Entry** entry); | |
93 int SyncDoomEntry(const std::string& key); | |
94 int SyncDoomAllEntries(); | |
95 int SyncDoomEntriesBetween(const base::Time initial_time, | |
96 const base::Time end_time); | |
97 int SyncDoomEntriesSince(const base::Time initial_time); | |
98 int SyncOpenNextEntry(void** iter, Entry** next_entry); | |
99 void SyncEndEnumeration(void* iter); | |
100 | |
101 // Sets the maximum size for the total amount of data stored by this instance. | 82 // Sets the maximum size for the total amount of data stored by this instance. |
102 bool SetMaxSize(int max_bytes); | 83 bool SetMaxSize(int max_bytes); |
103 | 84 |
104 // Sets the cache type for this backend. | 85 // Sets the cache type for this backend. |
105 void SetType(net::CacheType type); | 86 void SetType(net::CacheType type); |
106 | 87 |
107 // Returns the full name for an external storage file. | 88 // Returns the full name for an external storage file. |
108 FilePath GetFileName(Addr address) const; | 89 FilePath GetFileName(Addr address) const; |
109 | 90 |
110 // Returns the actual file used to store a given (non-external) address. | 91 // Returns the actual file used to store a given (non-external) address. |
111 MappedFile* File(Addr address); | 92 MappedFile* File(Addr address); |
112 | 93 |
113 InFlightBackendIO* background_queue() { | |
114 return &background_queue_; | |
115 } | |
116 | |
117 // Creates an external storage file. | 94 // Creates an external storage file. |
118 bool CreateExternalFile(Addr* address); | 95 bool CreateExternalFile(Addr* address); |
119 | 96 |
120 // Creates a new storage block of size block_count. | 97 // Creates a new storage block of size block_count. |
121 bool CreateBlock(FileType block_type, int block_count, | 98 bool CreateBlock(FileType block_type, int block_count, |
122 Addr* block_address); | 99 Addr* block_address); |
123 | 100 |
124 // Deletes a given storage block. deep set to true can be used to zero-fill | 101 // Deletes a given storage block. deep set to true can be used to zero-fill |
125 // the related storage in addition of releasing the related block. | 102 // the related storage in addition of releasing the related block. |
126 void DeleteBlock(Addr block_address, bool deep); | 103 void DeleteBlock(Addr block_address, bool deep); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 | 186 |
210 // Sets the eviction algorithm to version 2. | 187 // Sets the eviction algorithm to version 2. |
211 void SetNewEviction(); | 188 void SetNewEviction(); |
212 | 189 |
213 // Sets an explicit set of BackendFlags. | 190 // Sets an explicit set of BackendFlags. |
214 void SetFlags(uint32 flags); | 191 void SetFlags(uint32 flags); |
215 | 192 |
216 // Clears the counter of references to test handling of corruptions. | 193 // Clears the counter of references to test handling of corruptions. |
217 void ClearRefCountForTest(); | 194 void ClearRefCountForTest(); |
218 | 195 |
219 // Sends a dummy operation through the operation queue, for unit tests. | |
220 int FlushQueueForTest(CompletionCallback* callback); | |
221 | |
222 // Peforms a simple self-check, and returns the number of dirty items | 196 // Peforms a simple self-check, and returns the number of dirty items |
223 // or an error code (negative value). | 197 // or an error code (negative value). |
224 int SelfCheck(); | 198 int SelfCheck(); |
225 | 199 |
226 // Same bahavior as OpenNextEntry but walks the list from back to front. | 200 // Same bahavior as OpenNextEntry but walks the list from back to front. |
227 int OpenPrevEntry(void** iter, Entry** prev_entry, | 201 bool OpenPrevEntry(void** iter, Entry** prev_entry); |
228 CompletionCallback* callback); | |
229 int SyncOpenPrevEntry(void** iter, Entry** prev_entry); | |
230 | 202 |
231 // Old Backend interface. | 203 // Old Backend interface. |
232 bool OpenEntry(const std::string& key, Entry** entry); | 204 bool OpenEntry(const std::string& key, Entry** entry); |
233 bool CreateEntry(const std::string& key, Entry** entry); | 205 bool CreateEntry(const std::string& key, Entry** entry); |
234 bool DoomEntry(const std::string& key); | 206 bool DoomEntry(const std::string& key); |
235 bool DoomAllEntries(); | 207 bool DoomAllEntries(); |
236 bool DoomEntriesBetween(const base::Time initial_time, | 208 bool DoomEntriesBetween(const base::Time initial_time, |
237 const base::Time end_time); | 209 const base::Time end_time); |
238 bool DoomEntriesSince(const base::Time initial_time); | 210 bool DoomEntriesSince(const base::Time initial_time); |
| 211 bool OpenNextEntry(void** iter, Entry** next_entry); |
239 | 212 |
240 // Open or create an entry for the given |key| or |iter|. | 213 // Open or create an entry for the given |key|. |
241 EntryImpl* OpenEntryImpl(const std::string& key); | 214 EntryImpl* OpenEntryImpl(const std::string& key); |
242 EntryImpl* CreateEntryImpl(const std::string& key); | 215 EntryImpl* CreateEntryImpl(const std::string& key); |
243 EntryImpl* OpenNextEntryImpl(void** iter); | |
244 EntryImpl* OpenPrevEntryImpl(void** iter); | |
245 | 216 |
246 private: | 217 private: |
247 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; | 218 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; |
248 | 219 |
249 // Creates a new backing file for the cache index. | 220 // Creates a new backing file for the cache index. |
250 bool CreateBackingStore(disk_cache::File* file); | 221 bool CreateBackingStore(disk_cache::File* file); |
251 bool InitBackingStore(bool* file_created); | 222 bool InitBackingStore(bool* file_created); |
252 void AdjustMaxCacheSize(int table_len); | 223 void AdjustMaxCacheSize(int table_len); |
253 | 224 |
254 // Deletes the cache and starts again. | 225 // Deletes the cache and starts again. |
255 void RestartCache(); | 226 void RestartCache(); |
256 void PrepareForRestart(); | 227 void PrepareForRestart(); |
257 | 228 |
258 // Creates a new entry object and checks to see if it is dirty. Returns zero | 229 // Creates a new entry object and checks to see if it is dirty. Returns zero |
259 // on success, or a disk_cache error on failure. | 230 // on success, or a disk_cache error on failure. |
260 int NewEntry(Addr address, EntryImpl** entry, bool* dirty); | 231 int NewEntry(Addr address, EntryImpl** entry, bool* dirty); |
261 | 232 |
262 // Returns a given entry from the cache. The entry to match is determined by | 233 // Returns a given entry from the cache. The entry to match is determined by |
263 // key and hash, and the returned entry may be the matched one or it's parent | 234 // key and hash, and the returned entry may be the matched one or it's parent |
264 // on the list of entries with the same hash (or bucket). | 235 // on the list of entries with the same hash (or bucket). |
265 EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent); | 236 EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent); |
266 | 237 |
267 // Opens the next or previous entry on a cache iteration. | 238 // Opens the next or previous entry on a cache iteration. |
268 EntryImpl* OpenFollowingEntry(bool forward, void** iter); | 239 bool OpenFollowingEntry(bool forward, void** iter, Entry** next_entry); |
269 | 240 |
270 // Opens the next or previous entry on a single list. If successfull, | 241 // Opens the next or previous entry on a single list. If successfull, |
271 // |from_entry| will be updated to point to the new entry, otherwise it will | 242 // |from_entry| will be updated to point to the new entry, otherwise it will |
272 // be set to NULL; in other words, it is used as an explicit iterator. | 243 // be set to NULL; in other words, it is used as an explicit iterator. |
273 bool OpenFollowingEntryFromList(bool forward, Rankings::List list, | 244 bool OpenFollowingEntryFromList(bool forward, Rankings::List list, |
274 CacheRankingsBlock** from_entry, | 245 CacheRankingsBlock** from_entry, |
275 EntryImpl** next_entry); | 246 EntryImpl** next_entry); |
276 | 247 |
277 // Returns the entry that is pointed by |next|. If we are trimming the cache, | 248 // Returns the entry that is pointed by |next|. If we are trimming the cache, |
278 // |to_evict| should be true so that we don't perform extra disk writes. | 249 // |to_evict| should be true so that we don't perform extra disk writes. |
(...skipping 26 matching lines...) Expand all Loading... |
305 | 276 |
306 // Performs basic checks on the index file. Returns false on failure. | 277 // Performs basic checks on the index file. Returns false on failure. |
307 bool CheckIndex(); | 278 bool CheckIndex(); |
308 | 279 |
309 // Part of the selt test. Returns the number or dirty entries, or an error. | 280 // Part of the selt test. Returns the number or dirty entries, or an error. |
310 int CheckAllEntries(); | 281 int CheckAllEntries(); |
311 | 282 |
312 // Part of the self test. Returns false if the entry is corrupt. | 283 // Part of the self test. Returns false if the entry is corrupt. |
313 bool CheckEntry(EntryImpl* cache_entry); | 284 bool CheckEntry(EntryImpl* cache_entry); |
314 | 285 |
315 InFlightBackendIO background_queue_; // The controller of pending operations. | |
316 scoped_refptr<MappedFile> index_; // The main cache index. | 286 scoped_refptr<MappedFile> index_; // The main cache index. |
317 FilePath path_; // Path to the folder used as backing storage. | 287 FilePath path_; // Path to the folder used as backing storage. |
318 Index* data_; // Pointer to the index data. | 288 Index* data_; // Pointer to the index data. |
319 BlockFiles block_files_; // Set of files used to store all data. | 289 BlockFiles block_files_; // Set of files used to store all data. |
320 Rankings rankings_; // Rankings to be able to trim the cache. | 290 Rankings rankings_; // Rankings to be able to trim the cache. |
321 uint32 mask_; // Binary mask to map a hash to the hash table. | 291 uint32 mask_; // Binary mask to map a hash to the hash table. |
322 int32 max_size_; // Maximum data size for this instance. | 292 int32 max_size_; // Maximum data size for this instance. |
323 Eviction eviction_; // Handler of the eviction algorithm. | 293 Eviction eviction_; // Handler of the eviction algorithm. |
324 EntriesMap open_entries_; // Map of open entries. | 294 EntriesMap open_entries_; // Map of open entries. |
325 int num_refs_; // Number of referenced cache entries. | 295 int num_refs_; // Number of referenced cache entries. |
326 int max_refs_; // Max number of referenced cache entries. | 296 int max_refs_; // Max number of referenced cache entries. |
327 int num_pending_io_; // Number of pending IO operations. | 297 int num_pending_io_; // Number of pending IO operations. |
328 net::CacheType cache_type_; | 298 net::CacheType cache_type_; |
329 int uma_report_; // Controls transmision of UMA data. | 299 int uma_report_; // Controls transmision of UMA data. |
330 uint32 user_flags_; // Flags set by the user. | 300 uint32 user_flags_; // Flags set by the user. |
331 bool init_; // controls the initialization of the system. | 301 bool init_; // controls the initialization of the system. |
332 bool restarted_; | 302 bool restarted_; |
333 bool unit_test_; | 303 bool unit_test_; |
334 bool read_only_; // Prevents updates of the rankings data (used by tools). | 304 bool read_only_; // Prevents updates of the rankings data (used by tools). |
335 bool disabled_; | 305 bool disabled_; |
336 bool new_eviction_; // What eviction algorithm should be used. | 306 bool new_eviction_; // What eviction algorithm should be used. |
337 bool first_timer_; // True if the timer has not been called. | 307 bool first_timer_; // True if the timer has not been called. |
338 | 308 |
339 Stats stats_; // Usage statistcs. | 309 Stats stats_; // Usage statistcs. |
340 base::RepeatingTimer<BackendImpl> timer_; // Usage timer. | 310 base::RepeatingTimer<BackendImpl> timer_; // Usage timer. |
341 base::WaitableEvent done_; // Signals the end of background work. | |
342 scoped_refptr<TraceObject> trace_object_; // Inits internal tracing. | 311 scoped_refptr<TraceObject> trace_object_; // Inits internal tracing. |
343 ScopedRunnableMethodFactory<BackendImpl> factory_; | 312 ScopedRunnableMethodFactory<BackendImpl> factory_; |
344 | 313 |
345 DISALLOW_COPY_AND_ASSIGN(BackendImpl); | 314 DISALLOW_COPY_AND_ASSIGN(BackendImpl); |
346 }; | 315 }; |
347 | 316 |
348 // Returns the prefered max cache size given the available disk space. | 317 // Returns the prefered max cache size given the available disk space. |
349 int PreferedCacheSize(int64 available); | 318 int PreferedCacheSize(int64 available); |
350 | 319 |
351 } // namespace disk_cache | 320 } // namespace disk_cache |
352 | 321 |
353 #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ | 322 #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ |
OLD | NEW |