|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_DISK_CACHE_V3_INDEX_TABLE_H_ | |
6 #define NET_DISK_CACHE_V3_INDEX_TABLE_H_ | |
7 | |
8 // The IndexTable class is in charge of handling all the details about the main | |
9 // index table of the cache. It provides methods to locate entries in the cache, | |
10 // create new entries and modify existing entries. It hides the fact that the | |
11 // table is backed up across multiple physical files, and that the files can | |
12 // grow and be remapped while the cache is in use. However, note that this class | |
13 // doesn't do any direct management of the backing files, and it operates only | |
14 // with the tables in memory. | |
15 // | |
16 // When the current index needs to grow, the backend is notified so that files | |
17 // are extended and remapped as needed. After that, the IndexTable should be | |
18 // re-initialized with the new structures. Note that the IndexTable instance is | |
19 // still functional while the backend performs file IO. | |
20 | |
21 #include <vector> | |
22 | |
23 #include "base/basictypes.h" | |
24 #include "base/memory/ref_counted.h" | |
25 #include "base/memory/scoped_ptr.h" | |
26 #include "base/time/time.h" | |
27 #include "net/base/net_export.h" | |
28 #include "net/disk_cache/addr.h" | |
29 #include "net/disk_cache/bitmap.h" | |
30 #include "net/disk_cache/v3/disk_format_v3.h" | |
31 | |
32 namespace net { | |
33 class IOBuffer; | |
34 } | |
35 | |
36 namespace disk_cache { | |
37 | |
38 class BackendImplV3; | |
39 struct InitResult; | |
40 | |
41 // An EntryCell represents a single entity stored by the index table. Users are | |
42 // expected to handle and store EntryCells on their own to track operations that | |
43 // they are performing with a given entity, as opposed to deal with pointers to | |
44 // individual positions on the table, given that the whole table can be moved to | |
45 // another place, and that would invalidate any pointers to individual cells in | |
46 // the table. | |
47 // However, note that it is also possible for an entity to be moved from one | |
48 // position to another, so an EntryCell may be invalid by the time a long | |
49 // operation completes. In that case, the caller should consult the table again | |
50 // using FindEntryCell(). | |
51 class NET_EXPORT_PRIVATE EntryCell { | |
52 public: | |
53 ~EntryCell(); | |
54 | |
55 bool IsValid() const; | |
56 | |
57 int32 cell_id() const { return cell_id_; } | |
58 uint32 hash() const { return hash_; } | |
59 | |
60 Addr GetAddress() const; | |
61 EntryState GetState() const; | |
62 EntryGroup GetGroup() const; | |
63 int GetReuse() const; | |
64 int GetTimestamp() const; | |
65 | |
66 void SetState(EntryState state); | |
67 void SetGroup(EntryGroup group); | |
68 void SetReuse(int count); | |
69 void SetTimestamp(int timestamp); | |
70 | |
71 static EntryCell GetEntryCellForTest(int32 cell_id, | |
72 uint32 hash, | |
73 Addr address, | |
74 IndexCell* cell, | |
75 bool small_table); | |
76 void SerializaForTest(IndexCell* destination); | |
77 | |
78 private: | |
79 friend class IndexTable; | |
80 friend class CacheDumperHelper; | |
81 | |
82 EntryCell(); | |
83 EntryCell(int32 cell_id, uint32 hash, Addr address, bool small_table); | |
84 EntryCell(int32 cell_id, | |
85 uint32 hash, | |
86 const IndexCell& cell, | |
87 bool small_table); | |
88 | |
89 void Clear() { cell_.Clear(); } | |
90 void FixSum(); | |
91 | |
92 // Returns the raw value stored on the index table. | |
93 uint32 GetAddressValue() const; | |
94 | |
95 // Recalculates hash_ assuming that only the low order bits are valid and the | |
96 // rest come from cell_. | |
97 uint32 RecomputeHash(); | |
98 | |
99 void Serialize(IndexCell* destination) const; | |
100 | |
101 int32 cell_id_; | |
102 uint32 hash_; | |
103 IndexCell cell_; | |
104 bool small_table_; | |
105 }; | |
106 | |
107 // Keeps a collection of EntryCells in order to be processed. | |
108 struct NET_EXPORT_PRIVATE EntrySet { | |
109 EntrySet(); | |
110 ~EntrySet(); | |
111 | |
112 int evicted_count; // The numebr of evicted entries in this set. | |
113 size_t current; // The number of the cell that is being processed. | |
114 std::vector<EntryCell> cells; | |
115 }; | |
116 | |
117 // A given entity referenced by the index table is uniquely identified by the | |
118 // combination of hash and address. | |
119 struct CellInfo { uint32 hash; Addr address; }; | |
120 typedef std::vector<CellInfo> CellList; | |
121 | |
122 // An index iterator is used to get a group of cells all of which have the same | |
123 // timestamp, and when passed back in again to the appropriate function, to get | |
124 // the oldest / youngest group of cells from the current timestamp. | |
Randy Smith (Not in Mondays)
2013/11/07 20:25:15
This doesn't seem quite right to me / doesn't matc
rvargas (doing something else)
2013/11/08 04:22:30
I'll clarify the comment. It doesn't mean that the
| |
125 struct NET_EXPORT_PRIVATE IndexIterator { | |
126 IndexIterator(); | |
127 ~IndexIterator(); | |
128 | |
129 CellList cells; | |
130 int timestamp; // The current low resolution timestamp for |cells|. | |
Randy Smith (Not in Mondays)
2013/11/07 20:25:15
Worth having a reference to IndexTable::CalculateT
| |
131 bool forward; // The direction of the iteration, in time. | |
132 }; | |
133 | |
134 // Methods that the backend has to implement to support the table. Note that the | |
135 // backend is expected to own all IndexTable instances, so it is expected to | |
136 // outlive the table. | |
137 class NET_EXPORT_PRIVATE IndexTableBackend { | |
138 public: | |
139 virtual ~IndexTableBackend() {} | |
140 | |
141 // The index has to grow. | |
142 virtual void GrowIndex() = 0; | |
143 | |
144 // Save the index to the backup file. | |
145 virtual void SaveIndex(net::IOBuffer* buffer, int buffer_len) = 0; | |
146 | |
147 // Deletes or fixes an invalid cell from the backend. | |
148 virtual void DeleteCell(EntryCell cell) = 0; | |
149 virtual void FixCell(EntryCell cell) = 0; | |
150 }; | |
151 | |
152 // The data required to initialize an index. | |
153 struct NET_EXPORT_PRIVATE IndexTableInitData { | |
154 IndexTableInitData(); | |
155 ~IndexTableInitData(); | |
156 | |
157 IndexBitmap* index_bitmap; | |
158 IndexBucket* main_table; | |
159 IndexBucket* extra_table; | |
160 scoped_ptr<IndexHeaderV3> backup_header; | |
161 scoped_ptr<uint32[]> backup_bitmap; | |
162 }; | |
163 | |
164 // See the description at the top of this file. | |
165 class NET_EXPORT_PRIVATE IndexTable { | |
166 public: | |
167 explicit IndexTable(IndexTableBackend* backend); | |
168 | |
169 // Initializes the object, or re-initializes it when the backing files grow. | |
170 void Init(IndexTableInitData* params); | |
171 | |
172 // Releases the resources acquired during Init(). This method should not be | |
173 // called just to reinitialize the object. | |
174 void Reset(); | |
175 | |
176 // Locates a resouce on the index. Returns a list of all resources that match | |
177 // the provided hash. | |
178 EntrySet LookupEntry(uint32 hash); | |
Randy Smith (Not in Mondays)
2013/11/07 20:25:15
nit, suggestion: This is a bit dependent on the de
rvargas (doing something else)
2013/11/08 04:22:30
sure
| |
179 | |
180 // Creates a new cell to store a new resource. | |
181 EntryCell CreateEntryCell(uint32 hash, Addr address); | |
182 | |
183 // Locates a particular cell. | |
184 EntryCell FindEntryCell(uint32 hash, Addr address); | |
185 | |
186 // Returns an IndexTable timestamp for a given absolute time. The actual | |
187 // resolution of the timestamp should be considered an implementation detail, | |
188 // but it certainly is lower than seconds. The important part is that a group | |
189 // of cells will share the same timestamp. | |
Randy Smith (Not in Mondays)
2013/11/07 20:25:15
The comment looks good to me. Is it worthwhile ha
| |
190 int CalculateTimestamp(base::Time time); | |
191 | |
192 // Updates a particular cell. | |
193 void SetSate(uint32 hash, Addr address, EntryState state); | |
194 void UpdateTime(uint32 hash, Addr address, base::Time current); | |
195 | |
196 // Saves the contents of |cell| to the table. | |
197 void Save(EntryCell* cell); | |
198 | |
199 // Returns the oldest entries for each group of entries. | |
200 void GetOldest(CellList* no_use, CellList* low_use, CellList* high_use); | |
201 | |
202 // Returns the next group of entries for the provided iterator. | |
203 bool GetNextCells(IndexIterator* iterator); | |
204 | |
205 // Called each time the index should save the backup information. The caller | |
206 // can assume that anything that needs to be saved is saved when this method | |
207 // is called, and that there is only one source of timming information, and | |
208 // that source is controlled by the owner of this object. | |
209 void OnBackupTimer(); | |
210 | |
211 IndexHeaderV3* header() { return header_; } | |
212 const IndexHeaderV3* header() const { return header_; } | |
213 | |
214 private: | |
215 EntryCell FindEntryCellImpl(uint32 hash, Addr address, bool allow_deleted); | |
216 void CheckState(const EntryCell& cell); | |
217 void Write(const EntryCell& cell); | |
218 int NewExtraBucket(); | |
219 void GetOldestFromBucket(IndexBucket* bucket, int bucket_hash, | |
220 CellList* no_use, int* no_use_time, | |
221 CellList* low_use, int* low_use_time, | |
222 CellList* high_use, int* high_use_time); | |
223 void GetNewestFromBucket(IndexBucket* bucket, int bucket_hash, | |
224 int limit_time, IndexIterator* iterator); | |
Randy Smith (Not in Mondays)
2013/11/07 20:25:15
I'm bothered by the parallel names here for two fu
Randy Smith (Not in Mondays)
2013/11/11 20:54:54
I still feel this way; what are your thoughts?
rvargas (doing something else)
2013/11/12 00:24:04
Sorry, I missed this after the last refactor.
| |
225 void MoveCells(IndexBucket* old_extra_table); | |
226 void MoveSingleCell(IndexCell* current_cell, int cell_id, | |
227 int main_table_index, bool growing); | |
228 void HandleMisplacedCell(IndexCell* current_cell, int cell_id, | |
229 int main_table_index); | |
230 void CheckBucketList(int bucket_id); | |
231 | |
232 uint32 GetAddressValue(const IndexCell& cell); | |
233 uint32 GetHashValue(const IndexCell& cell); | |
234 uint32 GetFullHash(const IndexCell& cell, uint32 lower_part); | |
235 bool IsHashMatch(const IndexCell& cell, uint32 hash); | |
236 bool MisplacedHash(const IndexCell& cell, uint32 hash); | |
237 | |
238 IndexTableBackend* backend_; | |
239 IndexHeaderV3* header_; | |
240 scoped_ptr<Bitmap> bitmap_; | |
241 scoped_ptr<Bitmap> backup_bitmap_; | |
242 scoped_ptr<uint32[]> backup_bitmap_storage_; | |
243 scoped_ptr<IndexHeaderV3> backup_header_; | |
244 IndexBucket* main_table_; | |
245 IndexBucket* extra_table_; | |
246 uint32 mask_; // Binary mask to map a hash to the hash table. | |
247 int extra_bits_; // How many bits are in mask_ above the default value. | |
248 bool modified_; | |
249 bool small_table_; | |
250 | |
251 DISALLOW_COPY_AND_ASSIGN(IndexTable); | |
252 }; | |
253 | |
254 } // namespace disk_cache | |
255 | |
256 #endif // NET_DISK_CACHE_V3_INDEX_TABLE_H_ | |
OLD | NEW |