15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/disk_cache/blockfile/disk_format.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/disk_cache/blockfile/storage_block-inl.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/disk_cache/blockfile/storage_block.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/disk_cache/disk_cache.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace disk_cache {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BackendImpl;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class InFlightBackendIO;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SparseControl;
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef StorageBlock<EntryStore> CacheEntryBlock;
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef StorageBlock<RankingsNode> CacheRankingsBlock;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class implements the Entry interface. An object of this
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// class represents a single entry on the cache.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT_PRIVATE EntryImpl
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public Entry,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      public base::RefCounted<EntryImpl> {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCounted<EntryImpl>;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class SparseControl;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Operation {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kRead,
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kWrite,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kSparseRead,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kSparseWrite,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kAsyncIO,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kReadAsync1,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kWriteAsync1
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EntryImpl(BackendImpl* backend, Addr address, bool read_only);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Background implementation of the Entry interface.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DoomImpl();
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const CompletionCallback& callback);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const CompletionCallback& callback, bool truncate);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const CompletionCallback& callback);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const CompletionCallback& callback);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int GetAvailableRangeImpl(int64 offset, int len, int64* start);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CancelSparseIOImpl();
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int ReadyForSparseIOImpl(const CompletionCallback& callback);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  inline CacheEntryBlock* entry() {
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return &entry_;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  inline CacheRankingsBlock* rankings() {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return &node_;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 GetHash();
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Performs the initialization of a EntryImpl that will be added to the
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // cache.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateEntry(Addr node_address, const std::string& key, uint32 hash);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this entry matches the lookup arguments.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsSameEntry(const std::string& key, uint32 hash);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Permamently destroys this entry.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InternalDoom();
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes this entry from disk. If |everything| is false, only the user data
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will be removed, leaving the key and control data intact.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DeleteEntryData(bool everything);
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the address of the next entry on the list of entries with the same
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // hash.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CacheAddr GetNextAddress();
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the address of the next entry on the list of entries with the same
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // hash.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetNextAddress(Addr address);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reloads the rankings node information.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool LoadNodeAddress();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the stored data to reflect the run-time information for this entry.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the data could not be updated. The purpose of this method
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is to be able to detect entries that are currently in use.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Update();
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool dirty() {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return dirty_;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool doomed() {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return doomed_;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Marks this entry as dirty (in memory) if needed. This is intended only for
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // entries that are being read from disk, to be called during loading.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetDirtyFlag(int32 current_id);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fixes this entry so it can be treated as valid (to delete it).
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetPointerForInvalidEntry(int32 new_id);
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this entry is so meesed up that not everything is going to
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be removed.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool LeaveRankingsBehind();
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the entry is clearly invalid.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SanityCheck();
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DataSanityCheck();
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Attempts to make this entry reachable though the key.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FixForDelete();
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handle the pending asynchronous IO count.
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void IncrementIoCount();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DecrementIoCount();
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This entry is being returned to the user. It is always called from the
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // primary thread (not the dedicated cache thread).
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnEntryCreated(BackendImpl* backend);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the access times for this entry. This method provides support for
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the upgrade tool.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetTimes(base::Time last_used, base::Time last_modified);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates a histogram for the time spent working on this operation.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ReportIOTime(Operation op, const base::TimeTicks& start);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Logs a begin event and enables logging for the EntryImpl.  Will also cause
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an end event to be logged on destruction.  The EntryImpl must have its key
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // initialized before this is called.  |created| is true if the Entry was
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // created rather than opened.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void BeginLogging(net::NetLog* net_log, bool created);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const net::BoundNetLog& net_log() const;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of blocks needed to store an EntryStore.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int NumBlocksForEntry(int key_size);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Entry interface.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Doom() OVERRIDE;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Close() OVERRIDE;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetKey() const OVERRIDE;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetLastUsed() const OVERRIDE;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetLastModified() const OVERRIDE;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int32 GetDataSize(int index) const OVERRIDE;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len,
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const CompletionCallback& callback) OVERRIDE;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        const CompletionCallback& callback,
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        bool truncate) OVERRIDE;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const CompletionCallback& callback) OVERRIDE;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const CompletionCallback& callback) OVERRIDE;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetAvailableRange(int64 offset, int len, int64* start,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const CompletionCallback& callback) OVERRIDE;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CouldBeSparse() const OVERRIDE;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void CancelSparseIO() OVERRIDE;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum {
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     kNumStreams = 3
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class UserBuffer;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~EntryImpl();
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do all the work for ReadDataImpl and WriteDataImpl.  Implemented as
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // separate functions to make logging of results simpler.
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int InternalReadData(int index, int offset, IOBuffer* buf,
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       int buf_len, const CompletionCallback& callback);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int InternalWriteData(int index, int offset, IOBuffer* buf, int buf_len,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        const CompletionCallback& callback, bool truncate);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the storage for an internal or external data block.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateDataBlock(int index, int size);
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the storage for an internal or external generic block.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateBlock(int size, Addr* address);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the data pointed by address, maybe backed by files_[index].
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that most likely the caller should delete (and store) the reference to
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |address| *before* calling this method because we don't want to have an
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // entry using an address that is already free.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DeleteData(Addr address, int index);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates ranking information.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateRank(bool modified);
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a pointer to the file that stores the given address.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  File* GetBackingFile(Addr address, int index);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a pointer to the file that stores external data.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  File* GetExternalFile(Addr address, int index);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Prepares the target file or buffer for a write of buf_len bytes at the
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // given offset.
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrepareTarget(int index, int offset, int buf_len, bool truncate);
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adjusts the internal buffer and file handle for a write that truncates this
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // stream.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HandleTruncation(int index, int offset, int buf_len);
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Copies data from disk to the internal buffer.
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CopyToLocalBuffer(int index);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reads from a block data file to this object's memory buffer.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool MoveToLocalBuffer(int index);
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loads the external file to this object's memory buffer.
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ImportSeparateFile(int index, int new_size);
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Makes sure that the internal buffer can handle the a write of |buf_len|
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bytes to |offset|.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PrepareBuffer(int index, int offset, int buf_len);
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Flushes the in-memory data to the backing storage. The data destination
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is determined based on the current data length and |min_len|.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Flush(int index, int min_len);
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the size of a given data stream.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateSize(int index, int old_size, int new_size);
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the sparse control object. Returns a net error code.
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int InitSparseData();
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds the provided |flags| to the current EntryFlags for this entry.
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetEntryFlags(uint32 flags);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current EntryFlags for this entry.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 GetEntryFlags();
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the data stored at the given index. If the information is in memory,
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a buffer will be allocated and the data will be copied to it (the caller
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // can find out the size of the buffer before making this call). Otherwise,
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the cache address of the data will be returned, and that address will be
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // removed from the regular book keeping of this entry so the caller is
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // responsible for deleting the block (or file) from the backing store at some
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // point; there is no need to report any storage-size change, only to do the
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // actual cleanup.
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetData(int index, char** buffer, Addr* address);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Logs this entry to the internal trace buffer.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Log(const char* msg);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CacheEntryBlock entry_;     // Key related information for this entry.
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CacheRankingsBlock node_;   // Rankings related information for this entry.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::WeakPtr<BackendImpl> backend_;  // Back pointer to the cache.
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::WeakPtr<InFlightBackendIO> background_queue_;  // In-progress queue.
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<UserBuffer> user_buffers_[kNumStreams];  // Stores user data.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Files to store external user data and key.
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<File> files_[kNumStreams + 1];
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::string key_;           // Copy of the key.
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int unreported_size_[kNumStreams];  // Bytes not reported yet to the backend.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool doomed_;               // True if this entry was removed from the cache.
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool read_only_;            // True if not yet writing.
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool dirty_;                // True if we detected that this is a dirty entry.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SparseControl> sparse_;  // Support for sparse entries.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::BoundNetLog net_log_;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(EntryImpl);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace disk_cache
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
278a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // NET_DISK_CACHE_BLOCKFILE_ENTRY_IMPL_H_
279