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 #ifndef NET_DISK_CACHE_ENTRY_IMPL_H_ | 5 #ifndef NET_DISK_CACHE_ENTRY_IMPL_H_ |
6 #define NET_DISK_CACHE_ENTRY_IMPL_H_ | 6 #define NET_DISK_CACHE_ENTRY_IMPL_H_ |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "net/base/net_log.h" | 9 #include "net/base/net_log.h" |
10 #include "net/disk_cache/disk_cache.h" | 10 #include "net/disk_cache/disk_cache.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 int WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, | 44 int WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, |
45 const CompletionCallback& callback, bool truncate); | 45 const CompletionCallback& callback, bool truncate); |
46 int ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, | 46 int ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, |
47 const CompletionCallback& callback); | 47 const CompletionCallback& callback); |
48 int WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, | 48 int WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, |
49 const CompletionCallback& callback); | 49 const CompletionCallback& callback); |
50 int GetAvailableRangeImpl(int64 offset, int len, int64* start); | 50 int GetAvailableRangeImpl(int64 offset, int len, int64* start); |
51 void CancelSparseIOImpl(); | 51 void CancelSparseIOImpl(); |
52 int ReadyForSparseIOImpl(const CompletionCallback& callback); | 52 int ReadyForSparseIOImpl(const CompletionCallback& callback); |
53 | 53 |
54 inline CacheEntryBlock* entry() { | |
55 return &entry_; | |
56 } | |
57 | |
58 inline CacheRankingsBlock* rankings() { | |
59 return &node_; | |
60 } | |
61 | |
62 uint32 GetHash(); | |
63 | |
64 // Performs the initialization of a EntryImpl that will be added to the | 54 // Performs the initialization of a EntryImpl that will be added to the |
65 // cache. | 55 // cache. |
66 bool CreateEntry(Addr node_address, const std::string& key, uint32 hash); | 56 bool CreateEntry(Addr node_address, const std::string& key, uint32 hash); |
67 | 57 |
| 58 uint32 GetHash(); |
| 59 |
68 // Returns true if this entry matches the lookup arguments. | 60 // Returns true if this entry matches the lookup arguments. |
69 bool IsSameEntry(const std::string& key, uint32 hash); | 61 bool IsSameEntry(const std::string& key, uint32 hash); |
70 | 62 |
71 // Permamently destroys this entry. | 63 // Permamently destroys this entry. |
72 void InternalDoom(); | 64 void InternalDoom(); |
73 | 65 |
74 // Deletes this entry from disk. If |everything| is false, only the user data | |
75 // will be removed, leaving the key and control data intact. | |
76 void DeleteEntryData(bool everything); | |
77 | |
78 // Returns the address of the next entry on the list of entries with the same | |
79 // hash. | |
80 CacheAddr GetNextAddress(); | |
81 | |
82 // Sets the address of the next entry on the list of entries with the same | |
83 // hash. | |
84 void SetNextAddress(Addr address); | |
85 | |
86 // Reloads the rankings node information. | |
87 bool LoadNodeAddress(); | |
88 | |
89 // Updates the stored data to reflect the run-time information for this entry. | |
90 // Returns false if the data could not be updated. The purpose of this method | |
91 // is to be able to detect entries that are currently in use. | |
92 bool Update(); | |
93 | |
94 bool dirty() { | 66 bool dirty() { |
95 return dirty_; | 67 return dirty_; |
96 } | 68 } |
97 | 69 |
98 bool doomed() { | 70 bool doomed() { |
99 return doomed_; | 71 return doomed_; |
100 } | 72 } |
101 | 73 |
102 // Marks this entry as dirty (in memory) if needed. This is intended only for | |
103 // entries that are being read from disk, to be called during loading. | |
104 void SetDirtyFlag(int32 current_id); | |
105 | |
106 // Fixes this entry so it can be treated as valid (to delete it). | |
107 void SetPointerForInvalidEntry(int32 new_id); | |
108 | |
109 // Returns true if this entry is so meesed up that not everything is going to | |
110 // be removed. | |
111 bool LeaveRankingsBehind(); | |
112 | |
113 // Returns false if the entry is clearly invalid. | 74 // Returns false if the entry is clearly invalid. |
114 bool SanityCheck(); | 75 bool SanityCheck(); |
115 bool DataSanityCheck(); | 76 bool DataSanityCheck(); |
116 | 77 |
117 // Attempts to make this entry reachable though the key. | 78 // Attempts to make this entry reachable though the key. |
118 void FixForDelete(); | 79 void FixForDelete(); |
119 | 80 |
120 // Handle the pending asynchronous IO count. | |
121 void IncrementIoCount(); | |
122 void DecrementIoCount(); | |
123 | |
124 // This entry is being returned to the user. It is always called from the | |
125 // primary thread (not the dedicated cache thread). | |
126 void OnEntryCreated(BackendImpl* backend); | |
127 | |
128 // Set the access times for this entry. This method provides support for | 81 // Set the access times for this entry. This method provides support for |
129 // the upgrade tool. | 82 // the upgrade tool. |
130 void SetTimes(base::Time last_used, base::Time last_modified); | 83 void SetTimes(base::Time last_used, base::Time last_modified); |
131 | 84 |
132 // Generates a histogram for the time spent working on this operation. | |
133 void ReportIOTime(Operation op, const base::TimeTicks& start); | |
134 | |
135 // Logs a begin event and enables logging for the EntryImpl. Will also cause | 85 // Logs a begin event and enables logging for the EntryImpl. Will also cause |
136 // an end event to be logged on destruction. The EntryImpl must have its key | 86 // an end event to be logged on destruction. The EntryImpl must have its key |
137 // initialized before this is called. |created| is true if the Entry was | 87 // initialized before this is called. |created| is true if the Entry was |
138 // created rather than opened. | 88 // created rather than opened. |
139 void BeginLogging(net::NetLog* net_log, bool created); | 89 void BeginLogging(net::NetLog* net_log, bool created); |
140 | 90 |
141 const net::BoundNetLog& net_log() const; | 91 const net::BoundNetLog& net_log() const; |
142 | 92 |
143 // Returns the number of blocks needed to store an EntryStore. | |
144 static int NumBlocksForEntry(int key_size); | |
145 | |
146 // Entry interface. | 93 // Entry interface. |
147 virtual void Doom() OVERRIDE; | 94 virtual void Doom() OVERRIDE; |
148 virtual void Close() OVERRIDE; | 95 virtual void Close() OVERRIDE; |
149 virtual std::string GetKey() const OVERRIDE; | 96 virtual std::string GetKey() const OVERRIDE; |
150 virtual base::Time GetLastUsed() const OVERRIDE; | 97 virtual base::Time GetLastUsed() const OVERRIDE; |
151 virtual base::Time GetLastModified() const OVERRIDE; | 98 virtual base::Time GetLastModified() const OVERRIDE; |
152 virtual int32 GetDataSize(int index) const OVERRIDE; | 99 virtual int32 GetDataSize(int index) const OVERRIDE; |
153 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, | 100 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, |
154 const CompletionCallback& callback) OVERRIDE; | 101 const CompletionCallback& callback) OVERRIDE; |
155 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, | 102 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 135 |
189 // Deletes the data pointed by address, maybe backed by files_[index]. | 136 // Deletes the data pointed by address, maybe backed by files_[index]. |
190 // Note that most likely the caller should delete (and store) the reference to | 137 // Note that most likely the caller should delete (and store) the reference to |
191 // |address| *before* calling this method because we don't want to have an | 138 // |address| *before* calling this method because we don't want to have an |
192 // entry using an address that is already free. | 139 // entry using an address that is already free. |
193 void DeleteData(Addr address, int index); | 140 void DeleteData(Addr address, int index); |
194 | 141 |
195 // Updates ranking information. | 142 // Updates ranking information. |
196 void UpdateRank(bool modified); | 143 void UpdateRank(bool modified); |
197 | 144 |
198 // Returns a pointer to the file that stores the given address. | 145 // Deletes this entry from disk. If |everything| is false, only the user data |
199 File* GetBackingFile(Addr address, int index); | 146 // will be removed, leaving the key and control data intact. |
200 | 147 void DeleteEntryData(bool everything); |
201 // Returns a pointer to the file that stores external data. | |
202 File* GetExternalFile(Addr address, int index); | |
203 | 148 |
204 // Prepares the target file or buffer for a write of buf_len bytes at the | 149 // Prepares the target file or buffer for a write of buf_len bytes at the |
205 // given offset. | 150 // given offset. |
206 bool PrepareTarget(int index, int offset, int buf_len, bool truncate); | 151 bool PrepareTarget(int index, int offset, int buf_len, bool truncate); |
207 | 152 |
208 // Adjusts the internal buffer and file handle for a write that truncates this | 153 // Adjusts the internal buffer and file handle for a write that truncates this |
209 // stream. | 154 // stream. |
210 bool HandleTruncation(int index, int offset, int buf_len); | 155 bool HandleTruncation(int index, int offset, int buf_len); |
211 | 156 |
212 // Copies data from disk to the internal buffer. | 157 // Copies data from disk to the internal buffer. |
(...skipping 28 matching lines...) Expand all Loading... |
241 // Gets the data stored at the given index. If the information is in memory, | 186 // Gets the data stored at the given index. If the information is in memory, |
242 // a buffer will be allocated and the data will be copied to it (the caller | 187 // a buffer will be allocated and the data will be copied to it (the caller |
243 // can find out the size of the buffer before making this call). Otherwise, | 188 // can find out the size of the buffer before making this call). Otherwise, |
244 // the cache address of the data will be returned, and that address will be | 189 // the cache address of the data will be returned, and that address will be |
245 // removed from the regular book keeping of this entry so the caller is | 190 // removed from the regular book keeping of this entry so the caller is |
246 // responsible for deleting the block (or file) from the backing store at some | 191 // responsible for deleting the block (or file) from the backing store at some |
247 // point; there is no need to report any storage-size change, only to do the | 192 // point; there is no need to report any storage-size change, only to do the |
248 // actual cleanup. | 193 // actual cleanup. |
249 void GetData(int index, char** buffer, Addr* address); | 194 void GetData(int index, char** buffer, Addr* address); |
250 | 195 |
| 196 // Generates a histogram for the time spent working on this operation. |
| 197 void ReportIOTime(Operation op, const base::TimeTicks& start); |
| 198 |
251 // Logs this entry to the internal trace buffer. | 199 // Logs this entry to the internal trace buffer. |
252 void Log(const char* msg); | 200 void Log(const char* msg); |
253 | 201 |
254 CacheEntryBlock entry_; // Key related information for this entry. | 202 CacheEntryBlock entry_; // Key related information for this entry. |
255 CacheRankingsBlock node_; // Rankings related information for this entry. | 203 CacheRankingsBlock node_; // Rankings related information for this entry. |
256 base::WeakPtr<BackendImpl> backend_; // Back pointer to the cache. | 204 base::WeakPtr<BackendImpl> backend_; // Back pointer to the cache. |
257 base::WeakPtr<InFlightBackendIO> background_queue_; // In-progress queue. | 205 base::WeakPtr<InFlightBackendIO> background_queue_; // In-progress queue. |
258 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. | 206 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. |
259 // Files to store external user data and key. | 207 // Files to store external user data and key. |
260 scoped_refptr<File> files_[kNumStreams + 1]; | 208 scoped_refptr<File> files_[kNumStreams + 1]; |
261 mutable std::string key_; // Copy of the key. | 209 mutable std::string key_; // Copy of the key. |
262 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. | 210 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. |
263 bool doomed_; // True if this entry was removed from the cache. | 211 bool doomed_; // True if this entry was removed from the cache. |
264 bool read_only_; // True if not yet writing. | 212 bool read_only_; // True if not yet writing. |
265 bool dirty_; // True if we detected that this is a dirty entry. | 213 bool dirty_; // True if we detected that this is a dirty entry. |
266 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. | 214 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. |
267 | 215 |
268 net::BoundNetLog net_log_; | 216 net::BoundNetLog net_log_; |
269 | 217 |
270 DISALLOW_COPY_AND_ASSIGN(EntryImpl); | 218 DISALLOW_COPY_AND_ASSIGN(EntryImpl); |
271 }; | 219 }; |
272 | 220 |
273 } // namespace disk_cache | 221 } // namespace disk_cache |
274 | 222 |
275 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ | 223 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ |
OLD | NEW |