Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / disk_cache / blockfile / entry_impl.h
blobaafb59b66a295d0df8f1b417d85430ee0ef5bf28
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.
5 #ifndef NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_
6 #define NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "net/disk_cache/blockfile/disk_format.h"
10 #include "net/disk_cache/blockfile/storage_block-inl.h"
11 #include "net/disk_cache/blockfile/storage_block.h"
12 #include "net/disk_cache/disk_cache.h"
13 #include "net/log/net_log.h"
15 namespace disk_cache {
17 class BackendImpl;
18 class InFlightBackendIO;
19 class SparseControl;
20 typedef StorageBlock<EntryStore> CacheEntryBlock;
21 typedef StorageBlock<RankingsNode> CacheRankingsBlock;
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
41 EntryImpl(BackendImpl* backend, Addr address, bool read_only);
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);
57 inline CacheEntryBlock* entry() {
58 return &entry_;
61 inline CacheRankingsBlock* rankings() {
62 return &node_;
65 uint32 GetHash();
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);
71 // Returns true if this entry matches the lookup arguments.
72 bool IsSameEntry(const std::string& key, uint32 hash);
74 // Permamently destroys this entry.
75 void InternalDoom();
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);
81 // Returns the address of the next entry on the list of entries with the same
82 // hash.
83 CacheAddr GetNextAddress();
85 // Sets the address of the next entry on the list of entries with the same
86 // hash.
87 void SetNextAddress(Addr address);
89 // Reloads the rankings node information.
90 bool LoadNodeAddress();
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();
97 bool dirty() {
98 return dirty_;
101 bool doomed() {
102 return doomed_;
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);
109 // Fixes this entry so it can be treated as valid (to delete it).
110 void SetPointerForInvalidEntry(int32 new_id);
112 // Returns true if this entry is so meesed up that not everything is going to
113 // be removed.
114 bool LeaveRankingsBehind();
116 // Returns false if the entry is clearly invalid.
117 bool SanityCheck();
118 bool DataSanityCheck();
120 // Attempts to make this entry reachable though the key.
121 void FixForDelete();
123 // Handle the pending asynchronous IO count.
124 void IncrementIoCount();
125 void DecrementIoCount();
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);
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);
135 // Generates a histogram for the time spent working on this operation.
136 void ReportIOTime(Operation op, const base::TimeTicks& start);
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);
144 const net::BoundNetLog& net_log() const;
146 // Returns the number of blocks needed to store an EntryStore.
147 static int NumBlocksForEntry(int key_size);
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;
183 private:
184 enum {
185 kNumStreams = 3
187 class UserBuffer;
189 ~EntryImpl() override;
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);
198 // Initializes the storage for an internal or external data block.
199 bool CreateDataBlock(int index, int size);
201 // Initializes the storage for an internal or external generic block.
202 bool CreateBlock(int size, Addr* address);
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);
210 // Updates ranking information.
211 void UpdateRank(bool modified);
213 // Returns a pointer to the file that stores the given address.
214 File* GetBackingFile(Addr address, int index);
216 // Returns a pointer to the file that stores external data.
217 File* GetExternalFile(Addr address, int index);
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);
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);
227 // Copies data from disk to the internal buffer.
228 bool CopyToLocalBuffer(int index);
230 // Reads from a block data file to this object's memory buffer.
231 bool MoveToLocalBuffer(int index);
233 // Loads the external file to this object's memory buffer.
234 bool ImportSeparateFile(int index, int new_size);
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);
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);
244 // Updates the size of a given data stream.
245 void UpdateSize(int index, int old_size, int new_size);
247 // Initializes the sparse control object. Returns a net error code.
248 int InitSparseData();
250 // Adds the provided |flags| to the current EntryFlags for this entry.
251 void SetEntryFlags(uint32 flags);
253 // Returns the current EntryFlags for this entry.
254 uint32 GetEntryFlags();
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);
266 // Logs this entry to the internal trace buffer.
267 void Log(const char* msg);
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.
283 net::BoundNetLog net_log_;
285 DISALLOW_COPY_AND_ASSIGN(EntryImpl);
288 } // namespace disk_cache
290 #endif // NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_