• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_ENTRY_IMPL_H_
6 #define NET_DISK_CACHE_ENTRY_IMPL_H_
7 #pragma once
8 
9 #include "base/memory/scoped_ptr.h"
10 #include "net/base/net_log.h"
11 #include "net/disk_cache/disk_cache.h"
12 #include "net/disk_cache/storage_block.h"
13 #include "net/disk_cache/storage_block-inl.h"
14 
15 namespace disk_cache {
16 
17 class BackendImpl;
18 class SparseControl;
19 
20 // This class implements the Entry interface. An object of this
21 // class represents a single entry on the cache.
22 class EntryImpl : public Entry, public base::RefCounted<EntryImpl> {
23   friend class base::RefCounted<EntryImpl>;
24   friend class SparseControl;
25  public:
26   enum Operation {
27     kRead,
28     kWrite,
29     kSparseRead,
30     kSparseWrite,
31     kAsyncIO
32   };
33 
34   EntryImpl(BackendImpl* backend, Addr address, bool read_only);
35 
36   // Background implementation of the Entry interface.
37   void DoomImpl();
38   int ReadDataImpl(int index, int offset, net::IOBuffer* buf, int buf_len,
39                    CompletionCallback* callback);
40   int WriteDataImpl(int index, int offset, net::IOBuffer* buf, int buf_len,
41                     CompletionCallback* callback, bool truncate);
42   int ReadSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len,
43                          CompletionCallback* callback);
44   int WriteSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len,
45                           CompletionCallback* callback);
46   int GetAvailableRangeImpl(int64 offset, int len, int64* start);
47   void CancelSparseIOImpl();
48   int ReadyForSparseIOImpl(CompletionCallback* callback);
49 
entry()50   inline CacheEntryBlock* entry() {
51     return &entry_;
52   }
53 
rankings()54   inline CacheRankingsBlock* rankings() {
55     return &node_;
56   }
57 
58   uint32 GetHash();
59 
60   // Performs the initialization of a EntryImpl that will be added to the
61   // cache.
62   bool CreateEntry(Addr node_address, const std::string& key, uint32 hash);
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   // Deletes this entry from disk. If |everything| is false, only the user data
71   // will be removed, leaving the key and control data intact.
72   void DeleteEntryData(bool everything);
73 
74   // Returns the address of the next entry on the list of entries with the same
75   // hash.
76   CacheAddr GetNextAddress();
77 
78   // Sets the address of the next entry on the list of entries with the same
79   // hash.
80   void SetNextAddress(Addr address);
81 
82   // Reloads the rankings node information.
83   bool LoadNodeAddress();
84 
85   // Updates the stored data to reflect the run-time information for this entry.
86   // Returns false if the data could not be updated. The purpose of this method
87   // is to be able to detect entries that are currently in use.
88   bool Update();
89 
dirty()90   bool dirty() {
91     return dirty_;
92   }
93 
doomed()94   bool doomed() {
95     return doomed_;
96   }
97 
98   // Marks this entry as dirty (in memory) if needed. This is intended only for
99   // entries that are being read from disk, to be called during loading.
100   void SetDirtyFlag(int32 current_id);
101 
102   // Fixes this entry so it can be treated as valid (to delete it).
103   void SetPointerForInvalidEntry(int32 new_id);
104 
105   // Returns true if this entry is so meesed up that not everything is going to
106   // be removed.
107   bool LeaveRankingsBehind();
108 
109   // Returns false if the entry is clearly invalid.
110   bool SanityCheck();
111   bool DataSanityCheck();
112 
113   // Attempts to make this entry reachable though the key.
114   void FixForDelete();
115 
116   // Handle the pending asynchronous IO count.
117   void IncrementIoCount();
118   void DecrementIoCount();
119 
120   // Set the access times for this entry. This method provides support for
121   // the upgrade tool.
122   void SetTimes(base::Time last_used, base::Time last_modified);
123 
124   // Generates a histogram for the time spent working on this operation.
125   void ReportIOTime(Operation op, const base::TimeTicks& start);
126 
127   // Logs a begin event and enables logging for the EntryImpl.  Will also cause
128   // an end event to be logged on destruction.  The EntryImpl must have its key
129   // initialized before this is called.  |created| is true if the Entry was
130   // created rather than opened.
131   void BeginLogging(net::NetLog* net_log, bool created);
132 
133   const net::BoundNetLog& net_log() const;
134 
135   // Returns the number of blocks needed to store an EntryStore.
136   static int NumBlocksForEntry(int key_size);
137 
138   // Entry interface.
139   virtual void Doom();
140   virtual void Close();
141   virtual std::string GetKey() const;
142   virtual base::Time GetLastUsed() const;
143   virtual base::Time GetLastModified() const;
144   virtual int32 GetDataSize(int index) const;
145   virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
146                        net::CompletionCallback* completion_callback);
147   virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
148                         net::CompletionCallback* completion_callback,
149                         bool truncate);
150   virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
151                              net::CompletionCallback* completion_callback);
152   virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
153                               net::CompletionCallback* completion_callback);
154   virtual int GetAvailableRange(int64 offset, int len, int64* start,
155                                 CompletionCallback* callback);
156   virtual bool CouldBeSparse() const;
157   virtual void CancelSparseIO();
158   virtual int ReadyForSparseIO(net::CompletionCallback* completion_callback);
159 
160  private:
161   enum {
162      kNumStreams = 3
163   };
164   class UserBuffer;
165 
166   ~EntryImpl();
167 
168   // Do all the work for ReadDataImpl and WriteDataImpl.  Implemented as
169   // separate functions to make logging of results simpler.
170   int InternalReadData(int index, int offset, net::IOBuffer* buf,
171                        int buf_len, CompletionCallback* callback);
172   int InternalWriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
173                         CompletionCallback* callback, bool truncate);
174 
175   // Initializes the storage for an internal or external data block.
176   bool CreateDataBlock(int index, int size);
177 
178   // Initializes the storage for an internal or external generic block.
179   bool CreateBlock(int size, Addr* address);
180 
181   // Deletes the data pointed by address, maybe backed by files_[index].
182   // Note that most likely the caller should delete (and store) the reference to
183   // |address| *before* calling this method because we don't want to have an
184   // entry using an address that is already free.
185   void DeleteData(Addr address, int index);
186 
187   // Updates ranking information.
188   void UpdateRank(bool modified);
189 
190   // Returns a pointer to the file that stores the given address.
191   File* GetBackingFile(Addr address, int index);
192 
193   // Returns a pointer to the file that stores external data.
194   File* GetExternalFile(Addr address, int index);
195 
196   // Prepares the target file or buffer for a write of buf_len bytes at the
197   // given offset.
198   bool PrepareTarget(int index, int offset, int buf_len, bool truncate);
199 
200   // Adjusts the internal buffer and file handle for a write that truncates this
201   // stream.
202   bool HandleTruncation(int index, int offset, int buf_len);
203 
204   // Copies data from disk to the internal buffer.
205   bool CopyToLocalBuffer(int index);
206 
207   // Reads from a block data file to this object's memory buffer.
208   bool MoveToLocalBuffer(int index);
209 
210   // Loads the external file to this object's memory buffer.
211   bool ImportSeparateFile(int index, int new_size);
212 
213   // Makes sure that the internal buffer can handle the a write of |buf_len|
214   // bytes to |offset|.
215   bool PrepareBuffer(int index, int offset, int buf_len);
216 
217   // Flushes the in-memory data to the backing storage. The data destination
218   // is determined based on the current data length and |min_len|.
219   bool Flush(int index, int min_len);
220 
221   // Updates the size of a given data stream.
222   void UpdateSize(int index, int old_size, int new_size);
223 
224   // Initializes the sparse control object. Returns a net error code.
225   int InitSparseData();
226 
227   // Adds the provided |flags| to the current EntryFlags for this entry.
228   void SetEntryFlags(uint32 flags);
229 
230   // Returns the current EntryFlags for this entry.
231   uint32 GetEntryFlags();
232 
233   // Gets the data stored at the given index. If the information is in memory,
234   // a buffer will be allocated and the data will be copied to it (the caller
235   // can find out the size of the buffer before making this call). Otherwise,
236   // the cache address of the data will be returned, and that address will be
237   // removed from the regular book keeping of this entry so the caller is
238   // responsible for deleting the block (or file) from the backing store at some
239   // point; there is no need to report any storage-size change, only to do the
240   // actual cleanup.
241   void GetData(int index, char** buffer, Addr* address);
242 
243   // Logs this entry to the internal trace buffer.
244   void Log(const char* msg);
245 
246   CacheEntryBlock entry_;     // Key related information for this entry.
247   CacheRankingsBlock node_;   // Rankings related information for this entry.
248   BackendImpl* backend_;      // Back pointer to the cache.
249   scoped_ptr<UserBuffer> user_buffers_[kNumStreams];  // Stores user data.
250   // Files to store external user data and key.
251   scoped_refptr<File> files_[kNumStreams + 1];
252   mutable std::string key_;           // Copy of the key.
253   int unreported_size_[kNumStreams];  // Bytes not reported yet to the backend.
254   bool doomed_;               // True if this entry was removed from the cache.
255   bool read_only_;            // True if not yet writing.
256   bool dirty_;                // True if we detected that this is a dirty entry.
257   scoped_ptr<SparseControl> sparse_;  // Support for sparse entries.
258 
259   net::BoundNetLog net_log_;
260 
261   DISALLOW_COPY_AND_ASSIGN(EntryImpl);
262 };
263 
264 }  // namespace disk_cache
265 
266 #endif  // NET_DISK_CACHE_ENTRY_IMPL_H_
267