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