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