OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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. | 5 // See net/disk_cache/disk_cache.h for the public interface. |
6 | 6 |
7 #ifndef NET_DISK_CACHE_BLOCK_FILES_H_ | 7 #ifndef NET_DISK_CACHE_BLOCK_FILES_H_ |
8 #define NET_DISK_CACHE_BLOCK_FILES_H_ | 8 #define NET_DISK_CACHE_BLOCK_FILES_H_ |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
16 #include "net/disk_cache/addr.h" | 16 #include "net/disk_cache/addr.h" |
| 17 #include "net/disk_cache/disk_format_base.h" |
17 #include "net/disk_cache/mapped_file.h" | 18 #include "net/disk_cache/mapped_file.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class ThreadChecker; | 21 class ThreadChecker; |
21 } | 22 } |
22 | 23 |
23 namespace disk_cache { | 24 namespace disk_cache { |
24 | 25 |
| 26 // An instance of this class represents the header of a block file in memory. |
| 27 // Note that this class doesn't perform any file operation (as in it only deals |
| 28 // with entities in memory). |
| 29 // The header of a block file (and hence, this object) is all that is needed to |
| 30 // perform common operations like allocating or releasing space for storage; |
| 31 // actual access to that storage, however, is not performed through this class. |
| 32 class NET_EXPORT_PRIVATE BlockHeader { |
| 33 public: |
| 34 BlockHeader(); |
| 35 explicit BlockHeader(BlockFileHeader* header); |
| 36 explicit BlockHeader(MappedFile* file); |
| 37 BlockHeader(const BlockHeader& other); |
| 38 ~BlockHeader(); |
| 39 |
| 40 // Creates a new entry of |size| blocks on the allocation map, updating the |
| 41 // apropriate counters. |
| 42 bool CreateMapBlock(int size, int* index); |
| 43 |
| 44 // Deletes the block pointed by |index|. |
| 45 void DeleteMapBlock(int index, int block_size); |
| 46 |
| 47 // Returns true if the specified block is used. |
| 48 bool UsedMapBlock(int index, int size); |
| 49 |
| 50 // Restores the "empty counters" and allocation hints. |
| 51 void FixAllocationCounters(); |
| 52 |
| 53 // Returns true if the current block file should not be used as-is to store |
| 54 // more records. |block_count| is the number of blocks to allocate. |
| 55 bool NeedToGrowBlockFile(int block_count) const; |
| 56 |
| 57 // Returns true if this block file can be used to store an extra record of |
| 58 // size |block_count|. |
| 59 bool CanAllocate(int block_count) const; |
| 60 |
| 61 // Returns the number of empty blocks for this file. |
| 62 int EmptyBlocks() const; |
| 63 |
| 64 // Returns the minumum number of allocations that can be satisfied. |
| 65 int MinimumAllocations() const; |
| 66 |
| 67 // Returns the number of blocks that this file can store. |
| 68 int Capacity() const; |
| 69 |
| 70 // Returns true if the counters look OK. |
| 71 bool ValidateCounters() const; |
| 72 |
| 73 // Returns the identifiers of this and the next file (0 if there is none). |
| 74 int FileId() const; |
| 75 int NextFileId() const; |
| 76 |
| 77 // Returns a pointer to the underlying BlockFileHeader. |
| 78 // TODO(rvargas): This may be removed with the support for V2. |
| 79 BlockFileHeader* Header(); |
| 80 |
| 81 private: |
| 82 BlockFileHeader* header_; |
| 83 }; |
| 84 |
| 85 typedef std::vector<BlockHeader> BlockFilesBitmaps; |
| 86 |
25 // This class handles the set of block-files open by the disk cache. | 87 // This class handles the set of block-files open by the disk cache. |
26 class NET_EXPORT_PRIVATE BlockFiles { | 88 class NET_EXPORT_PRIVATE BlockFiles { |
27 public: | 89 public: |
28 explicit BlockFiles(const base::FilePath& path); | 90 explicit BlockFiles(const base::FilePath& path); |
29 ~BlockFiles(); | 91 ~BlockFiles(); |
30 | 92 |
31 // Performs the object initialization. create_files indicates if the backing | 93 // Performs the object initialization. create_files indicates if the backing |
32 // files should be created or just open. | 94 // files should be created or just open. |
33 bool Init(bool create_files); | 95 bool Init(bool create_files, int num_files); |
| 96 |
| 97 // Returns the allocation bitmaps for all the files managed by this object. |
| 98 void GetBitmaps(int num_files, BlockFilesBitmaps* bitmaps); |
34 | 99 |
35 // Returns the file that stores a given address. | 100 // Returns the file that stores a given address. |
36 MappedFile* GetFile(Addr address); | 101 MappedFile* GetFile(Addr address); |
37 | 102 |
38 // Creates a new entry on a block file. block_type indicates the size of block | 103 // Creates a new entry on a block file. block_type indicates the size of block |
39 // to be used (as defined on cache_addr.h), block_count is the number of | 104 // to be used (as defined on cache_addr.h), block_count is the number of |
40 // blocks to allocate, and block_address is the address of the new entry. | 105 // blocks to allocate, and block_address is the address of the new entry. |
41 bool CreateBlock(FileType block_type, int block_count, Addr* block_address); | 106 bool CreateBlock(FileType block_type, int block_count, Addr* block_address); |
42 | 107 |
43 // Removes an entry from the block files. If deep is true, the storage is zero | 108 // Removes an entry from the block files. If deep is true, the storage is zero |
44 // filled; otherwise the entry is removed but the data is not altered (must be | 109 // filled; otherwise the entry is removed but the data is not altered (must be |
45 // already zeroed). | 110 // already zeroed). |
46 void DeleteBlock(Addr address, bool deep); | 111 void DeleteBlock(Addr address, bool deep); |
47 | 112 |
48 // Close all the files and set the internal state to be initializad again. The | 113 // Close all the files and set the internal state to be initializad again. The |
49 // cache is being purged. | 114 // cache is being purged. |
50 void CloseFiles(); | 115 void CloseFiles(); |
51 | 116 |
52 // Sends UMA stats. | 117 // Sends UMA stats. |
53 void ReportStats(); | 118 void ReportStats(); |
54 | 119 |
55 // Returns true if the blocks pointed by a given address are currently used. | 120 // Returns true if the blocks pointed by a given address are currently used. |
56 // This method is only intended for debugging. | 121 // This method is only intended for debugging. |
57 bool IsValid(Addr address); | 122 bool IsValid(Addr address); |
58 | 123 |
| 124 // Increments the size of files very slowly. |
| 125 void UseSmallSizeIncrementsForTest() { small_steps_ = true; } |
| 126 |
59 private: | 127 private: |
60 // Set force to true to overwrite the file if it exists. | 128 // Set force to true to overwrite the file if it exists. |
61 bool CreateBlockFile(int index, FileType file_type, bool force); | 129 bool CreateBlockFile(int index, FileType file_type, bool force); |
62 bool OpenBlockFile(int index); | 130 bool OpenBlockFile(int index); |
63 | 131 |
64 // Attemp to grow this file. Fails if the file cannot be extended anymore. | 132 // Attemp to grow this file. Fails if the file cannot be extended anymore. |
65 bool GrowBlockFile(MappedFile* file, BlockFileHeader* header); | 133 bool GrowBlockFile(BlockFileHeader* header); |
66 | 134 |
67 // Returns the appropriate file to use for a new block. | 135 // Returns the appropriate file to use for a new block. |
68 MappedFile* FileForNewBlock(FileType block_type, int block_count); | 136 MappedFile* FileForNewBlock(FileType block_type, int block_count); |
69 | 137 |
70 // Returns the next block file on this chain, creating new files if needed. | 138 // Returns the next block file on this chain, creating new files if needed. |
71 MappedFile* NextFile(MappedFile* file); | 139 MappedFile* NextFile(MappedFile* file); |
72 | 140 |
| 141 // Returns the file index that stores a given address, or -1 on failure. |
| 142 int GetFileIndex(Addr address); |
| 143 |
| 144 // Returns the header file that stores a given address. |
| 145 MappedFile* GetFileHeader(Addr address); |
| 146 |
73 // Creates an empty block file and returns its index. | 147 // Creates an empty block file and returns its index. |
74 int CreateNextBlockFile(FileType block_type); | 148 int CreateNextBlockFile(FileType block_type); |
75 | 149 |
76 // Removes a chained block file that is now empty. | 150 // Removes a chained block file that is now empty. |
77 bool RemoveEmptyFile(FileType block_type); | 151 bool RemoveEmptyFile(FileType block_type); |
78 | 152 |
| 153 bool PreallocateSpace(FileType block_type); |
| 154 |
79 // Restores the header of a potentially inconsistent file. | 155 // Restores the header of a potentially inconsistent file. |
80 bool FixBlockFileHeader(MappedFile* file); | 156 bool FixBlockFileHeader(int index);//pass BlockHeader instead. |
81 | 157 |
82 // Retrieves stats for the given file index. | 158 // Retrieves stats for the given file index. |
83 void GetFileStats(int index, int* used_count, int* load); | 159 void GetFileStats(int index, int* used_count, int* load); |
84 | 160 |
85 // Returns the filename for a given file index. | 161 // Returns the filename for the header section, given a file index. |
86 base::FilePath Name(int index); | 162 base::FilePath HeaderName(int index); |
| 163 |
| 164 // Returns the filename for the data portion, given a file index. |
| 165 base::FilePath DataName(int index); |
87 | 166 |
88 bool init_; | 167 bool init_; |
| 168 bool small_steps_; |
| 169 int data_offset_; // Zero for V3. |
89 char* zero_buffer_; // Buffer to speed-up cleaning deleted entries. | 170 char* zero_buffer_; // Buffer to speed-up cleaning deleted entries. |
90 base::FilePath path_; // Path to the backing folder. | 171 base::FilePath path_; // Path to the backing folder. |
91 std::vector<MappedFile*> block_files_; // The actual files. | 172 std::vector<MappedFile*> block_headers_; // The block file headers. |
| 173 std::vector<MappedFile*> block_data_; // The user data (if not stored with |
| 174 // the header). |
92 scoped_ptr<base::ThreadChecker> thread_checker_; | 175 scoped_ptr<base::ThreadChecker> thread_checker_; |
93 | 176 |
94 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_ZeroSizeFile); | 177 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_ZeroSizeFile); |
95 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_TruncatedFile); | 178 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_TruncatedFile); |
96 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_InvalidFile); | 179 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_InvalidFile); |
97 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_Stats); | 180 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_Stats); |
98 | 181 |
99 DISALLOW_COPY_AND_ASSIGN(BlockFiles); | 182 DISALLOW_COPY_AND_ASSIGN(BlockFiles); |
100 }; | 183 }; |
101 | 184 |
102 } // namespace disk_cache | 185 } // namespace disk_cache |
103 | 186 |
104 #endif // NET_DISK_CACHE_BLOCK_FILES_H_ | 187 #endif // NET_DISK_CACHE_BLOCK_FILES_H_ |
OLD | NEW |