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/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"
15 namespace disk_cache
{
18 class InFlightBackendIO
;
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
27 public base::RefCounted
<EntryImpl
> {
28 friend class base::RefCounted
<EntryImpl
>;
29 friend class SparseControl
;
41 EntryImpl(BackendImpl
* backend
, Addr address
, bool read_only
);
43 // Background implementation of the Entry interface.
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() {
61 inline CacheRankingsBlock
* rankings() {
67 // Performs the initialization of a EntryImpl that will be added to the
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.
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
83 CacheAddr
GetNextAddress();
85 // Sets the address of the next entry on the list of entries with the same
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.
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
114 bool LeaveRankingsBehind();
116 // Returns false if the entry is clearly invalid.
118 bool DataSanityCheck();
120 // Attempts to make this entry reachable though the key.
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
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
);
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
;
177 virtual ~EntryImpl();
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
);
186 // Initializes the storage for an internal or external data block.
187 bool CreateDataBlock(int index
, int size
);
189 // Initializes the storage for an internal or external generic block.
190 bool CreateBlock(int size
, Addr
* address
);
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
);
198 // Updates ranking information.
199 void UpdateRank(bool modified
);
201 // Returns a pointer to the file that stores the given address.
202 File
* GetBackingFile(Addr address
, int index
);
204 // Returns a pointer to the file that stores external data.
205 File
* GetExternalFile(Addr address
, int index
);
207 // Prepares the target file or buffer for a write of buf_len bytes at the
209 bool PrepareTarget(int index
, int offset
, int buf_len
, bool truncate
);
211 // Adjusts the internal buffer and file handle for a write that truncates this
213 bool HandleTruncation(int index
, int offset
, int buf_len
);
215 // Copies data from disk to the internal buffer.
216 bool CopyToLocalBuffer(int index
);
218 // Reads from a block data file to this object's memory buffer.
219 bool MoveToLocalBuffer(int index
);
221 // Loads the external file to this object's memory buffer.
222 bool ImportSeparateFile(int index
, int new_size
);
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
);
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
);
232 // Updates the size of a given data stream.
233 void UpdateSize(int index
, int old_size
, int new_size
);
235 // Initializes the sparse control object. Returns a net error code.
236 int InitSparseData();
238 // Adds the provided |flags| to the current EntryFlags for this entry.
239 void SetEntryFlags(uint32 flags
);
241 // Returns the current EntryFlags for this entry.
242 uint32
GetEntryFlags();
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
252 void GetData(int index
, char** buffer
, Addr
* address
);
254 // Logs this entry to the internal trace buffer.
255 void Log(const char* msg
);
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.
271 net::BoundNetLog net_log_
;
273 DISALLOW_COPY_AND_ASSIGN(EntryImpl
);
276 } // namespace disk_cache
278 #endif // NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_