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_ENTRY_IMPL_H_ | |
6 #define NET_DISK_CACHE_ENTRY_IMPL_H_ | |
7 | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "net/base/net_log.h" | |
10 #include "net/disk_cache/disk_cache.h" | |
11 #include "net/disk_cache/disk_format.h" | |
12 #include "net/disk_cache/storage_block.h" | |
13 #include "net/disk_cache/storage_block-inl.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 virtual void Doom() OVERRIDE; | |
151 virtual void Close() OVERRIDE; | |
152 virtual std::string GetKey() const OVERRIDE; | |
153 virtual base::Time GetLastUsed() const OVERRIDE; | |
154 virtual base::Time GetLastModified() const OVERRIDE; | |
155 virtual int32 GetDataSize(int index) const OVERRIDE; | |
156 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, | |
157 const CompletionCallback& callback) OVERRIDE; | |
158 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, | |
159 const CompletionCallback& callback, | |
160 bool truncate) OVERRIDE; | |
161 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
162 const CompletionCallback& callback) OVERRIDE; | |
163 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
164 const CompletionCallback& callback) OVERRIDE; | |
165 virtual int GetAvailableRange(int64 offset, int len, int64* start, | |
166 const CompletionCallback& callback) OVERRIDE; | |
167 virtual bool CouldBeSparse() const OVERRIDE; | |
168 virtual void CancelSparseIO() OVERRIDE; | |
169 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; | |
170 | |
171 private: | |
172 enum { | |
173 kNumStreams = 3 | |
174 }; | |
175 class UserBuffer; | |
176 | |
177 virtual ~EntryImpl(); | |
178 | |
179 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as | |
180 // separate functions to make logging of results simpler. | |
181 int InternalReadData(int index, int offset, IOBuffer* buf, | |
182 int buf_len, const CompletionCallback& callback); | |
183 int InternalWriteData(int index, int offset, IOBuffer* buf, int buf_len, | |
184 const CompletionCallback& callback, bool truncate); | |
185 | |
186 // Initializes the storage for an internal or external data block. | |
187 bool CreateDataBlock(int index, int size); | |
188 | |
189 // Initializes the storage for an internal or external generic block. | |
190 bool CreateBlock(int size, Addr* address); | |
191 | |
192 // Deletes the data pointed by address, maybe backed by files_[index]. | |
193 // Note that most likely the caller should delete (and store) the reference to | |
194 // |address| *before* calling this method because we don't want to have an | |
195 // entry using an address that is already free. | |
196 void DeleteData(Addr address, int index); | |
197 | |
198 // Updates ranking information. | |
199 void UpdateRank(bool modified); | |
200 | |
201 // Returns a pointer to the file that stores the given address. | |
202 File* GetBackingFile(Addr address, int index); | |
203 | |
204 // Returns a pointer to the file that stores external data. | |
205 File* GetExternalFile(Addr address, int index); | |
206 | |
207 // Prepares the target file or buffer for a write of buf_len bytes at the | |
208 // given offset. | |
209 bool PrepareTarget(int index, int offset, int buf_len, bool truncate); | |
210 | |
211 // Adjusts the internal buffer and file handle for a write that truncates this | |
212 // stream. | |
213 bool HandleTruncation(int index, int offset, int buf_len); | |
214 | |
215 // Copies data from disk to the internal buffer. | |
216 bool CopyToLocalBuffer(int index); | |
217 | |
218 // Reads from a block data file to this object's memory buffer. | |
219 bool MoveToLocalBuffer(int index); | |
220 | |
221 // Loads the external file to this object's memory buffer. | |
222 bool ImportSeparateFile(int index, int new_size); | |
223 | |
224 // Makes sure that the internal buffer can handle the a write of |buf_len| | |
225 // bytes to |offset|. | |
226 bool PrepareBuffer(int index, int offset, int buf_len); | |
227 | |
228 // Flushes the in-memory data to the backing storage. The data destination | |
229 // is determined based on the current data length and |min_len|. | |
230 bool Flush(int index, int min_len); | |
231 | |
232 // Updates the size of a given data stream. | |
233 void UpdateSize(int index, int old_size, int new_size); | |
234 | |
235 // Initializes the sparse control object. Returns a net error code. | |
236 int InitSparseData(); | |
237 | |
238 // Adds the provided |flags| to the current EntryFlags for this entry. | |
239 void SetEntryFlags(uint32 flags); | |
240 | |
241 // Returns the current EntryFlags for this entry. | |
242 uint32 GetEntryFlags(); | |
243 | |
244 // Gets the data stored at the given index. If the information is in memory, | |
245 // a buffer will be allocated and the data will be copied to it (the caller | |
246 // can find out the size of the buffer before making this call). Otherwise, | |
247 // the cache address of the data will be returned, and that address will be | |
248 // removed from the regular book keeping of this entry so the caller is | |
249 // responsible for deleting the block (or file) from the backing store at some | |
250 // point; there is no need to report any storage-size change, only to do the | |
251 // actual cleanup. | |
252 void GetData(int index, char** buffer, Addr* address); | |
253 | |
254 // Logs this entry to the internal trace buffer. | |
255 void Log(const char* msg); | |
256 | |
257 CacheEntryBlock entry_; // Key related information for this entry. | |
258 CacheRankingsBlock node_; // Rankings related information for this entry. | |
259 base::WeakPtr<BackendImpl> backend_; // Back pointer to the cache. | |
260 base::WeakPtr<InFlightBackendIO> background_queue_; // In-progress queue. | |
261 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. | |
262 // Files to store external user data and key. | |
263 scoped_refptr<File> files_[kNumStreams + 1]; | |
264 mutable std::string key_; // Copy of the key. | |
265 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. | |
266 bool doomed_; // True if this entry was removed from the cache. | |
267 bool read_only_; // True if not yet writing. | |
268 bool dirty_; // True if we detected that this is a dirty entry. | |
269 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. | |
270 | |
271 net::BoundNetLog net_log_; | |
272 | |
273 DISALLOW_COPY_AND_ASSIGN(EntryImpl); | |
274 }; | |
275 | |
276 } // namespace disk_cache | |
277 | |
278 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ | |
OLD | NEW |