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_V3_H_
6 #define NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_V3_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "net/disk_cache/blockfile/disk_format_v3.h"
12 #include "net/disk_cache/blockfile/storage_block.h"
13 #include "net/disk_cache/disk_cache.h"
14 #include "net/log/net_log.h"
16 namespace disk_cache
{
19 class SparseControlV3
;
21 // This class implements the Entry interface. An object of this
22 // class represents a single entry on the cache.
23 class NET_EXPORT_PRIVATE EntryImplV3
25 public base::RefCounted
<EntryImplV3
> {
26 friend class base::RefCounted
<EntryImplV3
>;
27 // friend class SparseControlV3;
39 EntryImplV3(BackendImplV3
* backend
, Addr address
, bool read_only
);
41 // Performs the initialization of a EntryImplV3 that will be added to the
43 bool CreateEntry(Addr node_address
, const std::string
& key
, uint32 hash
);
47 uint32
GetHash() const;
48 Addr
GetAddress() const;
49 int GetReuseCounter() const;
50 void SetReuseCounter(int count
);
51 int GetRefetchCounter() const;
52 void SetRefetchCounter(int count
);
54 // Returns true if this entry matches the lookup arguments.
55 bool IsSameEntry(const std::string
& key
, uint32 hash
);
57 // Permamently destroys this entry.
60 // Returns false if the entry is clearly invalid.
62 bool DataSanityCheck();
64 // Attempts to make this entry reachable though the key.
67 // Set the access times for this entry. This method provides support for
69 void SetTimes(base::Time last_used
, base::Time last_modified
);
71 // Logs a begin event and enables logging for the EntryImplV3. Will also cause
72 // an end event to be logged on destruction. The EntryImplV3 must have its key
73 // initialized before this is called. |created| is true if the Entry was
74 // created rather than opened.
75 void BeginLogging(net::NetLog
* net_log
, bool created
);
77 const net::BoundNetLog
& net_log() const;
81 void Close() override
;
82 std::string
GetKey() const override
;
83 base::Time
GetLastUsed() const override
;
84 base::Time
GetLastModified() const override
;
85 int32
GetDataSize(int index
) const override
;
86 int ReadData(int index
,
90 const CompletionCallback
& callback
) override
;
91 int WriteData(int index
,
95 const CompletionCallback
& callback
,
96 bool truncate
) override
;
97 int ReadSparseData(int64 offset
,
100 const CompletionCallback
& callback
) override
;
101 int WriteSparseData(int64 offset
,
104 const CompletionCallback
& callback
) override
;
105 int GetAvailableRange(int64 offset
,
108 const CompletionCallback
& callback
) override
;
109 bool CouldBeSparse() const override
;
110 void CancelSparseIO() override
;
111 int ReadyForSparseIO(const CompletionCallback
& callback
) override
;
119 ~EntryImplV3() override
;
121 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as
122 // separate functions to make logging of results simpler.
123 int InternalReadData(int index
, int offset
, IOBuffer
* buf
,
124 int buf_len
, const CompletionCallback
& callback
);
125 int InternalWriteData(int index
, int offset
, IOBuffer
* buf
, int buf_len
,
126 const CompletionCallback
& callback
, bool truncate
);
128 // Initializes the storage for an internal or external data block.
129 bool CreateDataBlock(int index
, int size
);
131 // Initializes the storage for an internal or external generic block.
132 bool CreateBlock(int size
, Addr
* address
);
134 // Deletes the data pointed by address, maybe backed by files_[index].
135 // Note that most likely the caller should delete (and store) the reference to
136 // |address| *before* calling this method because we don't want to have an
137 // entry using an address that is already free.
138 void DeleteData(Addr address
, int index
);
140 // Updates ranking information.
141 void UpdateRank(bool modified
);
143 // Deletes this entry from disk. If |everything| is false, only the user data
144 // will be removed, leaving the key and control data intact.
145 void DeleteEntryData(bool everything
);
147 // Prepares the target file or buffer for a write of buf_len bytes at the
149 bool PrepareTarget(int index
, int offset
, int buf_len
, bool truncate
);
151 // Adjusts the internal buffer and file handle for a write that truncates this
153 bool HandleTruncation(int index
, int offset
, int buf_len
);
155 // Copies data from disk to the internal buffer.
156 bool CopyToLocalBuffer(int index
);
158 // Reads from a block data file to this object's memory buffer.
159 bool MoveToLocalBuffer(int index
);
161 // Loads the external file to this object's memory buffer.
162 bool ImportSeparateFile(int index
, int new_size
);
164 // Makes sure that the internal buffer can handle the a write of |buf_len|
165 // bytes to |offset|.
166 bool PrepareBuffer(int index
, int offset
, int buf_len
);
168 // Flushes the in-memory data to the backing storage. The data destination
169 // is determined based on the current data length and |min_len|.
170 bool Flush(int index
, int min_len
);
172 // Updates the size of a given data stream.
173 void UpdateSize(int index
, int old_size
, int new_size
);
175 // Initializes the sparse control object. Returns a net error code.
176 int InitSparseData();
178 // Adds the provided |flags| to the current EntryFlags for this entry.
179 void SetEntryFlags(uint32 flags
);
181 // Returns the current EntryFlags for this entry.
182 uint32
GetEntryFlags();
184 // Gets the data stored at the given index. If the information is in memory,
185 // a buffer will be allocated and the data will be copied to it (the caller
186 // can find out the size of the buffer before making this call). Otherwise,
187 // the cache address of the data will be returned, and that address will be
188 // removed from the regular book keeping of this entry so the caller is
189 // responsible for deleting the block (or file) from the backing store at some
190 // point; there is no need to report any storage-size change, only to do the
192 void GetData(int index
, char** buffer
, Addr
* address
);
194 // Generates a histogram for the time spent working on this operation.
195 void ReportIOTime(Operation op
, const base::TimeTicks
& start
);
197 // Logs this entry to the internal trace buffer.
198 void Log(const char* msg
);
200 scoped_ptr
<EntryRecord
> entry_
; // Basic record for this entry.
201 scoped_ptr
<ShortEntryRecord
> short_entry_
; // Valid for evicted entries.
202 base::WeakPtr
<BackendImplV3
> backend_
; // Back pointer to the cache.
203 scoped_ptr
<UserBuffer
> user_buffers_
[kNumStreams
]; // Stores user data.
204 mutable std::string key_
; // Copy of the key.
206 int unreported_size_
[kNumStreams
]; // Bytes not reported yet to the backend.
207 bool doomed_
; // True if this entry was removed from the cache.
209 bool dirty_
; // True if there is something to write.
211 // scoped_ptr<SparseControlV3> sparse_; // Support for sparse entries.
213 net::BoundNetLog net_log_
;
215 DISALLOW_COPY_AND_ASSIGN(EntryImplV3
);
218 } // namespace disk_cache
220 #endif // NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_V3_H_