| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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_INDEX_TABLE_V3_H_ | |
| 6 #define NET_DISK_CACHE_BLOCKFILE_INDEX_TABLE_V3_H_ | |
| 7 | |
| 8 // The IndexTable class is in charge of handling all the details about the main | |
| 9 // index table of the cache. It provides methods to locate entries in the cache, | |
| 10 // create new entries and modify existing entries. It hides the fact that the | |
| 11 // table is backed up across multiple physical files, and that the files can | |
| 12 // grow and be remapped while the cache is in use. However, note that this class | |
| 13 // doesn't do any direct management of the backing files, and it operates only | |
| 14 // with the tables in memory. | |
| 15 // | |
| 16 // When the current index needs to grow, the backend is notified so that files | |
| 17 // are extended and remapped as needed. After that, the IndexTable should be | |
| 18 // re-initialized with the new structures. Note that the IndexTable instance is | |
| 19 // still functional while the backend performs file IO. | |
| 20 | |
| 21 #include <stddef.h> | |
| 22 #include <stdint.h> | |
| 23 | |
| 24 #include <memory> | |
| 25 #include <vector> | |
| 26 | |
| 27 #include "base/macros.h" | |
| 28 #include "base/memory/ref_counted.h" | |
| 29 #include "base/time/time.h" | |
| 30 #include "net/base/net_export.h" | |
| 31 #include "net/disk_cache/blockfile/addr.h" | |
| 32 #include "net/disk_cache/blockfile/bitmap.h" | |
| 33 #include "net/disk_cache/blockfile/disk_format_v3.h" | |
| 34 | |
| 35 namespace net { | |
| 36 class IOBuffer; | |
| 37 } | |
| 38 | |
| 39 namespace disk_cache { | |
| 40 | |
| 41 class BackendImplV3; | |
| 42 struct InitResult; | |
| 43 | |
| 44 // An EntryCell represents a single entity stored by the index table. Users are | |
| 45 // expected to handle and store EntryCells on their own to track operations that | |
| 46 // they are performing with a given entity, as opposed to deal with pointers to | |
| 47 // individual positions on the table, given that the whole table can be moved to | |
| 48 // another place, and that would invalidate any pointers to individual cells in | |
| 49 // the table. | |
| 50 // However, note that it is also possible for an entity to be moved from one | |
| 51 // position to another, so an EntryCell may be invalid by the time a long | |
| 52 // operation completes. In that case, the caller should consult the table again | |
| 53 // using FindEntryCell(). | |
| 54 class NET_EXPORT_PRIVATE EntryCell { | |
| 55 public: | |
| 56 ~EntryCell(); | |
| 57 | |
| 58 bool IsValid() const; | |
| 59 | |
| 60 int32_t cell_num() const { return cell_num_; } | |
| 61 uint32_t hash() const { return hash_; } | |
| 62 | |
| 63 Addr GetAddress() const; | |
| 64 EntryState GetState() const; | |
| 65 EntryGroup GetGroup() const; | |
| 66 int GetReuse() const; | |
| 67 int GetTimestamp() const; | |
| 68 | |
| 69 void SetState(EntryState state); | |
| 70 void SetGroup(EntryGroup group); | |
| 71 void SetReuse(int count); | |
| 72 void SetTimestamp(int timestamp); | |
| 73 | |
| 74 static EntryCell GetEntryCellForTest(int32_t cell_num, | |
| 75 uint32_t hash, | |
| 76 Addr address, | |
| 77 IndexCell* cell, | |
| 78 bool small_table); | |
| 79 void SerializaForTest(IndexCell* destination); | |
| 80 | |
| 81 private: | |
| 82 friend class IndexTable; | |
| 83 friend class CacheDumperHelper; | |
| 84 | |
| 85 EntryCell(); | |
| 86 EntryCell(int32_t cell_num, uint32_t hash, Addr address, bool small_table); | |
| 87 EntryCell(int32_t cell_num, | |
| 88 uint32_t hash, | |
| 89 const IndexCell& cell, | |
| 90 bool small_table); | |
| 91 | |
| 92 void Clear() { cell_.Clear(); } | |
| 93 void FixSum(); | |
| 94 | |
| 95 // Returns the raw value stored on the index table. | |
| 96 uint32_t GetLocation() const; | |
| 97 | |
| 98 // Recalculates hash_ assuming that only the low order bits are valid and the | |
| 99 // rest come from cell_. | |
| 100 uint32_t RecomputeHash(); | |
| 101 | |
| 102 void Serialize(IndexCell* destination) const; | |
| 103 | |
| 104 int32_t cell_num_; | |
| 105 uint32_t hash_; | |
| 106 IndexCell cell_; | |
| 107 bool small_table_; | |
| 108 }; | |
| 109 | |
| 110 // Keeps a collection of EntryCells in order to be processed. | |
| 111 struct NET_EXPORT_PRIVATE EntrySet { | |
| 112 EntrySet(); | |
| 113 EntrySet(const EntrySet& other); | |
| 114 ~EntrySet(); | |
| 115 | |
| 116 int evicted_count; // The numebr of evicted entries in this set. | |
| 117 size_t current; // The number of the cell that is being processed. | |
| 118 std::vector<EntryCell> cells; | |
| 119 }; | |
| 120 | |
| 121 // A given entity referenced by the index table is uniquely identified by the | |
| 122 // combination of hash and address. | |
| 123 struct CellInfo { | |
| 124 uint32_t hash; | |
| 125 Addr address; | |
| 126 }; | |
| 127 typedef std::vector<CellInfo> CellList; | |
| 128 | |
| 129 // An index iterator is used to get a group of cells that share the same | |
| 130 // timestamp. When this structure is passed to GetNextCells(), the caller sets | |
| 131 // the initial timestamp and direction; whet it is used with GetOldest, the | |
| 132 // initial values are ignored. | |
| 133 struct NET_EXPORT_PRIVATE IndexIterator { | |
| 134 IndexIterator(); | |
| 135 ~IndexIterator(); | |
| 136 | |
| 137 CellList cells; | |
| 138 int timestamp; // The current low resolution timestamp for |cells|. | |
| 139 bool forward; // The direction of the iteration, in time. | |
| 140 }; | |
| 141 | |
| 142 // Methods that the backend has to implement to support the table. Note that the | |
| 143 // backend is expected to own all IndexTable instances, so it is expected to | |
| 144 // outlive the table. | |
| 145 class NET_EXPORT_PRIVATE IndexTableBackend { | |
| 146 public: | |
| 147 virtual ~IndexTableBackend() {} | |
| 148 | |
| 149 // The index has to grow. | |
| 150 virtual void GrowIndex() = 0; | |
| 151 | |
| 152 // Save the index to the backup file. | |
| 153 virtual void SaveIndex(net::IOBuffer* buffer, int buffer_len) = 0; | |
| 154 | |
| 155 // Deletes or fixes an invalid cell from the backend. | |
| 156 virtual void DeleteCell(EntryCell cell) = 0; | |
| 157 virtual void FixCell(EntryCell cell) = 0; | |
| 158 }; | |
| 159 | |
| 160 // The data required to initialize an index. Note that not all fields have to | |
| 161 // be provided when growing the tables. | |
| 162 struct NET_EXPORT_PRIVATE IndexTableInitData { | |
| 163 IndexTableInitData(); | |
| 164 ~IndexTableInitData(); | |
| 165 | |
| 166 IndexBitmap* index_bitmap; | |
| 167 IndexBucket* main_table; | |
| 168 IndexBucket* extra_table; | |
| 169 std::unique_ptr<IndexHeaderV3> backup_header; | |
| 170 std::unique_ptr<uint32_t[]> backup_bitmap; | |
| 171 }; | |
| 172 | |
| 173 // See the description at the top of this file. | |
| 174 class NET_EXPORT_PRIVATE IndexTable { | |
| 175 public: | |
| 176 explicit IndexTable(IndexTableBackend* backend); | |
| 177 ~IndexTable(); | |
| 178 | |
| 179 // Initializes the object, or re-initializes it when the backing files grow. | |
| 180 // Note that the only supported way to initialize this objeect is using | |
| 181 // pointers that come from the files being directly mapped in memory. If that | |
| 182 // is not the case, it must be emulated in a convincing way, for example | |
| 183 // making sure that the tables for re-init look the same as the tables to be | |
| 184 // replaced. | |
| 185 void Init(IndexTableInitData* params); | |
| 186 | |
| 187 // Releases the resources acquired during Init(). | |
| 188 void Shutdown(); | |
| 189 | |
| 190 // Locates a resouce on the index. Returns a list of all resources that match | |
| 191 // the provided hash. | |
| 192 EntrySet LookupEntries(uint32_t hash); | |
| 193 | |
| 194 // Creates a new cell to store a new resource. | |
| 195 EntryCell CreateEntryCell(uint32_t hash, Addr address); | |
| 196 | |
| 197 // Locates a particular cell. This method allows a caller to perform slow | |
| 198 // operations with some entries while the index evolves, by returning the | |
| 199 // current state of a cell. If the desired cell cannot be located, the return | |
| 200 // object will be invalid. | |
| 201 EntryCell FindEntryCell(uint32_t hash, Addr address); | |
| 202 | |
| 203 // Returns an IndexTable timestamp for a given absolute time. The actual | |
| 204 // resolution of the timestamp should be considered an implementation detail, | |
| 205 // but it certainly is lower than seconds. The important part is that a group | |
| 206 // of cells will share the same timestamp (see IndexIterator). | |
| 207 int CalculateTimestamp(base::Time time); | |
| 208 | |
| 209 // Returns the equivalent time for a cell timestamp. | |
| 210 base::Time TimeFromTimestamp(int timestamp); | |
| 211 | |
| 212 // Updates a particular cell. | |
| 213 void SetSate(uint32_t hash, Addr address, EntryState state); | |
| 214 void UpdateTime(uint32_t hash, Addr address, base::Time current); | |
| 215 | |
| 216 // Saves the contents of |cell| to the table. | |
| 217 void Save(EntryCell* cell); | |
| 218 | |
| 219 // Returns the oldest entries for each group of entries. The initial values | |
| 220 // for the provided iterators are ignored. Entries are assigned to iterators | |
| 221 // according to their EntryGroup. | |
| 222 void GetOldest(IndexIterator* no_use, | |
| 223 IndexIterator* low_use, | |
| 224 IndexIterator* high_use); | |
| 225 | |
| 226 // Returns the next group of entries for the provided iterator. This method | |
| 227 // does not return the cells matching the initial iterator's timestamp, | |
| 228 // but rather cells after (or before, depending on the iterator's |forward| | |
| 229 // member) that timestamp. | |
| 230 bool GetNextCells(IndexIterator* iterator); | |
| 231 | |
| 232 // Called each time the index should save the backup information. The caller | |
| 233 // can assume that anything that needs to be saved is saved when this method | |
| 234 // is called, and that there is only one source of timming information, and | |
| 235 // that source is controlled by the owner of this object. | |
| 236 void OnBackupTimer(); | |
| 237 | |
| 238 IndexHeaderV3* header() { return header_; } | |
| 239 const IndexHeaderV3* header() const { return header_; } | |
| 240 | |
| 241 private: | |
| 242 EntryCell FindEntryCellImpl(uint32_t hash, Addr address, bool allow_deleted); | |
| 243 void CheckState(const EntryCell& cell); | |
| 244 void Write(const EntryCell& cell); | |
| 245 int NewExtraBucket(); | |
| 246 void WalkTables(int limit_time, | |
| 247 IndexIterator* no_use, | |
| 248 IndexIterator* low_use, | |
| 249 IndexIterator* high_use); | |
| 250 void UpdateFromBucket(IndexBucket* bucket, int bucket_hash, | |
| 251 int limit_time, | |
| 252 IndexIterator* no_use, | |
| 253 IndexIterator* low_use, | |
| 254 IndexIterator* high_use); | |
| 255 void MoveCells(IndexBucket* old_extra_table); | |
| 256 void MoveSingleCell(IndexCell* current_cell, int cell_num, | |
| 257 int main_table_index, bool growing); | |
| 258 void HandleMisplacedCell(IndexCell* current_cell, int cell_num, | |
| 259 int main_table_index); | |
| 260 void CheckBucketList(int bucket_id); | |
| 261 | |
| 262 uint32_t GetLocation(const IndexCell& cell); | |
| 263 uint32_t GetHashValue(const IndexCell& cell); | |
| 264 uint32_t GetFullHash(const IndexCell& cell, uint32_t lower_part); | |
| 265 bool IsHashMatch(const IndexCell& cell, uint32_t hash); | |
| 266 bool MisplacedHash(const IndexCell& cell, uint32_t hash); | |
| 267 | |
| 268 IndexTableBackend* backend_; | |
| 269 IndexHeaderV3* header_; | |
| 270 std::unique_ptr<Bitmap> bitmap_; | |
| 271 std::unique_ptr<Bitmap> backup_bitmap_; | |
| 272 std::unique_ptr<uint32_t[]> backup_bitmap_storage_; | |
| 273 std::unique_ptr<IndexHeaderV3> backup_header_; | |
| 274 IndexBucket* main_table_; | |
| 275 IndexBucket* extra_table_; | |
| 276 uint32_t mask_; // Binary mask to map a hash to the hash table. | |
| 277 int extra_bits_; // How many bits are in mask_ above the default value. | |
| 278 bool modified_; | |
| 279 bool small_table_; | |
| 280 | |
| 281 DISALLOW_COPY_AND_ASSIGN(IndexTable); | |
| 282 }; | |
| 283 | |
| 284 } // namespace disk_cache | |
| 285 | |
| 286 #endif // NET_DISK_CACHE_BLOCKFILE_INDEX_TABLE_V3_H_ | |
| OLD | NEW |