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_V3_ENTRY_IMPL_V3_H_ |
6 #define NET_DISK_CACHE_ENTRY_IMPL_H_ | 6 #define NET_DISK_CACHE_V3_ENTRY_IMPL_V3_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <string> |
7 | 10 |
8 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
9 #include "net/base/net_log.h" | 12 #include "net/base/net_log.h" |
10 #include "net/disk_cache/disk_cache.h" | 13 #include "net/disk_cache/disk_cache.h" |
11 #include "net/disk_cache/storage_block.h" | 14 #include "net/disk_cache/storage_block.h" |
12 #include "net/disk_cache/storage_block-inl.h" | 15 #include "net/disk_cache/v3/disk_format_v3.h" |
13 | 16 |
14 namespace disk_cache { | 17 namespace disk_cache { |
15 | 18 |
16 class BackendImpl; | 19 class BackendImplV3; |
17 class InFlightBackendIO; | 20 class SparseControlV3; |
18 class SparseControl; | 21 |
| 22 //typedef StorageBlock<EntryRecord> CacheEntryBlockV3;//? |
| 23 //typedef StorageBlock<ShortEntryRecord> CacheShortEntryBlock; |
19 | 24 |
20 // This class implements the Entry interface. An object of this | 25 // This class implements the Entry interface. An object of this |
21 // class represents a single entry on the cache. | 26 // class represents a single entry on the cache. |
22 class NET_EXPORT_PRIVATE EntryImpl | 27 class NET_EXPORT_PRIVATE EntryImplV3 |
23 : public Entry, | 28 : public Entry, |
24 public base::RefCounted<EntryImpl> { | 29 public base::RefCounted<EntryImplV3> { |
25 friend class base::RefCounted<EntryImpl>; | 30 friend class base::RefCounted<EntryImplV3>; |
26 friend class SparseControl; | 31 friend class SparseControlV3; |
27 public: | 32 public: |
28 enum Operation { | 33 enum Operation { |
29 kRead, | 34 kRead, |
30 kWrite, | 35 kWrite, |
31 kSparseRead, | 36 kSparseRead, |
32 kSparseWrite, | 37 kSparseWrite, |
33 kAsyncIO, | 38 kAsyncIO, |
34 kReadAsync1, | 39 kReadAsync1, |
35 kWriteAsync1 | 40 kWriteAsync1 |
36 }; | 41 }; |
37 | 42 |
38 EntryImpl(BackendImpl* backend, Addr address, bool read_only); | 43 EntryImplV3(BackendImplV3* backend, Addr address, bool read_only); |
| 44 EntryImplV3(BackendImplV3* backend, Addr address, const std::string& key, |
| 45 scoped_ptr<EntryRecord> record); |
| 46 EntryImplV3(BackendImplV3* backend, Addr address, const std::string& key, |
| 47 scoped_ptr<ShortEntryRecord> record); |
39 | 48 |
40 // Background implementation of the Entry interface. | 49 // Performs the initialization of a EntryImplV3 that will be added to the |
41 void DoomImpl(); | 50 // cache. |
42 int ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len, | 51 void CreateEntry(const std::string& key, uint32 hash, |
43 const CompletionCallback& callback); | 52 ShortEntryRecord* old_info); |
44 int WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, | 53 void OnOpenEntry(); |
45 const CompletionCallback& callback, bool truncate); | |
46 int ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, | |
47 const CompletionCallback& callback); | |
48 int WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, | |
49 const CompletionCallback& callback); | |
50 int GetAvailableRangeImpl(int64 offset, int len, int64* start); | |
51 void CancelSparseIOImpl(); | |
52 int ReadyForSparseIOImpl(const CompletionCallback& callback); | |
53 | 54 |
54 // Performs the initialization of a EntryImpl that will be added to the | 55 scoped_ptr<ShortEntryRecord> GetShortEntryRecord(); |
55 // cache. | |
56 bool CreateEntry(Addr node_address, const std::string& key, uint32 hash); | |
57 | 56 |
58 uint32 GetHash(); | 57 uint32 GetHash() const; |
59 | 58 Addr GetAddress() const; |
| 59 int GetReuseCounter() const; |
| 60 void SetReuseCounter(int count); |
| 61 int GetRefetchCounter() const; |
| 62 void SetRefetchCounter(int count); |
| 63 |
60 // Returns true if this entry matches the lookup arguments. | 64 // Returns true if this entry matches the lookup arguments. |
61 bool IsSameEntry(const std::string& key, uint32 hash); | 65 bool IsSameEntry(const std::string& key, uint32 hash); |
62 | 66 |
63 // Permamently destroys this entry. | 67 // Permamently destroys this entry. |
64 void InternalDoom(); | 68 void InternalDoom(); |
65 | 69 |
66 bool dirty() { | |
67 return dirty_; | |
68 } | |
69 | |
70 bool doomed() { | |
71 return doomed_; | |
72 } | |
73 | |
74 // Returns false if the entry is clearly invalid. | 70 // Returns false if the entry is clearly invalid. |
75 bool SanityCheck(); | 71 bool SanityCheck(); |
76 bool DataSanityCheck(); | 72 bool DataSanityCheck(); |
| 73 static bool BasicSanityCheck(const EntryRecord& record); |
| 74 static bool DeletedSanityCheck(const ShortEntryRecord& record); |
77 | 75 |
78 // Attempts to make this entry reachable though the key. | 76 // Attempts to make this entry reachable though the key. |
79 void FixForDelete(); | 77 void FixForDelete(); |
80 | 78 |
81 // Set the access times for this entry. This method provides support for | 79 // Set the access times for this entry. This method provides support for |
82 // the upgrade tool. | 80 // the upgrade tool. |
83 void SetTimes(base::Time last_used, base::Time last_modified); | 81 void SetTimes(base::Time last_used, base::Time last_modified); |
84 | 82 |
85 // Logs a begin event and enables logging for the EntryImpl. Will also cause | 83 // Logs a begin event and enables logging for the EntryImplV3. Will also caus
e |
86 // an end event to be logged on destruction. The EntryImpl must have its key | 84 // an end event to be logged on destruction. The EntryImplV3 must have its ke
y |
87 // initialized before this is called. |created| is true if the Entry was | 85 // initialized before this is called. |created| is true if the Entry was |
88 // created rather than opened. | 86 // created rather than opened. |
89 void BeginLogging(net::NetLog* net_log, bool created); | 87 void BeginLogging(net::NetLog* net_log, bool created); |
90 | 88 |
91 const net::BoundNetLog& net_log() const; | 89 const net::BoundNetLog& net_log() const; |
92 | 90 |
| 91 void NotifyDestructionForTest(const CompletionCallback& callback); |
| 92 |
93 // Entry interface. | 93 // Entry interface. |
94 virtual void Doom() OVERRIDE; | 94 virtual void Doom() OVERRIDE; |
95 virtual void Close() OVERRIDE; | 95 virtual void Close() OVERRIDE; |
96 virtual std::string GetKey() const OVERRIDE; | 96 virtual std::string GetKey() const OVERRIDE; |
97 virtual base::Time GetLastUsed() const OVERRIDE; | 97 virtual base::Time GetLastUsed() const OVERRIDE; |
98 virtual base::Time GetLastModified() const OVERRIDE; | 98 virtual base::Time GetLastModified() const OVERRIDE; |
99 virtual int32 GetDataSize(int index) const OVERRIDE; | 99 virtual int32 GetDataSize(int index) const OVERRIDE; |
100 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, |
101 const CompletionCallback& callback) OVERRIDE; | 101 const CompletionCallback& callback) OVERRIDE; |
102 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, |
103 const CompletionCallback& callback, | 103 const CompletionCallback& callback, |
104 bool truncate) OVERRIDE; | 104 bool truncate) OVERRIDE; |
105 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | 105 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, |
106 const CompletionCallback& callback) OVERRIDE; | 106 const CompletionCallback& callback) OVERRIDE; |
107 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | 107 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, |
108 const CompletionCallback& callback) OVERRIDE; | 108 const CompletionCallback& callback) OVERRIDE; |
109 virtual int GetAvailableRange(int64 offset, int len, int64* start, | 109 virtual int GetAvailableRange(int64 offset, int len, int64* start, |
110 const CompletionCallback& callback) OVERRIDE; | 110 const CompletionCallback& callback) OVERRIDE; |
111 virtual bool CouldBeSparse() const OVERRIDE; | 111 virtual bool CouldBeSparse() const OVERRIDE; |
112 virtual void CancelSparseIO() OVERRIDE; | 112 virtual void CancelSparseIO() OVERRIDE; |
113 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; | 113 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; |
114 | 114 |
115 private: | 115 private: |
116 enum { | 116 enum { |
117 kNumStreams = 3 | 117 kNumStreams = 3 |
118 }; | 118 }; |
119 class UserBuffer; | 119 class UserBuffer; |
120 | 120 |
121 virtual ~EntryImpl(); | 121 enum OperationAction { |
| 122 PENDING_READ, |
| 123 PENDING_WRITE, |
| 124 PENDING_FLUSH, |
| 125 PENDING_CLEANUP, |
| 126 PENDING_DONE |
| 127 }; |
| 128 struct PendingOperation { |
| 129 OperationAction action; |
| 130 int index; |
| 131 int offset; |
| 132 scoped_refptr<IOBuffer> buf; |
| 133 int buf_len; |
| 134 CompletionCallback callback; |
| 135 bool truncate; |
| 136 }; |
| 137 typedef std::queue<PendingOperation> PendingOperations; |
| 138 |
| 139 virtual ~EntryImplV3(); |
| 140 |
| 141 void Cleanup(); |
| 142 |
| 143 void WriteKey(); |
122 | 144 |
123 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as | 145 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as |
124 // separate functions to make logging of results simpler. | 146 // separate functions to make logging of results simpler. |
125 int InternalReadData(int index, int offset, IOBuffer* buf, | 147 int ReadDataImpl(int index, int offset, IOBuffer* buf, |
126 int buf_len, const CompletionCallback& callback); | 148 int buf_len, PendingOperation* operation, |
127 int InternalWriteData(int index, int offset, IOBuffer* buf, int buf_len, | 149 const CompletionCallback& callback); |
| 150 int WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, |
| 151 PendingOperation* operation, |
128 const CompletionCallback& callback, bool truncate); | 152 const CompletionCallback& callback, bool truncate); |
129 | 153 |
130 // Initializes the storage for an internal or external data block. | 154 // Initializes the storage for an internal or external data block. |
131 bool CreateDataBlock(int index, int size); | 155 bool CreateDataBlock(int index, int size); |
132 | 156 |
133 // Initializes the storage for an internal or external generic block. | 157 // Initializes the storage for an internal or external generic block. |
134 bool CreateBlock(int size, Addr* address); | 158 bool CreateBlock(int size, Addr* address); |
135 | 159 |
136 // Deletes the data pointed by address, maybe backed by files_[index]. | 160 // Deletes the data pointed by address. |
137 // Note that most likely the caller should delete (and store) the reference to | 161 // Note that most likely the caller should delete (and store) the reference to |
138 // |address| *before* calling this method because we don't want to have an | 162 // |address| *before* calling this method because we don't want to have an |
139 // entry using an address that is already free. | 163 // entry using an address that is already free. |
140 void DeleteData(Addr address, int index); | 164 void DeleteData(Addr address); |
141 | 165 |
142 // Updates ranking information. | 166 // Updates ranking information. |
143 void UpdateRank(bool modified); | 167 void UpdateRank(bool modified); |
144 | 168 |
145 // Deletes this entry from disk. If |everything| is false, only the user data | 169 // Deletes this entry from disk. |
146 // will be removed, leaving the key and control data intact. | 170 bool DeleteEntryData(); |
147 void DeleteEntryData(bool everything); | |
148 | 171 |
149 // Prepares the target file or buffer for a write of buf_len bytes at the | 172 // Prepares the target file or buffer for a write of buf_len bytes at the |
150 // given offset. | 173 // given offset. |
151 bool PrepareTarget(int index, int offset, int buf_len, bool truncate); | 174 int PrepareTarget(int index, int offset, int buf_len, bool truncate); |
152 | 175 |
153 // Adjusts the internal buffer and file handle for a write that truncates this | 176 // Adjusts the internal buffer and file handle for a write that truncates this |
154 // stream. | 177 // stream. |
155 bool HandleTruncation(int index, int offset, int buf_len); | 178 int HandleTruncation(int index, int offset, int buf_len); |
156 | 179 |
157 // Copies data from disk to the internal buffer. | 180 bool IsSimpleWrite(int index, int offset, int buf_len); |
158 bool CopyToLocalBuffer(int index); | 181 int HandleOldData(int index, int offset, int buf_len); |
159 | |
160 // Reads from a block data file to this object's memory buffer. | |
161 bool MoveToLocalBuffer(int index); | |
162 | |
163 // Loads the external file to this object's memory buffer. | |
164 bool ImportSeparateFile(int index, int new_size); | |
165 | 182 |
166 // Makes sure that the internal buffer can handle the a write of |buf_len| | 183 // Makes sure that the internal buffer can handle the a write of |buf_len| |
167 // bytes to |offset|. | 184 // bytes to |offset|. |
168 bool PrepareBuffer(int index, int offset, int buf_len); | 185 int PrepareBuffer(int index, int offset, int buf_len); |
169 | 186 |
170 // Flushes the in-memory data to the backing storage. The data destination | 187 // Flushes the in-memory data to the backing storage. The data destination |
171 // is determined based on the current data length and |min_len|. | 188 // is determined based on the current data length and |min_len|. |
172 bool Flush(int index, int min_len); | 189 int Flush(int index, int min_len); |
173 | 190 |
174 // Updates the size of a given data stream. | 191 // Updates the size of a given data stream. |
175 void UpdateSize(int index, int old_size, int new_size); | 192 void UpdateSize(int index, int old_size, int new_size); |
176 | 193 |
177 // Initializes the sparse control object. Returns a net error code. | 194 void WriteEntryData(); |
178 int InitSparseData(); | |
179 | 195 |
180 // Adds the provided |flags| to the current EntryFlags for this entry. | 196 // Adds the provided |flags| to the current EntryFlags for this entry. |
181 void SetEntryFlags(uint32 flags); | 197 void SetEntryFlags(uint32 flags); |
182 | 198 |
183 // Returns the current EntryFlags for this entry. | 199 // Returns the current EntryFlags for this entry. |
184 uint32 GetEntryFlags(); | 200 uint32 GetEntryFlags(); |
185 | 201 |
| 202 void OnEntryModified(); |
| 203 |
| 204 int GetAdjustedSize(int index, int real_size) const; |
| 205 |
186 // Gets the data stored at the given index. If the information is in memory, | 206 // Gets the data stored at the given index. If the information is in memory, |
187 // a buffer will be allocated and the data will be copied to it (the caller | 207 // a buffer will be allocated and the data will be copied to it (the caller |
188 // can find out the size of the buffer before making this call). Otherwise, | 208 // can find out the size of the buffer before making this call). Otherwise, |
189 // the cache address of the data will be returned, and that address will be | 209 // the cache address of the data will be returned, and that address will be |
190 // removed from the regular book keeping of this entry so the caller is | 210 // removed from the regular book keeping of this entry so the caller is |
191 // responsible for deleting the block (or file) from the backing store at some | 211 // responsible for deleting the block (or file) from the backing store at some |
192 // point; there is no need to report any storage-size change, only to do the | 212 // point; there is no need to report any storage-size change, only to do the |
193 // actual cleanup. | 213 // actual cleanup. |
194 void GetData(int index, char** buffer, Addr* address); | 214 void GetData(int index, scoped_refptr<IOBuffer>* buffer, Addr* address); |
195 | 215 |
196 // Generates a histogram for the time spent working on this operation. | 216 // Generates a histogram for the time spent working on this operation. |
197 void ReportIOTime(Operation op, const base::TimeTicks& start); | 217 void ReportIOTime(Operation op, const base::TimeTicks& start); |
198 | 218 |
199 // Logs this entry to the internal trace buffer. | 219 // Logs this entry to the internal trace buffer. |
200 void Log(const char* msg); | 220 void Log(const char* msg); |
201 | 221 |
202 CacheEntryBlock entry_; // Key related information for this entry. | 222 void OnIOComplete(int result); |
203 CacheRankingsBlock node_; // Rankings related information for this entry. | 223 |
204 base::WeakPtr<BackendImpl> backend_; // Back pointer to the cache. | 224 scoped_ptr<EntryRecord> entry_; // Basic record for this entry. |
205 base::WeakPtr<InFlightBackendIO> background_queue_; // In-progress queue. | 225 scoped_ptr<ShortEntryRecord> short_entry_; // Valid for evicted entries. |
| 226 base::WeakPtr<BackendImplV3> backend_; // Back pointer to the cache. |
206 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. | 227 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. |
207 // Files to store external user data and key. | 228 // Files to store external user data and key. |
208 scoped_refptr<File> files_[kNumStreams + 1]; | 229 //scoped_refptr<File> files_[kNumStreams + 1]; |
209 mutable std::string key_; // Copy of the key. | 230 mutable std::string key_; // Copy of the key. |
| 231 Addr address_; |
210 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. | 232 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. |
| 233 int num_handles_; // References held by "real" users. |
211 bool doomed_; // True if this entry was removed from the cache. | 234 bool doomed_; // True if this entry was removed from the cache. |
212 bool read_only_; // True if not yet writing. | 235 bool read_only_; |
213 bool dirty_; // True if we detected that this is a dirty entry. | 236 bool dirty_; // True if there is something to write. |
214 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. | 237 bool modified_; |
| 238 scoped_ptr<SparseControlV3> sparse_; // Support for sparse entries. |
| 239 PendingOperations pending_operations_; |
| 240 CompletionCallback callback_; |
| 241 CompletionCallback destruction_callback_; |
215 | 242 |
216 net::BoundNetLog net_log_; | 243 net::BoundNetLog net_log_; |
217 | 244 |
218 DISALLOW_COPY_AND_ASSIGN(EntryImpl); | 245 DISALLOW_COPY_AND_ASSIGN(EntryImplV3); |
219 }; | 246 }; |
220 | 247 |
221 } // namespace disk_cache | 248 } // namespace disk_cache |
222 | 249 |
223 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ | 250 #endif // NET_DISK_CACHE_V3_ENTRY_IMPL_V3_H_ |
OLD | NEW |