OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_ | |
6 #define NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_ | |
7 | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "net/base/net_log.h" | |
10 #include "net/disk_cache/blockfile/disk_format.h" | |
11 #include "net/disk_cache/blockfile/storage_block-inl.h" | |
12 #include "net/disk_cache/blockfile/storage_block.h" | |
13 #include "net/disk_cache/disk_cache.h" | |
14 | |
15 namespace disk_cache { | |
16 | |
17 class BackendImpl; | |
18 class InFlightBackendIO; | |
19 class SparseControl; | |
20 typedef StorageBlock<EntryStore> CacheEntryBlock; | |
21 typedef StorageBlock<RankingsNode> CacheRankingsBlock; | |
22 | |
23 // This class implements the Entry interface. An object of this | |
24 // class represents a single entry on the cache. | |
25 class NET_EXPORT_PRIVATE EntryImpl | |
26 : public Entry, | |
27 public base::RefCounted<EntryImpl> { | |
28 friend class base::RefCounted<EntryImpl>; | |
29 friend class SparseControl; | |
30 public: | |
31 enum Operation { | |
32 kRead, | |
33 kWrite, | |
34 kSparseRead, | |
35 kSparseWrite, | |
36 kAsyncIO, | |
37 kReadAsync1, | |
38 kWriteAsync1 | |
39 }; | |
40 | |
41 EntryImpl(BackendImpl* backend, Addr address, bool read_only); | |
42 | |
43 // Background implementation of the Entry interface. | |
44 void DoomImpl(); | |
45 int ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len, | |
46 const CompletionCallback& callback); | |
47 int WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, | |
48 const CompletionCallback& callback, bool truncate); | |
49 int ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, | |
50 const CompletionCallback& callback); | |
51 int WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, | |
52 const CompletionCallback& callback); | |
53 int GetAvailableRangeImpl(int64 offset, int len, int64* start); | |
54 void CancelSparseIOImpl(); | |
55 int ReadyForSparseIOImpl(const CompletionCallback& callback); | |
56 | |
57 inline CacheEntryBlock* entry() { | |
58 return &entry_; | |
59 } | |
60 | |
61 inline CacheRankingsBlock* rankings() { | |
62 return &node_; | |
63 } | |
64 | |
65 uint32 GetHash(); | |
66 | |
67 // Performs the initialization of a EntryImpl that will be added to the | |
68 // cache. | |
69 bool CreateEntry(Addr node_address, const std::string& key, uint32 hash); | |
70 | |
71 // Returns true if this entry matches the lookup arguments. | |
72 bool IsSameEntry(const std::string& key, uint32 hash); | |
73 | |
74 // Permamently destroys this entry. | |
75 void InternalDoom(); | |
76 | |
77 // Deletes this entry from disk. If |everything| is false, only the user data | |
78 // will be removed, leaving the key and control data intact. | |
79 void DeleteEntryData(bool everything); | |
80 | |
81 // Returns the address of the next entry on the list of entries with the same | |
82 // hash. | |
83 CacheAddr GetNextAddress(); | |
84 | |
85 // Sets the address of the next entry on the list of entries with the same | |
86 // hash. | |
87 void SetNextAddress(Addr address); | |
88 | |
89 // Reloads the rankings node information. | |
90 bool LoadNodeAddress(); | |
91 | |
92 // Updates the stored data to reflect the run-time information for this entry. | |
93 // Returns false if the data could not be updated. The purpose of this method | |
94 // is to be able to detect entries that are currently in use. | |
95 bool Update(); | |
96 | |
97 bool dirty() { | |
98 return dirty_; | |
99 } | |
100 | |
101 bool doomed() { | |
102 return doomed_; | |
103 } | |
104 | |
105 // Marks this entry as dirty (in memory) if needed. This is intended only for | |
106 // entries that are being read from disk, to be called during loading. | |
107 void SetDirtyFlag(int32 current_id); | |
108 | |
109 // Fixes this entry so it can be treated as valid (to delete it). | |
110 void SetPointerForInvalidEntry(int32 new_id); | |
111 | |
112 // Returns true if this entry is so meesed up that not everything is going to | |
113 // be removed. | |
114 bool LeaveRankingsBehind(); | |
115 | |
116 // Returns false if the entry is clearly invalid. | |
117 bool SanityCheck(); | |
118 bool DataSanityCheck(); | |
119 | |
120 // Attempts to make this entry reachable though the key. | |
121 void FixForDelete(); | |
122 | |
123 // Handle the pending asynchronous IO count. | |
124 void IncrementIoCount(); | |
125 void DecrementIoCount(); | |
126 | |
127 // This entry is being returned to the user. It is always called from the | |
128 // primary thread (not the dedicated cache thread). | |
129 void OnEntryCreated(BackendImpl* backend); | |
130 | |
131 // Set the access times for this entry. This method provides support for | |
132 // the upgrade tool. | |
133 void SetTimes(base::Time last_used, base::Time last_modified); | |
134 | |
135 // Generates a histogram for the time spent working on this operation. | |
136 void ReportIOTime(Operation op, const base::TimeTicks& start); | |
137 | |
138 // Logs a begin event and enables logging for the EntryImpl. Will also cause | |
139 // an end event to be logged on destruction. The EntryImpl must have its key | |
140 // initialized before this is called. |created| is true if the Entry was | |
141 // created rather than opened. | |
142 void BeginLogging(net::NetLog* net_log, bool created); | |
143 | |
144 const net::BoundNetLog& net_log() const; | |
145 | |
146 // Returns the number of blocks needed to store an EntryStore. | |
147 static int NumBlocksForEntry(int key_size); | |
148 | |
149 // Entry interface. | |
150 void Doom() override; | |
151 void Close() override; | |
152 std::string GetKey() const override; | |
153 base::Time GetLastUsed() const override; | |
154 base::Time GetLastModified() const override; | |
155 int32 GetDataSize(int index) const override; | |
156 int ReadData(int index, | |
157 int offset, | |
158 IOBuffer* buf, | |
159 int buf_len, | |
160 const CompletionCallback& callback) override; | |
161 int WriteData(int index, | |
162 int offset, | |
163 IOBuffer* buf, | |
164 int buf_len, | |
165 const CompletionCallback& callback, | |
166 bool truncate) override; | |
167 int ReadSparseData(int64 offset, | |
168 IOBuffer* buf, | |
169 int buf_len, | |
170 const CompletionCallback& callback) override; | |
171 int WriteSparseData(int64 offset, | |
172 IOBuffer* buf, | |
173 int buf_len, | |
174 const CompletionCallback& callback) override; | |
175 int GetAvailableRange(int64 offset, | |
176 int len, | |
177 int64* start, | |
178 const CompletionCallback& callback) override; | |
179 bool CouldBeSparse() const override; | |
180 void CancelSparseIO() override; | |
181 int ReadyForSparseIO(const CompletionCallback& callback) override; | |
182 | |
183 private: | |
184 enum { | |
185 kNumStreams = 3 | |
186 }; | |
187 class UserBuffer; | |
188 | |
189 ~EntryImpl() override; | |
190 | |
191 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as | |
192 // separate functions to make logging of results simpler. | |
193 int InternalReadData(int index, int offset, IOBuffer* buf, | |
194 int buf_len, const CompletionCallback& callback); | |
195 int InternalWriteData(int index, int offset, IOBuffer* buf, int buf_len, | |
196 const CompletionCallback& callback, bool truncate); | |
197 | |
198 // Initializes the storage for an internal or external data block. | |
199 bool CreateDataBlock(int index, int size); | |
200 | |
201 // Initializes the storage for an internal or external generic block. | |
202 bool CreateBlock(int size, Addr* address); | |
203 | |
204 // Deletes the data pointed by address, maybe backed by files_[index]. | |
205 // Note that most likely the caller should delete (and store) the reference to | |
206 // |address| *before* calling this method because we don't want to have an | |
207 // entry using an address that is already free. | |
208 void DeleteData(Addr address, int index); | |
209 | |
210 // Updates ranking information. | |
211 void UpdateRank(bool modified); | |
212 | |
213 // Returns a pointer to the file that stores the given address. | |
214 File* GetBackingFile(Addr address, int index); | |
215 | |
216 // Returns a pointer to the file that stores external data. | |
217 File* GetExternalFile(Addr address, int index); | |
218 | |
219 // Prepares the target file or buffer for a write of buf_len bytes at the | |
220 // given offset. | |
221 bool PrepareTarget(int index, int offset, int buf_len, bool truncate); | |
222 | |
223 // Adjusts the internal buffer and file handle for a write that truncates this | |
224 // stream. | |
225 bool HandleTruncation(int index, int offset, int buf_len); | |
226 | |
227 // Copies data from disk to the internal buffer. | |
228 bool CopyToLocalBuffer(int index); | |
229 | |
230 // Reads from a block data file to this object's memory buffer. | |
231 bool MoveToLocalBuffer(int index); | |
232 | |
233 // Loads the external file to this object's memory buffer. | |
234 bool ImportSeparateFile(int index, int new_size); | |
235 | |
236 // Makes sure that the internal buffer can handle the a write of |buf_len| | |
237 // bytes to |offset|. | |
238 bool PrepareBuffer(int index, int offset, int buf_len); | |
239 | |
240 // Flushes the in-memory data to the backing storage. The data destination | |
241 // is determined based on the current data length and |min_len|. | |
242 bool Flush(int index, int min_len); | |
243 | |
244 // Updates the size of a given data stream. | |
245 void UpdateSize(int index, int old_size, int new_size); | |
246 | |
247 // Initializes the sparse control object. Returns a net error code. | |
248 int InitSparseData(); | |
249 | |
250 // Adds the provided |flags| to the current EntryFlags for this entry. | |
251 void SetEntryFlags(uint32 flags); | |
252 | |
253 // Returns the current EntryFlags for this entry. | |
254 uint32 GetEntryFlags(); | |
255 | |
256 // Gets the data stored at the given index. If the information is in memory, | |
257 // a buffer will be allocated and the data will be copied to it (the caller | |
258 // can find out the size of the buffer before making this call). Otherwise, | |
259 // the cache address of the data will be returned, and that address will be | |
260 // removed from the regular book keeping of this entry so the caller is | |
261 // responsible for deleting the block (or file) from the backing store at some | |
262 // point; there is no need to report any storage-size change, only to do the | |
263 // actual cleanup. | |
264 void GetData(int index, char** buffer, Addr* address); | |
265 | |
266 // Logs this entry to the internal trace buffer. | |
267 void Log(const char* msg); | |
268 | |
269 CacheEntryBlock entry_; // Key related information for this entry. | |
270 CacheRankingsBlock node_; // Rankings related information for this entry. | |
271 base::WeakPtr<BackendImpl> backend_; // Back pointer to the cache. | |
272 base::WeakPtr<InFlightBackendIO> background_queue_; // In-progress queue. | |
273 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. | |
274 // Files to store external user data and key. | |
275 scoped_refptr<File> files_[kNumStreams + 1]; | |
276 mutable std::string key_; // Copy of the key. | |
277 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. | |
278 bool doomed_; // True if this entry was removed from the cache. | |
279 bool read_only_; // True if not yet writing. | |
280 bool dirty_; // True if we detected that this is a dirty entry. | |
281 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. | |
282 | |
283 net::BoundNetLog net_log_; | |
284 | |
285 DISALLOW_COPY_AND_ASSIGN(EntryImpl); | |
286 }; | |
287 | |
288 } // namespace disk_cache | |
289 | |
290 #endif // NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_ | |
OLD | NEW |